forked from TuanKiri/socks5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocks5_test.go
174 lines (163 loc) Β· 4.69 KB
/
socks5_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
package socks5_test
import (
"crypto/tls"
"io"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/net/proxy"
"github.com/TuanKiri/socks5"
)
func TestProxyConnect(t *testing.T) {
t.Parallel()
cases := map[string]struct {
proxyAddress string
proxyOpts []socks5.Option
destinationUrl string
clientCredentials *proxy.Auth
wait []byte
errString string
}{
"IPv4_address": {
proxyAddress: "127.0.0.1:1151",
proxyOpts: []socks5.Option{
socks5.WithLogger(socks5.NopLogger),
socks5.WithPort(1151),
},
destinationUrl: "http://127.0.0.1:5444/ping",
wait: []byte("pong!"),
},
"FQDN_address": {
proxyAddress: "127.0.0.1:1152",
proxyOpts: []socks5.Option{
socks5.WithLogger(socks5.NopLogger),
socks5.WithPort(1152),
},
destinationUrl: "http://localhost:5444/ping",
wait: []byte("pong!"),
},
"authenticate_by_username_password": {
proxyAddress: "127.0.0.1:1153",
proxyOpts: []socks5.Option{
socks5.WithLogger(socks5.NopLogger),
socks5.WithPort(1153),
socks5.WithPasswordAuthentication(),
},
destinationUrl: "http://localhost:5444/ping",
clientCredentials: &proxy.Auth{
User: "root",
Password: "password",
},
wait: []byte("pong!"),
},
"wrong_authenticate_by_username_password": {
proxyAddress: "127.0.0.1:1154",
proxyOpts: []socks5.Option{
socks5.WithLogger(socks5.NopLogger),
socks5.WithPort(1154),
socks5.WithPasswordAuthentication(),
socks5.WithStaticCredentials(map[string]string{
"root": "password123",
}),
},
destinationUrl: "http://localhost:5444",
clientCredentials: &proxy.Auth{
User: "root",
Password: "password",
},
errString: "Get \"http://localhost:5444\": socks connect " +
"tcp 127.0.0.1:1154->localhost:5444: username/password authentication failed",
},
"over_tls": {
proxyAddress: "127.0.0.1:1155",
proxyOpts: []socks5.Option{
socks5.WithLogger(socks5.NopLogger),
socks5.WithPort(1155),
socks5.WithDriver(&testTLSDriver{
tlsConfig: &tls.Config{
Certificates: []tls.Certificate{cert},
},
}),
},
destinationUrl: "https://localhost:6444/ping",
wait: []byte("pong!"),
},
"connection_refused": {
proxyAddress: "127.0.0.1:1156",
proxyOpts: []socks5.Option{
socks5.WithLogger(socks5.NopLogger),
socks5.WithPort(1156),
},
destinationUrl: "http://localhost:4000",
errString: "Get \"http://localhost:4000\": socks connect " +
"tcp 127.0.0.1:1156->localhost:4000: unknown error connection refused",
},
"IPv6_address": {
proxyAddress: "127.0.0.1:1157",
proxyOpts: []socks5.Option{
socks5.WithLogger(socks5.NopLogger),
socks5.WithPort(1157),
},
destinationUrl: "http://[::1]:5444/ping",
wait: []byte("pong!"),
},
"host_unreachable": {
proxyAddress: "127.0.0.1:1158",
proxyOpts: []socks5.Option{
socks5.WithLogger(socks5.NopLogger),
socks5.WithPort(1158),
},
destinationUrl: "http://no_such_host.test",
errString: "Get \"http://no_such_host.test\": socks connect " +
"tcp 127.0.0.1:1158->no_such_host.test:80: unknown error host unreachable",
},
"not_allowed_command": {
proxyAddress: "127.0.0.1:1159",
proxyOpts: []socks5.Option{
socks5.WithAllowCommands(),
socks5.WithLogger(socks5.NopLogger),
socks5.WithPort(1159),
},
destinationUrl: "http://localhost:4000",
errString: "Get \"http://localhost:4000\": socks connect " +
"tcp 127.0.0.1:1159->localhost:4000: unknown error connection not allowed by ruleset",
},
"not_allowed_host": {
proxyAddress: "127.0.0.1:1160",
proxyOpts: []socks5.Option{
socks5.WithLogger(socks5.NopLogger),
socks5.WithPort(1160),
socks5.WithDriver(&testTLSDriver{
tlsConfig: &tls.Config{
Certificates: []tls.Certificate{cert},
},
}),
socks5.WithBlockListHosts(
"www.google.com",
),
},
destinationUrl: "https://www.google.com",
errString: "Get \"https://www.google.com\": socks connect " +
"tcp 127.0.0.1:1160->www.google.com:443: unknown error connection not allowed by ruleset",
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
go runProxy(tc.proxyOpts)
// Wait for socks5 proxy to start
time.Sleep(100 * time.Millisecond)
client, err := setupClient(tc.proxyAddress, tc.clientCredentials)
assert.NoError(t, err)
resp, err := client.Get(tc.destinationUrl)
if err != nil {
require.EqualError(t, err, tc.errString)
return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
require.Equal(t, tc.wait, body)
})
}
}