-
Notifications
You must be signed in to change notification settings - Fork 16
/
socks5.go
executable file
·268 lines (206 loc) · 4.67 KB
/
socks5.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
package socks
import (
"errors"
"fmt"
"io"
//"log"
"net"
//"strconv"
"encoding/binary"
)
/*
socks5 protocol
initial
byte | 0 | 1 | 2 | ...... | n |
|0x05|num auth| auth methods |
reply
byte | 0 | 1 |
|0x05| auth|
username/password auth request
byte | 0 | 1 | | 1 byte | |
|0x01|username_len| username | password_len | password |
username/password auth reponse
byte | 0 | 1 |
|0x01|status|
request
byte | 0 | 1 | 2 | 3 | 4 | .. | n-2 | n-1| n |
|0x05|cmd|0x00|addrtype| addr | port |
response
byte |0 | 1 | 2 | 3 | 4 | .. | n-2 | n-1 | n |
|0x05|status|0x00|addrtype| addr | port |
*/
// Socks5AuthRequired means socks5 server need auth or not
type socks5Conn struct {
//addr string
clientConn net.Conn
serverConn net.Conn
dial DialFunc
auth AuthService
}
func (s5 *socks5Conn) Serve(b []byte, n int) (err error) {
defer s5.Close()
if err = s5.handshake(b, n); err != nil {
//log.Println(err)
return
}
if err = s5.processRequest(); err != nil {
//log.Println(err)
return
}
return
}
func (s5 *socks5Conn) handshake(buf []byte, n int) (err error) {
// read auth methods
if n < 2 {
n1, err := io.ReadAtLeast(s5.clientConn, buf[1:], 1)
if err != nil {
return err
}
n += n1
}
l := int(buf[1])
if n != (l + 2) {
// read remains data
n1, err := io.ReadFull(s5.clientConn, buf[n:l+2+1])
if err != nil {
return err
}
n += n1
}
if s5.auth == nil {
// no auth required
s5.clientConn.Write([]byte{0x05, 0x00})
return nil
}
hasPassAuth := false
var passAuth byte = 0x02
// check auth method
// only password(0x02) supported
for i := 2; i < n; i++ {
if buf[i] == passAuth {
hasPassAuth = true
break
}
}
if !hasPassAuth {
s5.clientConn.Write([]byte{0x05, 0xff})
return errors.New("no supported auth method")
}
err = s5.passwordAuth()
return err
}
func (s5 *socks5Conn) passwordAuth() error {
buf := make([]byte, 32)
// username/password required
s5.clientConn.Write([]byte{0x05, 0x02})
n, err := io.ReadAtLeast(s5.clientConn, buf, 2)
if err != nil {
return err
}
//log.Printf("%+v", buf[:n])
// check auth version
if buf[0] != 0x01 {
return errors.New("unsupported auth version")
}
usernameLen := int(buf[1])
p0 := 2
p1 := p0 + usernameLen
for n < p1 {
n1, err := s5.clientConn.Read(buf[n:])
if err != nil {
return err
}
n += n1
}
username := buf[p0:p1]
passwordLen := int(buf[p1])
p3 := p1 + 1
p4 := p3 + passwordLen
for n < p4 {
n1, err := s5.clientConn.Read(buf[n:])
if err != nil {
return err
}
n += n1
}
password := buf[p3:p4]
// log.Printf("get username: %s, password: %s", username, password)
if s5.auth != nil {
ret := s5.auth.Authenticate(
string(username), string(password),
s5.clientConn.RemoteAddr())
if ret {
s5.clientConn.Write([]byte{0x01, 0x00})
return nil
}
s5.clientConn.Write([]byte{0x01, 0x01})
return errors.New("access denied")
}
return errors.New("no auth method")
}
func (s5 *socks5Conn) processRequest() error {
buf := make([]byte, 258)
// read header
n, err := io.ReadAtLeast(s5.clientConn, buf, 10)
if err != nil {
return err
}
if buf[0] != socks5Version {
return fmt.Errorf("error version %d", buf[0])
}
// command only support connect
if buf[1] != cmdConnect {
return fmt.Errorf("unsupported command %d", buf[1])
}
hlen := 0 // target address length
host := "" // target address
msglen := 0 // header length
switch buf[3] {
case addrTypeIPv4:
hlen = 4
case addrTypeDomain:
hlen = int(buf[4]) + 1
case addrTypeIPv6:
hlen = 16
}
msglen = 6 + hlen
if n < msglen {
// read remains header
_, err := io.ReadFull(s5.clientConn, buf[n:msglen])
if err != nil {
return err
}
}
// get target address
addr := buf[4 : 4+hlen]
if buf[3] == addrTypeDomain {
host = string(addr[1:])
} else {
host = net.IP(addr).String()
}
// get target port
port := binary.BigEndian.Uint16(buf[msglen-2 : msglen])
// target address
target := net.JoinHostPort(host, fmt.Sprintf("%d", port))
//log.Printf("connecing to %s\r\n", target)
// connect to the target
s5.serverConn, err = s5.dial("tcp", target)
if err != nil {
// connection failed
s5.clientConn.Write([]byte{0x05, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01})
return err
}
// connection success
s5.clientConn.Write([]byte{0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01})
// enter data exchange
forward(s5.clientConn, s5.serverConn)
return nil
}
func (s5 *socks5Conn) Close() {
if s5.serverConn != nil {
s5.serverConn.Close()
}
if s5.clientConn != nil {
s5.clientConn.Close()
}
}