refactor: move quicdialing base functionality to netxlite (#406)
Part of https://github.com/ooni/probe/issues/1505
This commit is contained in:
@@ -160,14 +160,14 @@ func NewQUICDialer(config Config) QUICDialer {
|
||||
if config.FullResolver == nil {
|
||||
config.FullResolver = NewResolver(config)
|
||||
}
|
||||
var ql quicdialer.QUICListener = &quicdialer.QUICListenerStdlib{}
|
||||
var ql quicdialer.QUICListener = &netxlite.QUICListenerStdlib{}
|
||||
if config.ReadWriteSaver != nil {
|
||||
ql = &quicdialer.QUICListenerSaver{
|
||||
QUICListener: ql,
|
||||
Saver: config.ReadWriteSaver,
|
||||
}
|
||||
}
|
||||
var d quicdialer.ContextDialer = &quicdialer.SystemDialer{
|
||||
var d quicdialer.ContextDialer = &netxlite.QUICDialerQUICGo{
|
||||
QUICListener: ql,
|
||||
}
|
||||
d = quicdialer.ErrorWrapperDialer{Dialer: d}
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
"github.com/lucas-clemente/quic-go"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/quicdialer"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
||||
)
|
||||
|
||||
type MockableResolver struct {
|
||||
@@ -25,8 +26,8 @@ func (r MockableResolver) LookupHost(ctx context.Context, host string) ([]string
|
||||
func TestDNSDialerSuccess(t *testing.T) {
|
||||
tlsConf := &tls.Config{NextProtos: []string{"h3"}}
|
||||
dialer := quicdialer.DNSDialer{
|
||||
Resolver: new(net.Resolver), Dialer: quicdialer.SystemDialer{
|
||||
QUICListener: &quicdialer.QUICListenerStdlib{},
|
||||
Resolver: new(net.Resolver), Dialer: &netxlite.QUICDialerQUICGo{
|
||||
QUICListener: &netxlite.QUICListenerStdlib{},
|
||||
}}
|
||||
sess, err := dialer.DialContext(
|
||||
context.Background(), "udp", "www.google.com:443",
|
||||
@@ -42,7 +43,7 @@ func TestDNSDialerSuccess(t *testing.T) {
|
||||
func TestDNSDialerNoPort(t *testing.T) {
|
||||
tlsConf := &tls.Config{NextProtos: []string{"h3"}}
|
||||
dialer := quicdialer.DNSDialer{
|
||||
Resolver: new(net.Resolver), Dialer: quicdialer.SystemDialer{}}
|
||||
Resolver: new(net.Resolver), Dialer: &netxlite.QUICDialerQUICGo{}}
|
||||
sess, err := dialer.DialContext(
|
||||
context.Background(), "udp", "www.google.com",
|
||||
tlsConf, &quic.Config{})
|
||||
@@ -90,8 +91,8 @@ func TestDNSDialerLookupHostFailure(t *testing.T) {
|
||||
func TestDNSDialerInvalidPort(t *testing.T) {
|
||||
tlsConf := &tls.Config{NextProtos: []string{"h3"}}
|
||||
dialer := quicdialer.DNSDialer{
|
||||
Resolver: new(net.Resolver), Dialer: quicdialer.SystemDialer{
|
||||
QUICListener: &quicdialer.QUICListenerStdlib{},
|
||||
Resolver: new(net.Resolver), Dialer: &netxlite.QUICDialerQUICGo{
|
||||
QUICListener: &netxlite.QUICListenerStdlib{},
|
||||
}}
|
||||
sess, err := dialer.DialContext(
|
||||
context.Background(), "udp", "www.google.com:0",
|
||||
@@ -111,8 +112,8 @@ func TestDNSDialerInvalidPort(t *testing.T) {
|
||||
func TestDNSDialerInvalidPortSyntax(t *testing.T) {
|
||||
tlsConf := &tls.Config{NextProtos: []string{"h3"}}
|
||||
dialer := quicdialer.DNSDialer{
|
||||
Resolver: new(net.Resolver), Dialer: quicdialer.SystemDialer{
|
||||
QUICListener: &quicdialer.QUICListenerStdlib{},
|
||||
Resolver: new(net.Resolver), Dialer: &netxlite.QUICDialerQUICGo{
|
||||
QUICListener: &netxlite.QUICListenerStdlib{},
|
||||
}}
|
||||
sess, err := dialer.DialContext(
|
||||
context.Background(), "udp", "www.google.com:port",
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/lucas-clemente/quic-go"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/errorx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/quicdialer"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
||||
)
|
||||
|
||||
func TestErrorWrapperFailure(t *testing.T) {
|
||||
@@ -48,8 +49,8 @@ func TestErrorWrapperInvalidCertificate(t *testing.T) {
|
||||
ServerName: servername,
|
||||
}
|
||||
|
||||
dlr := quicdialer.ErrorWrapperDialer{Dialer: &quicdialer.SystemDialer{
|
||||
QUICListener: &quicdialer.QUICListenerStdlib{},
|
||||
dlr := quicdialer.ErrorWrapperDialer{Dialer: &netxlite.QUICDialerQUICGo{
|
||||
QUICListener: &netxlite.QUICListenerStdlib{},
|
||||
}}
|
||||
// use Google IP
|
||||
sess, err := dlr.DialContext(context.Background(), "udp",
|
||||
@@ -71,8 +72,8 @@ func TestErrorWrapperSuccess(t *testing.T) {
|
||||
NextProtos: []string{"h3"},
|
||||
ServerName: "www.google.com",
|
||||
}
|
||||
d := quicdialer.ErrorWrapperDialer{Dialer: quicdialer.SystemDialer{
|
||||
QUICListener: &quicdialer.QUICListenerStdlib{},
|
||||
d := quicdialer.ErrorWrapperDialer{Dialer: &netxlite.QUICDialerQUICGo{
|
||||
QUICListener: &netxlite.QUICListenerStdlib{},
|
||||
}}
|
||||
sess, err := d.DialContext(ctx, "udp", "216.58.212.164:443", tlsConf, &quic.Config{})
|
||||
if err != nil {
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/lucas-clemente/quic-go"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/quicdialer"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
||||
)
|
||||
|
||||
type MockDialer struct {
|
||||
@@ -36,8 +37,8 @@ func TestHandshakeSaverSuccess(t *testing.T) {
|
||||
}
|
||||
saver := &trace.Saver{}
|
||||
dlr := quicdialer.HandshakeSaver{
|
||||
Dialer: quicdialer.SystemDialer{
|
||||
QUICListener: &quicdialer.QUICListenerStdlib{},
|
||||
Dialer: &netxlite.QUICDialerQUICGo{
|
||||
QUICListener: &netxlite.QUICListenerStdlib{},
|
||||
},
|
||||
Saver: saver,
|
||||
}
|
||||
@@ -94,8 +95,8 @@ func TestHandshakeSaverHostNameError(t *testing.T) {
|
||||
}
|
||||
saver := &trace.Saver{}
|
||||
dlr := quicdialer.HandshakeSaver{
|
||||
Dialer: quicdialer.SystemDialer{
|
||||
QUICListener: &quicdialer.QUICListenerStdlib{},
|
||||
Dialer: &netxlite.QUICDialerQUICGo{
|
||||
QUICListener: &netxlite.QUICListenerStdlib{},
|
||||
},
|
||||
Saver: saver,
|
||||
}
|
||||
|
||||
@@ -1,14 +1,10 @@
|
||||
package quicdialer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"net"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/lucas-clemente/quic-go"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/errorx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
|
||||
)
|
||||
@@ -19,14 +15,6 @@ type QUICListener interface {
|
||||
Listen(addr *net.UDPAddr) (net.PacketConn, error)
|
||||
}
|
||||
|
||||
// QUICListenerStdlib is a QUICListener using the standard library.
|
||||
type QUICListenerStdlib struct{}
|
||||
|
||||
// Listen implements QUICListener.Listen.
|
||||
func (qls *QUICListenerStdlib) Listen(addr *net.UDPAddr) (net.PacketConn, error) {
|
||||
return net.ListenUDP("udp", addr)
|
||||
}
|
||||
|
||||
// QUICListenerSaver is a QUICListener that also implements saving events.
|
||||
type QUICListenerSaver struct {
|
||||
// QUICListener is the underlying QUICListener.
|
||||
@@ -50,35 +38,6 @@ func (qls *QUICListenerSaver) Listen(addr *net.UDPAddr) (net.PacketConn, error)
|
||||
return saverUDPConn{UDPConn: udpConn, saver: qls.Saver}, nil
|
||||
}
|
||||
|
||||
// SystemDialer is the basic dialer for QUIC
|
||||
type SystemDialer struct {
|
||||
// QUICListener is the underlying QUICListener to use.
|
||||
QUICListener QUICListener
|
||||
}
|
||||
|
||||
// DialContext implements ContextDialer.DialContext
|
||||
func (d SystemDialer) DialContext(ctx context.Context, network string,
|
||||
host string, tlsCfg *tls.Config, cfg *quic.Config) (quic.EarlySession, error) {
|
||||
onlyhost, onlyport, err := net.SplitHostPort(host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
port, err := strconv.Atoi(onlyport)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ip := net.ParseIP(onlyhost)
|
||||
if ip == nil {
|
||||
return nil, errors.New("quicdialer: invalid IP representation")
|
||||
}
|
||||
pconn, err := d.QUICListener.Listen(&net.UDPAddr{IP: net.IPv4zero, Port: 0})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
udpAddr := &net.UDPAddr{IP: ip, Port: port, Zone: ""}
|
||||
return quic.DialEarlyContext(ctx, pconn, udpAddr, host, tlsCfg, cfg)
|
||||
}
|
||||
|
||||
type saverUDPConn struct {
|
||||
*net.UDPConn
|
||||
saver *trace.Saver
|
||||
|
||||
@@ -9,32 +9,9 @@ import (
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/errorx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/quicdialer"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
||||
)
|
||||
|
||||
func TestSystemDialerInvalidIPFailure(t *testing.T) {
|
||||
tlsConf := &tls.Config{
|
||||
NextProtos: []string{"h3"},
|
||||
ServerName: "www.google.com",
|
||||
}
|
||||
saver := &trace.Saver{}
|
||||
systemdialer := quicdialer.SystemDialer{
|
||||
QUICListener: &quicdialer.QUICListenerSaver{
|
||||
QUICListener: &quicdialer.QUICListenerStdlib{},
|
||||
Saver: saver,
|
||||
},
|
||||
}
|
||||
sess, err := systemdialer.DialContext(context.Background(), "udp", "a.b.c.d:0", tlsConf, &quic.Config{})
|
||||
if err == nil {
|
||||
t.Fatal("expected an error here")
|
||||
}
|
||||
if sess != nil {
|
||||
t.Fatal("expected nil sess here")
|
||||
}
|
||||
if err.Error() != "quicdialer: invalid IP representation" {
|
||||
t.Fatal("expected another error here")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemDialerSuccessWithReadWrite(t *testing.T) {
|
||||
// This is the most common use case for collecting reads, writes
|
||||
tlsConf := &tls.Config{
|
||||
@@ -42,9 +19,9 @@ func TestSystemDialerSuccessWithReadWrite(t *testing.T) {
|
||||
ServerName: "www.google.com",
|
||||
}
|
||||
saver := &trace.Saver{}
|
||||
systemdialer := quicdialer.SystemDialer{
|
||||
systemdialer := &netxlite.QUICDialerQUICGo{
|
||||
QUICListener: &quicdialer.QUICListenerSaver{
|
||||
QUICListener: &quicdialer.QUICListenerStdlib{},
|
||||
QUICListener: &netxlite.QUICListenerStdlib{},
|
||||
Saver: saver,
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user