Repository navigation
Expand file tree
/
Copy pathscheme.go
More file actions
658 lines (604 loc) · 17.1 KB
/
Copy pathscheme.go
File metadata and controls
658 lines (604 loc) · 17.1 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
package edge
import (
"bufio"
"context"
"crypto/rand"
"crypto/tls"
"encoding/binary"
"net"
"net/http"
"net/netip"
"slices"
"strings"
"sync"
"sync/atomic"
"time"
"golang.org/x/net/http2/hpack"
)
// h2HeaderTableSize is the HPACK table size the front's HTTP/2 server advertises, which is
// the table a schemeConn mirrors.
const h2HeaderTableSize = 4096
// h2MaxReadFrameSize is the largest frame the front's HTTP/2 server reads.
const h2MaxReadFrameSize = 1 << 20
// schemeName is the name of the field a schemeConn adds to an HTTP/2 request for its
// :scheme. It is random per process, so that no client can send it.
var schemeName = "edge-scheme-" + strings.ToLower(rand.Text())
var schemeHeader = http.CanonicalHeaderKey(schemeName)
// The field as a never-indexed literal, which leaves the HPACK table of both ends as the
// client left it. Its value is one byte, so a frame grows by one length whatever its
// :scheme.
var (
schemeFieldHTTP = schemeField('h')
schemeFieldHTTPS = schemeField('s')
schemeFieldNone = schemeField('-')
)
func schemeField(code byte) string {
return string(slices.Concat([]byte{0x10, byte(len(schemeName))}, []byte(schemeName), []byte{1, code})) //nolint:gosec // schemeName is 38 bytes
}
// schemeOutKeep is the capacity of the output buffer a schemeConn keeps once drained. A
// frame held in full may grow it to h2MaxReadFrameSize.
const schemeOutKeep = 64 << 10
const h2Preface = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
const (
h2FrameHeaders = 0x1
h2FrameContinuation = 0x9
h2FlagEndHeaders = 0x4
h2FlagPadded = 0x8
h2FlagPriority = 0x20
)
const (
schemeUndecided uint8 = iota
schemeFrames
schemeRaw
// schemeRefused ends every read of a connection the proxy opened itself. net/http reads
// again after an error and would find the buffered request.
schemeRefused
)
// schemeConn adds the :scheme of each HTTP/2 request as schemeHeader, at the end of its
// header block. net/http exposes :scheme nowhere, and schemeConn goes once net/http puts it
// on the Request (golang/go#68365).
type schemeConn struct {
net.Conn
br *bufio.Reader
dec *hpack.Decoder
// p refuses a connection the proxy opened itself, nil where nothing is refused.
p *Proxy
scheme string
out []byte
// headerBy is the bound of the first request header in nanoseconds since the epoch, zero
// once it is read. asked is the read deadline last asked for, applied once the bound goes.
headerBy atomic.Int64
asked atomic.Int64
// dmu guards reached, set once the connection reached its server, inDrain, once it reached it
// after the start of the drain, h2, once it reached it over TLS as HTTP/2, and begun, once the
// first byte of a request arrived. The drain bounds the connection between reached and begun
// alone.
dmu sync.Mutex
// wmu orders the wake of the front stage of the cut against the deadlines set meanwhile.
wmu sync.Mutex
outPos int
maxStr int
// The frame being read.
left int
pos int
fragStart int
fragEnd int
// stage is stageWoken from the wake, which keeps every deadline of the connection in the past,
// to stageEnding, once dispatchFront began its end.
stage atomic.Uint32
// The header block being read.
stream uint32
last uint32
state uint8
end bool
inBlock bool
request bool
skip bool
desync bool
reached bool
inDrain bool
h2 bool
begun bool
// held marks the frame ending a request block, kept in out until it is read in full since
// the field is added only to a block that decodes.
held bool
}
const (
stageNone uint32 = iota
stageWoken
stageEnding
)
// tlsSchemeConn is a schemeConn over TLS. Its ConnectionState and HandshakeContext have
// net/http negotiate h2 on it as on a *tls.Conn.
type tlsSchemeConn struct {
*schemeConn
}
type connectionStater interface {
ConnectionState() tls.ConnectionState
}
func (c tlsSchemeConn) ConnectionState() tls.ConnectionState {
return c.Conn.(connectionStater).ConnectionState()
}
func (c tlsSchemeConn) HandshakeContext(ctx context.Context) error {
if h, ok := c.Conn.(interface{ HandshakeContext(context.Context) error }); ok {
return h.HandshakeContext(ctx)
}
return nil
}
func newSchemeConn(c net.Conn, maxHeaderBytes int) net.Conn {
sc := &schemeConn{Conn: c, maxStr: maxHeaderBytes}
if _, ok := c.(connectionStater); ok {
return tlsSchemeConn{sc}
}
return sc
}
// schemeConnOf returns the schemeConn c is, if it is one.
func schemeConnOf(c net.Conn) *schemeConn {
switch c := c.(type) {
case *schemeConn:
return c
case tlsSchemeConn:
return c.schemeConn
}
return nil
}
func (c *schemeConn) NetConn() net.Conn { return c.Conn }
// markBegun records that the first byte of a request arrived, and gives the read the cap of the
// first request header when toHeader is set.
func (c *schemeConn) markBegun(toHeader bool) {
c.dmu.Lock()
defer c.dmu.Unlock()
c.begun = true
if toHeader {
var header time.Time
if n := c.headerBy.Load(); n != 0 {
header = time.Unix(0, n)
}
_ = c.Conn.SetReadDeadline(header)
c.keepWoken()
}
}
// markReached records that the connection reached its server, after the start of the drain when
// inDrain is set, and over TLS as HTTP/2 when h2 is.
func (c *schemeConn) markReached(inDrain, h2 bool) {
c.dmu.Lock()
c.reached, c.inDrain, c.h2 = true, inDrain, h2
c.dmu.Unlock()
}
// wake ends the read or write in progress and every later one, as net/http's abortPendingRead
// wakes its reader, unless the end of the connection began.
func (c *schemeConn) wake() {
c.wmu.Lock()
defer c.wmu.Unlock()
if c.stage.CompareAndSwap(stageNone, stageWoken) {
_ = c.Conn.SetDeadline(time.Unix(1, 0))
}
}
// beginEnd gives the deadlines back to the end of the connection, which bounds itself.
func (c *schemeConn) beginEnd() {
c.wmu.Lock()
c.stage.Store(stageEnding)
c.wmu.Unlock()
}
// expire gives the connection its failed end at the deadline of the front stage of the cut unless
// its end began, which bounds itself, I4.
func (c *schemeConn) expire() {
c.wmu.Lock()
defer c.wmu.Unlock()
if c.stage.Load() == stageWoken {
abort(c.Conn)
}
}
// keepWoken puts back the past deadline of the wake over a deadline just set.
func (c *schemeConn) keepWoken() {
if c.stage.Load() != stageWoken {
return
}
c.wmu.Lock()
if c.stage.Load() == stageWoken {
_ = c.Conn.SetDeadline(time.Unix(1, 0))
}
c.wmu.Unlock()
}
// drainBy sets the deadline of the drain on conn, the front connection over this one, until the
// first byte of a request arrived, docs/lifecycle.md The drain. A TLS handshake still in progress
// is bounded by the read header timeout, T then counting from its end, and by the deadline of the
// drain when there is none.
func (c *schemeConn) drainBy(conn net.Conn, deadline time.Time) {
c.dmu.Lock()
defer c.dmu.Unlock()
if c.inDrain || (!c.reached && c.headerBy.Load() != 0) || c.begun {
// One that reached its server after the start has its T from then already.
return
}
if c.reached && c.h2 {
// An HTTP/2 connection gets its GOAWAY at the start, endDrained, which the read that
// ends now hands it to.
deadline = time.Now()
}
_ = conn.SetDeadline(deadline)
}
// SetReadDeadline is the read deadline of the connection, capped by the bound of its first
// request header, which counts from the accept.
func (c *schemeConn) SetReadDeadline(t time.Time) error {
var asked int64
if !t.IsZero() {
asked = t.UnixNano()
}
c.asked.Store(asked)
if by := c.headerBy.Load(); by != 0 && (t.IsZero() || t.UnixNano() > by) {
t = time.Unix(0, by)
}
err := c.Conn.SetReadDeadline(t)
c.keepWoken()
return err
}
func (c *schemeConn) SetWriteDeadline(t time.Time) error {
err := c.Conn.SetWriteDeadline(t)
c.keepWoken()
return err
}
func (c *schemeConn) SetDeadline(t time.Time) error {
if err := c.SetReadDeadline(t); err != nil {
return err
}
return c.SetWriteDeadline(t)
}
// boundHeader caps the read deadlines at t until the first request header is read. A zero t
// lifts the cap and gives the connection the read deadline last asked for.
func (c *schemeConn) boundHeader(t time.Time) {
if !t.IsZero() {
c.headerBy.Store(t.UnixNano())
return
}
if c.headerBy.Swap(0) == 0 {
return
}
var asked time.Time
if n := c.asked.Load(); n != 0 {
asked = time.Unix(0, n)
}
_ = c.Conn.SetReadDeadline(asked)
c.keepWoken()
}
func (c *schemeConn) CloseWrite() error { return closeWrite(c.Conn) }
func (c *schemeConn) Abort() { abort(c.Conn) }
func (c *schemeConn) Read(p []byte) (int, error) {
if len(p) == 0 {
return 0, nil
}
for {
if c.held {
if err := c.fill(); err != nil {
return 0, err
}
continue
}
if c.outPos < len(c.out) {
n := copy(p, c.out[c.outPos:])
c.outPos += n
if c.outPos == len(c.out) {
c.out, c.outPos = c.out[:0], 0
if cap(c.out) > schemeOutKeep {
c.out = nil
}
}
return n, nil
}
switch c.state {
case schemeUndecided:
if err := c.decide(); err != nil {
return 0, err
}
continue
case schemeRefused:
return 0, errFromItself
case schemeRaw:
if c.br != nil {
if c.br.Buffered() > 0 {
return c.br.Read(p)
}
c.br, c.dec, c.out = nil, nil, nil
}
return c.Conn.Read(p)
}
if c.left > 0 {
return c.readPayload(p)
}
if err := c.nextFrame(); err != nil {
return 0, err
}
}
}
// http2 decides, as the first read would, whether the connection uses HTTP/2, from the ALPN of
// a TLS connection or the preface of a plaintext one. What it reads is read again by the server.
func (c *schemeConn) http2() (bool, error) {
if c.state == schemeUndecided {
if err := c.decide(); err != nil {
return false, err
}
}
return c.state == schemeFrames, nil
}
// decide reads the frames of an HTTP/2 connection and passes any other through. In plaintext
// it decides on the first byte that leaves the preface, so a short HTTP/1.1 request is never
// delayed.
func (c *schemeConn) decide() error {
cs, isTLS := c.Conn.(connectionStater)
if !isTLS {
if c.br == nil {
c.br = bufio.NewReader(c.Conn)
}
if _, err := c.br.Peek(1); err != nil {
return err
}
c.markBegun(false)
}
// net/http has run the handshake of a TLS connection before its first read.
if c.p != nil && c.p.fromItself(c.RemoteAddr(), c.LocalAddr(), netip.AddrPort{}) {
abort(c.Conn)
c.state = schemeRefused
c.br = nil
return errFromItself
}
if isTLS && cs.ConnectionState().NegotiatedProtocol != "h2" {
// The first byte of a request, awaited under the deadline of the front as on plaintext.
if c.br == nil {
c.br = bufio.NewReader(c.Conn)
}
if _, err := c.br.Peek(1); err != nil {
return err
}
c.markBegun(false)
c.state = schemeRaw
return nil
}
if c.br == nil {
c.br = bufio.NewReader(c.Conn)
}
if _, err := c.br.Peek(1); err != nil {
return err
}
if !isTLS {
// The first byte may begin an HTTP/1.1 request, so the rest of the preface waits under the
// cap of the first request header. Over TLS, ALPN gave h2, and the preface begins none.
c.markBegun(true)
}
n := max(c.br.Buffered(), 1)
for {
b, err := c.br.Peek(min(n, len(h2Preface)))
if string(b) != h2Preface[:len(b)] {
c.state = schemeRaw
return nil
}
if len(b) == len(h2Preface) {
c.out = append(c.out, b...)
_, _ = c.br.Discard(len(b))
c.dec = hpack.NewDecoder(h2HeaderTableSize, c.emit)
c.dec.SetMaxStringLength(c.maxStr)
c.state = schemeFrames
return nil
}
if err != nil {
return err
}
n = max(n+1, c.br.Buffered())
}
}
func (c *schemeConn) emit(f hpack.HeaderField) {
if !f.IsPseudo() {
c.dec.SetEmitEnabled(false)
return
}
if f.Name == ":scheme" {
c.scheme = f.Value
}
}
// A frame on which net/http ends the connection stops the parsing, and the rest of the
// connection passes through unread.
func (c *schemeConn) nextFrame() error {
h, err := c.br.Peek(9)
if err != nil {
return err
}
length := int(h[0])<<16 | int(h[1])<<8 | int(h[2])
typ, flags := h[3], h[4]
stream := binary.BigEndian.Uint32(h[5:9]) & 0x7fffffff
if length > h2MaxReadFrameSize {
c.state = schemeRaw
return nil
}
if typ != h2FrameHeaders && typ != h2FrameContinuation {
if c.inBlock {
c.state = schemeRaw
return nil
}
c.out = append(c.out, h...)
_, _ = c.br.Discard(9)
c.left, c.fragStart, c.fragEnd = length, 0, 0
return nil
}
fragStart, fragEnd := 0, length
if typ == h2FrameHeaders {
if c.inBlock {
c.state = schemeRaw
return nil
}
if flags&h2FlagPadded != 0 {
if length == 0 {
c.state = schemeRaw
return nil
}
p, err := c.br.Peek(10)
if err != nil {
return err
}
fragStart, fragEnd = 1, length-int(p[9])
}
if flags&h2FlagPriority != 0 {
fragStart += 5
}
if fragStart > length {
c.state = schemeRaw
return nil
}
c.inBlock, c.stream = true, stream
// net/http resets the stream of a HEADERS frame padded past its fragment and keeps the
// connection, with its HPACK table and its highest stream as they were.
c.skip = fragStart > fragEnd
c.request = !c.skip && stream > c.last
if c.request {
c.last, c.scheme = stream, ""
}
c.dec.SetEmitEnabled(c.request)
} else if !c.inBlock || stream != c.stream {
c.state = schemeRaw
return nil
}
if c.skip {
fragStart, fragEnd = 0, 0
}
c.out = append(c.out, h...)
_, _ = c.br.Discard(9)
c.end = flags&h2FlagEndHeaders != 0
c.left, c.pos, c.fragStart, c.fragEnd = length, 0, fragStart, fragEnd
c.held = c.end && c.request
if !c.held {
c.advance()
}
return nil
}
func (c *schemeConn) fill() error {
for c.left > 0 {
// Grown with what arrives, so a length the client does not send costs nothing.
c.out = slices.Grow(c.out, min(c.left, 16<<10))
n, err := c.br.Read(c.out[len(c.out):min(cap(c.out), len(c.out)+c.left)])
c.out = c.out[:len(c.out)+n]
c.pos += n
c.left -= n
// An error returned with the last octets of the frame comes back on the next read.
if err != nil && c.left > 0 {
return err
}
}
c.held = false
c.seal()
return nil
}
// seal decodes the fragment of the held frame and adds the field if the block decodes. A
// block that does not decode passes unchanged, so the server fails the connection on it,
// where a field completing a truncated value would make it a stream error.
func (c *schemeConn) seal() {
frame := c.out[len(c.out)-9-c.pos:]
if !c.desync {
if _, err := c.dec.Write(frame[9+c.fragStart : 9+c.fragEnd]); err != nil {
c.desync = true
}
}
c.inBlock = false
c.closeBlock()
if c.desync {
return
}
field := c.field()
// net/http ends the connection on a CONTINUATION after an invalid or a truncated field
// list, where the same list in one frame is a stream error.
if grown := c.pos + len(field); grown <= h2MaxReadFrameSize {
frame[0], frame[1], frame[2] = byte(grown>>16), byte(grown>>8), byte(grown) //nolint:gosec // at most h2MaxReadFrameSize
at := len(c.out) - (c.pos - c.fragEnd)
c.out = append(c.out, field...)
copy(c.out[at+len(field):], c.out[at:len(c.out)-len(field)])
copy(c.out[at:], field)
return
}
frame[4] &^= h2FlagEndHeaders
c.out = append(c.out, 0, 0, byte(len(field)), h2FrameContinuation, h2FlagEndHeaders) //nolint:gosec // a field is 42 bytes
c.out = binary.BigEndian.AppendUint32(c.out, c.stream)
c.out = append(c.out, field...)
}
func (c *schemeConn) readPayload(p []byte) (int, error) {
n, err := c.br.Read(p[:min(len(p), c.left)])
if c.inBlock && !c.desync {
lo, hi := max(c.pos, c.fragStart), min(c.pos+n, c.fragEnd)
if lo < hi {
if _, werr := c.dec.Write(p[lo-c.pos : hi-c.pos]); werr != nil {
c.desync = true
}
}
}
c.pos += n
c.left -= n
c.advance()
return n, err
}
// The frame ending a request block never reaches advance, seal ends that block.
func (c *schemeConn) advance() {
if c.left > 0 || !c.inBlock || !c.end {
return
}
c.inBlock = false
if c.skip {
c.skip = false
return
}
c.closeBlock()
}
func (c *schemeConn) closeBlock() {
if !c.desync && c.dec.Close() != nil {
c.desync = true
}
}
func (c *schemeConn) field() string {
switch c.scheme {
case "http":
return schemeFieldHTTP
case "https":
return schemeFieldHTTPS
}
return schemeFieldNone
}
// schemeListener wraps each connection a front accepts in a schemeConn.
type schemeListener struct {
net.Listener
p *Proxy
maxHeaderBytes int
}
func (l schemeListener) Accept() (net.Conn, error) {
c, err := l.Listener.Accept()
if err != nil {
return nil, err
}
sc := newSchemeConn(c, l.maxHeaderBytes)
schemeConnOf(sc).p = l.p
return sc, nil
}
// takeScheme returns the scheme of an HTTP/2 or HTTP/3 request and removes schemeHeader from
// it. It returns "" for an HTTP/1.1 request, whose absolute target has the scheme.
//
// An HTTP/2 request on a stream below the last one, which net/http accepts and the schemeConn
// takes for trailers, has no schemeHeader and gets "".
func takeScheme(r *http.Request) string {
if r.ProtoMajor < 2 {
return ""
}
if sessionFromRequest(r).hasSchemeField {
vs := r.Header[schemeHeader]
delete(r.Header, schemeHeader)
if len(vs) != 1 {
return ""
}
switch vs[0] {
case "h":
return "http"
case "s":
return "https"
}
return ""
}
// quic-go does not expose :scheme either, so the scheme is the connection's. A client
// sending "http" over TLS is forwarded as "https".
if r.TLS != nil {
return "https"
}
return "http"
}