cli: upgrade to lucas-clemente/quic-go@v0.27.0 (#715)

* quic-go upgrade: replaced Session/EarlySession with Connection/EarlyConnection

* quic-go upgrade: added context to RoundTripper.Dial

* quic-go upgrade: made corresponding changes to tutorial

* quic-go upgrade: changed sess variable instances to qconn

* quic-go upgrade: made corresponding changes to tutorial

* cleanup: remove unnecessary comments

Those comments made sense in terms of illustrating the changes
but they're going to be less useful once we merge.

* fix(go.mod): apparently we needed `go1.18.1 mod tidy`

VSCode just warned me about this. It seems fine to apply this
change as part of the pull request at hand.

* cleanup(netxlite): http3dialer can be removed

We used to use http3dialer to glue a QUIC dialer, which had a
context as its first argument, to the Dial function used by the
HTTP3 transport, which did not have a context as its first
argument.

Now that HTTP3 transport has a Dial function taking a context as
its first argument, we don't need http3dialer
anymore, since we can use the QUIC dialer directly.

Cc: @DecFox

* Revert "cleanup(netxlite): http3dialer can be removed"

This reverts commit c62244c620cee5fadcc2ca89d8228c8db0b96add
to investigate the build failure mentioned at
https://github.com/ooni/probe-cli/pull/715#issuecomment-1119450484

* chore(netx): show that test was already broken

We didn't see the breakage before because we were not using
the created transport, but the issue of using a nil dialer was
already present before, we just didn't see it.

Now we understand why removing the http3transport in
c62244c620cee5fadcc2ca89d8228c8db0b96add did cause the
breakage mentioned at
https://github.com/ooni/probe-cli/pull/715#issuecomment-1119450484

* fix(netx): convert broken integration test to working unit test

There's no point in using the network here. Add a fake dialer that
breaks and ensure we're getting the expected error.

We've now improved upon the original test because the original test was
not doing anything while now we're testing whether we get back a QUIC
dialer that _can be used_.

After this commit, I can then readd the cleanup commit
c62244c620cee5fadcc2ca89d8228c8db0b96add and it won't be
broken anymore (at least, this is what I expected to happen).

* Revert "Revert "cleanup(netxlite): http3dialer can be removed""

This reverts commit 0e254bfc6ba3bfd65365ce3d8de2c8ec51b925ff
because now we should have fixed the broken test.

Co-authored-by: decfox <decfox>
Co-authored-by: Simone Basso <bassosimone@gmail.com>
This commit is contained in:
DecFox 2022-05-06 15:54:03 +05:30 committed by GitHub
parent a72cc7151c
commit 5d2afaade4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 285 additions and 294 deletions

3
go.mod
View File

@ -19,7 +19,7 @@ require (
github.com/gorilla/websocket v1.5.0 github.com/gorilla/websocket v1.5.0
github.com/hexops/gotextdiff v1.0.3 github.com/hexops/gotextdiff v1.0.3
github.com/iancoleman/strcase v0.2.0 github.com/iancoleman/strcase v0.2.0
github.com/lucas-clemente/quic-go v0.26.0 github.com/lucas-clemente/quic-go v0.27.0
github.com/marten-seemann/qtls-go1-17 v0.1.1 github.com/marten-seemann/qtls-go1-17 v0.1.1
github.com/mattn/go-colorable v0.1.12 github.com/mattn/go-colorable v0.1.12
github.com/miekg/dns v1.1.48 github.com/miekg/dns v1.1.48
@ -40,6 +40,7 @@ require (
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4
golang.org/x/net v0.0.0-20220412020605-290c469a71a5 golang.org/x/net v0.0.0-20220412020605-290c469a71a5
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 golang.org/x/sys v0.0.0-20220422013727-9388b58f7150
gopkg.in/yaml.v2 v2.4.0
upper.io/db.v3 v3.8.0+incompatible upper.io/db.v3 v3.8.0+incompatible
) )

4
go.sum
View File

@ -437,8 +437,8 @@ github.com/lib/pq v1.10.0 h1:Zx5DJFEYQXio93kgXnQ09fXNiUKsqv4OUEu2UtGcB1E=
github.com/lib/pq v1.10.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/lib/pq v1.10.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM= github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM=
github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4= github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4=
github.com/lucas-clemente/quic-go v0.26.0 h1:ALBQXr9UJ8A1LyzvceX4jd9QFsHvlI0RR6BkV16o00A= github.com/lucas-clemente/quic-go v0.27.0 h1:v6WY87q9zD4dKASbG8hy/LpzAVNzEQzw8sEIeloJsc4=
github.com/lucas-clemente/quic-go v0.26.0/go.mod h1:AzgQoPda7N+3IqMMMkywBKggIFo2KT6pfnlrQ2QieeI= github.com/lucas-clemente/quic-go v0.27.0/go.mod h1:AzgQoPda7N+3IqMMMkywBKggIFo2KT6pfnlrQ2QieeI=
github.com/lunixbochs/vtclean v1.0.0/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI= github.com/lunixbochs/vtclean v1.0.0/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI=
github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ= github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=

View File

@ -59,7 +59,7 @@ func (s *Saver) safeAddrString(addr net.Addr) (out string) {
// QUICDialContext dials a QUIC session using the given dialer // QUICDialContext dials a QUIC session using the given dialer
// and saves the results inside of the saver. // and saves the results inside of the saver.
func (s *Saver) QUICDialContext(ctx context.Context, dialer model.QUICDialer, func (s *Saver) QUICDialContext(ctx context.Context, dialer model.QUICDialer,
network, address string, tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlySession, error) { network, address string, tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlyConnection, error) {
started := time.Now() started := time.Now()
var state tls.ConnectionState var state tls.ConnectionState
sess, err := dialer.DialContext(ctx, network, address, tlsConfig, quicConfig) sess, err := dialer.DialContext(ctx, network, address, tlsConfig, quicConfig)

View File

@ -184,20 +184,20 @@ func TestSaverReadFrom(t *testing.T) {
func TestSaverQUICDialContext(t *testing.T) { func TestSaverQUICDialContext(t *testing.T) {
// newQUICDialer creates a new QUICDialer for testing. // newQUICDialer creates a new QUICDialer for testing.
newQUICDialer := func(sess quic.EarlySession, err error) model.QUICDialer { newQUICDialer := func(qconn quic.EarlyConnection, err error) model.QUICDialer {
return &mocks.QUICDialer{ return &mocks.QUICDialer{
MockDialContext: func( MockDialContext: func(
ctx context.Context, network, address string, tlsConfig *tls.Config, ctx context.Context, network, address string, tlsConfig *tls.Config,
quicConfig *quic.Config) (quic.EarlySession, error) { quicConfig *quic.Config) (quic.EarlyConnection, error) {
time.Sleep(time.Microsecond) time.Sleep(time.Microsecond)
return sess, err return qconn, err
}, },
} }
} }
// newQUICSession creates a new quic.EarlySession for testing. // newQUICConnection creates a new quic.EarlyConnection for testing.
newQUICSession := func(handshakeComplete context.Context, state tls.ConnectionState) quic.EarlySession { newQUICConnection := func(handshakeComplete context.Context, state tls.ConnectionState) quic.EarlyConnection {
return &mocks.QUICEarlySession{ return &mocks.QUICEarlyConnection{
MockHandshakeComplete: func() context.Context { MockHandshakeComplete: func() context.Context {
return handshakeComplete return handshakeComplete
}, },
@ -245,18 +245,18 @@ func TestSaverQUICDialContext(t *testing.T) {
ExpectedFailure: nil, ExpectedFailure: nil,
Saver: saver, Saver: saver,
} }
sess := newQUICSession(handshakeCtx, v.NewTLSConnectionState()) qconn := newQUICConnection(handshakeCtx, v.NewTLSConnectionState())
dialer := newQUICDialer(sess, nil) dialer := newQUICDialer(qconn, nil)
ctx := context.Background() ctx := context.Background()
sess, err := saver.QUICDialContext(ctx, dialer, expectedNetwork, qconn, err := saver.QUICDialContext(ctx, dialer, expectedNetwork,
mockedEndpoint, v.NewTLSConfig(), v.QUICConfig) mockedEndpoint, v.NewTLSConfig(), v.QUICConfig)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if sess == nil { if qconn == nil {
t.Fatal("expected nil sess") t.Fatal("expected nil qconn")
} }
sess.CloseWithError(0, "") qconn.CloseWithError(0, "")
if err := v.Validate(); err != nil { if err := v.Validate(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -287,18 +287,18 @@ func TestSaverQUICDialContext(t *testing.T) {
ExpectedFailure: context.DeadlineExceeded, ExpectedFailure: context.DeadlineExceeded,
Saver: saver, Saver: saver,
} }
sess := newQUICSession(handshakeCtx, tls.ConnectionState{}) qconn := newQUICConnection(handshakeCtx, tls.ConnectionState{})
dialer := newQUICDialer(sess, nil) dialer := newQUICDialer(qconn, nil)
ctx := context.Background() ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, time.Microsecond) ctx, cancel := context.WithTimeout(ctx, time.Microsecond)
defer cancel() defer cancel()
sess, err := saver.QUICDialContext(ctx, dialer, expectedNetwork, qconn, err := saver.QUICDialContext(ctx, dialer, expectedNetwork,
mockedEndpoint, v.NewTLSConfig(), v.QUICConfig) mockedEndpoint, v.NewTLSConfig(), v.QUICConfig)
if !errors.Is(err, context.DeadlineExceeded) { if !errors.Is(err, context.DeadlineExceeded) {
t.Fatal("unexpected error") t.Fatal("unexpected error")
} }
if sess != nil { if qconn != nil {
t.Fatal("expected nil sess") t.Fatal("expected nil connection")
} }
if err := v.Validate(); err != nil { if err := v.Validate(); err != nil {
t.Fatal(err) t.Fatal(err)
@ -330,13 +330,13 @@ func TestSaverQUICDialContext(t *testing.T) {
} }
dialer := newQUICDialer(nil, mockedError) dialer := newQUICDialer(nil, mockedError)
ctx := context.Background() ctx := context.Background()
sess, err := saver.QUICDialContext(ctx, dialer, expectedNetwork, qconn, err := saver.QUICDialContext(ctx, dialer, expectedNetwork,
mockedEndpoint, v.NewTLSConfig(), v.QUICConfig) mockedEndpoint, v.NewTLSConfig(), v.QUICConfig)
if !errors.Is(err, mockedError) { if !errors.Is(err, mockedError) {
t.Fatal("unexpected error") t.Fatal("unexpected error")
} }
if sess != nil { if qconn != nil {
t.Fatal("expected nil sess") t.Fatal("expected nil connection")
} }
if err := v.Validate(); err != nil { if err := v.Validate(); err != nil {
t.Fatal(err) t.Fatal(err)

View File

@ -32,7 +32,7 @@ type fakeQUICDialer struct {
} }
func (d fakeQUICDialer) DialContext(ctx context.Context, network, address string, func (d fakeQUICDialer) DialContext(ctx context.Context, network, address string,
tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlySession, error) { tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlyConnection, error) {
return nil, d.err return nil, d.err
} }

View File

@ -59,12 +59,12 @@ func NewQUICDialerResolver(resolver model.Resolver) model.QUICDialer {
} }
// NewSingleH3Transport creates an http3.RoundTripper. // NewSingleH3Transport creates an http3.RoundTripper.
func NewSingleH3Transport(qsess quic.EarlySession, tlscfg *tls.Config, qcfg *quic.Config) http.RoundTripper { func NewSingleH3Transport(qconn quic.EarlyConnection, tlscfg *tls.Config, qcfg *quic.Config) http.RoundTripper {
transport := &http3.RoundTripper{ transport := &http3.RoundTripper{
DisableCompression: true, DisableCompression: true,
TLSClientConfig: tlscfg, TLSClientConfig: tlscfg,
QuicConfig: qcfg, QuicConfig: qcfg,
Dial: (&SingleDialerH3{qsess: &qsess}).Dial, Dial: (&SingleDialerH3{qconn: &qconn}).Dial,
} }
return transport return transport
} }
@ -117,16 +117,16 @@ func (s *SingleDialer) DialContext(ctx context.Context, network string, addr str
type SingleDialerH3 struct { type SingleDialerH3 struct {
sync.Mutex sync.Mutex
qsess *quic.EarlySession qconn *quic.EarlyConnection
} }
func (s *SingleDialerH3) Dial(network, addr string, tlsCfg *tls.Config, cfg *quic.Config) (quic.EarlySession, error) { func (s *SingleDialerH3) Dial(ctx context.Context, network, addr string, tlsCfg *tls.Config, cfg *quic.Config) (quic.EarlyConnection, error) {
s.Lock() s.Lock()
defer s.Unlock() defer s.Unlock()
if s.qsess == nil { if s.qconn == nil {
return nil, ErrNoConnReuse return nil, ErrNoConnReuse
} }
qs := s.qsess qs := s.qconn
s.qsess = nil s.qconn = nil
return *qs, nil return *qs, nil
} }

View File

@ -17,7 +17,7 @@ type QUICConfig struct {
} }
// QUICDo performs the QUIC check. // QUICDo performs the QUIC check.
func QUICDo(ctx context.Context, config QUICConfig) (quic.EarlySession, error) { func QUICDo(ctx context.Context, config QUICConfig) (quic.EarlyConnection, error) {
if config.QUICDialer != nil { if config.QUICDialer != nil {
return config.QUICDialer.DialContext(ctx, "udp", config.Endpoint, config.TLSConf, &quic.Config{}) return config.QUICDialer.DialContext(ctx, "udp", config.Endpoint, config.TLSConf, &quic.Config{})
} }

View File

@ -1,12 +1,40 @@
package httptransport_test package httptransport_test
import ( import (
"context"
"crypto/tls"
"errors"
"net/http"
"testing" "testing"
"github.com/lucas-clemente/quic-go"
"github.com/ooni/probe-cli/v3/internal/engine/netx/httptransport" "github.com/ooni/probe-cli/v3/internal/engine/netx/httptransport"
"github.com/ooni/probe-cli/v3/internal/model/mocks"
) )
func TestNewHTTP3Transport(t *testing.T) { func TestNewHTTP3Transport(t *testing.T) {
// mainly to cover a line which otherwise won't be directly covered // make sure we can create a working transport using this factory.
httptransport.NewHTTP3Transport(httptransport.Config{}) expected := errors.New("mocked error")
txp := httptransport.NewHTTP3Transport(httptransport.Config{
QUICDialer: &mocks.QUICDialer{
MockDialContext: func(ctx context.Context, network, address string,
tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlyConnection, error) {
return nil, expected
},
MockCloseIdleConnections: func() {
// nothing
},
},
})
req, err := http.NewRequest("GET", "https://google.com", nil)
if err != nil {
t.Fatal(err)
}
resp, err := txp.RoundTrip(req)
if !errors.Is(err, expected) {
t.Fatal("unexpected err", err)
}
if resp != nil {
t.Fatal("expected nil resp")
}
} }

View File

@ -7,6 +7,6 @@ import (
) )
// connectionState returns the ConnectionState of a QUIC Session. // connectionState returns the ConnectionState of a QUIC Session.
func connectionState(sess quic.EarlySession) tls.ConnectionState { func connectionState(sess quic.EarlyConnection) tls.ConnectionState {
return sess.ConnectionState().TLS.ConnectionState return sess.ConnectionState().TLS.ConnectionState
} }

View File

@ -19,7 +19,7 @@ type HandshakeSaver struct {
// DialContext implements ContextDialer.DialContext // DialContext implements ContextDialer.DialContext
func (h HandshakeSaver) DialContext(ctx context.Context, network string, func (h HandshakeSaver) DialContext(ctx context.Context, network string,
host string, tlsCfg *tls.Config, cfg *quic.Config) (quic.EarlySession, error) { host string, tlsCfg *tls.Config, cfg *quic.Config) (quic.EarlyConnection, error) {
start := time.Now() start := time.Now()
// TODO(bassosimone): in the future we probably want to also save // TODO(bassosimone): in the future we probably want to also save
// information about what versions we're willing to accept. // information about what versions we're willing to accept.

View File

@ -18,12 +18,12 @@ import (
type MockDialer struct { type MockDialer struct {
Dialer model.QUICDialer Dialer model.QUICDialer
Sess quic.EarlySession Sess quic.EarlyConnection
Err error Err error
} }
func (d MockDialer) DialContext(ctx context.Context, network, host string, func (d MockDialer) DialContext(ctx context.Context, network, host string,
tlsCfg *tls.Config, cfg *quic.Config) (quic.EarlySession, error) { tlsCfg *tls.Config, cfg *quic.Config) (quic.EarlyConnection, error) {
if d.Dialer != nil { if d.Dialer != nil {
return d.Dialer.DialContext(ctx, network, host, tlsCfg, cfg) return d.Dialer.DialContext(ctx, network, host, tlsCfg, cfg)
} }

View File

@ -93,12 +93,12 @@ func (mx *Measurer) NewHTTPTransportWithTLSConn(
logger, netxlite.NewNullDialer(), netxlite.NewSingleUseTLSDialer(conn))) logger, netxlite.NewNullDialer(), netxlite.NewSingleUseTLSDialer(conn)))
} }
// NewHTTPTransportWithQUICSess creates and wraps an HTTPTransport that // NewHTTPTransportWithQUICConn creates and wraps an HTTPTransport that
// does not dial and only uses the given QUIC session. // does not dial and only uses the given QUIC connection.
func (mx *Measurer) NewHTTPTransportWithQUICSess( func (mx *Measurer) NewHTTPTransportWithQUICConn(
logger model.Logger, db WritableDB, sess quic.EarlySession) *HTTPTransportDB { logger model.Logger, db WritableDB, qconn quic.EarlyConnection) *HTTPTransportDB {
return mx.WrapHTTPTransport(db, netxlite.NewHTTP3Transport( return mx.WrapHTTPTransport(db, netxlite.NewHTTP3Transport(
logger, netxlite.NewSingleUseQUICDialer(sess), &tls.Config{})) logger, netxlite.NewSingleUseQUICDialer(qconn), &tls.Config{}))
} }
// HTTPTransportDB is an implementation of HTTPTransport that // HTTPTransportDB is an implementation of HTTPTransport that

View File

@ -387,11 +387,11 @@ func (mx *Measurer) TLSConnectAndHandshakeWithDB(ctx context.Context,
func (mx *Measurer) QUICHandshake(ctx context.Context, address string, func (mx *Measurer) QUICHandshake(ctx context.Context, address string,
config *tls.Config) *EndpointMeasurement { config *tls.Config) *EndpointMeasurement {
db := &MeasurementDB{} db := &MeasurementDB{}
sess, _ := mx.QUICHandshakeWithDB(ctx, db, address, config) qconn, _ := mx.QUICHandshakeWithDB(ctx, db, address, config)
measurement := db.AsMeasurement() measurement := db.AsMeasurement()
if sess != nil { if qconn != nil {
// TODO(bassosimone): close session with correct message // TODO(bassosimone): close connection with correct message
sess.CloseWithError(0, "") qconn.CloseWithError(0, "")
} }
return &EndpointMeasurement{ return &EndpointMeasurement{
Network: NetworkQUIC, Network: NetworkQUIC,
@ -413,9 +413,9 @@ func (mx *Measurer) quicHandshakeTimeout() time.Duration {
// QUICHandshakeWithDB is like QUICHandshake but uses the given // QUICHandshakeWithDB is like QUICHandshake but uses the given
// db to store events rather than creating a temporary one and // db to store events rather than creating a temporary one and
// use it to generate a new Measuremet. // use it to generate a new Measurement.
func (mx *Measurer) QUICHandshakeWithDB(ctx context.Context, db WritableDB, func (mx *Measurer) QUICHandshakeWithDB(ctx context.Context, db WritableDB,
address string, config *tls.Config) (quic.EarlySession, error) { address string, config *tls.Config) (quic.EarlyConnection, error) {
timeout := mx.quicHandshakeTimeout() timeout := mx.quicHandshakeTimeout()
ol := NewOperationLogger(mx.Logger, ol := NewOperationLogger(mx.Logger,
"QUICHandshake %s with sni=%s", address, config.ServerName) "QUICHandshake %s with sni=%s", address, config.ServerName)
@ -423,9 +423,9 @@ func (mx *Measurer) QUICHandshakeWithDB(ctx context.Context, db WritableDB,
defer cancel() defer cancel()
qd := mx.NewQUICDialerWithoutResolver(db, mx.Logger) qd := mx.NewQUICDialerWithoutResolver(db, mx.Logger)
defer qd.CloseIdleConnections() defer qd.CloseIdleConnections()
sess, err := qd.DialContext(ctx, "udp", address, config, &quic.Config{}) qconn, err := qd.DialContext(ctx, "udp", address, config, &quic.Config{})
ol.Stop(err) ol.Stop(err)
return sess, err return qconn, err
} }
// HTTPEndpointGet performs a GET request for an HTTP endpoint. // HTTPEndpointGet performs a GET request for an HTTP endpoint.
@ -575,7 +575,7 @@ func (mx *Measurer) httpEndpointGetHTTPS(ctx context.Context,
// httpEndpointGetQUIC specializes httpEndpointGetTCP for QUIC. // httpEndpointGetQUIC specializes httpEndpointGetTCP for QUIC.
func (mx *Measurer) httpEndpointGetQUIC(ctx context.Context, func (mx *Measurer) httpEndpointGetQUIC(ctx context.Context,
db WritableDB, epnt *HTTPEndpoint, jar http.CookieJar) (*http.Response, error) { db WritableDB, epnt *HTTPEndpoint, jar http.CookieJar) (*http.Response, error) {
sess, err := mx.QUICHandshakeWithDB(ctx, db, epnt.Address, &tls.Config{ qconn, err := mx.QUICHandshakeWithDB(ctx, db, epnt.Address, &tls.Config{
ServerName: epnt.SNI, ServerName: epnt.SNI,
NextProtos: epnt.ALPN, NextProtos: epnt.ALPN,
RootCAs: netxlite.NewDefaultCertPool(), RootCAs: netxlite.NewDefaultCertPool(),
@ -583,10 +583,10 @@ func (mx *Measurer) httpEndpointGetQUIC(ctx context.Context,
if err != nil { if err != nil {
return nil, err return nil, err
} }
// TODO(bassosimone): close session with correct message // TODO(bassosimone): close connection with correct message
defer sess.CloseWithError(0, "") // we own it defer qconn.CloseWithError(0, "") // we own it
clnt := NewHTTPClientWithoutRedirects(db, jar, clnt := NewHTTPClientWithoutRedirects(db, jar,
mx.NewHTTPTransportWithQUICSess(mx.Logger, db, sess)) mx.NewHTTPTransportWithQUICConn(mx.Logger, db, qconn))
defer clnt.CloseIdleConnections() defer clnt.CloseIdleConnections()
return mx.httpClientDo(ctx, clnt, epnt) return mx.httpClientDo(ctx, clnt, epnt)
} }

View File

@ -106,7 +106,7 @@ type quicDialerDB struct {
} }
func (qh *quicDialerDB) DialContext(ctx context.Context, network, address string, func (qh *quicDialerDB) DialContext(ctx context.Context, network, address string,
tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlySession, error) { tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlyConnection, error) {
started := time.Since(qh.begin).Seconds() started := time.Since(qh.begin).Seconds()
var state tls.ConnectionState var state tls.ConnectionState
listener := &quicListenerDB{ listener := &quicListenerDB{

View File

@ -25,7 +25,7 @@ func (ql *QUICListener) Listen(addr *net.UDPAddr) (model.UDPLikeConn, error) {
type QUICDialer struct { type QUICDialer struct {
// MockDialContext allows mocking DialContext. // MockDialContext allows mocking DialContext.
MockDialContext func(ctx context.Context, network, address string, MockDialContext func(ctx context.Context, network, address string,
tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlySession, error) tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlyConnection, error)
// MockCloseIdleConnections allows mocking CloseIdleConnections. // MockCloseIdleConnections allows mocking CloseIdleConnections.
MockCloseIdleConnections func() MockCloseIdleConnections func()
@ -33,7 +33,7 @@ type QUICDialer struct {
// DialContext calls MockDialContext. // DialContext calls MockDialContext.
func (qcd *QUICDialer) DialContext(ctx context.Context, network, address string, func (qcd *QUICDialer) DialContext(ctx context.Context, network, address string,
tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlySession, error) { tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlyConnection, error) {
return qcd.MockDialContext(ctx, network, address, tlsConfig, quicConfig) return qcd.MockDialContext(ctx, network, address, tlsConfig, quicConfig)
} }
@ -42,8 +42,8 @@ func (qcd *QUICDialer) CloseIdleConnections() {
qcd.MockCloseIdleConnections() qcd.MockCloseIdleConnections()
} }
// QUICEarlySession is a mockable quic.EarlySession. // QUICEarlyConnection is a mockable quic.EarlyConnection.
type QUICEarlySession struct { type QUICEarlyConnection struct {
MockAcceptStream func(context.Context) (quic.Stream, error) MockAcceptStream func(context.Context) (quic.Stream, error)
MockAcceptUniStream func(context.Context) (quic.ReceiveStream, error) MockAcceptUniStream func(context.Context) (quic.ReceiveStream, error)
MockOpenStream func() (quic.Stream, error) MockOpenStream func() (quic.Stream, error)
@ -56,86 +56,86 @@ type QUICEarlySession struct {
MockContext func() context.Context MockContext func() context.Context
MockConnectionState func() quic.ConnectionState MockConnectionState func() quic.ConnectionState
MockHandshakeComplete func() context.Context MockHandshakeComplete func() context.Context
MockNextSession func() quic.Session MockNextConnection func() quic.Connection
MockSendMessage func(b []byte) error MockSendMessage func(b []byte) error
MockReceiveMessage func() ([]byte, error) MockReceiveMessage func() ([]byte, error)
} }
var _ quic.EarlySession = &QUICEarlySession{} var _ quic.EarlyConnection = &QUICEarlyConnection{}
// AcceptStream calls MockAcceptStream. // AcceptStream calls MockAcceptStream.
func (s *QUICEarlySession) AcceptStream(ctx context.Context) (quic.Stream, error) { func (s *QUICEarlyConnection) AcceptStream(ctx context.Context) (quic.Stream, error) {
return s.MockAcceptStream(ctx) return s.MockAcceptStream(ctx)
} }
// AcceptUniStream calls MockAcceptUniStream. // AcceptUniStream calls MockAcceptUniStream.
func (s *QUICEarlySession) AcceptUniStream(ctx context.Context) (quic.ReceiveStream, error) { func (s *QUICEarlyConnection) AcceptUniStream(ctx context.Context) (quic.ReceiveStream, error) {
return s.MockAcceptUniStream(ctx) return s.MockAcceptUniStream(ctx)
} }
// OpenStream calls MockOpenStream. // OpenStream calls MockOpenStream.
func (s *QUICEarlySession) OpenStream() (quic.Stream, error) { func (s *QUICEarlyConnection) OpenStream() (quic.Stream, error) {
return s.MockOpenStream() return s.MockOpenStream()
} }
// OpenStreamSync calls MockOpenStreamSync. // OpenStreamSync calls MockOpenStreamSync.
func (s *QUICEarlySession) OpenStreamSync(ctx context.Context) (quic.Stream, error) { func (s *QUICEarlyConnection) OpenStreamSync(ctx context.Context) (quic.Stream, error) {
return s.MockOpenStreamSync(ctx) return s.MockOpenStreamSync(ctx)
} }
// OpenUniStream calls MockOpenUniStream. // OpenUniStream calls MockOpenUniStream.
func (s *QUICEarlySession) OpenUniStream() (quic.SendStream, error) { func (s *QUICEarlyConnection) OpenUniStream() (quic.SendStream, error) {
return s.MockOpenUniStream() return s.MockOpenUniStream()
} }
// OpenUniStreamSync calls MockOpenUniStreamSync. // OpenUniStreamSync calls MockOpenUniStreamSync.
func (s *QUICEarlySession) OpenUniStreamSync(ctx context.Context) (quic.SendStream, error) { func (s *QUICEarlyConnection) OpenUniStreamSync(ctx context.Context) (quic.SendStream, error) {
return s.MockOpenUniStreamSync(ctx) return s.MockOpenUniStreamSync(ctx)
} }
// LocalAddr class MockLocalAddr. // LocalAddr class MockLocalAddr.
func (c *QUICEarlySession) LocalAddr() net.Addr { func (c *QUICEarlyConnection) LocalAddr() net.Addr {
return c.MockLocalAddr() return c.MockLocalAddr()
} }
// RemoteAddr calls MockRemoteAddr. // RemoteAddr calls MockRemoteAddr.
func (c *QUICEarlySession) RemoteAddr() net.Addr { func (c *QUICEarlyConnection) RemoteAddr() net.Addr {
return c.MockRemoteAddr() return c.MockRemoteAddr()
} }
// CloseWithError calls MockCloseWithError. // CloseWithError calls MockCloseWithError.
func (c *QUICEarlySession) CloseWithError( func (c *QUICEarlyConnection) CloseWithError(
code quic.ApplicationErrorCode, reason string) error { code quic.ApplicationErrorCode, reason string) error {
return c.MockCloseWithError(code, reason) return c.MockCloseWithError(code, reason)
} }
// Context calls MockContext. // Context calls MockContext.
func (s *QUICEarlySession) Context() context.Context { func (s *QUICEarlyConnection) Context() context.Context {
return s.MockContext() return s.MockContext()
} }
// ConnectionState calls MockConnectionState. // ConnectionState calls MockConnectionState.
func (s *QUICEarlySession) ConnectionState() quic.ConnectionState { func (s *QUICEarlyConnection) ConnectionState() quic.ConnectionState {
return s.MockConnectionState() return s.MockConnectionState()
} }
// HandshakeComplete calls MockHandshakeComplete. // HandshakeComplete calls MockHandshakeComplete.
func (s *QUICEarlySession) HandshakeComplete() context.Context { func (s *QUICEarlyConnection) HandshakeComplete() context.Context {
return s.MockHandshakeComplete() return s.MockHandshakeComplete()
} }
// NextSession calls MockNextSession. // NextConnection calls MockNextConnection.
func (s *QUICEarlySession) NextSession() quic.Session { func (s *QUICEarlyConnection) NextConnection() quic.Connection {
return s.MockNextSession() return s.MockNextConnection()
} }
// SendMessage calls MockSendMessage. // SendMessage calls MockSendMessage.
func (s *QUICEarlySession) SendMessage(b []byte) error { func (s *QUICEarlyConnection) SendMessage(b []byte) error {
return s.MockSendMessage(b) return s.MockSendMessage(b)
} }
// ReceiveMessage calls MockReceiveMessage. // ReceiveMessage calls MockReceiveMessage.
func (s *QUICEarlySession) ReceiveMessage() ([]byte, error) { func (s *QUICEarlyConnection) ReceiveMessage() ([]byte, error) {
return s.MockReceiveMessage() return s.MockReceiveMessage()
} }

View File

@ -37,19 +37,19 @@ func TestQUICDialer(t *testing.T) {
t.Run("DialContext", func(t *testing.T) { t.Run("DialContext", func(t *testing.T) {
expected := errors.New("mocked error") expected := errors.New("mocked error")
qcd := &QUICDialer{ qcd := &QUICDialer{
MockDialContext: func(ctx context.Context, network string, address string, tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlySession, error) { MockDialContext: func(ctx context.Context, network string, address string, tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlyConnection, error) {
return nil, expected return nil, expected
}, },
} }
ctx := context.Background() ctx := context.Background()
tlsConfig := &tls.Config{} tlsConfig := &tls.Config{}
quicConfig := &quic.Config{} quicConfig := &quic.Config{}
sess, err := qcd.DialContext(ctx, "udp", "dns.google:443", tlsConfig, quicConfig) qconn, err := qcd.DialContext(ctx, "udp", "dns.google:443", tlsConfig, quicConfig)
if !errors.Is(err, expected) { if !errors.Is(err, expected) {
t.Fatal("not the error we expected") t.Fatal("not the error we expected")
} }
if sess != nil { if qconn != nil {
t.Fatal("expected nil session") t.Fatal("expected nil connection")
} }
}) })
@ -67,16 +67,16 @@ func TestQUICDialer(t *testing.T) {
}) })
} }
func TestQUICEarlySession(t *testing.T) { func TestQUICEarlyConnection(t *testing.T) {
t.Run("AcceptStream", func(t *testing.T) { t.Run("AcceptStream", func(t *testing.T) {
expected := errors.New("mocked error") expected := errors.New("mocked error")
sess := &QUICEarlySession{ qconn := &QUICEarlyConnection{
MockAcceptStream: func(ctx context.Context) (quic.Stream, error) { MockAcceptStream: func(ctx context.Context) (quic.Stream, error) {
return nil, expected return nil, expected
}, },
} }
ctx := context.Background() ctx := context.Background()
stream, err := sess.AcceptStream(ctx) stream, err := qconn.AcceptStream(ctx)
if !errors.Is(err, expected) { if !errors.Is(err, expected) {
t.Fatal("not the error we expected", err) t.Fatal("not the error we expected", err)
} }
@ -87,13 +87,13 @@ func TestQUICEarlySession(t *testing.T) {
t.Run("AcceptUniStream", func(t *testing.T) { t.Run("AcceptUniStream", func(t *testing.T) {
expected := errors.New("mocked error") expected := errors.New("mocked error")
sess := &QUICEarlySession{ qconn := &QUICEarlyConnection{
MockAcceptUniStream: func(ctx context.Context) (quic.ReceiveStream, error) { MockAcceptUniStream: func(ctx context.Context) (quic.ReceiveStream, error) {
return nil, expected return nil, expected
}, },
} }
ctx := context.Background() ctx := context.Background()
stream, err := sess.AcceptUniStream(ctx) stream, err := qconn.AcceptUniStream(ctx)
if !errors.Is(err, expected) { if !errors.Is(err, expected) {
t.Fatal("not the error we expected", err) t.Fatal("not the error we expected", err)
} }
@ -104,12 +104,12 @@ func TestQUICEarlySession(t *testing.T) {
t.Run("OpenStream", func(t *testing.T) { t.Run("OpenStream", func(t *testing.T) {
expected := errors.New("mocked error") expected := errors.New("mocked error")
sess := &QUICEarlySession{ qconn := &QUICEarlyConnection{
MockOpenStream: func() (quic.Stream, error) { MockOpenStream: func() (quic.Stream, error) {
return nil, expected return nil, expected
}, },
} }
stream, err := sess.OpenStream() stream, err := qconn.OpenStream()
if !errors.Is(err, expected) { if !errors.Is(err, expected) {
t.Fatal("not the error we expected", err) t.Fatal("not the error we expected", err)
} }
@ -120,13 +120,13 @@ func TestQUICEarlySession(t *testing.T) {
t.Run("OpenStreamSync", func(t *testing.T) { t.Run("OpenStreamSync", func(t *testing.T) {
expected := errors.New("mocked error") expected := errors.New("mocked error")
sess := &QUICEarlySession{ qconn := &QUICEarlyConnection{
MockOpenStreamSync: func(ctx context.Context) (quic.Stream, error) { MockOpenStreamSync: func(ctx context.Context) (quic.Stream, error) {
return nil, expected return nil, expected
}, },
} }
ctx := context.Background() ctx := context.Background()
stream, err := sess.OpenStreamSync(ctx) stream, err := qconn.OpenStreamSync(ctx)
if !errors.Is(err, expected) { if !errors.Is(err, expected) {
t.Fatal("not the error we expected", err) t.Fatal("not the error we expected", err)
} }
@ -137,12 +137,12 @@ func TestQUICEarlySession(t *testing.T) {
t.Run("OpenUniStream", func(t *testing.T) { t.Run("OpenUniStream", func(t *testing.T) {
expected := errors.New("mocked error") expected := errors.New("mocked error")
sess := &QUICEarlySession{ qconn := &QUICEarlyConnection{
MockOpenUniStream: func() (quic.SendStream, error) { MockOpenUniStream: func() (quic.SendStream, error) {
return nil, expected return nil, expected
}, },
} }
stream, err := sess.OpenUniStream() stream, err := qconn.OpenUniStream()
if !errors.Is(err, expected) { if !errors.Is(err, expected) {
t.Fatal("not the error we expected", err) t.Fatal("not the error we expected", err)
} }
@ -153,13 +153,13 @@ func TestQUICEarlySession(t *testing.T) {
t.Run("OpenUniStreamSync", func(t *testing.T) { t.Run("OpenUniStreamSync", func(t *testing.T) {
expected := errors.New("mocked error") expected := errors.New("mocked error")
sess := &QUICEarlySession{ qconn := &QUICEarlyConnection{
MockOpenUniStreamSync: func(ctx context.Context) (quic.SendStream, error) { MockOpenUniStreamSync: func(ctx context.Context) (quic.SendStream, error) {
return nil, expected return nil, expected
}, },
} }
ctx := context.Background() ctx := context.Background()
stream, err := sess.OpenUniStreamSync(ctx) stream, err := qconn.OpenUniStreamSync(ctx)
if !errors.Is(err, expected) { if !errors.Is(err, expected) {
t.Fatal("not the error we expected", err) t.Fatal("not the error we expected", err)
} }
@ -169,24 +169,24 @@ func TestQUICEarlySession(t *testing.T) {
}) })
t.Run("LocalAddr", func(t *testing.T) { t.Run("LocalAddr", func(t *testing.T) {
sess := &QUICEarlySession{ qconn := &QUICEarlyConnection{
MockLocalAddr: func() net.Addr { MockLocalAddr: func() net.Addr {
return &net.UDPAddr{} return &net.UDPAddr{}
}, },
} }
addr := sess.LocalAddr() addr := qconn.LocalAddr()
if !reflect.ValueOf(addr).Elem().IsZero() { if !reflect.ValueOf(addr).Elem().IsZero() {
t.Fatal("expected a zero address here") t.Fatal("expected a zero address here")
} }
}) })
t.Run("RemoteAddr", func(t *testing.T) { t.Run("RemoteAddr", func(t *testing.T) {
sess := &QUICEarlySession{ qconn := &QUICEarlyConnection{
MockRemoteAddr: func() net.Addr { MockRemoteAddr: func() net.Addr {
return &net.UDPAddr{} return &net.UDPAddr{}
}, },
} }
addr := sess.RemoteAddr() addr := qconn.RemoteAddr()
if !reflect.ValueOf(addr).Elem().IsZero() { if !reflect.ValueOf(addr).Elem().IsZero() {
t.Fatal("expected a zero address here") t.Fatal("expected a zero address here")
} }
@ -194,13 +194,13 @@ func TestQUICEarlySession(t *testing.T) {
t.Run("CloseWithError", func(t *testing.T) { t.Run("CloseWithError", func(t *testing.T) {
expected := errors.New("mocked error") expected := errors.New("mocked error")
sess := &QUICEarlySession{ qconn := &QUICEarlyConnection{
MockCloseWithError: func( MockCloseWithError: func(
code quic.ApplicationErrorCode, reason string) error { code quic.ApplicationErrorCode, reason string) error {
return expected return expected
}, },
} }
err := sess.CloseWithError(0, "") err := qconn.CloseWithError(0, "")
if !errors.Is(err, expected) { if !errors.Is(err, expected) {
t.Fatal("not the error we expected", err) t.Fatal("not the error we expected", err)
} }
@ -208,12 +208,12 @@ func TestQUICEarlySession(t *testing.T) {
t.Run("Context", func(t *testing.T) { t.Run("Context", func(t *testing.T) {
ctx := context.Background() ctx := context.Background()
sess := &QUICEarlySession{ qconn := &QUICEarlyConnection{
MockContext: func() context.Context { MockContext: func() context.Context {
return ctx return ctx
}, },
} }
out := sess.Context() out := qconn.Context()
if !reflect.DeepEqual(ctx, out) { if !reflect.DeepEqual(ctx, out) {
t.Fatal("not the context we expected") t.Fatal("not the context we expected")
} }
@ -221,12 +221,12 @@ func TestQUICEarlySession(t *testing.T) {
t.Run("ConnectionState", func(t *testing.T) { t.Run("ConnectionState", func(t *testing.T) {
state := quic.ConnectionState{SupportsDatagrams: true} state := quic.ConnectionState{SupportsDatagrams: true}
sess := &QUICEarlySession{ qconn := &QUICEarlyConnection{
MockConnectionState: func() quic.ConnectionState { MockConnectionState: func() quic.ConnectionState {
return state return state
}, },
} }
out := sess.ConnectionState() out := qconn.ConnectionState()
if !reflect.DeepEqual(state, out) { if !reflect.DeepEqual(state, out) {
t.Fatal("not the context we expected") t.Fatal("not the context we expected")
} }
@ -234,25 +234,25 @@ func TestQUICEarlySession(t *testing.T) {
t.Run("HandshakeComplete", func(t *testing.T) { t.Run("HandshakeComplete", func(t *testing.T) {
ctx := context.Background() ctx := context.Background()
sess := &QUICEarlySession{ qconn := &QUICEarlyConnection{
MockHandshakeComplete: func() context.Context { MockHandshakeComplete: func() context.Context {
return ctx return ctx
}, },
} }
out := sess.HandshakeComplete() out := qconn.HandshakeComplete()
if !reflect.DeepEqual(ctx, out) { if !reflect.DeepEqual(ctx, out) {
t.Fatal("not the context we expected") t.Fatal("not the context we expected")
} }
}) })
t.Run("NextSession", func(t *testing.T) { t.Run("NextConnection", func(t *testing.T) {
next := &QUICEarlySession{} next := &QUICEarlyConnection{}
sess := &QUICEarlySession{ qconn := &QUICEarlyConnection{
MockNextSession: func() quic.Session { MockNextConnection: func() quic.Connection {
return next return next
}, },
} }
out := sess.NextSession() out := qconn.NextConnection()
if !reflect.DeepEqual(next, out) { if !reflect.DeepEqual(next, out) {
t.Fatal("not the context we expected") t.Fatal("not the context we expected")
} }
@ -260,13 +260,13 @@ func TestQUICEarlySession(t *testing.T) {
t.Run("SendMessage", func(t *testing.T) { t.Run("SendMessage", func(t *testing.T) {
expected := errors.New("mocked error") expected := errors.New("mocked error")
sess := &QUICEarlySession{ qconn := &QUICEarlyConnection{
MockSendMessage: func(b []byte) error { MockSendMessage: func(b []byte) error {
return expected return expected
}, },
} }
b := make([]byte, 17) b := make([]byte, 17)
err := sess.SendMessage(b) err := qconn.SendMessage(b)
if !errors.Is(err, expected) { if !errors.Is(err, expected) {
t.Fatal("not the error we expected", err) t.Fatal("not the error we expected", err)
} }
@ -274,12 +274,12 @@ func TestQUICEarlySession(t *testing.T) {
t.Run("ReceiveMessage", func(t *testing.T) { t.Run("ReceiveMessage", func(t *testing.T) {
expected := errors.New("mocked error") expected := errors.New("mocked error")
sess := &QUICEarlySession{ qconn := &QUICEarlyConnection{
MockReceiveMessage: func() ([]byte, error) { MockReceiveMessage: func() ([]byte, error) {
return nil, expected return nil, expected
}, },
} }
b, err := sess.ReceiveMessage() b, err := qconn.ReceiveMessage()
if !errors.Is(err, expected) { if !errors.Is(err, expected) {
t.Fatal("not the error we expected", err) t.Fatal("not the error we expected", err)
} }

View File

@ -152,7 +152,7 @@ type QUICDialer interface {
// //
// Typically, you want to pass `&quic.Config{}` as quicConfig. // Typically, you want to pass `&quic.Config{}` as quicConfig.
DialContext(ctx context.Context, network, address string, DialContext(ctx context.Context, network, address string,
tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlySession, error) tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlyConnection, error)
// CloseIdleConnections closes idle connections, if any. // CloseIdleConnections closes idle connections, if any.
CloseIdleConnections() CloseIdleConnections()

View File

@ -1,30 +1,14 @@
package netxlite package netxlite
import ( import (
"context"
"crypto/tls" "crypto/tls"
"io" "io"
"net/http" "net/http"
"github.com/lucas-clemente/quic-go"
"github.com/lucas-clemente/quic-go/http3" "github.com/lucas-clemente/quic-go/http3"
"github.com/ooni/probe-cli/v3/internal/model" "github.com/ooni/probe-cli/v3/internal/model"
) )
// http3Dialer adapts a QUICContextDialer to work with
// an http3.RoundTripper. This is necessary because the
// http3.RoundTripper does not support DialContext.
type http3Dialer struct {
model.QUICDialer
}
// dial is like QUICContextDialer.DialContext but without context.
func (d *http3Dialer) dial(network, address string, tlsConfig *tls.Config,
quicConfig *quic.Config) (quic.EarlySession, error) {
return d.QUICDialer.DialContext(
context.Background(), network, address, tlsConfig, quicConfig)
}
// http3RoundTripper is the abstract type of quic-go/http3.RoundTripper. // http3RoundTripper is the abstract type of quic-go/http3.RoundTripper.
type http3RoundTripper interface { type http3RoundTripper interface {
http.RoundTripper http.RoundTripper
@ -63,7 +47,7 @@ func NewHTTP3Transport(
return &httpTransportLogger{ return &httpTransportLogger{
HTTPTransport: &http3Transport{ HTTPTransport: &http3Transport{
child: &http3.RoundTripper{ child: &http3.RoundTripper{
Dial: (&http3Dialer{dialer}).dial, Dial: dialer.DialContext,
// The following (1) reduces the number of headers that Go will // The following (1) reduces the number of headers that Go will
// automatically send for us and (2) ensures that we always receive // automatically send for us and (2) ensures that we always receive
// back the true headers, such as Content-Length. This change is // back the true headers, such as Content-Length. This change is

View File

@ -1,39 +1,17 @@
package netxlite package netxlite
import ( import (
"context"
"crypto/tls" "crypto/tls"
"errors" "errors"
"net/http" "net/http"
"testing" "testing"
"github.com/apex/log" "github.com/apex/log"
"github.com/lucas-clemente/quic-go"
"github.com/lucas-clemente/quic-go/http3" "github.com/lucas-clemente/quic-go/http3"
"github.com/ooni/probe-cli/v3/internal/model/mocks" "github.com/ooni/probe-cli/v3/internal/model/mocks"
nlmocks "github.com/ooni/probe-cli/v3/internal/netxlite/mocks" nlmocks "github.com/ooni/probe-cli/v3/internal/netxlite/mocks"
) )
func TestHTTP3Dialer(t *testing.T) {
t.Run("Dial", func(t *testing.T) {
expected := errors.New("mocked error")
d := &http3Dialer{
QUICDialer: &mocks.QUICDialer{
MockDialContext: func(ctx context.Context, network, address string, tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlySession, error) {
return nil, expected
},
},
}
sess, err := d.dial("", "", &tls.Config{}, &quic.Config{})
if !errors.Is(err, expected) {
t.Fatal("unexpected err", err)
}
if sess != nil {
t.Fatal("unexpected resp")
}
})
}
func TestHTTP3Transport(t *testing.T) { func TestHTTP3Transport(t *testing.T) {
t.Run("CloseIdleConnections", func(t *testing.T) { t.Run("CloseIdleConnections", func(t *testing.T) {
var ( var (

View File

@ -12,11 +12,11 @@ import (
// DEPRECATED: please use QUICDialer. // DEPRECATED: please use QUICDialer.
type QUICContextDialer struct { type QUICContextDialer struct {
MockDialContext func(ctx context.Context, network, address string, MockDialContext func(ctx context.Context, network, address string,
tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlySession, error) tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlyConnection, error)
} }
// DialContext calls MockDialContext. // DialContext calls MockDialContext.
func (qcd *QUICContextDialer) DialContext(ctx context.Context, network, address string, func (qcd *QUICContextDialer) DialContext(ctx context.Context, network, address string,
tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlySession, error) { tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlyConnection, error) {
return qcd.MockDialContext(ctx, network, address, tlsConfig, quicConfig) return qcd.MockDialContext(ctx, network, address, tlsConfig, quicConfig)
} }

View File

@ -13,7 +13,7 @@ func TestQUICContextDialer(t *testing.T) {
t.Run("DialContext", func(t *testing.T) { t.Run("DialContext", func(t *testing.T) {
expected := errors.New("mocked error") expected := errors.New("mocked error")
qcd := &QUICContextDialer{ qcd := &QUICContextDialer{
MockDialContext: func(ctx context.Context, network string, address string, tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlySession, error) { MockDialContext: func(ctx context.Context, network string, address string, tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlyConnection, error) {
return nil, expected return nil, expected
}, },
} }

View File

@ -83,7 +83,7 @@ type quicDialerQUICGo struct {
// mockDialEarlyContext allows to mock quic.DialEarlyContext. // mockDialEarlyContext allows to mock quic.DialEarlyContext.
mockDialEarlyContext func(ctx context.Context, pconn net.PacketConn, mockDialEarlyContext func(ctx context.Context, pconn net.PacketConn,
remoteAddr net.Addr, host string, tlsConfig *tls.Config, remoteAddr net.Addr, host string, tlsConfig *tls.Config,
quicConfig *quic.Config) (quic.EarlySession, error) quicConfig *quic.Config) (quic.EarlyConnection, error)
} }
var _ model.QUICDialer = &quicDialerQUICGo{} var _ model.QUICDialer = &quicDialerQUICGo{}
@ -101,7 +101,7 @@ var errInvalidIP = errors.New("netxlite: invalid IP")
// then we configure, respectively, "h3" and "dq". // then we configure, respectively, "h3" and "dq".
func (d *quicDialerQUICGo) DialContext(ctx context.Context, network string, func (d *quicDialerQUICGo) DialContext(ctx context.Context, network string,
address string, tlsConfig *tls.Config, quicConfig *quic.Config) ( address string, tlsConfig *tls.Config, quicConfig *quic.Config) (
quic.EarlySession, error) { quic.EarlyConnection, error) {
onlyhost, onlyport, err := net.SplitHostPort(address) onlyhost, onlyport, err := net.SplitHostPort(address)
if err != nil { if err != nil {
return nil, err return nil, err
@ -120,18 +120,18 @@ func (d *quicDialerQUICGo) DialContext(ctx context.Context, network string,
} }
udpAddr := &net.UDPAddr{IP: ip, Port: port, Zone: ""} udpAddr := &net.UDPAddr{IP: ip, Port: port, Zone: ""}
tlsConfig = d.maybeApplyTLSDefaults(tlsConfig, port) tlsConfig = d.maybeApplyTLSDefaults(tlsConfig, port)
sess, err := d.dialEarlyContext( qconn, err := d.dialEarlyContext(
ctx, pconn, udpAddr, address, tlsConfig, quicConfig) ctx, pconn, udpAddr, address, tlsConfig, quicConfig)
if err != nil { if err != nil {
pconn.Close() // we own it on failure pconn.Close() // we own it on failure
return nil, err return nil, err
} }
return &quicSessionOwnsConn{EarlySession: sess, conn: pconn}, nil return &quicConnectionOwnsConn{EarlyConnection: qconn, conn: pconn}, nil
} }
func (d *quicDialerQUICGo) dialEarlyContext(ctx context.Context, func (d *quicDialerQUICGo) dialEarlyContext(ctx context.Context,
pconn net.PacketConn, remoteAddr net.Addr, address string, pconn net.PacketConn, remoteAddr net.Addr, address string,
tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlySession, error) { tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlyConnection, error) {
if d.mockDialEarlyContext != nil { if d.mockDialEarlyContext != nil {
return d.mockDialEarlyContext( return d.mockDialEarlyContext(
ctx, pconn, remoteAddr, address, tlsConfig, quicConfig) ctx, pconn, remoteAddr, address, tlsConfig, quicConfig)
@ -164,20 +164,20 @@ func (d *quicDialerQUICGo) CloseIdleConnections() {
// nothing to do // nothing to do
} }
// quicSessionOwnsConn ensures that we close the UDPLikeConn. // quicConnectionOwnsConn ensures that we close the UDPLikeConn.
type quicSessionOwnsConn struct { type quicConnectionOwnsConn struct {
// EarlySession is the embedded early session // EarlyConnection is the embedded early connection
quic.EarlySession quic.EarlyConnection
// conn is the connection we own // conn is the connection we own
conn model.UDPLikeConn conn model.UDPLikeConn
} }
// CloseWithError implements quic.EarlySession.CloseWithError. // CloseWithError implements quic.EarlyConnection.CloseWithError.
func (sess *quicSessionOwnsConn) CloseWithError( func (qconn *quicConnectionOwnsConn) CloseWithError(
code quic.ApplicationErrorCode, reason string) error { code quic.ApplicationErrorCode, reason string) error {
err := sess.EarlySession.CloseWithError(code, reason) err := qconn.EarlyConnection.CloseWithError(code, reason)
sess.conn.Close() qconn.conn.Close()
return err return err
} }
@ -200,7 +200,7 @@ var _ model.QUICDialer = &quicDialerResolver{}
// contained inside of the `address` endpoint. // contained inside of the `address` endpoint.
func (d *quicDialerResolver) DialContext( func (d *quicDialerResolver) DialContext(
ctx context.Context, network, address string, ctx context.Context, network, address string,
tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlySession, error) { tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlyConnection, error) {
onlyhost, onlyport, err := net.SplitHostPort(address) onlyhost, onlyport, err := net.SplitHostPort(address)
if err != nil { if err != nil {
return nil, err return nil, err
@ -217,10 +217,10 @@ func (d *quicDialerResolver) DialContext(
var errorslist []error var errorslist []error
for _, addr := range addrs { for _, addr := range addrs {
target := net.JoinHostPort(addr, onlyport) target := net.JoinHostPort(addr, onlyport)
sess, err := d.Dialer.DialContext( qconn, err := d.Dialer.DialContext(
ctx, network, target, tlsConfig, quicConfig) ctx, network, target, tlsConfig, quicConfig)
if err == nil { if err == nil {
return sess, nil return qconn, nil
} }
errorslist = append(errorslist, err) errorslist = append(errorslist, err)
} }
@ -272,16 +272,16 @@ var _ model.QUICDialer = &quicDialerLogger{}
// DialContext implements QUICContextDialer.DialContext. // DialContext implements QUICContextDialer.DialContext.
func (d *quicDialerLogger) DialContext( func (d *quicDialerLogger) DialContext(
ctx context.Context, network, address string, ctx context.Context, network, address string,
tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlySession, error) { tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlyConnection, error) {
d.Logger.Debugf("quic_dial%s %s/%s...", d.operationSuffix, address, network) d.Logger.Debugf("quic_dial%s %s/%s...", d.operationSuffix, address, network)
sess, err := d.Dialer.DialContext(ctx, network, address, tlsConfig, quicConfig) qconn, err := d.Dialer.DialContext(ctx, network, address, tlsConfig, quicConfig)
if err != nil { if err != nil {
d.Logger.Debugf("quic_dial%s %s/%s... %s", d.operationSuffix, d.Logger.Debugf("quic_dial%s %s/%s... %s", d.operationSuffix,
address, network, err) address, network, err)
return nil, err return nil, err
} }
d.Logger.Debugf("quic_dial%s %s/%s... ok", d.operationSuffix, address, network) d.Logger.Debugf("quic_dial%s %s/%s... ok", d.operationSuffix, address, network)
return sess, nil return qconn, nil
} }
// CloseIdleConnections implements QUICDialer.CloseIdleConnections. // CloseIdleConnections implements QUICDialer.CloseIdleConnections.
@ -290,14 +290,14 @@ func (d *quicDialerLogger) CloseIdleConnections() {
} }
// NewSingleUseQUICDialer is like NewSingleUseDialer but for QUIC. // NewSingleUseQUICDialer is like NewSingleUseDialer but for QUIC.
func NewSingleUseQUICDialer(sess quic.EarlySession) model.QUICDialer { func NewSingleUseQUICDialer(qconn quic.EarlyConnection) model.QUICDialer {
return &quicDialerSingleUse{sess: sess} return &quicDialerSingleUse{qconn: qconn}
} }
// quicDialerSingleUse is the QUICDialer returned by NewSingleQUICDialer. // quicDialerSingleUse is the QUICDialer returned by NewSingleQUICDialer.
type quicDialerSingleUse struct { type quicDialerSingleUse struct {
sync.Mutex sync.Mutex
sess quic.EarlySession qconn quic.EarlyConnection
} }
var _ model.QUICDialer = &quicDialerSingleUse{} var _ model.QUICDialer = &quicDialerSingleUse{}
@ -305,15 +305,15 @@ var _ model.QUICDialer = &quicDialerSingleUse{}
// DialContext implements QUICDialer.DialContext. // DialContext implements QUICDialer.DialContext.
func (s *quicDialerSingleUse) DialContext( func (s *quicDialerSingleUse) DialContext(
ctx context.Context, network, addr string, tlsCfg *tls.Config, ctx context.Context, network, addr string, tlsCfg *tls.Config,
cfg *quic.Config) (quic.EarlySession, error) { cfg *quic.Config) (quic.EarlyConnection, error) {
var sess quic.EarlySession var qconn quic.EarlyConnection
defer s.Unlock() defer s.Unlock()
s.Lock() s.Lock()
if s.sess == nil { if s.qconn == nil {
return nil, ErrNoConnReuse return nil, ErrNoConnReuse
} }
sess, s.sess = s.sess, nil qconn, s.qconn = s.qconn, nil
return sess, nil return qconn, nil
} }
// CloseIdleConnections closes idle connections. // CloseIdleConnections closes idle connections.
@ -381,11 +381,11 @@ type quicDialerErrWrapper struct {
// DialContext implements ContextDialer.DialContext // DialContext implements ContextDialer.DialContext
func (d *quicDialerErrWrapper) DialContext( func (d *quicDialerErrWrapper) DialContext(
ctx context.Context, network string, host string, ctx context.Context, network string, host string,
tlsCfg *tls.Config, cfg *quic.Config) (quic.EarlySession, error) { tlsCfg *tls.Config, cfg *quic.Config) (quic.EarlyConnection, error) {
sess, err := d.QUICDialer.DialContext(ctx, network, host, tlsCfg, cfg) qconn, err := d.QUICDialer.DialContext(ctx, network, host, tlsCfg, cfg)
if err != nil { if err != nil {
return nil, NewErrWrapper( return nil, NewErrWrapper(
classifyQUICHandshakeError, QUICHandshakeOperation, err) classifyQUICHandshakeError, QUICHandshakeOperation, err)
} }
return sess, nil return qconn, nil
} }

View File

@ -55,13 +55,13 @@ func TestQUICDialerQUICGo(t *testing.T) {
} }
defer systemdialer.CloseIdleConnections() // just to see it running defer systemdialer.CloseIdleConnections() // just to see it running
ctx := context.Background() ctx := context.Background()
sess, err := systemdialer.DialContext( qconn, err := systemdialer.DialContext(
ctx, "udp", "a.b.c.d", tlsConfig, &quic.Config{}) ctx, "udp", "a.b.c.d", tlsConfig, &quic.Config{})
if err == nil || !strings.HasSuffix(err.Error(), "missing port in address") { if err == nil || !strings.HasSuffix(err.Error(), "missing port in address") {
t.Fatal("not the error we expected", err) t.Fatal("not the error we expected", err)
} }
if sess != nil { if qconn != nil {
t.Fatal("expected nil sess here") t.Fatal("expected nil connection here")
} }
}) })
@ -73,13 +73,13 @@ func TestQUICDialerQUICGo(t *testing.T) {
QUICListener: &quicListenerStdlib{}, QUICListener: &quicListenerStdlib{},
} }
ctx := context.Background() ctx := context.Background()
sess, err := systemdialer.DialContext( qconn, err := systemdialer.DialContext(
ctx, "udp", "8.8.4.4:xyz", tlsConfig, &quic.Config{}) ctx, "udp", "8.8.4.4:xyz", tlsConfig, &quic.Config{})
if err == nil || !strings.HasSuffix(err.Error(), "invalid syntax") { if err == nil || !strings.HasSuffix(err.Error(), "invalid syntax") {
t.Fatal("not the error we expected", err) t.Fatal("not the error we expected", err)
} }
if sess != nil { if qconn != nil {
t.Fatal("expected nil sess here") t.Fatal("expected nil connection here")
} }
}) })
@ -91,13 +91,13 @@ func TestQUICDialerQUICGo(t *testing.T) {
QUICListener: &quicListenerStdlib{}, QUICListener: &quicListenerStdlib{},
} }
ctx := context.Background() ctx := context.Background()
sess, err := systemdialer.DialContext( qconn, err := systemdialer.DialContext(
ctx, "udp", "a.b.c.d:0", tlsConfig, &quic.Config{}) ctx, "udp", "a.b.c.d:0", tlsConfig, &quic.Config{})
if !errors.Is(err, errInvalidIP) { if !errors.Is(err, errInvalidIP) {
t.Fatal("not the error we expected", err) t.Fatal("not the error we expected", err)
} }
if sess != nil { if qconn != nil {
t.Fatal("expected nil sess here") t.Fatal("expected nil connection here")
} }
}) })
@ -114,13 +114,13 @@ func TestQUICDialerQUICGo(t *testing.T) {
}, },
} }
ctx := context.Background() ctx := context.Background()
sess, err := systemdialer.DialContext( qconn, err := systemdialer.DialContext(
ctx, "udp", "8.8.8.8:443", tlsConfig, &quic.Config{}) ctx, "udp", "8.8.8.8:443", tlsConfig, &quic.Config{})
if !errors.Is(err, expected) { if !errors.Is(err, expected) {
t.Fatal("not the error we expected", err) t.Fatal("not the error we expected", err)
} }
if sess != nil { if qconn != nil {
t.Fatal("expected nil sess here") t.Fatal("expected nil connection here")
} }
}) })
@ -133,13 +133,13 @@ func TestQUICDialerQUICGo(t *testing.T) {
} }
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
cancel() // fail immediately cancel() // fail immediately
sess, err := systemdialer.DialContext( qconn, err := systemdialer.DialContext(
ctx, "udp", "8.8.8.8:443", tlsConfig, &quic.Config{}) ctx, "udp", "8.8.8.8:443", tlsConfig, &quic.Config{})
if !errors.Is(err, context.Canceled) { if !errors.Is(err, context.Canceled) {
t.Fatal("not the error we expected", err) t.Fatal("not the error we expected", err)
} }
if sess != nil { if qconn != nil {
log.Fatal("expected nil session here") log.Fatal("expected nil connection here")
} }
}) })
@ -153,19 +153,19 @@ func TestQUICDialerQUICGo(t *testing.T) {
QUICListener: &quicListenerStdlib{}, QUICListener: &quicListenerStdlib{},
mockDialEarlyContext: func(ctx context.Context, pconn net.PacketConn, mockDialEarlyContext: func(ctx context.Context, pconn net.PacketConn,
remoteAddr net.Addr, host string, tlsConfig *tls.Config, remoteAddr net.Addr, host string, tlsConfig *tls.Config,
quicConfig *quic.Config) (quic.EarlySession, error) { quicConfig *quic.Config) (quic.EarlyConnection, error) {
gotTLSConfig = tlsConfig gotTLSConfig = tlsConfig
return nil, expected return nil, expected
}, },
} }
ctx := context.Background() ctx := context.Background()
sess, err := systemdialer.DialContext( qconn, err := systemdialer.DialContext(
ctx, "udp", "8.8.8.8:443", tlsConfig, &quic.Config{}) ctx, "udp", "8.8.8.8:443", tlsConfig, &quic.Config{})
if !errors.Is(err, expected) { if !errors.Is(err, expected) {
t.Fatal("not the error we expected", err) t.Fatal("not the error we expected", err)
} }
if sess != nil { if qconn != nil {
t.Fatal("expected nil session here") t.Fatal("expected nil connection here")
} }
if tlsConfig.RootCAs != nil { if tlsConfig.RootCAs != nil {
t.Fatal("tlsConfig.RootCAs should not have been changed") t.Fatal("tlsConfig.RootCAs should not have been changed")
@ -194,19 +194,19 @@ func TestQUICDialerQUICGo(t *testing.T) {
QUICListener: &quicListenerStdlib{}, QUICListener: &quicListenerStdlib{},
mockDialEarlyContext: func(ctx context.Context, pconn net.PacketConn, mockDialEarlyContext: func(ctx context.Context, pconn net.PacketConn,
remoteAddr net.Addr, host string, tlsConfig *tls.Config, remoteAddr net.Addr, host string, tlsConfig *tls.Config,
quicConfig *quic.Config) (quic.EarlySession, error) { quicConfig *quic.Config) (quic.EarlyConnection, error) {
gotTLSConfig = tlsConfig gotTLSConfig = tlsConfig
return nil, expected return nil, expected
}, },
} }
ctx := context.Background() ctx := context.Background()
sess, err := systemdialer.DialContext( qconn, err := systemdialer.DialContext(
ctx, "udp", "8.8.8.8:8853", tlsConfig, &quic.Config{}) ctx, "udp", "8.8.8.8:8853", tlsConfig, &quic.Config{})
if !errors.Is(err, expected) { if !errors.Is(err, expected) {
t.Fatal("not the error we expected", err) t.Fatal("not the error we expected", err)
} }
if sess != nil { if qconn != nil {
t.Fatal("expected nil session here") t.Fatal("expected nil connection here")
} }
if tlsConfig.RootCAs != nil { if tlsConfig.RootCAs != nil {
t.Fatal("tlsConfig.RootCAs should not have been changed") t.Fatal("tlsConfig.RootCAs should not have been changed")
@ -257,14 +257,14 @@ func TestQUICDialerResolver(t *testing.T) {
dialer := &quicDialerResolver{ dialer := &quicDialerResolver{
Resolver: NewResolverStdlib(log.Log), Resolver: NewResolverStdlib(log.Log),
Dialer: &quicDialerQUICGo{}} Dialer: &quicDialerQUICGo{}}
sess, err := dialer.DialContext( qconn, err := dialer.DialContext(
context.Background(), "udp", "www.google.com", context.Background(), "udp", "www.google.com",
tlsConfig, &quic.Config{}) tlsConfig, &quic.Config{})
if err == nil || !strings.HasSuffix(err.Error(), "missing port in address") { if err == nil || !strings.HasSuffix(err.Error(), "missing port in address") {
t.Fatal("not the error we expected") t.Fatal("not the error we expected")
} }
if sess != nil { if qconn != nil {
t.Fatal("expected a nil sess here") t.Fatal("expected a nil connection here")
} }
}) })
@ -276,14 +276,14 @@ func TestQUICDialerResolver(t *testing.T) {
return nil, expected return nil, expected
}, },
}} }}
sess, err := dialer.DialContext( qconn, err := dialer.DialContext(
context.Background(), "udp", "dns.google.com:853", context.Background(), "udp", "dns.google.com:853",
tlsConfig, &quic.Config{}) tlsConfig, &quic.Config{})
if !errors.Is(err, expected) { if !errors.Is(err, expected) {
t.Fatal("not the error we expected") t.Fatal("not the error we expected")
} }
if sess != nil { if qconn != nil {
t.Fatal("expected nil sess") t.Fatal("expected nil connection")
} }
}) })
@ -296,7 +296,7 @@ func TestQUICDialerResolver(t *testing.T) {
Dialer: &quicDialerQUICGo{ Dialer: &quicDialerQUICGo{
QUICListener: &quicListenerStdlib{}, QUICListener: &quicListenerStdlib{},
}} }}
sess, err := dialer.DialContext( qconn, err := dialer.DialContext(
context.Background(), "udp", "8.8.4.4:x", context.Background(), "udp", "8.8.4.4:x",
tlsConf, &quic.Config{}) tlsConf, &quic.Config{})
if err == nil { if err == nil {
@ -305,8 +305,8 @@ func TestQUICDialerResolver(t *testing.T) {
if !strings.HasSuffix(err.Error(), "invalid syntax") { if !strings.HasSuffix(err.Error(), "invalid syntax") {
t.Fatal("not the error we expected", err) t.Fatal("not the error we expected", err)
} }
if sess != nil { if qconn != nil {
t.Fatal("expected nil sess") t.Fatal("expected nil connection")
} }
}) })
@ -318,19 +318,19 @@ func TestQUICDialerResolver(t *testing.T) {
Resolver: NewResolverStdlib(log.Log), Resolver: NewResolverStdlib(log.Log),
Dialer: &mocks.QUICDialer{ Dialer: &mocks.QUICDialer{
MockDialContext: func(ctx context.Context, network, address string, MockDialContext: func(ctx context.Context, network, address string,
tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlySession, error) { tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlyConnection, error) {
gotTLSConfig = tlsConfig gotTLSConfig = tlsConfig
return nil, expected return nil, expected
}, },
}} }}
sess, err := dialer.DialContext( qconn, err := dialer.DialContext(
context.Background(), "udp", "8.8.4.4:443", context.Background(), "udp", "8.8.4.4:443",
tlsConfig, &quic.Config{}) tlsConfig, &quic.Config{})
if !errors.Is(err, expected) { if !errors.Is(err, expected) {
t.Fatal("not the error we expected", err) t.Fatal("not the error we expected", err)
} }
if sess != nil { if qconn != nil {
t.Fatal("expected nil session here") t.Fatal("expected nil connection here")
} }
if tlsConfig.ServerName != "" { if tlsConfig.ServerName != "" {
t.Fatal("should not have changed tlsConfig.ServerName") t.Fatal("should not have changed tlsConfig.ServerName")
@ -387,8 +387,8 @@ func TestQUICLoggerDialer(t *testing.T) {
Dialer: &mocks.QUICDialer{ Dialer: &mocks.QUICDialer{
MockDialContext: func(ctx context.Context, network string, MockDialContext: func(ctx context.Context, network string,
address string, tlsConfig *tls.Config, address string, tlsConfig *tls.Config,
quicConfig *quic.Config) (quic.EarlySession, error) { quicConfig *quic.Config) (quic.EarlyConnection, error) {
return &mocks.QUICEarlySession{ return &mocks.QUICEarlyConnection{
MockCloseWithError: func( MockCloseWithError: func(
code quic.ApplicationErrorCode, reason string) error { code quic.ApplicationErrorCode, reason string) error {
return nil return nil
@ -401,11 +401,11 @@ func TestQUICLoggerDialer(t *testing.T) {
ctx := context.Background() ctx := context.Background()
tlsConfig := &tls.Config{} tlsConfig := &tls.Config{}
quicConfig := &quic.Config{} quicConfig := &quic.Config{}
sess, err := d.DialContext(ctx, "udp", "8.8.8.8:443", tlsConfig, quicConfig) qconn, err := d.DialContext(ctx, "udp", "8.8.8.8:443", tlsConfig, quicConfig)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if err := sess.CloseWithError(0, ""); err != nil { if err := qconn.CloseWithError(0, ""); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if called != 2 { if called != 2 {
@ -425,7 +425,7 @@ func TestQUICLoggerDialer(t *testing.T) {
Dialer: &mocks.QUICDialer{ Dialer: &mocks.QUICDialer{
MockDialContext: func(ctx context.Context, network string, MockDialContext: func(ctx context.Context, network string,
address string, tlsConfig *tls.Config, address string, tlsConfig *tls.Config,
quicConfig *quic.Config) (quic.EarlySession, error) { quicConfig *quic.Config) (quic.EarlyConnection, error) {
return nil, expected return nil, expected
}, },
}, },
@ -434,12 +434,12 @@ func TestQUICLoggerDialer(t *testing.T) {
ctx := context.Background() ctx := context.Background()
tlsConfig := &tls.Config{} tlsConfig := &tls.Config{}
quicConfig := &quic.Config{} quicConfig := &quic.Config{}
sess, err := d.DialContext(ctx, "udp", "8.8.8.8:443", tlsConfig, quicConfig) qconn, err := d.DialContext(ctx, "udp", "8.8.8.8:443", tlsConfig, quicConfig)
if !errors.Is(err, expected) { if !errors.Is(err, expected) {
t.Fatal("not the error we expected", err) t.Fatal("not the error we expected", err)
} }
if sess != nil { if qconn != nil {
t.Fatal("expected nil session") t.Fatal("expected nil connection")
} }
if called != 2 { if called != 2 {
t.Fatal("invalid number of calls") t.Fatal("invalid number of calls")
@ -449,24 +449,24 @@ func TestQUICLoggerDialer(t *testing.T) {
} }
func TestNewSingleUseQUICDialer(t *testing.T) { func TestNewSingleUseQUICDialer(t *testing.T) {
sess := &mocks.QUICEarlySession{} qconn := &mocks.QUICEarlyConnection{}
qd := NewSingleUseQUICDialer(sess) qd := NewSingleUseQUICDialer(qconn)
defer qd.CloseIdleConnections() defer qd.CloseIdleConnections()
outsess, err := qd.DialContext( outconn, err := qd.DialContext(
context.Background(), "", "", &tls.Config{}, &quic.Config{}) context.Background(), "", "", &tls.Config{}, &quic.Config{})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if sess != outsess { if qconn != outconn {
t.Fatal("invalid outsess") t.Fatal("invalid outconn")
} }
for i := 0; i < 4; i++ { for i := 0; i < 4; i++ {
outsess, err = qd.DialContext( outconn, err = qd.DialContext(
context.Background(), "", "", &tls.Config{}, &quic.Config{}) context.Background(), "", "", &tls.Config{}, &quic.Config{})
if !errors.Is(err, ErrNoConnReuse) { if !errors.Is(err, ErrNoConnReuse) {
t.Fatal("not the error we expected", err) t.Fatal("not the error we expected", err)
} }
if outsess != nil { if outconn != nil {
t.Fatal("expected nil outconn here") t.Fatal("expected nil outconn here")
} }
} }
@ -649,21 +649,21 @@ func TestQUICDialerErrWrapper(t *testing.T) {
t.Run("DialContext", func(t *testing.T) { t.Run("DialContext", func(t *testing.T) {
t.Run("on success", func(t *testing.T) { t.Run("on success", func(t *testing.T) {
expectedSess := &mocks.QUICEarlySession{} expectedConn := &mocks.QUICEarlyConnection{}
d := &quicDialerErrWrapper{ d := &quicDialerErrWrapper{
QUICDialer: &mocks.QUICDialer{ QUICDialer: &mocks.QUICDialer{
MockDialContext: func(ctx context.Context, network, address string, tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlySession, error) { MockDialContext: func(ctx context.Context, network, address string, tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlyConnection, error) {
return expectedSess, nil return expectedConn, nil
}, },
}, },
} }
ctx := context.Background() ctx := context.Background()
sess, err := d.DialContext(ctx, "", "", &tls.Config{}, &quic.Config{}) qconn, err := d.DialContext(ctx, "", "", &tls.Config{}, &quic.Config{})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if sess != expectedSess { if qconn != expectedConn {
t.Fatal("unexpected sess") t.Fatal("unexpected connection")
} }
}) })
@ -671,18 +671,18 @@ func TestQUICDialerErrWrapper(t *testing.T) {
expectedErr := io.EOF expectedErr := io.EOF
d := &quicDialerErrWrapper{ d := &quicDialerErrWrapper{
QUICDialer: &mocks.QUICDialer{ QUICDialer: &mocks.QUICDialer{
MockDialContext: func(ctx context.Context, network, address string, tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlySession, error) { MockDialContext: func(ctx context.Context, network, address string, tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlyConnection, error) {
return nil, expectedErr return nil, expectedErr
}, },
}, },
} }
ctx := context.Background() ctx := context.Background()
sess, err := d.DialContext(ctx, "", "", &tls.Config{}, &quic.Config{}) qconn, err := d.DialContext(ctx, "", "", &tls.Config{}, &quic.Config{})
if err == nil || err.Error() != FailureEOFError { if err == nil || err.Error() != FailureEOFError {
t.Fatal("unexpected err", err) t.Fatal("unexpected err", err)
} }
if sess != nil { if qconn != nil {
t.Fatal("unexpected sess") t.Fatal("unexpected connection")
} }
}) })
}) })

View File

@ -2,7 +2,7 @@
# Chapter I: Using QUIC # Chapter I: Using QUIC
In this chapter we will write together a `main.go` file that In this chapter we will write together a `main.go` file that
uses netxlite to establish a new QUIC session with an UDP endpoint. uses netxlite to establish a new QUIC connection with an UDP endpoint.
Conceptually, this program is very similar to the ones presented Conceptually, this program is very similar to the ones presented
in chapters 2 and 3, except that here we use QUIC. in chapters 2 and 3, except that here we use QUIC.
@ -58,7 +58,7 @@ Also, where previously we called `dialTLS` now we call
a function with a similar API called `dialQUIC`. a function with a similar API called `dialQUIC`.
``` ```
sess, state, err := dialQUIC(ctx, *address, config) qconn, state, err := dialQUIC(ctx, *address, config)
``` ```
The rest of the main function is pretty much the same. The rest of the main function is pretty much the same.
@ -67,11 +67,11 @@ The rest of the main function is pretty much the same.
if err != nil { if err != nil {
fatal(err) fatal(err)
} }
log.Infof("Sess type : %T", sess) log.Infof("Connection type : %T", qconn)
log.Infof("Cipher suite : %s", netxlite.TLSCipherSuiteString(state.CipherSuite)) log.Infof("Cipher suite : %s", netxlite.TLSCipherSuiteString(state.CipherSuite))
log.Infof("Negotiated protocol: %s", state.NegotiatedProtocol) log.Infof("Negotiated protocol: %s", state.NegotiatedProtocol)
log.Infof("TLS version : %s", netxlite.TLSVersionString(state.Version)) log.Infof("TLS version : %s", netxlite.TLSVersionString(state.Version))
sess.CloseWithError(0, "") qconn.CloseWithError(0, "")
} }
``` ```
@ -90,10 +90,10 @@ in the next two chapters.)
```Go ```Go
func dialQUIC(ctx context.Context, address string, func dialQUIC(ctx context.Context, address string,
config *tls.Config) (quic.EarlySession, tls.ConnectionState, error) { config *tls.Config) (quic.EarlyConnection, tls.ConnectionState, error) {
ql := netxlite.NewQUICListener() ql := netxlite.NewQUICListener()
d := netxlite.NewQUICDialerWithoutResolver(ql, log.Log) d := netxlite.NewQUICDialerWithoutResolver(ql, log.Log)
sess, err := d.DialContext(ctx, "udp", address, config, &quic.Config{}) qconn, err := d.DialContext(ctx, "udp", address, config, &quic.Config{})
if err != nil { if err != nil {
return nil, tls.ConnectionState{}, err return nil, tls.ConnectionState{}, err
} }
@ -104,7 +104,7 @@ QUIC code to be of the same type of the ConnectionState that
we returned in the previous chapters. we returned in the previous chapters.
```Go ```Go
return sess, sess.ConnectionState().TLS.ConnectionState, nil return qconn, qconn.ConnectionState().TLS.ConnectionState, nil
} }
``` ```
@ -157,5 +157,5 @@ should give you a TLS error mentioning that the certificate is invalid.
## Conclusions ## Conclusions
We have seen how to use netxlite to establish a QUIC session We have seen how to use netxlite to establish a QUIC connection
with a remote UDP endpoint speaking QUIC. with a remote UDP endpoint speaking QUIC.

View File

@ -3,7 +3,7 @@
// # Chapter I: Using QUIC // # Chapter I: Using QUIC
// //
// In this chapter we will write together a `main.go` file that // In this chapter we will write together a `main.go` file that
// uses netxlite to establish a new QUIC session with an UDP endpoint. // uses netxlite to establish a new QUIC connection with an UDP endpoint.
// //
// Conceptually, this program is very similar to the ones presented // Conceptually, this program is very similar to the ones presented
// in chapters 2 and 3, except that here we use QUIC. // in chapters 2 and 3, except that here we use QUIC.
@ -59,7 +59,7 @@ func main() {
// a function with a similar API called `dialQUIC`. // a function with a similar API called `dialQUIC`.
// //
// ``` // ```
sess, state, err := dialQUIC(ctx, *address, config) qconn, state, err := dialQUIC(ctx, *address, config)
// ``` // ```
// //
// The rest of the main function is pretty much the same. // The rest of the main function is pretty much the same.
@ -68,11 +68,11 @@ func main() {
if err != nil { if err != nil {
fatal(err) fatal(err)
} }
log.Infof("Sess type : %T", sess) log.Infof("Connection type : %T", qconn)
log.Infof("Cipher suite : %s", netxlite.TLSCipherSuiteString(state.CipherSuite)) log.Infof("Cipher suite : %s", netxlite.TLSCipherSuiteString(state.CipherSuite))
log.Infof("Negotiated protocol: %s", state.NegotiatedProtocol) log.Infof("Negotiated protocol: %s", state.NegotiatedProtocol)
log.Infof("TLS version : %s", netxlite.TLSVersionString(state.Version)) log.Infof("TLS version : %s", netxlite.TLSVersionString(state.Version))
sess.CloseWithError(0, "") qconn.CloseWithError(0, "")
} }
// ``` // ```
@ -91,10 +91,10 @@ func main() {
// //
// ```Go // ```Go
func dialQUIC(ctx context.Context, address string, func dialQUIC(ctx context.Context, address string,
config *tls.Config) (quic.EarlySession, tls.ConnectionState, error) { config *tls.Config) (quic.EarlyConnection, tls.ConnectionState, error) {
ql := netxlite.NewQUICListener() ql := netxlite.NewQUICListener()
d := netxlite.NewQUICDialerWithoutResolver(ql, log.Log) d := netxlite.NewQUICDialerWithoutResolver(ql, log.Log)
sess, err := d.DialContext(ctx, "udp", address, config, &quic.Config{}) qconn, err := d.DialContext(ctx, "udp", address, config, &quic.Config{})
if err != nil { if err != nil {
return nil, tls.ConnectionState{}, err return nil, tls.ConnectionState{}, err
} }
@ -105,7 +105,7 @@ func dialQUIC(ctx context.Context, address string,
// we returned in the previous chapters. // we returned in the previous chapters.
// //
// ```Go // ```Go
return sess, sess.ConnectionState().TLS.ConnectionState, nil return qconn, qconn.ConnectionState().TLS.ConnectionState, nil
} }
// ``` // ```
@ -158,5 +158,5 @@ func fatal(err error) {
// //
// ## Conclusions // ## Conclusions
// //
// We have seen how to use netxlite to establish a QUIC session // We have seen how to use netxlite to establish a QUIC connection
// with a remote UDP endpoint speaking QUIC. // with a remote UDP endpoint speaking QUIC.

View File

@ -1,8 +1,8 @@
# Chapter I: HTTP GET with QUIC sess # Chapter I: HTTP GET with QUIC conn
In this chapter we will write together a `main.go` file that In this chapter we will write together a `main.go` file that
uses netxlite to establish a QUIC session to a remote endpoint uses netxlite to establish a QUIC connection to a remote endpoint
and then fetches a webpage from it using GET. and then fetches a webpage from it using GET.
This file is basically the same as the one used in chapter04 This file is basically the same as the one used in chapter04
@ -49,11 +49,11 @@ func main() {
NextProtos: []string{"h3"}, NextProtos: []string{"h3"},
RootCAs: netxlite.NewDefaultCertPool(), RootCAs: netxlite.NewDefaultCertPool(),
} }
sess, _, err := dialQUIC(ctx, *address, config) qconn, _, err := dialQUIC(ctx, *address, config)
if err != nil { if err != nil {
fatal(err) fatal(err)
} }
log.Infof("Sess type : %T", sess) log.Infof("Connection type : %T", qconn)
``` ```
This is where things diverge. We create an HTTP client This is where things diverge. We create an HTTP client
@ -61,13 +61,13 @@ using a transport created with `netxlite.NewHTTP3Transport`.
This transport will use a "single use" QUIC dialer. This transport will use a "single use" QUIC dialer.
What does this mean? Well, we create such a QUICDialer What does this mean? Well, we create such a QUICDialer
using the session we already established. The first using the connection we already established. The first
time the HTTP code dials for QUIC, the QUICDialer will time the HTTP code dials for QUIC, the QUICDialer will
return the session we passed to its constructor return the connection we passed to its constructor
immediately. Every subsequent QUIC dial attempt will fail. immediately. Every subsequent QUIC dial attempt will fail.
The result is an HTTPTransport suitable for performing The result is an HTTPTransport suitable for performing
a single request using the given QUIC sess. a single request using the given QUIC conn.
(A similar construct allows to create an HTTPTransport that (A similar construct allows to create an HTTPTransport that
uses a cleartext TCP connection. In the previous chapter we've uses a cleartext TCP connection. In the previous chapter we've
@ -75,7 +75,7 @@ seen how to do the same using TLS conns.)
```Go ```Go
clnt := &http.Client{Transport: netxlite.NewHTTP3Transport( clnt := &http.Client{Transport: netxlite.NewHTTP3Transport(
log.Log, netxlite.NewSingleUseQUICDialer(sess), &tls.Config{}, log.Log, netxlite.NewSingleUseQUICDialer(qconn), &tls.Config{},
)} )}
``` ```
@ -103,14 +103,14 @@ exactly like what we've seen in chapter04.
```Go ```Go
func dialQUIC(ctx context.Context, address string, func dialQUIC(ctx context.Context, address string,
config *tls.Config) (quic.EarlySession, tls.ConnectionState, error) { config *tls.Config) (quic.EarlyConnection, tls.ConnectionState, error) {
ql := netxlite.NewQUICListener() ql := netxlite.NewQUICListener()
d := netxlite.NewQUICDialerWithoutResolver(ql, log.Log) d := netxlite.NewQUICDialerWithoutResolver(ql, log.Log)
sess, err := d.DialContext(ctx, "udp", address, config, &quic.Config{}) qconn, err := d.DialContext(ctx, "udp", address, config, &quic.Config{})
if err != nil { if err != nil {
return nil, tls.ConnectionState{}, err return nil, tls.ConnectionState{}, err
} }
return sess, sess.ConnectionState().TLS.ConnectionState, nil return qconn, qconn.ConnectionState().TLS.ConnectionState, nil
} }
func fatal(err error) { func fatal(err error) {
@ -158,5 +158,5 @@ should give you an error mentioning the certificate is invalid.
## Conclusions ## Conclusions
We have seen how to establish a QUIC session with a website We have seen how to establish a QUIC connection with a website
and then how to GET a webpage using such a session. and then how to GET a webpage using such a connection.

View File

@ -1,9 +1,9 @@
// -=-=- StartHere -=-=- // -=-=- StartHere -=-=-
// //
// # Chapter I: HTTP GET with QUIC sess // # Chapter I: HTTP GET with QUIC conn
// //
// In this chapter we will write together a `main.go` file that // In this chapter we will write together a `main.go` file that
// uses netxlite to establish a QUIC session to a remote endpoint // uses netxlite to establish a QUIC connection to a remote endpoint
// and then fetches a webpage from it using GET. // and then fetches a webpage from it using GET.
// //
// This file is basically the same as the one used in chapter04 // This file is basically the same as the one used in chapter04
@ -50,11 +50,11 @@ func main() {
NextProtos: []string{"h3"}, NextProtos: []string{"h3"},
RootCAs: netxlite.NewDefaultCertPool(), RootCAs: netxlite.NewDefaultCertPool(),
} }
sess, _, err := dialQUIC(ctx, *address, config) qconn, _, err := dialQUIC(ctx, *address, config)
if err != nil { if err != nil {
fatal(err) fatal(err)
} }
log.Infof("Sess type : %T", sess) log.Infof("Connection type : %T", qconn)
// ``` // ```
// //
// This is where things diverge. We create an HTTP client // This is where things diverge. We create an HTTP client
@ -62,13 +62,13 @@ func main() {
// //
// This transport will use a "single use" QUIC dialer. // This transport will use a "single use" QUIC dialer.
// What does this mean? Well, we create such a QUICDialer // What does this mean? Well, we create such a QUICDialer
// using the session we already established. The first // using the connection we already established. The first
// time the HTTP code dials for QUIC, the QUICDialer will // time the HTTP code dials for QUIC, the QUICDialer will
// return the session we passed to its constructor // return the connection we passed to its constructor
// immediately. Every subsequent QUIC dial attempt will fail. // immediately. Every subsequent QUIC dial attempt will fail.
// //
// The result is an HTTPTransport suitable for performing // The result is an HTTPTransport suitable for performing
// a single request using the given QUIC sess. // a single request using the given QUIC conn.
// //
// (A similar construct allows to create an HTTPTransport that // (A similar construct allows to create an HTTPTransport that
// uses a cleartext TCP connection. In the previous chapter we've // uses a cleartext TCP connection. In the previous chapter we've
@ -76,7 +76,7 @@ func main() {
// //
// ```Go // ```Go
clnt := &http.Client{Transport: netxlite.NewHTTP3Transport( clnt := &http.Client{Transport: netxlite.NewHTTP3Transport(
log.Log, netxlite.NewSingleUseQUICDialer(sess), &tls.Config{}, log.Log, netxlite.NewSingleUseQUICDialer(qconn), &tls.Config{},
)} )}
// ``` // ```
// //
@ -104,14 +104,14 @@ func main() {
// ```Go // ```Go
func dialQUIC(ctx context.Context, address string, func dialQUIC(ctx context.Context, address string,
config *tls.Config) (quic.EarlySession, tls.ConnectionState, error) { config *tls.Config) (quic.EarlyConnection, tls.ConnectionState, error) {
ql := netxlite.NewQUICListener() ql := netxlite.NewQUICListener()
d := netxlite.NewQUICDialerWithoutResolver(ql, log.Log) d := netxlite.NewQUICDialerWithoutResolver(ql, log.Log)
sess, err := d.DialContext(ctx, "udp", address, config, &quic.Config{}) qconn, err := d.DialContext(ctx, "udp", address, config, &quic.Config{})
if err != nil { if err != nil {
return nil, tls.ConnectionState{}, err return nil, tls.ConnectionState{}, err
} }
return sess, sess.ConnectionState().TLS.ConnectionState, nil return qconn, qconn.ConnectionState().TLS.ConnectionState, nil
} }
func fatal(err error) { func fatal(err error) {
@ -159,5 +159,5 @@ func fatal(err error) {
// //
// ## Conclusions // ## Conclusions
// //
// We have seen how to establish a QUIC session with a website // We have seen how to establish a QUIC connection with a website
// and then how to GET a webpage using such a session. // and then how to GET a webpage using such a connection.