refactor: move quicdialing base functionality to netxlite (#406)

Part of https://github.com/ooni/probe/issues/1505
This commit is contained in:
Simone Basso
2021-06-25 17:04:24 +02:00
committed by GitHub
parent c00cad1382
commit 925ca22b88
10 changed files with 254 additions and 84 deletions
+81
View File
@@ -0,0 +1,81 @@
package netxlite
import (
"context"
"crypto/tls"
"errors"
"net"
"strconv"
"github.com/lucas-clemente/quic-go"
)
// QUICDialerContext is a dialer for QUIC using Context.
type QUICContextDialer interface {
// DialContext establishes a new QUIC session using the given
// network and address. The tlsConfig and the quicConfig arguments
// MUST NOT be nil. Returns either the session or an error.
DialContext(ctx context.Context, network, address string,
tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlySession, error)
}
// QUICDialer dials QUIC connections.
type QUICDialer interface {
// DialContext establishes a new QUIC session using the given
// network and address. The tlsConfig and the quicConfig arguments
// MUST NOT be nil. Returns either the session or an error.
Dial(network, address string, tlsConfig *tls.Config,
quicConfig *quic.Config) (quic.EarlySession, error)
}
// QUICListener listens for QUIC connections.
type QUICListener interface {
// Listen creates a new listening net.PacketConn.
Listen(addr *net.UDPAddr) (net.PacketConn, error)
}
// QUICListenerStdlib is a QUICListener using the standard library.
type QUICListenerStdlib struct{}
var _ QUICListener = &QUICListenerStdlib{}
// Listen implements QUICListener.Listen.
func (qls *QUICListenerStdlib) Listen(addr *net.UDPAddr) (net.PacketConn, error) {
return net.ListenUDP("udp", addr)
}
// QUICDialerQUICGo dials using the lucas-clemente/quic-go library.
type QUICDialerQUICGo struct {
// QUICListener is the underlying QUICListener to use.
QUICListener QUICListener
}
var _ QUICContextDialer = &QUICDialerQUICGo{}
// errInvalidIP indicates that a string is not a valid IP.
var errInvalidIP = errors.New("netxlite: invalid IP")
// DialContext implements ContextDialer.DialContext
func (d *QUICDialerQUICGo) DialContext(ctx context.Context, network string,
address string, tlsConfig *tls.Config, quicConfig *quic.Config) (
quic.EarlySession, error) {
onlyhost, onlyport, err := net.SplitHostPort(address)
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, errInvalidIP
}
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, address, tlsConfig, quicConfig)
}
+114
View File
@@ -0,0 +1,114 @@
package netxlite
import (
"context"
"crypto/tls"
"errors"
"log"
"net"
"strings"
"testing"
"github.com/lucas-clemente/quic-go"
"github.com/ooni/probe-cli/v3/internal/netxmocks"
)
func TestQUICDialerQUICGoCannotSplitHostPort(t *testing.T) {
tlsConfig := &tls.Config{
NextProtos: []string{"h3"},
ServerName: "www.google.com",
}
systemdialer := QUICDialerQUICGo{
QUICListener: &QUICListenerStdlib{},
}
ctx := context.Background()
sess, err := systemdialer.DialContext(
ctx, "udp", "a.b.c.d", tlsConfig, &quic.Config{})
if err == nil || !strings.HasSuffix(err.Error(), "missing port in address") {
t.Fatal("not the error we expected", err)
}
if sess != nil {
t.Fatal("expected nil sess here")
}
}
func TestQUICDialerQUICGoInvalidPort(t *testing.T) {
tlsConfig := &tls.Config{
NextProtos: []string{"h3"},
ServerName: "www.google.com",
}
systemdialer := QUICDialerQUICGo{
QUICListener: &QUICListenerStdlib{},
}
ctx := context.Background()
sess, err := systemdialer.DialContext(
ctx, "udp", "8.8.4.4:xyz", tlsConfig, &quic.Config{})
if err == nil || !strings.HasSuffix(err.Error(), "invalid syntax") {
t.Fatal("not the error we expected", err)
}
if sess != nil {
t.Fatal("expected nil sess here")
}
}
func TestQUICDialerQUICGoInvalidIP(t *testing.T) {
tlsConfig := &tls.Config{
NextProtos: []string{"h3"},
ServerName: "www.google.com",
}
systemdialer := QUICDialerQUICGo{
QUICListener: &QUICListenerStdlib{},
}
ctx := context.Background()
sess, err := systemdialer.DialContext(
ctx, "udp", "a.b.c.d:0", tlsConfig, &quic.Config{})
if !errors.Is(err, errInvalidIP) {
t.Fatal("not the error we expected", err)
}
if sess != nil {
t.Fatal("expected nil sess here")
}
}
func TestQUICDialerQUICGoCannotListen(t *testing.T) {
expected := errors.New("mocked error")
tlsConfig := &tls.Config{
NextProtos: []string{"h3"},
ServerName: "www.google.com",
}
systemdialer := QUICDialerQUICGo{
QUICListener: &netxmocks.QUICListener{
MockListen: func(addr *net.UDPAddr) (net.PacketConn, error) {
return nil, expected
},
},
}
ctx := context.Background()
sess, err := systemdialer.DialContext(
ctx, "udp", "8.8.8.8:443", tlsConfig, &quic.Config{})
if !errors.Is(err, expected) {
t.Fatal("not the error we expected", err)
}
if sess != nil {
t.Fatal("expected nil sess here")
}
}
func TestQUICDialerWorksAsIntended(t *testing.T) {
tlsConfig := &tls.Config{
NextProtos: []string{"h3"},
ServerName: "dns.google",
}
systemdialer := QUICDialerQUICGo{
QUICListener: &QUICListenerStdlib{},
}
ctx := context.Background()
sess, err := systemdialer.DialContext(
ctx, "udp", "8.8.8.8:443", tlsConfig, &quic.Config{})
if err != nil {
t.Fatal("not the error we expected", err)
}
if err := sess.CloseWithError(0, ""); err != nil {
log.Fatal(err)
}
}