-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
243 lines (207 loc) · 5.75 KB
/
client.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
package gocks5
import (
"errors"
"net"
"time"
"github.com/yulon/go-netil"
)
func PassRawConn(rawCon net.Conn, username, password string) (*Conn, time.Duration, error) {
con := newConn(rawCon)
rtt, err := con.sendHandshake(username, password)
if err != nil {
con.Close()
return nil, rtt, err
}
return con, rtt, nil
}
func Pass(proxy, username, password string) (*Conn, time.Duration, error) {
tcpCon, err := net.Dial("tcp", proxy)
if err != nil {
return nil, -1, err
}
return PassRawConn(tcpCon, username, password)
}
var ErrorUnsupportedGSSAPI = errors.New("unsupported GSSAPI")
var ErrorUnsupportedMethods = errors.New("unsupported methods")
var ErrorUnknowMethod = errors.New("unknow method")
var ErrorBadCertificate = errors.New("bad certificate")
var ErrorInvalidCertificate = errors.New("invalid certificate")
var ErrorUnauthorized = errors.New("unauthorized")
func (con *Conn) sendHandshake(username, password string) (time.Duration, error) {
usernameLen := len(username)
passwordLen := len(password)
hasUsernamePassword := usernameLen > 0 || passwordLen > 0
/*
+----+----------+----------+
|VER | NMETHODS | METHODS |
+----+----------+----------+
| 1 | 1 | 1 to 255 |
+----+----------+----------+
*/
p := []byte{Ver, 1, MethodNone}
if hasUsernamePassword {
if usernameLen == 0 || usernameLen > 255 || passwordLen == 0 || passwordLen > 255 {
return -1, ErrorBadCertificate
}
p[1]++
p = append(p, MethodUsernamePassword)
}
now := time.Now()
err := netil.WriteAll(con, p)
if err != nil {
return -1, err
}
authSz := 3 + usernameLen + passwordLen
buf := make([]byte, authSz)
/*
+----+--------+
|VER | METHOD |
+----+--------+
| 1 | 1 |
+----+--------+
*/
err = netil.ReadFull(con, buf[:2])
if err != nil {
return -1, err
}
rtt := time.Now().Sub(now)
switch buf[1] {
case MethodNone:
return rtt, nil
case MethodGSSAPI:
return rtt, ErrorUnsupportedGSSAPI
case MethodUsernamePassword:
/*
+----+------+----------+------+----------+
|VER | ULEN | UNAME | PLEN | PASSWD |
+----+------+----------+------+----------+
| 1 | 1 | 1 to 255 | 1 | 1 to 255 |
+----+------+----------+------+----------+
*/
base := 0
buf[base] = UserPassVer
base++
buf[base] = byte(usernameLen)
base++
copy(buf[base:], []byte(username))
base += usernameLen
buf[base] = byte(passwordLen)
base++
copy(buf[base:], []byte(password))
err := netil.WriteAll(con, buf)
if err != nil {
return rtt, err
}
/*
+----+--------+
|VER | STATUS |
+----+--------+
| 1 | 1 |
+----+--------+
*/
err = netil.ReadFull(con, buf[:2])
if err != nil {
return rtt, err
}
if buf[1] != UserPassStatusSuccess {
return rtt, ErrorUnauthorized
}
return rtt, nil
case MethodUnsupportAll:
return rtt, ErrorUnsupportedMethods
}
return rtt, ErrorUnknowMethod
}
var ErrorServerFailure = errors.New("server failure")
var ErrorNotAllowed = errors.New("not allowed")
var ErrorNetworkUnreachable = errors.New("network unreachable")
var ErrorHostUnreachable = errors.New("host unreachable")
var ErrorConnectionRefused = errors.New("connection refused")
var ErrorTTLExpired = errors.New("TTL expired")
var ErrorCommandNotSupported = errors.New("command not supported")
var ErrorAddressNotSupported = errors.New("address not supported")
var ErrorUnknowReply = errors.New("unknow reply")
func (con *Conn) Command(cmd byte, addr net.Addr) (net.Addr, time.Duration, error) {
now := time.Now()
err := con.WriteAddr(cmd, addr)
if err != nil {
return nil, -1, err
}
rep, addr, err := con.ReadAddr()
if err != nil {
return nil, -1, err
}
rtt := time.Now().Sub(now)
if rep != RepSuccess {
switch rep {
case RepServerFailure:
return nil, rtt, ErrorServerFailure
case RepNotAllowed:
return nil, rtt, ErrorNotAllowed
case RepNetworkUnreachable:
return nil, rtt, ErrorNetworkUnreachable
case RepHostUnreachable:
return nil, rtt, ErrorHostUnreachable
case RepConnectionRefused:
return nil, rtt, ErrorConnectionRefused
case RepTTLExpired:
return nil, rtt, ErrorTTLExpired
case RepCommandNotSupported:
return nil, rtt, ErrorCommandNotSupported
case RepAddressNotSupported:
return nil, rtt, ErrorAddressNotSupported
}
return nil, rtt, ErrorUnknowReply
}
return addr, rtt, nil
}
func (con *Conn) DialTCP(addr net.Addr) (net.Conn, time.Duration, error) {
_, rtt, err := con.Command(CmdConnect, addr)
if err != nil {
return nil, rtt, err
}
return con.Conn, rtt, nil
}
func DialTCPPass(proxy, username, password string, addr net.Addr) (net.Conn, time.Duration, time.Duration, error) {
con, pxyRtt, err := Pass(proxy, username, password)
if err != nil {
return nil, pxyRtt, -1, err
}
tcpCon, dstRtt, err := con.DialTCP(addr)
if err != nil {
con.Close()
return nil, pxyRtt, dstRtt, err
}
return tcpCon, pxyRtt, dstRtt, err
}
func (con *Conn) ListenUDP() (net.PacketConn, time.Duration, error) {
udpPxyAddr, rtt, err := con.Command(CmdUDPAssociate, &net.UDPAddr{IP: net.IPv4zero, Port: 0})
if err != nil {
return nil, rtt, err
}
udpPxyPubAddr, err := netil.PublicAddr(udpPxyAddr, con.RemoteAddr())
if err != nil {
return nil, rtt, err
}
udpPxyPubUDPAddr, err := netil.ToUDPAddr(udpPxyPubAddr)
if err != nil {
return nil, rtt, err
}
udpPxyClt, err := net.ListenUDP("udp", nil)
if err != nil {
return nil, rtt, err
}
return &packetConn{udpPxyClt, udpPxyPubUDPAddr, con.Conn}, rtt, nil
}
func ListenUDPPass(proxy, username, password string) (net.PacketConn, time.Duration, time.Duration, error) {
con, pxyRtt, err := Pass(proxy, username, password)
if err != nil {
return nil, pxyRtt, -1, err
}
udpCon, dstRtt, err := con.ListenUDP()
if err != nil {
con.Close()
return nil, pxyRtt, dstRtt, err
}
return udpCon, pxyRtt, dstRtt, err
}