2022-05-31 21:53:01 +02:00
|
|
|
package tracex
|
|
|
|
|
2022-06-01 07:44:54 +02:00
|
|
|
//
|
|
|
|
// QUIC
|
|
|
|
//
|
|
|
|
|
2022-05-31 21:53:01 +02:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
// QUICDialerSaver saves events occurring during the QUIC handshake.
|
|
|
|
type QUICDialerSaver struct {
|
2022-06-01 07:44:54 +02:00
|
|
|
// QUICDialer is the wrapped dialer
|
|
|
|
QUICDialer model.QUICDialer
|
|
|
|
|
|
|
|
// Saver saves events
|
2022-05-31 21:53:01 +02:00
|
|
|
Saver *Saver
|
|
|
|
}
|
|
|
|
|
2022-06-01 07:44:54 +02:00
|
|
|
// WrapQUICDialer wraps a model.QUICDialer with a QUICHandshakeSaver that will
|
|
|
|
// save the QUIC handshake results into this Saver.
|
|
|
|
//
|
|
|
|
// When this function is invoked on a nil Saver, it will directly return
|
|
|
|
// the original QUICDialer without any wrapping.
|
|
|
|
func (s *Saver) WrapQUICDialer(qd model.QUICDialer) model.QUICDialer {
|
|
|
|
if s == nil {
|
|
|
|
return qd
|
|
|
|
}
|
2022-06-01 23:15:47 +02:00
|
|
|
return &QUICDialerSaver{
|
2022-06-01 07:44:54 +02:00
|
|
|
QUICDialer: qd,
|
|
|
|
Saver: s,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DialContext implements QUICDialer.DialContext
|
2022-08-19 11:26:50 +02:00
|
|
|
func (h *QUICDialerSaver) DialContext(ctx context.Context,
|
2022-05-31 21:53:01 +02:00
|
|
|
host string, tlsCfg *tls.Config, cfg *quic.Config) (quic.EarlyConnection, error) {
|
|
|
|
start := time.Now()
|
|
|
|
// TODO(bassosimone): in the future we probably want to also save
|
|
|
|
// information about what versions we're willing to accept.
|
2022-06-01 14:32:16 +02:00
|
|
|
h.Saver.Write(&EventQUICHandshakeStart{&EventValue{
|
2022-05-31 21:53:01 +02:00
|
|
|
Address: host,
|
|
|
|
NoTLSVerify: tlsCfg.InsecureSkipVerify,
|
2022-08-19 11:26:50 +02:00
|
|
|
Proto: "udp",
|
2022-05-31 21:53:01 +02:00
|
|
|
TLSNextProtos: tlsCfg.NextProtos,
|
|
|
|
TLSServerName: tlsCfg.ServerName,
|
|
|
|
Time: start,
|
2022-06-01 14:32:16 +02:00
|
|
|
}})
|
2022-08-19 11:26:50 +02:00
|
|
|
sess, err := h.QUICDialer.DialContext(ctx, host, tlsCfg, cfg)
|
2022-05-31 21:53:01 +02:00
|
|
|
stop := time.Now()
|
|
|
|
if err != nil {
|
2022-06-01 07:44:54 +02:00
|
|
|
// TODO(bassosimone): here we should save the peer certs
|
2022-06-01 14:32:16 +02:00
|
|
|
h.Saver.Write(&EventQUICHandshakeDone{&EventValue{
|
2022-06-01 23:15:47 +02:00
|
|
|
Address: host,
|
2022-05-31 21:53:01 +02:00
|
|
|
Duration: stop.Sub(start),
|
refactor(tracex): internally represent errors as strings (#786)
There are two reasons why this is beneficial:
1. github.com/google/go-cmp is more annoying to use for comparing
data structures when there are interfaces to compare. Sure, there's
a recipe for teaching it to compare errors, but how about making
the errors trivially comparable instead?
2. if we want to send errors over the network, JSON serialization
works but we cannot unmarshal the resulting string back to an error,
so how about making this representation trivial to serialize (we
are not going this now, but we need this property for websteps and
it may be sensible to try to avoid to have duplicate code because
of that -- measurex currently duplicates many tracex functionality
and this is quite unfortunate because it slows development down)
Additionally, if an error is a string:
3. we can very easily use a switch for comparing its possible
values with "" representing the absence of errors, while it is
more complex to do the same when using a nullable string or even
an error (i.e., an interface)
4. if a type is not nullable, it's easier to write safe code for
it and we may want to refactor experiments to use the internal
representation of measurements for more robust processing code
For all these reasons, let's internally use strings in tracex.
The overall aim here is to reduce the duplicated code between pre
and post-measurex measurements (see https://github.com/ooni/probe/issues/2035).
2022-06-02 10:37:07 +02:00
|
|
|
Err: NewFailureStr(err),
|
2022-05-31 21:53:01 +02:00
|
|
|
NoTLSVerify: tlsCfg.InsecureSkipVerify,
|
2022-08-19 11:26:50 +02:00
|
|
|
Proto: "udp",
|
2022-05-31 21:53:01 +02:00
|
|
|
TLSNextProtos: tlsCfg.NextProtos,
|
2022-06-02 11:07:02 +02:00
|
|
|
TLSPeerCerts: [][]byte{},
|
2022-05-31 21:53:01 +02:00
|
|
|
TLSServerName: tlsCfg.ServerName,
|
|
|
|
Time: stop,
|
2022-06-01 14:32:16 +02:00
|
|
|
}})
|
2022-05-31 21:53:01 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
state := quicConnectionState(sess)
|
2022-06-01 14:32:16 +02:00
|
|
|
h.Saver.Write(&EventQUICHandshakeDone{&EventValue{
|
2022-06-01 23:15:47 +02:00
|
|
|
Address: host,
|
2022-05-31 21:53:01 +02:00
|
|
|
Duration: stop.Sub(start),
|
|
|
|
NoTLSVerify: tlsCfg.InsecureSkipVerify,
|
2022-08-19 11:26:50 +02:00
|
|
|
Proto: "udp",
|
2022-05-31 21:53:01 +02:00
|
|
|
TLSCipherSuite: netxlite.TLSCipherSuiteString(state.CipherSuite),
|
|
|
|
TLSNegotiatedProto: state.NegotiatedProtocol,
|
|
|
|
TLSNextProtos: tlsCfg.NextProtos,
|
2022-06-01 07:44:54 +02:00
|
|
|
TLSPeerCerts: tlsPeerCerts(state, err),
|
2022-05-31 21:53:01 +02:00
|
|
|
TLSServerName: tlsCfg.ServerName,
|
|
|
|
TLSVersion: netxlite.TLSVersionString(state.Version),
|
|
|
|
Time: stop,
|
2022-06-01 14:32:16 +02:00
|
|
|
}})
|
2022-05-31 21:53:01 +02:00
|
|
|
return sess, nil
|
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
func (h *QUICDialerSaver) CloseIdleConnections() {
|
2022-06-01 07:44:54 +02:00
|
|
|
h.QUICDialer.CloseIdleConnections()
|
|
|
|
}
|
|
|
|
|
2022-05-31 21:53:01 +02:00
|
|
|
// quicConnectionState returns the ConnectionState of a QUIC Session.
|
|
|
|
func quicConnectionState(sess quic.EarlyConnection) tls.ConnectionState {
|
|
|
|
return sess.ConnectionState().TLS.ConnectionState
|
|
|
|
}
|
|
|
|
|
|
|
|
// QUICListenerSaver is a QUICListener that also implements saving events.
|
|
|
|
type QUICListenerSaver struct {
|
|
|
|
// QUICListener is the underlying QUICListener.
|
2022-06-01 07:44:54 +02:00
|
|
|
QUICListener model.QUICListener
|
2022-05-31 21:53:01 +02:00
|
|
|
|
|
|
|
// Saver is the underlying Saver.
|
|
|
|
Saver *Saver
|
|
|
|
}
|
|
|
|
|
2022-06-01 07:44:54 +02:00
|
|
|
// WrapQUICListener wraps a model.QUICDialer with a QUICListenerSaver that will
|
|
|
|
// save the QUIC I/O packet conn events into this Saver.
|
|
|
|
//
|
|
|
|
// When this function is invoked on a nil Saver, it will directly return
|
|
|
|
// the original QUICListener without any wrapping.
|
|
|
|
func (s *Saver) WrapQUICListener(ql model.QUICListener) model.QUICListener {
|
|
|
|
if s == nil {
|
|
|
|
return ql
|
|
|
|
}
|
|
|
|
return &QUICListenerSaver{
|
|
|
|
QUICListener: ql,
|
|
|
|
Saver: s,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 21:53:01 +02:00
|
|
|
// Listen implements QUICListener.Listen.
|
|
|
|
func (qls *QUICListenerSaver) Listen(addr *net.UDPAddr) (model.UDPLikeConn, error) {
|
|
|
|
pconn, err := qls.QUICListener.Listen(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-06-01 23:15:47 +02:00
|
|
|
pconn = &quicPacketConnWrapper{
|
2022-05-31 21:53:01 +02:00
|
|
|
UDPLikeConn: pconn,
|
|
|
|
saver: qls.Saver,
|
2022-06-01 07:44:54 +02:00
|
|
|
}
|
|
|
|
return pconn, nil
|
2022-05-31 21:53:01 +02:00
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
// quicPacketConnWrapper saves I/O events
|
|
|
|
type quicPacketConnWrapper struct {
|
2022-06-01 07:44:54 +02:00
|
|
|
// UDPLikeConn is the wrapped underlying conn
|
2022-05-31 21:53:01 +02:00
|
|
|
model.UDPLikeConn
|
2022-06-01 07:44:54 +02:00
|
|
|
|
|
|
|
// Saver saves events
|
2022-05-31 21:53:01 +02:00
|
|
|
saver *Saver
|
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
func (c *quicPacketConnWrapper) WriteTo(p []byte, addr net.Addr) (int, error) {
|
2022-05-31 21:53:01 +02:00
|
|
|
start := time.Now()
|
|
|
|
count, err := c.UDPLikeConn.WriteTo(p, addr)
|
|
|
|
stop := time.Now()
|
2022-06-01 14:32:16 +02:00
|
|
|
c.saver.Write(&EventWriteToOperation{&EventValue{
|
2022-05-31 21:53:01 +02:00
|
|
|
Address: addr.String(),
|
|
|
|
Data: p[:count],
|
|
|
|
Duration: stop.Sub(start),
|
refactor(tracex): internally represent errors as strings (#786)
There are two reasons why this is beneficial:
1. github.com/google/go-cmp is more annoying to use for comparing
data structures when there are interfaces to compare. Sure, there's
a recipe for teaching it to compare errors, but how about making
the errors trivially comparable instead?
2. if we want to send errors over the network, JSON serialization
works but we cannot unmarshal the resulting string back to an error,
so how about making this representation trivial to serialize (we
are not going this now, but we need this property for websteps and
it may be sensible to try to avoid to have duplicate code because
of that -- measurex currently duplicates many tracex functionality
and this is quite unfortunate because it slows development down)
Additionally, if an error is a string:
3. we can very easily use a switch for comparing its possible
values with "" representing the absence of errors, while it is
more complex to do the same when using a nullable string or even
an error (i.e., an interface)
4. if a type is not nullable, it's easier to write safe code for
it and we may want to refactor experiments to use the internal
representation of measurements for more robust processing code
For all these reasons, let's internally use strings in tracex.
The overall aim here is to reduce the duplicated code between pre
and post-measurex measurements (see https://github.com/ooni/probe/issues/2035).
2022-06-02 10:37:07 +02:00
|
|
|
Err: NewFailureStr(err),
|
2022-05-31 21:53:01 +02:00
|
|
|
NumBytes: count,
|
|
|
|
Time: stop,
|
2022-06-01 14:32:16 +02:00
|
|
|
}})
|
2022-05-31 21:53:01 +02:00
|
|
|
return count, err
|
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
func (c *quicPacketConnWrapper) ReadFrom(b []byte) (int, net.Addr, error) {
|
2022-05-31 21:53:01 +02:00
|
|
|
start := time.Now()
|
|
|
|
n, addr, err := c.UDPLikeConn.ReadFrom(b)
|
|
|
|
stop := time.Now()
|
|
|
|
var data []byte
|
|
|
|
if n > 0 {
|
|
|
|
data = b[:n]
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
c.saver.Write(&EventReadFromOperation{&EventValue{
|
2022-05-31 21:53:01 +02:00
|
|
|
Address: c.safeAddrString(addr),
|
|
|
|
Data: data,
|
|
|
|
Duration: stop.Sub(start),
|
refactor(tracex): internally represent errors as strings (#786)
There are two reasons why this is beneficial:
1. github.com/google/go-cmp is more annoying to use for comparing
data structures when there are interfaces to compare. Sure, there's
a recipe for teaching it to compare errors, but how about making
the errors trivially comparable instead?
2. if we want to send errors over the network, JSON serialization
works but we cannot unmarshal the resulting string back to an error,
so how about making this representation trivial to serialize (we
are not going this now, but we need this property for websteps and
it may be sensible to try to avoid to have duplicate code because
of that -- measurex currently duplicates many tracex functionality
and this is quite unfortunate because it slows development down)
Additionally, if an error is a string:
3. we can very easily use a switch for comparing its possible
values with "" representing the absence of errors, while it is
more complex to do the same when using a nullable string or even
an error (i.e., an interface)
4. if a type is not nullable, it's easier to write safe code for
it and we may want to refactor experiments to use the internal
representation of measurements for more robust processing code
For all these reasons, let's internally use strings in tracex.
The overall aim here is to reduce the duplicated code between pre
and post-measurex measurements (see https://github.com/ooni/probe/issues/2035).
2022-06-02 10:37:07 +02:00
|
|
|
Err: NewFailureStr(err),
|
2022-05-31 21:53:01 +02:00
|
|
|
NumBytes: n,
|
|
|
|
Time: stop,
|
2022-06-01 14:32:16 +02:00
|
|
|
}})
|
2022-05-31 21:53:01 +02:00
|
|
|
return n, addr, err
|
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
func (c *quicPacketConnWrapper) safeAddrString(addr net.Addr) (out string) {
|
2022-05-31 21:53:01 +02:00
|
|
|
if addr != nil {
|
|
|
|
out = addr.String()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2022-06-01 07:44:54 +02:00
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
var _ model.QUICDialer = &QUICDialerSaver{}
|
2022-06-01 07:44:54 +02:00
|
|
|
var _ model.QUICListener = &QUICListenerSaver{}
|
2022-06-01 23:15:47 +02:00
|
|
|
var _ model.UDPLikeConn = &quicPacketConnWrapper{}
|