-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquic_fingerprint.go
278 lines (228 loc) · 7.21 KB
/
quic_fingerprint.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package clienthellod
import (
"crypto/sha1" // skipcq: GSC-G505
"encoding/binary"
"errors"
"io"
"net"
"runtime"
"sync"
"sync/atomic"
"time"
"github.com/refraction-networking/clienthellod/internal/utils"
)
// QUICFingerprint can be used to generate a fingerprint of a QUIC connection.
type QUICFingerprint struct {
ClientInitials *GatheredClientInitials
HexID string `json:"hex_id,omitempty"`
NumID uint64 `json:"num_id,omitempty"`
UserAgent string `json:"user_agent,omitempty"` // User-Agent header, set by the caller
}
// GenerateQUICFingerprint generates a QUICFingerprint from the gathered ClientInitials.
func GenerateQUICFingerprint(gci *GatheredClientInitials) (*QUICFingerprint, error) {
if err := gci.Wait(); err != nil {
return nil, err // GatheringClientInitials failed (expired before complete)
}
qfp := &QUICFingerprint{
ClientInitials: gci,
// UserAgent: userAgent,
}
// TODO: calculate hash
h := sha1.New() // skipcq: GO-S1025, GSC-G401
updateU64(h, gci.NumID)
updateU64(h, uint64(gci.ClientHello.NormNumID))
updateU64(h, gci.TransportParameters.NumID)
qfp.NumID = binary.BigEndian.Uint64(h.Sum(nil))
qfp.HexID = FingerprintID(qfp.NumID).AsHex()
runtime.SetFinalizer(qfp, func(q *QUICFingerprint) {
q.ClientInitials = nil
})
return qfp, nil
}
const DEFAULT_QUICFINGERPRINT_EXPIRY = 60 * time.Second
// QUICFingerprinter can be used to fingerprint QUIC connections.
type QUICFingerprinter struct {
mapGatheringClientInitials *sync.Map
timeout time.Duration
closed atomic.Bool
}
// NewQUICFingerprinter creates a new QUICFingerprinter.
func NewQUICFingerprinter() *QUICFingerprinter {
return &QUICFingerprinter{
mapGatheringClientInitials: new(sync.Map),
closed: atomic.Bool{},
}
}
// NewQUICFingerprinterWithTimeout creates a new QUICFingerprinter with a timeout.
func NewQUICFingerprinterWithTimeout(timeout time.Duration) *QUICFingerprinter {
return &QUICFingerprinter{
mapGatheringClientInitials: new(sync.Map),
timeout: timeout,
closed: atomic.Bool{},
}
}
// SetTimeout sets the timeout for gathering ClientInitials.
func (qfp *QUICFingerprinter) SetTimeout(timeout time.Duration) {
qfp.timeout = timeout
}
// HandlePacket handles a QUIC packet.
func (qfp *QUICFingerprinter) HandlePacket(from string, p []byte) error {
if qfp.closed.Load() {
return errors.New("QUICFingerprinter closed")
}
ci, err := UnmarshalQUICClientInitialPacket(p)
if err != nil {
if errors.Is(err, ErrNotQUICLongHeaderFormat) || errors.Is(err, ErrNotQUICInitialPacket) {
return nil // totally fine, we don't care about non QUIC initials
}
return err
}
var testGci *GatheredClientInitials
if qfp.timeout == time.Duration(0) {
testGci = GatherClientInitials()
} else {
testGci = GatherClientInitialsWithDeadline(time.Now().Add(qfp.timeout))
}
chosenGci, existing := qfp.mapGatheringClientInitials.LoadOrStore(from, testGci)
if !existing {
// if we stored the testGci, we need to delete it after the timeout
funcExpiringAfter := func(d time.Duration) {
<-time.After(d)
qfp.mapGatheringClientInitials.Delete(from)
}
if qfp.timeout == time.Duration(0) {
go funcExpiringAfter(DEFAULT_QUICFINGERPRINT_EXPIRY)
} else {
go funcExpiringAfter(qfp.timeout)
}
}
gci, ok := chosenGci.(*GatheredClientInitials)
if !ok {
return errors.New("GatheredClientInitials loaded from sync.Map failed type assertion")
}
return gci.AddPacket(ci)
}
// HandleUDPConn handles a QUIC connection over UDP.
func (qfp *QUICFingerprinter) HandleUDPConn(pc net.PacketConn) error {
var buf [2048]byte
for {
if qfp.closed.Load() {
return errors.New("QUICFingerprinter closed")
}
n, addr, err := pc.ReadFrom(buf[:])
if err != nil {
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrClosedPipe) || errors.Is(err, net.ErrClosed) {
return err
}
continue // ignore errors unless connection is closed
}
qfp.HandlePacket(addr.String(), buf[:n])
}
}
// HandleIPConn handles a QUIC connection over IP.
func (qfp *QUICFingerprinter) HandleIPConn(ipc *net.IPConn) error {
var buf [2048]byte
for {
if qfp.closed.Load() {
return errors.New("QUICFingerprinter closed")
}
n, ipAddr, err := ipc.ReadFromIP(buf[:])
if err != nil {
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrClosedPipe) || errors.Is(err, net.ErrClosed) {
return err
}
continue // ignore errors unless connection is closed
}
udpPkt, err := utils.ParseUDPPacket(buf[:n])
if err != nil {
continue
}
if udpPkt.DstPort != 443 {
continue
}
udpAddr := &net.UDPAddr{IP: ipAddr.IP, Port: int(udpPkt.SrcPort)}
qfp.HandlePacket(udpAddr.String(), udpPkt.Payload)
}
}
// Peek looks up a QUICFingerprint for a given key.
func (qfp *QUICFingerprinter) Peek(from string) *QUICFingerprint {
gci, ok := qfp.mapGatheringClientInitials.Load(from)
if !ok {
return nil
}
gatheredCI, ok := gci.(*GatheredClientInitials)
if !ok {
return nil
}
if !gatheredCI.Completed() {
return nil // gathering incomplete
}
qf, err := GenerateQUICFingerprint(gatheredCI)
if err != nil {
return nil
}
return qf
}
// PeekAwait looks up a QUICFingerprint for a given key.
// It will wait for the gathering to complete if the key exists but the
// gathering is not yet complete, e.g., when CRYPTO frames spread across
// multiple initial packets and some but not all of them are received.
func (qfp *QUICFingerprinter) PeekAwait(from string) (*QUICFingerprint, error) {
gci, ok := qfp.mapGatheringClientInitials.Load(from)
if !ok {
return nil, errors.New("GatheredClientInitials not found for the given key")
}
gatheredCI, ok := gci.(*GatheredClientInitials)
if !ok {
return nil, errors.New("GatheredClientInitials loaded from sync.Map failed type assertion")
}
qf, err := GenerateQUICFingerprint(gatheredCI)
if err != nil {
return nil, err
}
return qf, nil
}
// Pop looks up a QUICFingerprint for a given key and deletes it from
// the fingerprinter if found.
func (qfp *QUICFingerprinter) Pop(from string) *QUICFingerprint {
gci, ok := qfp.mapGatheringClientInitials.LoadAndDelete(from)
if !ok {
return nil
}
gatheredCI, ok := gci.(*GatheredClientInitials)
if !ok {
return nil
}
if !gatheredCI.Completed() {
return nil // gathering incomplete
}
qf, err := GenerateQUICFingerprint(gatheredCI)
if err != nil {
return nil
}
return qf
}
// PopAwait looks up a QUICFingerprint for a given key and deletes it from
// the fingerprinter if found.
// It will wait for the gathering to complete if the key exists but the
// gathering is not yet complete, e.g., when CRYPTO frames spread across
// multiple initial packets and some but not all of them are received.
func (qfp *QUICFingerprinter) PopAwait(from string) (*QUICFingerprint, error) {
gci, ok := qfp.mapGatheringClientInitials.LoadAndDelete(from)
if !ok {
return nil, errors.New("GatheredClientInitials not found for the given key")
}
gatheredCI, ok := gci.(*GatheredClientInitials)
if !ok {
return nil, errors.New("GatheredClientInitials loaded from sync.Map failed type assertion")
}
qf, err := GenerateQUICFingerprint(gatheredCI)
if err != nil {
return nil, err
}
return qf, nil
}
// Close closes the QUICFingerprinter.
func (qfp *QUICFingerprinter) Close() {
qfp.closed.Store(true)
}