forked from justzx2011/goproxy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
frame.go
384 lines (338 loc) · 6.8 KB
/
frame.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
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
package msocks
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
)
const (
MSG_UNKNOWN = iota
MSG_RESULT
MSG_AUTH
MSG_DATA
MSG_SYN
MSG_WND
MSG_FIN
MSG_RST
MSG_PING
MSG_DNS
MSG_ADDRS
)
func ReadString(r io.Reader) (s string, err error) {
var length uint16
err = binary.Read(r, binary.BigEndian, &length)
if err != nil {
return
}
buf := make([]byte, length)
_, err = io.ReadFull(r, buf)
if err != nil {
return
}
return string(buf), nil
}
func WriteString(w io.Writer, s string) (err error) {
err = binary.Write(w, binary.BigEndian, uint16(len(s)))
if err != nil {
return
}
_, err = w.Write([]byte(s))
return
}
type Frame interface {
GetStreamid() uint16
GetSize() uint16
Packed() (buf *bytes.Buffer, err error)
Unpack(r io.Reader) error
Debug(prefix string)
}
func ReadFrame(r io.Reader) (f Frame, err error) {
fb := new(FrameBase)
err = binary.Read(r, binary.BigEndian, fb)
if err != nil {
return
}
switch fb.Type {
default:
err = fmt.Errorf("unknown frame: type(%d), length(%d), streamid(%d).",
fb.Type, fb.Length, fb.Streamid)
return
case MSG_RESULT:
f = &FrameResult{FrameBase: *fb}
case MSG_AUTH:
f = &FrameAuth{FrameBase: *fb}
case MSG_DATA:
f = &FrameData{FrameBase: *fb}
case MSG_SYN:
f = &FrameSyn{FrameBase: *fb}
case MSG_WND:
f = &FrameWnd{FrameBase: *fb}
case MSG_FIN:
f = &FrameFin{FrameBase: *fb}
case MSG_RST:
f = &FrameRst{FrameBase: *fb}
case MSG_PING:
f = &FramePing{FrameBase: *fb}
}
err = f.Unpack(r)
return
}
type FrameBase struct {
Type uint8
Length uint16
Streamid uint16
}
func (f *FrameBase) GetStreamid() uint16 {
return f.Streamid
}
func (f *FrameBase) GetSize() uint16 {
return f.Length
}
func (f *FrameBase) Packed() (buf *bytes.Buffer, err error) {
buf = bytes.NewBuffer(nil)
buf.Grow(int(5 + f.Length))
binary.Write(buf, binary.BigEndian, f)
return
}
func (f *FrameBase) Unpack(r io.Reader) (err error) {
err = binary.Read(r, binary.BigEndian, f)
return
}
func (f *FrameBase) Debug(prefix string) {
log.Debug("%sframe: type(%d), stream(%d), len(%d).",
prefix, f.Type, f.Streamid, f.Length)
}
type FrameResult struct {
FrameBase
Errno uint32
}
func NewFrameResult(streamid uint16, errno uint32) (f *FrameResult) {
return &FrameResult{
FrameBase: FrameBase{
Type: MSG_RESULT,
Streamid: streamid,
Length: 4,
},
Errno: errno,
}
}
func (f *FrameResult) Packed() (buf *bytes.Buffer, err error) {
buf, err = f.FrameBase.Packed()
if err != nil {
return
}
binary.Write(buf, binary.BigEndian, f.Errno)
return
}
func (f *FrameResult) Unpack(r io.Reader) (err error) {
err = binary.Read(r, binary.BigEndian, &f.Errno)
if err != nil {
return
}
if f.Length != 4 {
err = errors.New("frame result with length not 4.")
return
}
return
}
type FrameAuth struct {
FrameBase
Username string
Password string
}
func NewFrameAuth(streamid uint16, username, password string) (f *FrameAuth) {
return &FrameAuth{
FrameBase: FrameBase{
Type: MSG_AUTH,
Streamid: streamid,
Length: uint16(len(username) + len(password) + 4),
},
Username: username,
Password: password,
}
}
func (f *FrameAuth) Packed() (buf *bytes.Buffer, err error) {
buf, err = f.FrameBase.Packed()
if err != nil {
return
}
err = WriteString(buf, f.Username)
if err != nil {
return
}
err = WriteString(buf, f.Password)
return
}
func (f *FrameAuth) Unpack(r io.Reader) (err error) {
f.Username, err = ReadString(r)
if err != nil {
return
}
f.Password, err = ReadString(r)
if err != nil {
return
}
if f.Length != uint16(len(f.Username)+len(f.Password)+4) {
err = errors.New("frame auth length not match.")
}
return
}
type FrameData struct {
FrameBase
Data []byte
}
func NewFrameData(streamid uint16, data []byte) (f *FrameData) {
return &FrameData{
FrameBase: FrameBase{
Type: MSG_DATA,
Streamid: streamid,
Length: uint16(len(data)),
},
Data: data,
}
}
func (f *FrameData) Packed() (buf *bytes.Buffer, err error) {
buf, err = f.FrameBase.Packed()
if err != nil {
return
}
_, err = buf.Write(f.Data)
return
}
func (f *FrameData) Unpack(r io.Reader) (err error) {
f.Data = make([]byte, f.Length)
_, err = io.ReadFull(r, f.Data)
return
}
type FrameSyn struct {
FrameBase
Address string
}
func NewFrameSyn(streamid uint16, addr string) (f *FrameSyn) {
return &FrameSyn{
FrameBase: FrameBase{
Type: MSG_SYN,
Streamid: streamid,
Length: uint16(len(addr) + 2),
},
Address: addr,
}
}
func (f *FrameSyn) Packed() (buf *bytes.Buffer, err error) {
buf, err = f.FrameBase.Packed()
if err != nil {
return
}
err = WriteString(buf, f.Address)
return
}
func (f *FrameSyn) Unpack(r io.Reader) (err error) {
f.Address, err = ReadString(r)
if err != nil {
return
}
if f.Length != uint16(len(f.Address)+2) {
err = errors.New("frame sync length not match.")
}
return
}
func (f *FrameSyn) Debug(prefix string) {
log.Debug("%sframe syn: stream(%d), len(%d), addr(%s).",
prefix, f.Streamid, f.Length, f.Address)
}
type FrameWnd struct {
FrameBase
Window uint32
}
func NewFrameWnd(streamid uint16, window uint32) (f *FrameWnd) {
return &FrameWnd{
FrameBase: FrameBase{
Type: MSG_WND,
Streamid: streamid,
Length: 4,
},
Window: window,
}
}
func (f *FrameWnd) Packed() (buf *bytes.Buffer, err error) {
buf, err = f.FrameBase.Packed()
if err != nil {
return
}
binary.Write(buf, binary.BigEndian, f.Window)
return
}
func (f *FrameWnd) Unpack(r io.Reader) (err error) {
err = binary.Read(r, binary.BigEndian, &f.Window)
if err != nil {
return
}
if f.Length != 4 {
err = errors.New("frame ack with length not 4.")
return
}
return
}
func (f *FrameWnd) Debug(prefix string) {
log.Debug("%sframe wnd: stream(%d), len(%d), window(%d).",
prefix, f.Streamid, f.Length, f.Window)
}
type FrameFin struct {
FrameBase
}
func NewFrameFin(streamid uint16) (f *FrameFin) {
return &FrameFin{
FrameBase: FrameBase{
Type: MSG_FIN,
Streamid: streamid,
Length: 0,
},
}
}
func (f *FrameFin) Unpack(r io.Reader) (err error) {
if f.Length != 0 {
return errors.New("frame fin with length not 0.")
}
return
}
type FrameRst struct {
FrameBase
}
func NewFrameRst(streamid uint16) (f *FrameRst) {
return &FrameRst{
FrameBase: FrameBase{
Type: MSG_RST,
Streamid: streamid,
Length: 0,
},
}
}
func (f *FrameRst) Unpack(r io.Reader) (err error) {
if f.Length != 0 {
return errors.New("frame rst with length not 0.")
}
return
}
type FramePing struct {
FrameBase
}
func NewFramePing() (f *FramePing) {
return &FramePing{
FrameBase: FrameBase{
Type: MSG_PING,
Streamid: 0,
Length: 0,
},
}
}
func (f *FramePing) Unpack(r io.Reader) (err error) {
if f.Length != 0 {
return errors.New("frame ping with length not 0.")
}
return
}
type FrameSender interface {
SendFrame(Frame) error
CloseFrame() error
}