2021-02-02 12:05:47 +01:00
|
|
|
package quicdialer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
|
2021-09-07 17:09:30 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
|
2021-09-05 14:49:38 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite/quicx"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
2021-06-25 16:20:08 +02:00
|
|
|
// QUICListener listens for QUIC connections.
|
|
|
|
type QUICListener interface {
|
2021-07-01 21:56:29 +02:00
|
|
|
// Listen creates a new listening UDPConn.
|
2021-08-23 16:49:22 +02:00
|
|
|
Listen(addr *net.UDPAddr) (quicx.UDPLikeConn, error)
|
2021-06-25 16:20:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2021-08-23 16:49:22 +02:00
|
|
|
func (qls *QUICListenerSaver) Listen(addr *net.UDPAddr) (quicx.UDPLikeConn, error) {
|
2021-06-25 16:20:08 +02:00
|
|
|
pconn, err := qls.QUICListener.Listen(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-23 16:49:22 +02:00
|
|
|
return &saverUDPConn{
|
|
|
|
UDPLikeConn: pconn,
|
|
|
|
saver: qls.Saver,
|
|
|
|
}, nil
|
2021-06-25 16:20:08 +02:00
|
|
|
}
|
|
|
|
|
2021-02-02 12:05:47 +01:00
|
|
|
type saverUDPConn struct {
|
2021-08-23 16:49:22 +02:00
|
|
|
quicx.UDPLikeConn
|
2021-02-02 12:05:47 +01:00
|
|
|
saver *trace.Saver
|
|
|
|
}
|
|
|
|
|
2021-08-23 16:49:22 +02:00
|
|
|
var _ quicx.UDPLikeConn = &saverUDPConn{}
|
|
|
|
|
|
|
|
func (c *saverUDPConn) WriteTo(p []byte, addr net.Addr) (int, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
start := time.Now()
|
2021-08-23 16:49:22 +02:00
|
|
|
count, err := c.UDPLikeConn.WriteTo(p, addr)
|
2021-02-02 12:05:47 +01:00
|
|
|
stop := time.Now()
|
|
|
|
c.saver.Write(trace.Event{
|
|
|
|
Address: addr.String(),
|
|
|
|
Data: p[:count],
|
|
|
|
Duration: stop.Sub(start),
|
|
|
|
Err: err,
|
|
|
|
NumBytes: count,
|
2021-07-01 16:34:36 +02:00
|
|
|
Name: errorsx.WriteToOperation,
|
2021-02-02 12:05:47 +01:00
|
|
|
Time: stop,
|
|
|
|
})
|
|
|
|
return count, err
|
|
|
|
}
|
|
|
|
|
2021-08-23 16:49:22 +02:00
|
|
|
func (c *saverUDPConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
start := time.Now()
|
2021-08-23 16:49:22 +02:00
|
|
|
n, addr, err := c.UDPLikeConn.ReadFrom(b)
|
2021-02-02 12:05:47 +01:00
|
|
|
stop := time.Now()
|
|
|
|
var data []byte
|
|
|
|
if n > 0 {
|
|
|
|
data = b[:n]
|
|
|
|
}
|
|
|
|
c.saver.Write(trace.Event{
|
2021-08-23 16:49:22 +02:00
|
|
|
Address: c.safeAddrString(addr),
|
2021-02-02 12:05:47 +01:00
|
|
|
Data: data,
|
|
|
|
Duration: stop.Sub(start),
|
|
|
|
Err: err,
|
|
|
|
NumBytes: n,
|
2021-07-01 16:34:36 +02:00
|
|
|
Name: errorsx.ReadFromOperation,
|
2021-02-02 12:05:47 +01:00
|
|
|
Time: stop,
|
|
|
|
})
|
2021-08-23 16:49:22 +02:00
|
|
|
return n, addr, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *saverUDPConn) safeAddrString(addr net.Addr) (out string) {
|
|
|
|
if addr != nil {
|
|
|
|
out = addr.String()
|
|
|
|
}
|
|
|
|
return
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|