2021-06-08 11:24:13 +02:00
|
|
|
// Package tlsdialer contains code to establish TLS connections.
|
|
|
|
package tlsdialer
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/modelx"
|
|
|
|
)
|
|
|
|
|
2021-06-08 11:24:13 +02:00
|
|
|
// UnderlyingDialer is the underlying dialer type.
|
|
|
|
type UnderlyingDialer interface {
|
|
|
|
DialContext(ctx context.Context, network, address string) (net.Conn, error)
|
|
|
|
}
|
|
|
|
|
2021-02-02 12:05:47 +01:00
|
|
|
// TLSHandshaker is the generic TLS handshaker
|
|
|
|
type TLSHandshaker interface {
|
|
|
|
Handshake(ctx context.Context, conn net.Conn, config *tls.Config) (
|
|
|
|
net.Conn, tls.ConnectionState, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EmitterTLSHandshaker emits events using the MeasurementRoot
|
|
|
|
type EmitterTLSHandshaker struct {
|
|
|
|
TLSHandshaker
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handshake implements Handshaker.Handshake
|
|
|
|
func (h EmitterTLSHandshaker) Handshake(
|
|
|
|
ctx context.Context, conn net.Conn, config *tls.Config,
|
|
|
|
) (net.Conn, tls.ConnectionState, error) {
|
|
|
|
root := modelx.ContextMeasurementRootOrDefault(ctx)
|
|
|
|
root.Handler.OnMeasurement(modelx.Measurement{
|
|
|
|
TLSHandshakeStart: &modelx.TLSHandshakeStartEvent{
|
2021-06-23 13:36:45 +02:00
|
|
|
DurationSinceBeginning: time.Since(root.Beginning),
|
2021-02-02 12:05:47 +01:00
|
|
|
SNI: config.ServerName,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
tlsconn, state, err := h.TLSHandshaker.Handshake(ctx, conn, config)
|
|
|
|
root.Handler.OnMeasurement(modelx.Measurement{
|
|
|
|
TLSHandshakeDone: &modelx.TLSHandshakeDoneEvent{
|
|
|
|
ConnectionState: modelx.NewTLSConnectionState(state),
|
|
|
|
Error: err,
|
2021-06-23 13:36:45 +02:00
|
|
|
DurationSinceBeginning: time.Since(root.Beginning),
|
2021-02-02 12:05:47 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
return tlsconn, state, err
|
|
|
|
}
|