forked from txthinking/socks5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_side.go
286 lines (272 loc) · 6.61 KB
/
server_side.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
package socks5
import (
"errors"
"io"
"log"
"net"
)
var (
// ErrVersion is version error
ErrVersion = errors.New("Invalid Version")
// ErrUserPassVersion is username/password auth version error
ErrUserPassVersion = errors.New("Invalid Version of Username Password Auth")
// ErrBadRequest is bad request error
ErrBadRequest = errors.New("Bad Request")
)
// NewNegotiationRequestFrom read negotiation requst packet from client
func NewNegotiationRequestFrom(r *net.TCPConn) (*NegotiationRequest, error) {
// memory strict
bb := make([]byte, 2)
if _, err := io.ReadFull(r, bb); err != nil {
return nil, err
}
if bb[0] != Ver {
return nil, ErrVersion
}
if bb[1] == 0 {
return nil, ErrBadRequest
}
ms := make([]byte, int(bb[1]))
if _, err := io.ReadFull(r, ms); err != nil {
return nil, err
}
if Debug {
log.Printf("Got NegotiationRequest: %#v %#v %#v\n", bb[0], bb[1], ms)
}
return &NegotiationRequest{
Ver: bb[0],
NMethods: bb[1],
Methods: ms,
}, nil
}
// NewNegotiationReply return negotiation reply packet can be writed into client
func NewNegotiationReply(method byte) *NegotiationReply {
return &NegotiationReply{
Ver: Ver,
Method: method,
}
}
// WriteTo write negotiation reply packet into client
func (r *NegotiationReply) WriteTo(w *net.TCPConn) error {
if _, err := w.Write([]byte{r.Ver, r.Method}); err != nil {
return err
}
if Debug {
log.Printf("Sent NegotiationReply: %#v %#v\n", r.Ver, r.Method)
}
return nil
}
// NewUserPassNegotiationRequestFrom read user password negotiation request packet from client
func NewUserPassNegotiationRequestFrom(r *net.TCPConn) (*UserPassNegotiationRequest, error) {
bb := make([]byte, 2)
if _, err := io.ReadFull(r, bb); err != nil {
return nil, err
}
if bb[0] != UserPassVer {
return nil, ErrUserPassVersion
}
if bb[1] == 0 {
return nil, ErrBadRequest
}
ub := make([]byte, int(bb[1])+1)
if _, err := io.ReadFull(r, ub); err != nil {
return nil, err
}
if ub[int(bb[1])] == 0 {
return nil, ErrBadRequest
}
p := make([]byte, int(ub[int(bb[1])]))
if _, err := io.ReadFull(r, p); err != nil {
return nil, err
}
if Debug {
log.Printf("Got UserPassNegotiationRequest: %#v %#v %#v %#v %#v\n", bb[0], bb[1], ub[:int(bb[1])], ub[int(bb[1])], p)
}
return &UserPassNegotiationRequest{
Ver: bb[0],
Ulen: bb[1],
Uname: ub[:int(bb[1])],
Plen: ub[int(bb[1])],
Passwd: p,
}, nil
}
// NewUserPassNegotiationReply return negotiation username password reply packet can be writed into client
func NewUserPassNegotiationReply(status byte) *UserPassNegotiationReply {
return &UserPassNegotiationReply{
Ver: UserPassVer,
Status: status,
}
}
// WriteTo write negotiation username password reply packet into client
func (r *UserPassNegotiationReply) WriteTo(w *net.TCPConn) error {
if _, err := w.Write([]byte{r.Ver, r.Status}); err != nil {
return err
}
if Debug {
log.Printf("Sent UserPassNegotiationReply: %#v %#v \n", r.Ver, r.Status)
}
return nil
}
// NewRequestFrom read requst packet from client
func NewRequestFrom(r *net.TCPConn) (*Request, error) {
bb := make([]byte, 4)
if _, err := io.ReadFull(r, bb); err != nil {
return nil, err
}
if bb[0] != Ver {
return nil, ErrVersion
}
var addr []byte
if bb[3] == ATYPIPv4 {
addr = make([]byte, 4)
if _, err := io.ReadFull(r, addr); err != nil {
return nil, err
}
} else if bb[3] == ATYPIPv6 {
addr = make([]byte, 16)
if _, err := io.ReadFull(r, addr); err != nil {
return nil, err
}
} else if bb[3] == ATYPDomain {
dal := make([]byte, 1)
if _, err := io.ReadFull(r, dal); err != nil {
return nil, err
}
if dal[0] == 0 {
return nil, ErrBadRequest
}
addr = make([]byte, int(dal[0]))
if _, err := io.ReadFull(r, addr); err != nil {
return nil, err
}
addr = append(dal, addr...)
} else {
return nil, ErrBadRequest
}
port := make([]byte, 2)
if _, err := io.ReadFull(r, port); err != nil {
return nil, err
}
if Debug {
log.Printf("Got Request: %#v %#v %#v %#v %#v %#v\n", bb[0], bb[1], bb[2], bb[3], addr, port)
}
return &Request{
Ver: bb[0],
Cmd: bb[1],
Rsv: bb[2],
Atyp: bb[3],
DstAddr: addr,
DstPort: port,
}, nil
}
// NewReply return reply packet can be writed into client
func NewReply(rep byte, atyp byte, bndaddr []byte, bndport []byte) *Reply {
if atyp == ATYPDomain {
bndaddr = append([]byte{byte(len(bndaddr))}, bndaddr...)
}
return &Reply{
Ver: Ver,
Rep: rep,
Rsv: 0x00,
Atyp: atyp,
BndAddr: bndaddr,
BndPort: bndport,
}
}
// WriteTo write reply packet into client
func (r *Reply) WriteTo(w *net.TCPConn) error {
if _, err := w.Write([]byte{r.Ver, r.Rep, r.Rsv, r.Atyp}); err != nil {
return err
}
if _, err := w.Write(r.BndAddr); err != nil {
return err
}
if _, err := w.Write(r.BndPort); err != nil {
return err
}
if Debug {
log.Printf("Sent Reply: %#v %#v %#v %#v %#v %#v\n", r.Ver, r.Rep, r.Rsv, r.Atyp, r.BndAddr, r.BndPort)
}
return nil
}
func NewDatagramFromBytes(bb []byte) (*Datagram, error) {
n := len(bb)
minl := 4
if n < minl {
return nil, ErrBadRequest
}
var addr []byte
if bb[3] == ATYPIPv4 {
minl += 4
if n < minl {
return nil, ErrBadRequest
}
addr = bb[minl-4 : minl]
} else if bb[3] == ATYPIPv6 {
minl += 16
if n < minl {
return nil, ErrBadRequest
}
addr = bb[minl-16 : minl]
} else if bb[3] == ATYPDomain {
minl += 1
if n < minl {
return nil, ErrBadRequest
}
l := bb[4]
if l == 0 {
return nil, ErrBadRequest
}
minl += int(l)
if n < minl {
return nil, ErrBadRequest
}
addr = bb[minl-int(l) : minl]
addr = append([]byte{l}, addr...)
} else {
return nil, ErrBadRequest
}
minl += 2
if n <= minl {
return nil, ErrBadRequest
}
port := bb[minl-2 : minl]
data := bb[minl:]
d := &Datagram{
Rsv: bb[0:2],
Frag: bb[2],
Atyp: bb[3],
DstAddr: addr,
DstPort: port,
Data: data,
}
if Debug {
log.Printf("Got Datagram. data: %#v %#v %#v %#v %#v %#v datagram address: %#v\n", d.Rsv, d.Frag, d.Atyp, d.DstAddr, d.DstPort, d.Data, d.Address())
}
return d, nil
}
// NewDatagram return datagram packet can be writed into client
func NewDatagram(atyp byte, dstaddr []byte, dstport []byte, data []byte) *Datagram {
if atyp == ATYPDomain {
dstaddr = append([]byte{byte(len(dstaddr))}, dstaddr...)
}
return &Datagram{
Rsv: []byte{0x00, 0x00},
Frag: 0x00,
Atyp: atyp,
DstAddr: dstaddr,
DstPort: dstport,
Data: data,
}
}
// Bytes return []byte
func (d *Datagram) Bytes() []byte {
b := make([]byte, 0)
b = append(b, d.Rsv...)
b = append(b, d.Frag)
b = append(b, d.Atyp)
b = append(b, d.DstAddr...)
b = append(b, d.DstPort...)
b = append(b, d.Data...)
return b
}