/
quic.go
172 lines (139 loc) · 4.8 KB
/
quic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package dslvm
import (
"context"
"crypto/tls"
"crypto/x509"
"sync"
"time"
"github.com/ooni/probe-engine/pkg/logx"
"github.com/ooni/probe-engine/pkg/model"
"github.com/ooni/probe-engine/pkg/netxlite"
"github.com/quic-go/quic-go"
)
// QUICHandshakeStage is a [Stage] that creates [*QUICConnection].
type QUICHandshakeStage struct {
// Input contains the MANDATORY channel from which to read endpoints. We
// assume that this channel will be closed when done.
Input <-chan string
// InsecureSkipVerify OPTIONALLY skips QUIC verification.
InsecureSkipVerify bool
// NextProtos OPTIONALLY configures the ALPN.
NextProtos []string
// Output is the MANDATORY channel emitting [*QUICConnection]. We will close this
// channel when the Input channel has been closed.
Output chan<- *QUICConnection
// RootCAs OPTIONALLY configures alternative root CAs.
RootCAs *x509.CertPool
// ServerName is the MANDATORY server name.
ServerName string
// Tags contains OPTIONAL tags to add to the endpoint observations.
Tags []string
}
// QUICConnection is a QUIC connection.
type QUICConnection struct {
Conn quic.EarlyConnection
tlsConfig *tls.Config
tx Trace
}
// AsSingleUseTransport implements HTTPConnection.
func (c *QUICConnection) AsSingleUseTransport(logger model.Logger) model.HTTPTransport {
return netxlite.NewHTTP3Transport(logger, netxlite.NewSingleUseQUICDialer(c.Conn), c.tlsConfig.Clone())
}
// Close implements HTTPConnection.
func (c *QUICConnection) Close(logger model.Logger) error {
ol := logx.NewOperationLogger(logger, "[#%d] QUICClose %s", c.tx.Index(), c.RemoteAddress())
err := c.Conn.CloseWithError(0, "")
ol.Stop(err)
return err
}
// Network implements HTTPConnection.
func (c *QUICConnection) Network() string {
return "udp"
}
// RemoteAddress implements HTTPConnection.
func (c *QUICConnection) RemoteAddress() (addr string) {
if v := c.Conn.RemoteAddr(); v != nil {
addr = v.String()
}
return
}
// Scheme implements HTTPConnection.
func (c *QUICConnection) Scheme() string {
return "https"
}
// TLSNegotiatedProtocol implements HTTPConnection.
func (c *QUICConnection) TLSNegotiatedProtocol() string {
return c.Conn.ConnectionState().TLS.NegotiatedProtocol
}
// Trace implements HTTPConnection.
func (c *QUICConnection) Trace() Trace {
return c.tx
}
var _ HTTPConnection = &QUICConnection{}
// Run is like [*TCPConnect.Run] except that it reads [endpoints] in Input and
// emits [*QUICConnection] in Output. Each QUIC handshake runs in its own background
// goroutine. The parallelism is controlled by the [Runtime] ActiveConnections [Semaphore] and
// you MUST arrange for the [*QUICConnection] to eventually enter into a [*CloseStage]
// such that the code can release the above mentioned [Semaphore] and close the conn. Note
// that this code TAKES OWNERSHIP of the [*TCPConnection] it reads. We will close these
// conns automatically on failure. On success, they will be closed when the [*QUICConnection]
// wrapping them eventually enters into a [*CloseStage].
func (sx *QUICHandshakeStage) Run(ctx context.Context, rtx Runtime) {
// make sure we close the output channel
defer close(sx.Output)
// track the number of running goroutines
waitGroup := &sync.WaitGroup{}
for endpoint := range sx.Input {
// wait for authorization to process a connection
rtx.ActiveConnections().Wait()
// process connection in a background goroutine
waitGroup.Add(1)
go func(endpoint string) {
defer waitGroup.Done()
sx.handshake(ctx, rtx, endpoint)
}(endpoint)
}
// wait for pending work to finish
waitGroup.Wait()
}
func (sx *QUICHandshakeStage) handshake(ctx context.Context, rtx Runtime, endpoint string) {
// create trace
trace := rtx.NewTrace(rtx.IDGenerator().Add(1), rtx.ZeroTime(), sx.Tags...)
// create a suitable QUIC configuration
config := sx.newTLSConfig()
// start the operation logger
ol := logx.NewOperationLogger(
rtx.Logger(),
"[#%d] QUICHandshake with %s SNI=%s ALPN=%v",
trace.Index(),
endpoint,
config.ServerName,
config.NextProtos,
)
// setup
udpListener := trace.NewUDPListener()
quicDialer := trace.NewQUICDialerWithoutResolver(udpListener, rtx.Logger())
const timeout = 10 * time.Second
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
// handshake
quicConn, err := quicDialer.DialContext(ctx, endpoint, config, &quic.Config{})
// stop the operation logger
ol.Stop(err)
// save the observations
rtx.SaveObservations(maybeTraceToObservations(trace)...)
// handle error case
if err != nil {
return
}
// handle success
sx.Output <- &QUICConnection{Conn: quicConn, tx: trace, tlsConfig: config}
}
func (sx *QUICHandshakeStage) newTLSConfig() *tls.Config {
return &tls.Config{
NextProtos: sx.NextProtos,
InsecureSkipVerify: sx.InsecureSkipVerify,
RootCAs: sx.RootCAs,
ServerName: sx.ServerName,
}
}