cli: upgrade to lucas-clemente/quic-go 0.23.0 (#449)

See https://github.com/ooni/probe/issues/1754 for a comprehensive description.
This commit is contained in:
Simone Basso
2021-08-23 16:49:22 +02:00
committed by GitHub
parent a9014e7950
commit 8f18813e17
12 changed files with 192 additions and 130 deletions
+23 -11
View File
@@ -4,15 +4,15 @@ import (
"net"
"time"
"github.com/lucas-clemente/quic-go"
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
"github.com/ooni/probe-cli/v3/internal/errorsx"
"github.com/ooni/probe-cli/v3/internal/quicx"
)
// QUICListener listens for QUIC connections.
type QUICListener interface {
// Listen creates a new listening UDPConn.
Listen(addr *net.UDPAddr) (quic.OOBCapablePacketConn, error)
Listen(addr *net.UDPAddr) (quicx.UDPLikeConn, error)
}
// QUICListenerSaver is a QUICListener that also implements saving events.
@@ -25,22 +25,27 @@ type QUICListenerSaver struct {
}
// Listen implements QUICListener.Listen.
func (qls *QUICListenerSaver) Listen(addr *net.UDPAddr) (quic.OOBCapablePacketConn, error) {
func (qls *QUICListenerSaver) Listen(addr *net.UDPAddr) (quicx.UDPLikeConn, error) {
pconn, err := qls.QUICListener.Listen(addr)
if err != nil {
return nil, err
}
return saverUDPConn{OOBCapablePacketConn: pconn, saver: qls.Saver}, nil
return &saverUDPConn{
UDPLikeConn: pconn,
saver: qls.Saver,
}, nil
}
type saverUDPConn struct {
quic.OOBCapablePacketConn
quicx.UDPLikeConn
saver *trace.Saver
}
func (c saverUDPConn) WriteTo(p []byte, addr net.Addr) (int, error) {
var _ quicx.UDPLikeConn = &saverUDPConn{}
func (c *saverUDPConn) WriteTo(p []byte, addr net.Addr) (int, error) {
start := time.Now()
count, err := c.OOBCapablePacketConn.WriteTo(p, addr)
count, err := c.UDPLikeConn.WriteTo(p, addr)
stop := time.Now()
c.saver.Write(trace.Event{
Address: addr.String(),
@@ -54,16 +59,16 @@ func (c saverUDPConn) WriteTo(p []byte, addr net.Addr) (int, error) {
return count, err
}
func (c saverUDPConn) ReadMsgUDP(b, oob []byte) (int, int, int, *net.UDPAddr, error) {
func (c *saverUDPConn) ReadFrom(b []byte) (int, net.Addr, error) {
start := time.Now()
n, oobn, flags, addr, err := c.OOBCapablePacketConn.ReadMsgUDP(b, oob)
n, addr, err := c.UDPLikeConn.ReadFrom(b)
stop := time.Now()
var data []byte
if n > 0 {
data = b[:n]
}
c.saver.Write(trace.Event{
Address: addr.String(),
Address: c.safeAddrString(addr),
Data: data,
Duration: stop.Sub(start),
Err: err,
@@ -71,5 +76,12 @@ func (c saverUDPConn) ReadMsgUDP(b, oob []byte) (int, int, int, *net.UDPAddr, er
Name: errorsx.ReadFromOperation,
Time: stop,
})
return n, oobn, flags, addr, err
return n, addr, err
}
func (c *saverUDPConn) safeAddrString(addr net.Addr) (out string) {
if addr != nil {
out = addr.String()
}
return
}
@@ -3,6 +3,8 @@ package quicdialer_test
import (
"context"
"crypto/tls"
"errors"
"net"
"testing"
"github.com/lucas-clemente/quic-go"
@@ -10,8 +12,33 @@ import (
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
"github.com/ooni/probe-cli/v3/internal/errorsx"
"github.com/ooni/probe-cli/v3/internal/netxlite"
"github.com/ooni/probe-cli/v3/internal/netxmocks"
"github.com/ooni/probe-cli/v3/internal/quicx"
)
func TestQUICListenerSaverCannotListen(t *testing.T) {
expected := errors.New("mocked error")
qls := &quicdialer.QUICListenerSaver{
QUICListener: &netxmocks.QUICListener{
MockListen: func(addr *net.UDPAddr) (quicx.UDPLikeConn, error) {
return nil, expected
},
},
Saver: &trace.Saver{},
}
pconn, err := qls.Listen(&net.UDPAddr{
IP: []byte{},
Port: 8080,
Zone: "",
})
if !errors.Is(err, expected) {
t.Fatal("unexpected error", err)
}
if pconn != nil {
t.Fatal("expected nil pconn here")
}
}
func TestSystemDialerSuccessWithReadWrite(t *testing.T) {
// This is the most common use case for collecting reads, writes
tlsConf := &tls.Config{
+14 -13
View File
@@ -7,6 +7,7 @@ import (
"net"
"github.com/lucas-clemente/quic-go"
"github.com/ooni/probe-cli/v3/internal/quicx"
)
// QUICContextDialer is a dialer for QUIC using Context.
@@ -21,7 +22,7 @@ type QUICContextDialer interface {
// QUICListener listens for QUIC connections.
type QUICListener interface {
// Listen creates a new listening UDPConn.
Listen(addr *net.UDPAddr) (quic.OOBCapablePacketConn, error)
Listen(addr *net.UDPAddr) (quicx.UDPLikeConn, error)
}
// ErrorWrapperQUICListener is a QUICListener that wraps errors.
@@ -33,7 +34,7 @@ type ErrorWrapperQUICListener struct {
var _ QUICListener = &ErrorWrapperQUICListener{}
// Listen implements QUICListener.Listen.
func (qls *ErrorWrapperQUICListener) Listen(addr *net.UDPAddr) (quic.OOBCapablePacketConn, error) {
func (qls *ErrorWrapperQUICListener) Listen(addr *net.UDPAddr) (quicx.UDPLikeConn, error) {
pconn, err := qls.QUICListener.Listen(addr)
if err != nil {
return nil, SafeErrWrapperBuilder{
@@ -44,17 +45,17 @@ func (qls *ErrorWrapperQUICListener) Listen(addr *net.UDPAddr) (quic.OOBCapableP
return &errorWrapperUDPConn{pconn}, nil
}
// errorWrapperUDPConn is a quic.OOBCapablePacketConn that wraps errors.
// errorWrapperUDPConn is a quicx.UDPLikeConn that wraps errors.
type errorWrapperUDPConn struct {
// OOBCapablePacketConn is the underlying conn.
quic.OOBCapablePacketConn
// UDPLikeConn is the underlying conn.
quicx.UDPLikeConn
}
var _ quic.OOBCapablePacketConn = &errorWrapperUDPConn{}
var _ quicx.UDPLikeConn = &errorWrapperUDPConn{}
// WriteTo implements quic.OOBCapablePacketConn.WriteTo.
// WriteTo implements quicx.UDPLikeConn.WriteTo.
func (c *errorWrapperUDPConn) WriteTo(p []byte, addr net.Addr) (int, error) {
count, err := c.OOBCapablePacketConn.WriteTo(p, addr)
count, err := c.UDPLikeConn.WriteTo(p, addr)
if err != nil {
return 0, SafeErrWrapperBuilder{
Error: err,
@@ -64,16 +65,16 @@ func (c *errorWrapperUDPConn) WriteTo(p []byte, addr net.Addr) (int, error) {
return count, nil
}
// ReadMsgUDP implements quic.OOBCapablePacketConn.ReadMsgUDP.
func (c *errorWrapperUDPConn) ReadMsgUDP(b, oob []byte) (int, int, int, *net.UDPAddr, error) {
n, oobn, flags, addr, err := c.OOBCapablePacketConn.ReadMsgUDP(b, oob)
// ReadFrom implements quicx.UDPLikeConn.ReadFrom.
func (c *errorWrapperUDPConn) ReadFrom(b []byte) (int, net.Addr, error) {
n, addr, err := c.UDPLikeConn.ReadFrom(b)
if err != nil {
return 0, 0, 0, nil, SafeErrWrapperBuilder{
return 0, nil, SafeErrWrapperBuilder{
Error: err,
Operation: ReadFromOperation,
}.MaybeBuild()
}
return n, oobn, flags, addr, nil
return n, addr, nil
}
// ErrorWrapperQUICDialer is a dialer that performs quic err wrapping
+15 -28
View File
@@ -10,12 +10,13 @@ import (
"github.com/lucas-clemente/quic-go"
"github.com/ooni/probe-cli/v3/internal/netxmocks"
"github.com/ooni/probe-cli/v3/internal/quicx"
)
func TestErrorWrapperQUICListenerSuccess(t *testing.T) {
ql := &ErrorWrapperQUICListener{
QUICListener: &netxmocks.QUICListener{
MockListen: func(addr *net.UDPAddr) (quic.OOBCapablePacketConn, error) {
MockListen: func(addr *net.UDPAddr) (quicx.UDPLikeConn, error) {
return &net.UDPConn{}, nil
},
},
@@ -30,7 +31,7 @@ func TestErrorWrapperQUICListenerSuccess(t *testing.T) {
func TestErrorWrapperQUICListenerFailure(t *testing.T) {
ql := &ErrorWrapperQUICListener{
QUICListener: &netxmocks.QUICListener{
MockListen: func(addr *net.UDPAddr) (quic.OOBCapablePacketConn, error) {
MockListen: func(addr *net.UDPAddr) (quicx.UDPLikeConn, error) {
return nil, io.EOF
},
},
@@ -46,7 +47,7 @@ func TestErrorWrapperQUICListenerFailure(t *testing.T) {
func TestErrorWrapperUDPConnWriteToSuccess(t *testing.T) {
quc := &errorWrapperUDPConn{
OOBCapablePacketConn: &netxmocks.QUICUDPConn{
UDPLikeConn: &netxmocks.QUICUDPConn{
MockWriteTo: func(p []byte, addr net.Addr) (int, error) {
return 10, nil
},
@@ -66,7 +67,7 @@ func TestErrorWrapperUDPConnWriteToSuccess(t *testing.T) {
func TestErrorWrapperUDPConnWriteToFailure(t *testing.T) {
expected := errors.New("mocked error")
quc := &errorWrapperUDPConn{
OOBCapablePacketConn: &netxmocks.QUICUDPConn{
UDPLikeConn: &netxmocks.QUICUDPConn{
MockWriteTo: func(p []byte, addr net.Addr) (int, error) {
return 0, expected
},
@@ -83,58 +84,44 @@ func TestErrorWrapperUDPConnWriteToFailure(t *testing.T) {
}
}
func TestErrorWrapperUDPConnReadMsgUDPSuccess(t *testing.T) {
func TestErrorWrapperUDPConnReadFromSuccess(t *testing.T) {
expected := errors.New("mocked error")
quc := &errorWrapperUDPConn{
OOBCapablePacketConn: &netxmocks.QUICUDPConn{
MockReadMsgUDP: func(b, oob []byte) (int, int, int, *net.UDPAddr, error) {
return 0, 0, 0, nil, expected
UDPLikeConn: &netxmocks.QUICUDPConn{
MockReadFrom: func(b []byte) (int, net.Addr, error) {
return 0, nil, expected
},
},
}
b := make([]byte, 128)
oob := make([]byte, 128)
n, oobn, flags, addr, err := quc.ReadMsgUDP(b, oob)
n, addr, err := quc.ReadFrom(b)
if !errors.Is(err, expected) {
t.Fatal("not the error we expected", err)
}
if n != 0 {
t.Fatal("expected 0 here")
}
if oobn != 0 {
t.Fatal("expected 0 here")
}
if flags != 0 {
t.Fatal("expected 0 here")
}
if addr != nil {
t.Fatal("expected nil here")
}
}
func TestErrorWrapperUDPConnReadMsgUDPFailure(t *testing.T) {
func TestErrorWrapperUDPConnReadFromFailure(t *testing.T) {
quc := &errorWrapperUDPConn{
OOBCapablePacketConn: &netxmocks.QUICUDPConn{
MockReadMsgUDP: func(b, oob []byte) (int, int, int, *net.UDPAddr, error) {
return 10, 1, 0, nil, nil
UDPLikeConn: &netxmocks.QUICUDPConn{
MockReadFrom: func(b []byte) (int, net.Addr, error) {
return 10, nil, nil
},
},
}
b := make([]byte, 128)
oob := make([]byte, 128)
n, oobn, flags, addr, err := quc.ReadMsgUDP(b, oob)
n, addr, err := quc.ReadFrom(b)
if err != nil {
t.Fatal("not the error we expected", err)
}
if n != 10 {
t.Fatal("expected 10 here")
}
if oobn != 1 {
t.Fatal("expected 1 here")
}
if flags != 0 {
t.Fatal("expected 0 here")
}
if addr != nil {
t.Fatal("expected nil here")
}
+5 -4
View File
@@ -8,6 +8,7 @@ import (
"strconv"
"github.com/lucas-clemente/quic-go"
"github.com/ooni/probe-cli/v3/internal/quicx"
)
// QUICContextDialer is a dialer for QUIC using Context.
@@ -22,7 +23,7 @@ type QUICContextDialer interface {
// QUICListener listens for QUIC connections.
type QUICListener interface {
// Listen creates a new listening UDPConn.
Listen(addr *net.UDPAddr) (quic.OOBCapablePacketConn, error)
Listen(addr *net.UDPAddr) (quicx.UDPLikeConn, error)
}
// QUICListenerStdlib is a QUICListener using the standard library.
@@ -31,7 +32,7 @@ type QUICListenerStdlib struct{}
var _ QUICListener = &QUICListenerStdlib{}
// Listen implements QUICListener.Listen.
func (qls *QUICListenerStdlib) Listen(addr *net.UDPAddr) (quic.OOBCapablePacketConn, error) {
func (qls *QUICListenerStdlib) Listen(addr *net.UDPAddr) (quicx.UDPLikeConn, error) {
return net.ListenUDP("udp", addr)
}
@@ -118,13 +119,13 @@ func (d *QUICDialerQUICGo) maybeApplyTLSDefaults(config *tls.Config, port int) *
return config
}
// quicSessionOwnsConn ensures that we close the PacketConn.
// quicSessionOwnsConn ensures that we close the UDPLikeConn.
type quicSessionOwnsConn struct {
// EarlySession is the embedded early session
quic.EarlySession
// conn is the connection we own
conn net.PacketConn
conn quicx.UDPLikeConn
}
// CloseWithError implements quic.EarlySession.CloseWithError.
+2 -1
View File
@@ -12,6 +12,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/lucas-clemente/quic-go"
"github.com/ooni/probe-cli/v3/internal/netxmocks"
"github.com/ooni/probe-cli/v3/internal/quicx"
)
func TestQUICDialerQUICGoCannotSplitHostPort(t *testing.T) {
@@ -75,7 +76,7 @@ func TestQUICDialerQUICGoCannotListen(t *testing.T) {
}
systemdialer := QUICDialerQUICGo{
QUICListener: &netxmocks.QUICListener{
MockListen: func(addr *net.UDPAddr) (quic.OOBCapablePacketConn, error) {
MockListen: func(addr *net.UDPAddr) (quicx.UDPLikeConn, error) {
return nil, expected
},
},
+3
View File
@@ -185,6 +185,9 @@ func TestUTLSHandshakerChrome(t *testing.T) {
}
cfg := &tls.Config{ServerName: "google.com"}
conn, err := net.Dial("tcp", "google.com:443")
if err != nil {
t.Fatal("unexpected error", err)
}
conn, _, err = h.Handshake(context.Background(), conn, cfg)
if err != nil {
t.Fatal("unexpected error", err)
+8 -13
View File
@@ -8,15 +8,16 @@ import (
"time"
"github.com/lucas-clemente/quic-go"
"github.com/ooni/probe-cli/v3/internal/quicx"
)
// QUICListener is a mockable netxlite.QUICListener.
type QUICListener struct {
MockListen func(addr *net.UDPAddr) (quic.OOBCapablePacketConn, error)
MockListen func(addr *net.UDPAddr) (quicx.UDPLikeConn, error)
}
// Listen calls MockListen.
func (ql *QUICListener) Listen(addr *net.UDPAddr) (quic.OOBCapablePacketConn, error) {
func (ql *QUICListener) Listen(addr *net.UDPAddr) (quicx.UDPLikeConn, error) {
return ql.MockListen(addr)
}
@@ -132,7 +133,6 @@ func (s *QUICEarlySession) ReceiveMessage() ([]byte, error) {
// QUICUDPConn is an UDP conn used by QUIC.
type QUICUDPConn struct {
MockWriteTo func(p []byte, addr net.Addr) (int, error)
MockReadMsgUDP func(b, oob []byte) (int, int, int, *net.UDPAddr, error)
MockClose func() error
MockLocalAddr func() net.Addr
MockRemoteAddr func() net.Addr
@@ -141,21 +141,16 @@ type QUICUDPConn struct {
MockSetWriteDeadline func(t time.Time) error
MockReadFrom func(p []byte) (n int, addr net.Addr, err error)
MockSyscallConn func() (syscall.RawConn, error)
MockWriteMsgUDP func(b, oob []byte, addr *net.UDPAddr) (n, oobn int, err error)
MockSetReadBuffer func(n int) error
}
var _ quic.OOBCapablePacketConn = &QUICUDPConn{}
var _ quicx.UDPLikeConn = &QUICUDPConn{}
// WriteTo calls MockWriteTo.
func (c *QUICUDPConn) WriteTo(p []byte, addr net.Addr) (int, error) {
return c.MockWriteTo(p, addr)
}
// ReadMsgUDP calls MockReadMsgUDP.
func (c *QUICUDPConn) ReadMsgUDP(b, oob []byte) (int, int, int, *net.UDPAddr, error) {
return c.MockReadMsgUDP(b, oob)
}
// Close calls MockClose.
func (c *QUICUDPConn) Close() error {
return c.MockClose()
@@ -196,7 +191,7 @@ func (c *QUICUDPConn) SyscallConn() (syscall.RawConn, error) {
return c.MockSyscallConn()
}
// WriteMsgUDP calls MockReadMsgUDP.
func (c *QUICUDPConn) WriteMsgUDP(b, oob []byte, addr *net.UDPAddr) (n, oobn int, err error) {
return c.MockWriteMsgUDP(b, oob, addr)
// SetReadBuffer calls MockSetReadBuffer.
func (c *QUICUDPConn) SetReadBuffer(n int) error {
return c.MockSetReadBuffer(n)
}
+6 -41
View File
@@ -12,12 +12,13 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/lucas-clemente/quic-go"
"github.com/ooni/probe-cli/v3/internal/quicx"
)
func TestQUICListenerListen(t *testing.T) {
expected := errors.New("mocked error")
ql := &QUICListener{
MockListen: func(addr *net.UDPAddr) (quic.OOBCapablePacketConn, error) {
MockListen: func(addr *net.UDPAddr) (quicx.UDPLikeConn, error) {
return nil, expected
},
}
@@ -287,33 +288,6 @@ func TestQUICUDPConnWriteTo(t *testing.T) {
}
}
func TestQUICUDPConnReadMsgUDP(t *testing.T) {
expected := errors.New("mocked error")
quc := &QUICUDPConn{
MockReadMsgUDP: func(b, oob []byte) (int, int, int, *net.UDPAddr, error) {
return 0, 0, 0, nil, expected
},
}
b := make([]byte, 128)
oob := make([]byte, 128)
n, oobn, flags, addr, err := quc.ReadMsgUDP(b, oob)
if !errors.Is(err, expected) {
t.Fatal("not the error we expected", err)
}
if n != 0 {
t.Fatal("expected zero here")
}
if oobn != 0 {
t.Fatal("expected zero here")
}
if flags != 0 {
t.Fatal("expected zero here")
}
if addr != nil {
t.Fatal("expected nil here")
}
}
func TestQUICUDPConnClose(t *testing.T) {
expected := errors.New("mocked error")
quc := &QUICUDPConn{
@@ -434,24 +408,15 @@ func TestQUICUDPConnSyscallConn(t *testing.T) {
}
}
func TestQUICUDPConnWriteMsgUDP(t *testing.T) {
func TestQUICUDPConnSetReadBuffer(t *testing.T) {
expected := errors.New("mocked error")
quc := &QUICUDPConn{
MockWriteMsgUDP: func(b, oob []byte, addr *net.UDPAddr) (n int, oobn int, err error) {
return 0, 0, expected
MockSetReadBuffer: func(n int) error {
return expected
},
}
b := make([]byte, 128)
oob := make([]byte, 128)
addr := &net.UDPAddr{}
n, oobn, err := quc.WriteMsgUDP(b, oob, addr)
err := quc.SetReadBuffer(1 << 10)
if !errors.Is(err, expected) {
t.Fatal("not the error we expected", err)
}
if n != 0 {
t.Fatal("expected 0 here")
}
if oobn != 0 {
t.Fatal("expected 0 here")
}
}
+40
View File
@@ -0,0 +1,40 @@
// Package quicx contains lucas-clemente/quic-go extensions.
//
// This code introduces the UDPLikeConn, whose documentation explain
// why we need to introduce this new type. We could not put this
// code inside an existing package because it's used (as of 20 Aug 2021)
// by the netxlite package as well as by the netxmocks package.
package quicx
import (
"net"
"syscall"
)
// UDPLikeConn is a net.PacketConn with some extra functions
// required to convince the QUIC library (lucas-clemente/quic-go)
// to inflate the receive buffer of the connection.
//
// The QUIC library will treat this connection as a "dumb"
// net.PacketConn, calling its ReadFrom and WriteTo methods
// as opposed to more advanced methods that are available
// under Linux and FreeBSD and improve the performance.
//
// It seems fine to avoid performance optimizations, because
// they would complicate the implementation on our side and
// our use cases (blocking and heavy throttling) do not seem
// to require such optimizations.
//
// See https://github.com/ooni/probe/issues/1754 for a more
// comprehensive discussion of UDPLikeConn.
type UDPLikeConn interface {
// An UDPLikeConn is a net.PacketConn conn.
net.PacketConn
// SetReadBuffer allows setting the read buffer.
SetReadBuffer(bytes int) error
// SyscallConn returns a conn suitable for calling syscalls,
// which is also instrumental to setting the read buffer.
SyscallConn() (syscall.RawConn, error)
}