/
protocol.go
151 lines (132 loc) · 3.16 KB
/
protocol.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
package tnt
import (
"bytes"
"encoding/binary"
"fmt"
"io"
)
type (
Socks5Negotiation struct {
Version uint8
NumOfMethods uint8
Methods []uint8
}
Socks5Request struct {
Version uint8
Command uint8
RSV uint8
AddressType uint8
Address string
Port uint16
AddressWithPort string
RawAddr []byte
}
// TrafficType 0: meaningless, 1: request, 2: response, other: invalid
TrafficType uint8
// Traffic represent traffic throughout c/s
Traffic struct {
Type TrafficType
PayloadLen uint16 // length of payload
Payload []byte // rawaddr
}
)
const (
TrafficMeaningless TrafficType = iota
TrafficRequest
)
const (
layoutType = 0
layoutPayloadLen = 1
layoutPayload = 3
lenType = 1
lenPayloadLen = 2
requestBuf = 269
)
func methodMeaning(n uint8) (result string) {
switch n {
case 0x00:
result = "NO AUTHENTICATION REQUIRED"
case 0x01:
result = "GSSAPI"
case 0x02:
result = "USERNAME/PASSWORD"
case 0x03:
result = "to X'7F' IANA ASSIGNED"
case 0x80:
result = "to X'FE' RESERVED FOR PRIVATE METHODS"
default:
result = `illegal method of ${n}`
}
return
}
func commandMeaning(n uint8) (result string) {
switch n {
case 0x01:
result = "CONNECT"
case 0x02:
result = "BIND"
case 0x03:
result = "UDP ASSOCIATE"
default:
result = "Unknown Command"
}
return
}
func (s *Socks5Negotiation) String() string {
var buf bytes.Buffer
buf.WriteString("[Socks5 Negotiation]")
if s.NumOfMethods > 0 {
for i := uint8(0); i < s.NumOfMethods; i++ {
buf.WriteString(fmt.Sprintf(" [%v]", methodMeaning(s.Methods[i])))
}
}
return buf.String()
}
func (s *Socks5Request) String() string {
var buf bytes.Buffer
buf.WriteString("[Socks5 Request]")
buf.WriteString(fmt.Sprintf(" [Command:%s]", commandMeaning(s.Command)))
buf.WriteString(fmt.Sprintf(" [%s]", s.AddressWithPort))
return buf.String()
}
// NewTraffic payload stand for:
// rawaddr + payload
func NewTraffic(tp TrafficType, payload []byte) (r *Traffic) {
return &Traffic{
Type: tp,
PayloadLen: uint16(len(payload)),
Payload: payload,
}
}
func (r *Traffic) Bytes() []byte {
buf := new(bytes.Buffer)
buf.WriteByte(uint8(r.Type))
binary.Write(buf, binary.BigEndian, r.PayloadLen)
buf.Write(r.Payload)
return buf.Bytes()
}
// UnMarshalTraffic unmarshal TNTrequest via conn
func UnMarshalTraffic(conn io.Reader) (request *Traffic, err error) {
buf := make([]byte, requestBuf)
if _, err = io.ReadFull(conn, buf[:layoutType+lenType]); err != nil {
return
}
tp := uint8(buf[layoutType])
if tp > uint8(TrafficRequest) {
err = fmt.Errorf("Invalid request type: %v", tp)
return
}
if _, err = io.ReadFull(conn, buf[layoutPayloadLen:layoutPayloadLen+lenPayloadLen]); err != nil {
return
}
lenPayload := binary.BigEndian.Uint16(buf[layoutPayloadLen : layoutPayloadLen+lenPayloadLen])
if _, err = io.ReadFull(conn, buf[layoutPayload:layoutPayload+lenPayload]); err != nil {
return
}
payload := buf[layoutPayload : layoutPayload+lenPayload]
request = new(Traffic)
request.Type = TrafficType(tp)
request.PayloadLen = lenPayload
request.Payload = payload
return
}