ooni-probe-cli/internal/measurex/quic.go

160 lines
4.2 KiB
Go
Raw Permalink Normal View History

package measurex
//
// QUIC
//
// Wrappers for QUIC to store events into a WritableDB.
//
import (
"context"
"crypto/tls"
"net"
"time"
"github.com/lucas-clemente/quic-go"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
type quicListenerDB struct {
model.QUICListener
begin time.Time
db WritableDB
}
func (ql *quicListenerDB) Listen(addr *net.UDPAddr) (model.UDPLikeConn, error) {
pconn, err := ql.QUICListener.Listen(addr)
if err != nil {
return nil, err
}
return &udpLikeConnDB{
UDPLikeConn: pconn,
begin: ql.begin,
db: ql.db,
}, nil
}
type udpLikeConnDB struct {
model.UDPLikeConn
begin time.Time
db WritableDB
}
func (c *udpLikeConnDB) WriteTo(p []byte, addr net.Addr) (int, error) {
started := time.Since(c.begin).Seconds()
count, err := c.UDPLikeConn.WriteTo(p, addr)
finished := time.Since(c.begin).Seconds()
c.db.InsertIntoReadWrite(&NetworkEvent{
Operation: "write_to",
Network: "udp",
RemoteAddr: addr.String(),
Started: started,
Finished: finished,
Failure: NewFailure(err),
Count: count,
})
return count, err
}
func (c *udpLikeConnDB) ReadFrom(b []byte) (int, net.Addr, error) {
started := time.Since(c.begin).Seconds()
count, addr, err := c.UDPLikeConn.ReadFrom(b)
finished := time.Since(c.begin).Seconds()
c.db.InsertIntoReadWrite(&NetworkEvent{
Operation: "read_from",
Network: "udp",
RemoteAddr: addrStringIfNotNil(addr),
Started: started,
Finished: finished,
Failure: NewFailure(err),
Count: count,
})
return count, addr, err
}
func (c *udpLikeConnDB) Close() error {
started := time.Since(c.begin).Seconds()
err := c.UDPLikeConn.Close()
finished := time.Since(c.begin).Seconds()
c.db.InsertIntoClose(&NetworkEvent{
Operation: "close",
Network: "udp",
RemoteAddr: "",
Started: started,
Finished: finished,
Failure: NewFailure(err),
Count: 0,
})
return err
}
// NewQUICDialerWithoutResolver creates a new QUICDialer that is not
// attached to any resolver. This means that every attempt to dial any
// address containing a domain name will fail. This QUICDialer will
// save any event into the WritableDB. Any QUICConn created by it will
// likewise save any event into the WritableDB.
func (mx *Measurer) NewQUICDialerWithoutResolver(db WritableDB, logger model.Logger) model.QUICDialer {
return &quicDialerDB{db: db, logger: logger, begin: mx.Begin}
}
type quicDialerDB struct {
model.QUICDialer
begin time.Time
db WritableDB
logger model.Logger
}
func (qh *quicDialerDB) DialContext(ctx context.Context, address string,
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>
2022-05-06 12:24:03 +02:00
tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlyConnection, error) {
started := time.Since(qh.begin).Seconds()
var state tls.ConnectionState
listener := &quicListenerDB{
QUICListener: netxlite.NewQUICListener(),
begin: qh.begin,
db: qh.db,
}
dialer := netxlite.NewQUICDialerWithoutResolver(listener, qh.logger)
defer dialer.CloseIdleConnections()
sess, err := dialer.DialContext(ctx, address, tlsConfig, quicConfig)
if err == nil {
<-sess.HandshakeComplete().Done() // robustness (the dialer already does that)
state = sess.ConnectionState().TLS.ConnectionState
}
finished := time.Since(qh.begin).Seconds()
qh.db.InsertIntoQUICHandshake(&QUICTLSHandshakeEvent{
Network: "udp",
RemoteAddr: address,
SNI: tlsConfig.ServerName,
ALPN: tlsConfig.NextProtos,
SkipVerify: tlsConfig.InsecureSkipVerify,
Started: started,
Finished: finished,
Failure: NewFailure(err),
Oddity: qh.computeOddity(err),
TLSVersion: netxlite.TLSVersionString(state.Version),
CipherSuite: netxlite.TLSCipherSuiteString(state.CipherSuite),
NegotiatedProto: state.NegotiatedProtocol,
PeerCerts: peerCerts(nil, &state),
})
return sess, err
}
func (qh *quicDialerDB) computeOddity(err error) Oddity {
if err == nil {
return ""
}
switch err.Error() {
case netxlite.FailureGenericTimeoutError:
return OddityQUICHandshakeTimeout
case netxlite.FailureHostUnreachable:
return OddityQUICHandshakeHostUnreachable
default:
return OddityQUICHandshakeOther
}
}
func (qh *quicDialerDB) CloseIdleConnections() {
// nothing to do
}