forked from 456vv/vsocks5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
238 lines (209 loc) · 4.95 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
package vsocks5
import (
"errors"
"net"
"time"
)
type clientConn struct {
sconn net.Conn
conn net.Conn
istcp bool
remoteAddress net.Addr // udp used
}
func (c *clientConn) Read(b []byte) (n int, err error) {
if n, err = c.conn.Read(b); err != nil || c.istcp {
return
}
d, err := ReadRequestUDP(b[0:n])
if err != nil {
return 0, err
}
n = copy(b, d.Data)
return n, nil
}
func (c *clientConn) Write(b []byte) (n int, err error) {
if c.istcp {
return c.conn.Write(b)
}
a, h, p, err := ParseAddress(c.remoteAddress.String())
if err != nil {
return 0, err
}
d := NewRequestUDP(a, h, p, b)
b1 := d.Bytes()
if n, err = c.conn.Write(b1); err != nil {
return 0, err
}
if len(b1) != n {
return 0, errors.New("not write full")
}
return len(b), nil
}
func (c *clientConn) Close() error {
if !c.istcp {
// 使用udp代理后,需要关闭原有的tcp连接
c.sconn.Close()
}
return c.conn.Close()
}
func (c *clientConn) LocalAddr() net.Addr {
return c.conn.LocalAddr()
}
func (c *clientConn) RemoteAddr() net.Addr {
return c.remoteAddress
}
func (c *clientConn) SetDeadline(t time.Time) error {
return c.conn.SetDeadline(t)
}
func (c *clientConn) SetReadDeadline(t time.Time) error {
return c.conn.SetReadDeadline(t)
}
func (c *clientConn) SetWriteDeadline(t time.Time) error {
return c.conn.SetWriteDeadline(t)
}
type Client struct {
Server string
Username string
Password string
Key string
DialTCP func(network string, laddr, raddr *net.TCPAddr) (net.Conn, error)
}
func (c *Client) dial(laddr *net.TCPAddr) (conn net.Conn, err error) {
raddr, err := net.ResolveTCPAddr("tcp", c.Server)
if err != nil {
return nil, err
}
if c.DialTCP != nil {
return c.DialTCP("tcp", laddr, raddr)
}
return net.DialTCP("tcp", laddr, raddr)
}
func (c *Client) Dial(network, addr string) (net.Conn, error) {
return c.DialWithLocalAddr(network, "", addr)
}
func (c Client) DialWithLocalAddr(network, src, dst string) (net.Conn, error) {
var err error
var la *net.TCPAddr
if src != "" {
la, err = net.ResolveTCPAddr("tcp", src)
if err != nil {
return nil, err
}
}
conn, err := c.dial(la)
if err != nil {
return nil, err
}
// happy fix
if c.Key == "" || len(c.Key) != 16 {
return nil, errors.New("key is too short")
}
conn.Write([]byte(c.Key))
nt, ok := conn.(*Negotiate)
if !ok || !nt.done {
if err = negotiate(conn, c.Username, c.Password); err != nil {
return nil, err
}
}
raddr, err := net.ResolveTCPAddr("tcp", dst)
if err != nil {
return nil, err
}
switch network {
case "tcp":
c.requestTCP(conn, dst)
return &clientConn{conn: conn, istcp: true, remoteAddress: raddr}, nil
case "udp":
udpConn, err := c.requestUDP(conn, dst)
if err != nil {
conn.Close()
return nil, err
}
return &clientConn{sconn: conn, conn: udpConn, remoteAddress: raddr}, nil
}
return nil, errors.New("unsupport network")
}
func (c *Client) requestTCP(conn net.Conn, dst string) (*ReplyTCP, error) {
a, h, p, err := ParseAddress(dst)
if err != nil {
return nil, err
}
r := NewRequestTCP(CmdConnect, a, h, p)
return c.request(conn, r)
}
func (c *Client) requestUDP(conn net.Conn, dst string) (net.Conn, error) {
// 告诉代理服务器,我使用这个地址端口向代理服务器发起UDP请求
laddr := &net.UDPAddr{
IP: conn.LocalAddr().(*net.TCPAddr).IP,
Port: conn.LocalAddr().(*net.TCPAddr).Port,
Zone: conn.LocalAddr().(*net.TCPAddr).Zone,
}
a, h, p, err := ParseAddress(laddr.String())
if err != nil {
return nil, err
}
r := NewRequestTCP(CmdUDP, a, h, p)
rp, err := c.request(conn, r)
if err != nil {
return nil, err
}
// 服务给监听的端口地址
raddr, err := net.ResolveUDPAddr("udp", rp.Address())
if err != nil {
return nil, err
}
return net.DialUDP("udp", laddr, raddr)
}
func (c *Client) request(conn net.Conn, r *RequestTCP) (*ReplyTCP, error) {
if _, err := r.WriteTo(conn); err != nil {
return nil, err
}
rp, err := ReadReplyTCP(conn)
if err != nil {
return nil, err
}
if rp.Rep != RepSuccess {
return nil, errors.New("host unreachable")
}
return rp, nil
}
type Negotiate struct {
net.Conn
done bool
}
// 验证账号
func (T *Negotiate) Auth(username, password string) error {
T.done = true
return negotiate(T, username, password)
}
func negotiate(conn net.Conn, username, passwod string) error {
m := MethodNone
if username != "" {
m = MethodUsernamePassword
}
rq := NewNegotiateMethodRequest([]byte{m})
if _, err := rq.WriteTo(conn); err != nil {
return err
}
rp, err := ReadNegotiateMethodReply(conn)
if err != nil {
return err
}
if rp.Method != m {
return errors.New("unsupport method")
}
if m == MethodUsernamePassword {
urq := NewNegotiateAuthRequest([]byte(username), []byte(passwod))
if _, err := urq.WriteTo(conn); err != nil {
return err
}
urp, err := ReadNegotiateAuthReply(conn)
if err != nil {
return err
}
if urp.Status != UserPassStatusSuccess {
return ErrUserPassAuth
}
}
return nil
}