-
Notifications
You must be signed in to change notification settings - Fork 11
/
request.go
70 lines (63 loc) · 1.68 KB
/
request.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
package socks5
import (
"bytes"
"errors"
)
// Request The SOCKS request is formed as follows:
// +----+-----+-------+------+----------+----------+
// |VER | CMD | RSV | ATYP | DST.ADDR | DST.PORT |
// +----+-----+-------+------+----------+----------+
// | 1 | 1 | X'00' | 1 | Variable | 2 |
// +----+-----+-------+------+----------+----------+
type Request struct {
VER
CMD
RSV uint8
*Address
}
// UDPHeader Each UDP datagram carries a UDP request
// header with it:
// +----+------+------+----------+----------+----------+
// |RSV | FRAG | ATYP | DST.ADDR | DST.PORT | DATA |
// +----+------+------+----------+----------+----------+
// | 2 | 1 | 1 | Variable | 2 | Variable |
// +----+------+------+----------+----------+----------+
type UDPHeader struct {
RSV uint16
FRAG uint8
*Address
Data []byte
}
var errEmptyPayload = errors.New("empty payload")
// PackUDPData add UDP request header before payload.
func PackUDPData(addr *Address, payload []byte) ([]byte, error) {
if len(payload) == 0 {
return nil, errEmptyPayload
}
if addr == nil {
return nil, errors.New("addr is nil")
}
// RSV, FRAG
data := []byte{0x00, 0x00, 0x00}
dest, err := addr.Bytes(Version5)
if err != nil {
return nil, err
}
// ATYP, DEST.IP, DEST.PORT
data = append(data, dest...)
// DATA
data = append(data, payload...)
return data, nil
}
// UnpackUDPData split UDP header and payload.
func UnpackUDPData(data []byte) (addr *Address, payload []byte, err error) {
// trim RSV, FRAG
data = data[3:]
buf := bytes.NewBuffer(data)
addr, _, err = readAddress(buf, Version5)
if err != nil {
return nil, nil, err
}
payload = buf.Bytes()
return
}