forked from haochen233/socks5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
200 lines (169 loc) · 3.83 KB
/
client_test.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
package socks5
import (
"encoding/binary"
"log"
"net"
"net/http"
"strconv"
"testing"
)
func runLocalServer(addr string, bindIP string) {
// create socks server.
srv := &Server{
// socks server listen address.
Addr: addr,
// UDP assocaite and bind command listen ip.
// Don't need port, the port will automatically chosen.
BindIP: bindIP,
// if nil server will provide no authentication required method.
Authenticators: nil,
}
// start listen
err := srv.ListenAndServe()
if err != nil {
log.Fatal(err)
}
}
func TestClient_UDPForward(t *testing.T) {
go runLocalServer("127.0.0.1:1080", "127.0.0.1")
c := Client{
ProxyAddr: "127.0.0.1:1080",
Timeout: 0,
Auth: map[METHOD]Authenticator{
USERNAME_PASSWORD: &UserPasswd{Username: "admin", Password: "123456"},
NO_AUTHENTICATION_REQUIRED: NoAuth{},
},
}
local := "127.0.0.1:19999"
_, err := c.UDPForward(local)
if err != nil {
log.Println(err)
return
}
}
func TestClient_Connect(t *testing.T) {
go runLocalServer("127.0.0.1:1080", "127.0.0.1")
c := Client{
ProxyAddr: "127.0.0.1:1080",
Auth: map[METHOD]Authenticator{
USERNAME_PASSWORD: &UserPasswd{
Username: "admin",
Password: "123456",
},
NO_AUTHENTICATION_REQUIRED: &NoAuth{},
},
}
// socks4 connect
dest := "www.baidu.com:80"
conn, err := c.Connect(Version4, dest)
if err != nil {
t.Fatal(err)
return
}
// send http Get request via socks proxy.
req, err := http.NewRequest("GET", "http://www.baidu.com", nil)
if err != nil {
t.Fatal(err)
return
}
err = req.WriteProxy(conn)
if err != nil {
t.Fatal(err)
}
// socks5 connect
conn5, err := c.Connect(Version5, dest)
if err != nil {
t.Fatal(err)
}
err = req.WriteProxy(conn5)
if err != nil {
t.Fatal(err)
}
}
func TestClient_Bind(t *testing.T) {
socksServerAddr := "127.0.0.1:1080"
socksServerBindIP := "127.0.0.1"
go runLocalServer(socksServerAddr, socksServerBindIP)
destServerAddr := "127.0.0.1:9000"
destServerLaddr := "127.0.0.1:9001"
go runDestServer(destServerAddr, destServerLaddr)
c := Client{
ProxyAddr: socksServerAddr,
Auth: map[METHOD]Authenticator{
USERNAME_PASSWORD: &UserPasswd{
Username: "admin",
Password: "123456",
},
NO_AUTHENTICATION_REQUIRED: &NoAuth{},
},
}
// connect
conn1, err := c.Connect(5, destServerAddr)
if err != nil {
t.Fatal(err)
}
// bind
bindAddr, errors, conn, err := c.Bind(4, destServerLaddr)
if err != nil {
t.Fatal(err)
}
port := make([]byte, 2)
binary.BigEndian.PutUint16(port, bindAddr.Port)
conn1.Write(append(bindAddr.Addr, port...))
err = <-errors
if err != nil {
t.Fatal(err)
}
// success
_, err = conn.Write([]byte("hello"))
if err != nil {
t.Fatal(err)
}
buf := make([]byte, 2)
_, err = conn.Read(buf)
if err != nil {
t.Fatal(err)
}
if string(buf) != "hi" {
t.Errorf("want hi but get %s", string(buf))
}
}
// bindAddr is tcp listen address
// laddr is which address connect to socks bind server.
func runDestServer(bindAddr, laddr string) {
ListenAddr, _ := net.ResolveTCPAddr("tcp", bindAddr)
ln, err := net.ListenTCP("tcp", ListenAddr)
if err != nil {
log.Fatal(err)
}
localAddr, _ := net.ResolveTCPAddr("tcp", laddr)
conn, err := ln.AcceptTCP()
if err != nil {
log.Fatal(err)
}
addr := make([]byte, 6)
_, err = conn.Read(addr)
if err != nil {
log.Fatal(err)
}
ip := addr[:4]
portBytes := addr[4:]
IP := net.IPv4(ip[0], ip[1], ip[2], ip[3])
port := binary.BigEndian.Uint16(portBytes)
portStr := strconv.Itoa(int(port))
address := net.JoinHostPort(IP.To4().String(), portStr)
raddr, _ := net.ResolveTCPAddr("tcp", address)
tcpConn, err := net.DialTCP("tcp", localAddr, raddr)
if err != nil {
log.Fatal(err)
}
buf := make([]byte, 1024)
_, err = tcpConn.Read(buf)
if err != nil {
log.Fatal()
}
_, err = tcpConn.Write([]byte("hi"))
if err != nil {
log.Fatal(err)
}
}