a4d61a4be4
Noticed when working on https://github.com/ooni/probe/issues/1505. Justification for this diff: 1. [DialEarlyContext calls dialContext with the last argument set to false](https://github.com/lucas-clemente/quic-go/blob/v0.21.1/client.go#L153); 2. [the semantics of the last argument is whether we own the connection](https://github.com/lucas-clemente/quic-go/blob/v0.21.1/client.go#L187); 3. [this value is propagated to the client data structure](https://github.com/lucas-clemente/quic-go/blob/v0.21.1/client.go#L269); 4. [client.dial](https://github.com/lucas-clemente/quic-go/blob/v0.21.1/client.go#L302) runs the session in a background goroutine and only destroys the `packetHandlers` when the connection is owned; 5. [packetHandlerMap.Destroy](https://github.com/lucas-clemente/quic-go/blob/v0.21.1/packet_handler_map.go#L293) closes the underlying PacketConn. 6. also, the documentation clearly states that when you use `DialEarlyContext` you can use the same packet conn multiple times, so it does not take ownership.
81 lines
1.9 KiB
Go
81 lines
1.9 KiB
Go
package quicdialer
|
|
|
|
import (
|
|
"errors"
|
|
"net"
|
|
"time"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/errorx"
|
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
|
|
)
|
|
|
|
// QUICListener listens for QUIC connections.
|
|
type QUICListener interface {
|
|
// Listen creates a new listening PacketConn.
|
|
Listen(addr *net.UDPAddr) (net.PacketConn, error)
|
|
}
|
|
|
|
// QUICListenerSaver is a QUICListener that also implements saving events.
|
|
type QUICListenerSaver struct {
|
|
// QUICListener is the underlying QUICListener.
|
|
QUICListener QUICListener
|
|
|
|
// Saver is the underlying Saver.
|
|
Saver *trace.Saver
|
|
}
|
|
|
|
// Listen implements QUICListener.Listen.
|
|
func (qls *QUICListenerSaver) Listen(addr *net.UDPAddr) (net.PacketConn, error) {
|
|
pconn, err := qls.QUICListener.Listen(addr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// TODO(bassosimone): refactor to remove this restriction.
|
|
udpConn, ok := pconn.(*net.UDPConn)
|
|
if !ok {
|
|
return nil, errors.New("quicdialer: cannot convert to udpConn")
|
|
}
|
|
return saverUDPConn{UDPConn: udpConn, saver: qls.Saver}, nil
|
|
}
|
|
|
|
type saverUDPConn struct {
|
|
*net.UDPConn
|
|
saver *trace.Saver
|
|
}
|
|
|
|
func (c saverUDPConn) WriteTo(p []byte, addr net.Addr) (int, error) {
|
|
start := time.Now()
|
|
count, err := c.UDPConn.WriteTo(p, addr)
|
|
stop := time.Now()
|
|
c.saver.Write(trace.Event{
|
|
Address: addr.String(),
|
|
Data: p[:count],
|
|
Duration: stop.Sub(start),
|
|
Err: err,
|
|
NumBytes: count,
|
|
Name: errorx.WriteToOperation,
|
|
Time: stop,
|
|
})
|
|
return count, err
|
|
}
|
|
|
|
func (c saverUDPConn) ReadMsgUDP(b, oob []byte) (int, int, int, *net.UDPAddr, error) {
|
|
start := time.Now()
|
|
n, oobn, flags, addr, err := c.UDPConn.ReadMsgUDP(b, oob)
|
|
stop := time.Now()
|
|
var data []byte
|
|
if n > 0 {
|
|
data = b[:n]
|
|
}
|
|
c.saver.Write(trace.Event{
|
|
Address: addr.String(),
|
|
Data: data,
|
|
Duration: stop.Sub(start),
|
|
Err: err,
|
|
NumBytes: n,
|
|
Name: errorx.ReadFromOperation,
|
|
Time: stop,
|
|
})
|
|
return n, oobn, flags, addr, err
|
|
}
|