-
Notifications
You must be signed in to change notification settings - Fork 2
/
tcp.go
148 lines (129 loc) · 3.4 KB
/
tcp.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
package main
import (
"bytes"
"errors"
"io"
"io/ioutil"
"log"
"sync"
"github.com/google/gopacket/tcpassembly"
"github.com/google/gopacket/tcpassembly/tcpreader"
"golang.org/x/net/http2"
"golang.org/x/net/http2/hpack"
)
var clientPrefix = []byte("PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n")
const clientPrefixLen = 24
// tcpStream just one way
type tcpStream struct {
tcpreader.ReaderStream
tuple tcp4Tuple
room *meeting
// on-the-fly, no SYN got
noSyn bool
firstChk bool
}
func newTcpStream(tuple tcp4Tuple, room *meeting) *tcpStream {
return &tcpStream{
ReaderStream: tcpreader.NewReaderStream(),
tuple: tuple,
room: room,
}
}
func (ts *tcpStream) Process(done func()) {
defer done()
var err error
defer func() {
// if not EOF error so ignore packet after
if !errors.Is(err, io.EOF) && !errors.Is(err, io.ErrUnexpectedEOF) {
// ts.ReaderStream.Close() can't use after Read()
io.Copy(ioutil.Discard, &ts.ReaderStream)
}
}()
var data [clientPrefixLen]byte
if _, err = io.ReadFull(&ts.ReaderStream, data[:]); err != nil {
if conf.Verbose && !errors.Is(err, io.EOF) && !errors.Is(err, io.ErrUnexpectedEOF) {
log.Println(ts.tuple, "io.ReadFull failed")
}
return
}
if ts.noSyn {
if conf.ForceHttp2 {
// try get the HeadersFrame
if data, err1 := skipToHeadersFrame(data[:], &ts.ReaderStream); err1 != nil {
err = err1
if conf.Verbose {
log.Println(ts.tuple, "skipToHeadersFrame failed")
}
} else {
if conf.Verbose {
log.Println(ts.tuple, "skipToHeadersFrame pass")
}
err = ts.processHttp2(io.MultiReader(bytes.NewReader(data), &ts.ReaderStream))
}
}
} else if bytes.Equal(data[:], clientPrefix) {
// client need skip h2c prefix
if conf.Verbose {
log.Println(ts.tuple, "got client h2c")
}
err = ts.processHttp2(&ts.ReaderStream)
} else if isServerSettingFrame(data[:]) {
// server first frame must be SettingFrame
if conf.Verbose {
log.Println(ts.tuple, "got server h2c")
}
err = ts.processHttp2(io.MultiReader(bytes.NewReader(data[:]), &ts.ReaderStream))
} else if conf.Verbose {
log.Println(ts.tuple, "not support")
}
}
var dummyHeader []byte
var dummyOne sync.Once
func getDummyHeader() []byte {
dummyOne.Do(func() {
var buf bytes.Buffer
w := hpack.NewEncoder(&buf)
w.WriteField(hpack.HeaderField{Name: "dummy"})
dummyHeader = bytes.Repeat(buf.Bytes(), 100)
})
return dummyHeader
}
func (ts *tcpStream) processHttp2(r io.Reader) (err error) {
// talk in the meeting-room (hahaha)
ts.room.Talk(ts.tuple)
defer func() {
ts.room.RecvPacket(&h2Packet{Tuple: ts.tuple, End: true})
}()
framer := http2.NewFramer(ioutil.Discard, r)
// framer.SetReuseFrames()
framer.ReadMetaHeaders = hpack.NewDecoder(4096, nil)
framer.ReadMetaHeaders.SetEmitEnabled(false)
if ts.noSyn {
// on-the-fly need feed dummy header make sure decoder no-error
if _, err1 := framer.ReadMetaHeaders.Write(getDummyHeader()); err1 != nil {
panic(err1)
}
}
bulder := make(h2PacketBuilder)
for {
f, err1 := framer.ReadFrame()
if err1 != nil {
err = err1
if conf.Verbose {
log.Println(ts.tuple, "framer.ReadFrame failed", err)
}
return
}
bulder.Process(f, ts.tuple, ts.room)
}
}
func (ts *tcpStream) Reassembled(reassembly []tcpassembly.Reassembly) {
// detect on-the-fly
if !ts.firstChk {
ts.firstChk = true
if !reassembly[0].Start {
ts.noSyn = true
}
}
ts.ReaderStream.Reassembled(reassembly)
}