forked from txthinking/socks5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
254 lines (238 loc) · 5.4 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
244
245
246
247
248
249
250
251
252
253
254
package socks5
import (
"errors"
"net"
"time"
)
// Client is socks5 client wrapper
type Client struct {
Server string
UserName string
Password string
// On cmd UDP, let server control the tcp and udp connection relationship
TCPConn net.Conn
UDPConn net.Conn
RemoteAddress net.Addr
TCPTimeout int
UDPTimeout int
Dst string
}
// This is just create a client, you need to use Dial to create conn
func NewClient(addr, username, password string, tcpTimeout, udpTimeout int) (*Client, error) {
c := &Client{
Server: addr,
UserName: username,
Password: password,
TCPTimeout: tcpTimeout,
UDPTimeout: udpTimeout,
}
return c, nil
}
func (c *Client) Dial(network, addr string) (net.Conn, error) {
return c.DialWithLocalAddr(network, "", addr, nil)
}
// If you want to send address that expects to use to send UDP, just assign it to src, otherwise it will send zero address.
// Recommend specifying the src address in a non-NAT environment, and leave it blank in other cases.
func (c *Client) DialWithLocalAddr(network, src, dst string, remoteAddr net.Addr) (net.Conn, error) {
c = &Client{
Server: c.Server,
UserName: c.UserName,
Password: c.Password,
TCPTimeout: c.TCPTimeout,
UDPTimeout: c.UDPTimeout,
Dst: dst,
RemoteAddress: remoteAddr,
}
var err error
if network == "tcp" {
var laddr net.Addr
if src != "" {
laddr, err = net.ResolveTCPAddr("tcp", src)
if err != nil {
return nil, err
}
}
if err := c.Negotiate(laddr); err != nil {
return nil, err
}
a, h, p, err := ParseAddress(dst)
if err != nil {
return nil, err
}
if a == ATYPDomain {
h = h[1:]
}
if _, err := c.Request(NewRequest(CmdConnect, a, h, p)); err != nil {
return nil, err
}
return c, nil
}
if network == "udp" {
var laddr net.Addr
if src != "" {
laddr, err = net.ResolveTCPAddr("tcp", src)
if err != nil {
return nil, err
}
}
if err := c.Negotiate(laddr); err != nil {
return nil, err
}
a, h, p := ATYPIPv4, net.IPv4zero, []byte{0x00, 0x00}
if src != "" {
a, h, p, err = ParseAddress(src)
if err != nil {
return nil, err
}
if a == ATYPDomain {
h = h[1:]
}
}
rp, err := c.Request(NewRequest(CmdUDP, a, h, p))
if err != nil {
return nil, err
}
c.UDPConn, err = DialUDP("udp", src, rp.Address())
if err != nil {
return nil, err
}
if c.UDPTimeout != 0 {
if err := c.UDPConn.SetDeadline(time.Now().Add(time.Duration(c.UDPTimeout) * time.Second)); err != nil {
return nil, err
}
}
return c, nil
}
return nil, errors.New("unsupport network")
}
func (c *Client) Read(b []byte) (int, error) {
if c.UDPConn == nil {
return c.TCPConn.Read(b)
}
n, err := c.UDPConn.Read(b)
if err != nil {
return 0, err
}
d, err := NewDatagramFromBytes(b[0:n])
if err != nil {
return 0, err
}
n = copy(b, d.Data)
return n, nil
}
func (c *Client) Write(b []byte) (int, error) {
if c.UDPConn == nil {
return c.TCPConn.Write(b)
}
a, h, p, err := ParseAddress(c.Dst)
if err != nil {
return 0, err
}
if a == ATYPDomain {
h = h[1:]
}
d := NewDatagram(a, h, p, b)
b1 := d.Bytes()
n, err := c.UDPConn.Write(b1)
if err != nil {
return 0, err
}
if len(b1) != n {
return 0, errors.New("not write full")
}
return len(b), nil
}
func (c *Client) Close() error {
if c.UDPConn == nil {
return c.TCPConn.Close()
}
if c.TCPConn != nil {
c.TCPConn.Close()
}
return c.UDPConn.Close()
}
func (c *Client) LocalAddr() net.Addr {
if c.UDPConn == nil {
return c.TCPConn.LocalAddr()
}
return c.UDPConn.LocalAddr()
}
func (c *Client) RemoteAddr() net.Addr {
return c.RemoteAddress
}
func (c *Client) SetDeadline(t time.Time) error {
if c.UDPConn == nil {
return c.TCPConn.SetDeadline(t)
}
return c.UDPConn.SetDeadline(t)
}
func (c *Client) SetReadDeadline(t time.Time) error {
if c.UDPConn == nil {
return c.TCPConn.SetReadDeadline(t)
}
return c.UDPConn.SetReadDeadline(t)
}
func (c *Client) SetWriteDeadline(t time.Time) error {
if c.UDPConn == nil {
return c.TCPConn.SetWriteDeadline(t)
}
return c.UDPConn.SetWriteDeadline(t)
}
func (c *Client) Negotiate(laddr net.Addr) error {
src := ""
if laddr != nil {
src = laddr.String()
}
var err error
c.TCPConn, err = DialTCP("tcp", src, c.Server)
if err != nil {
return err
}
if c.TCPTimeout != 0 {
if err := c.TCPConn.SetDeadline(time.Now().Add(time.Duration(c.TCPTimeout) * time.Second)); err != nil {
return err
}
}
m := MethodNone
if c.UserName != "" && c.Password != "" {
m = MethodUsernamePassword
}
rq := NewNegotiationRequest([]byte{m})
if _, err := rq.WriteTo(c.TCPConn); err != nil {
return err
}
rp, err := NewNegotiationReplyFrom(c.TCPConn)
if err != nil {
return err
}
if rp.Method != m {
return errors.New("Unsupport method")
}
if m == MethodUsernamePassword {
urq := NewUserPassNegotiationRequest([]byte(c.UserName), []byte(c.Password))
if _, err := urq.WriteTo(c.TCPConn); err != nil {
return err
}
urp, err := NewUserPassNegotiationReplyFrom(c.TCPConn)
if err != nil {
return err
}
if urp.Status != UserPassStatusSuccess {
return ErrUserPassAuth
}
}
return nil
}
func (c *Client) Request(r *Request) (*Reply, error) {
if _, err := r.WriteTo(c.TCPConn); err != nil {
return nil, err
}
rp, err := NewReplyFrom(c.TCPConn)
if err != nil {
return nil, err
}
if rp.Rep != RepSuccess {
return nil, errors.New("Host unreachable")
}
return rp, nil
}