2022-08-17 09:19:11 +02:00
|
|
|
package measurexlite
|
|
|
|
|
|
|
|
//
|
|
|
|
// QUIC tracing
|
|
|
|
//
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/lucas-clemente/quic-go"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewQUICDialerWithoutResolver is equivalent to netxlite.NewQUICDialerWithoutResolver
|
|
|
|
// except that it returns a model.QUICDialer that uses this trace.
|
|
|
|
func (tx *Trace) NewQUICDialerWithoutResolver(listener model.QUICListener, dl model.DebugLogger) model.QUICDialer {
|
|
|
|
return &quicDialerTrace{
|
|
|
|
qd: tx.newQUICDialerWithoutResolver(listener, dl),
|
|
|
|
tx: tx,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// quicDialerTrace is a trace-aware QUIC dialer.
|
|
|
|
type quicDialerTrace struct {
|
|
|
|
qd model.QUICDialer
|
|
|
|
tx *Trace
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ model.QUICDialer = &quicDialerTrace{}
|
|
|
|
|
|
|
|
// DialContext implements model.QUICDialer.DialContext.
|
2022-08-19 11:26:50 +02:00
|
|
|
func (qdx *quicDialerTrace) DialContext(ctx context.Context,
|
2022-08-17 09:19:11 +02:00
|
|
|
address string, tlsConfig *tls.Config, quicConfig *quic.Config) (
|
|
|
|
quic.EarlyConnection, error) {
|
2022-08-19 11:26:50 +02:00
|
|
|
return qdx.qd.DialContext(netxlite.ContextWithTrace(ctx, qdx.tx), address, tlsConfig, quicConfig)
|
2022-08-17 09:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CloseIdleConnections implements model.QUICDialer.CloseIdleConnections.
|
|
|
|
func (qdx *quicDialerTrace) CloseIdleConnections() {
|
|
|
|
qdx.qd.CloseIdleConnections()
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnQUICHandshakeStart implements model.Trace.OnQUICHandshakeStart
|
|
|
|
func (tx *Trace) OnQUICHandshakeStart(now time.Time, remoteAddr string, config *quic.Config) {
|
|
|
|
t := now.Sub(tx.ZeroTime)
|
|
|
|
select {
|
2022-08-17 20:10:48 +02:00
|
|
|
case tx.networkEvent <- NewAnnotationArchivalNetworkEvent(tx.Index, t, "quic_handshake_start"):
|
2022-08-17 09:19:11 +02:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnQUICHandshakeDone implements model.Trace.OnQUICHandshakeDone
|
|
|
|
func (tx *Trace) OnQUICHandshakeDone(started time.Time, remoteAddr string, qconn quic.EarlyConnection,
|
|
|
|
config *tls.Config, err error, finished time.Time) {
|
|
|
|
t := finished.Sub(tx.ZeroTime)
|
|
|
|
state := tls.ConnectionState{}
|
|
|
|
if qconn != nil {
|
|
|
|
state = qconn.ConnectionState().TLS.ConnectionState
|
|
|
|
}
|
|
|
|
select {
|
2022-08-17 20:10:48 +02:00
|
|
|
case tx.quicHandshake <- NewArchivalTLSOrQUICHandshakeResult(
|
2022-08-17 09:19:11 +02:00
|
|
|
tx.Index,
|
|
|
|
started.Sub(tx.ZeroTime),
|
2022-09-08 17:19:59 +02:00
|
|
|
"udp",
|
2022-08-17 09:19:11 +02:00
|
|
|
remoteAddr,
|
|
|
|
config,
|
|
|
|
state,
|
|
|
|
err,
|
|
|
|
t,
|
|
|
|
):
|
|
|
|
default: // buffer is full
|
|
|
|
}
|
|
|
|
select {
|
2022-08-17 20:10:48 +02:00
|
|
|
case tx.networkEvent <- NewAnnotationArchivalNetworkEvent(tx.Index, t, "quic_handshake_done"):
|
2022-08-17 09:19:11 +02:00
|
|
|
default: // buffer is full
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// QUICHandshakes drains the network events buffered inside the QUICHandshake channel.
|
|
|
|
func (tx *Trace) QUICHandshakes() (out []*model.ArchivalTLSOrQUICHandshakeResult) {
|
|
|
|
for {
|
|
|
|
select {
|
2022-08-17 20:10:48 +02:00
|
|
|
case ev := <-tx.quicHandshake:
|
2022-08-17 09:19:11 +02:00
|
|
|
out = append(out, ev)
|
|
|
|
default:
|
|
|
|
return // done
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-17 20:10:48 +02:00
|
|
|
|
|
|
|
// FirstQUICHandshakeOrNil drains the network events buffered inside the QUICHandshake channel
|
|
|
|
// and returns the first QUICHandshake, if any. Otherwise, it returns nil.
|
|
|
|
func (tx *Trace) FirstQUICHandshakeOrNil() *model.ArchivalTLSOrQUICHandshakeResult {
|
|
|
|
ev := tx.QUICHandshakes()
|
|
|
|
if len(ev) < 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ev[0]
|
|
|
|
}
|