-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathsocks5_proxy_test.go
298 lines (234 loc) · 9.87 KB
/
socks5_proxy_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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package proxy_test
import (
"bufio"
"errors"
"net"
"net/http"
"net/http/httptest"
"strings"
"time"
proxy "github.com/cloudfoundry/socks5-proxy"
"github.com/cloudfoundry/socks5-proxy/fakes"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"golang.org/x/crypto/ssh"
goproxy "golang.org/x/net/proxy"
)
var _ = Describe("Socks5Proxy", func() {
var (
socks5Proxy *proxy.Socks5Proxy
hostKey *fakes.HostKey
httpServerHostPort string
)
BeforeEach(func() {
httpServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusOK)
}))
httpServerHostPort = strings.Split(httpServer.URL, "http://")[1]
signer, err := ssh.ParsePrivateKey([]byte(privateKey))
Expect(err).NotTo(HaveOccurred())
hostKey = &fakes.HostKey{}
hostKey.GetCall.Returns.PublicKey = signer.PublicKey()
socks5Proxy = proxy.NewSocks5Proxy(hostKey, nil, time.Millisecond) //sock5 server defaults to a stdout logger for us
})
AfterEach(func() {
proxy.ResetNetListen()
})
Describe("Start", func() {
Context("when empty username is given", func() {
It("starts a proxy to the jumpbox", func() {
serverURL := proxy.StartTestSSHServer(httpServerHostPort, privateKey, "")
err := socks5Proxy.Start("", privateKey, serverURL)
Expect(err).NotTo(HaveOccurred())
socks5Addr, err := socks5Proxy.Addr()
Expect(err).NotTo(HaveOccurred())
socks5Client, err := goproxy.SOCKS5("tcp", socks5Addr, nil, goproxy.Direct)
Expect(err).NotTo(HaveOccurred())
Expect(hostKey.GetCall.CallCount).To(Equal(1))
Expect(hostKey.GetCall.Receives.Username).To(Equal("jumpbox"))
Expect(hostKey.GetCall.Receives.PrivateKey).To(Equal(privateKey))
Expect(hostKey.GetCall.Receives.ServerURL).To(Equal(serverURL))
conn, err := socks5Client.Dial("tcp", httpServerHostPort)
Expect(err).NotTo(HaveOccurred())
_, err = conn.Write([]byte("GET / HTTP/1.0\r\n\r\n"))
Expect(err).NotTo(HaveOccurred())
defer conn.Close()
status, err := bufio.NewReader(conn).ReadString('\n')
Expect(status).To(Equal("HTTP/1.0 200 OK\r\n"))
})
Context("when starting the proxy a second time", func() {
It("no-ops on the second run", func() {
serverURL := proxy.StartTestSSHServer(httpServerHostPort, privateKey, "")
err := socks5Proxy.Start("", privateKey, serverURL)
Expect(err).NotTo(HaveOccurred())
err = socks5Proxy.Start("", privateKey, serverURL)
Expect(err).NotTo(HaveOccurred())
socks5Addr, err := socks5Proxy.Addr()
Expect(err).NotTo(HaveOccurred())
socks5Client, err := goproxy.SOCKS5("tcp", socks5Addr, nil, goproxy.Direct)
Expect(err).NotTo(HaveOccurred())
conn, err := socks5Client.Dial("tcp", httpServerHostPort)
Expect(err).NotTo(HaveOccurred())
_, err = conn.Write([]byte("GET / HTTP/1.0\r\n\r\n"))
Expect(err).NotTo(HaveOccurred())
defer conn.Close()
status, err := bufio.NewReader(conn).ReadString('\n')
Expect(status).To(Equal("HTTP/1.0 200 OK\r\n"))
})
})
Context("when opening the port fails", func() {
It("returns an error", func() {
serverURL := proxy.StartTestSSHServer(httpServerHostPort, privateKey, "")
proxy.SetNetListen(func(string, string) (net.Listener, error) {
return nil, errors.New("coconut")
})
err := socks5Proxy.Start("", privateKey, serverURL)
Expect(err).To(MatchError("open port: coconut"))
})
})
})
Context("when a custom username is given", func() {
JustBeforeEach(func() {
signer, err := ssh.ParsePrivateKey([]byte(privateKey))
Expect(err).NotTo(HaveOccurred())
hostKey = &fakes.HostKey{}
hostKey.GetCall.Returns.PublicKey = signer.PublicKey()
socks5Proxy = proxy.NewSocks5Proxy(hostKey, nil, time.Millisecond) //sock5 server defaults to a stdout logger for us
})
It("returns a dialer that proxies to the jumpbox with a custom user", func() {
serverURL := proxy.StartTestSSHServer(httpServerHostPort, privateKey, "custom-username")
err := socks5Proxy.Start("custom-username", privateKey, serverURL)
Expect(err).NotTo(HaveOccurred())
socks5Addr, err := socks5Proxy.Addr()
Expect(err).NotTo(HaveOccurred())
socks5Client, err := goproxy.SOCKS5("tcp", socks5Addr, nil, goproxy.Direct)
Expect(err).NotTo(HaveOccurred())
Expect(hostKey.GetCall.CallCount).To(Equal(1))
Expect(hostKey.GetCall.Receives.Username).To(Equal("custom-username"))
Expect(hostKey.GetCall.Receives.PrivateKey).To(Equal(privateKey))
Expect(hostKey.GetCall.Receives.ServerURL).To(Equal(serverURL))
conn, err := socks5Client.Dial("tcp", httpServerHostPort)
Expect(err).NotTo(HaveOccurred())
_, err = conn.Write([]byte("GET / HTTP/1.0\r\n\r\n"))
Expect(err).NotTo(HaveOccurred())
defer conn.Close()
status, err := bufio.NewReader(conn).ReadString('\n')
Expect(status).To(Equal("HTTP/1.0 200 OK\r\n"))
})
})
})
Describe("Dialer", func() {
Context("when empty username is given", func() {
It("returns a dialer that proxies to the jumpbox with user 'jumpbox'", func() {
serverURL := proxy.StartTestSSHServer(httpServerHostPort, privateKey, "")
dialer, err := socks5Proxy.Dialer("", privateKey, serverURL)
Expect(err).NotTo(HaveOccurred())
Expect(hostKey.GetCall.CallCount).To(Equal(1))
Expect(hostKey.GetCall.Receives.Username).To(Equal("jumpbox"))
Expect(hostKey.GetCall.Receives.PrivateKey).To(Equal(privateKey))
Expect(hostKey.GetCall.Receives.ServerURL).To(Equal(serverURL))
conn, err := dialer("tcp", httpServerHostPort)
Expect(err).NotTo(HaveOccurred())
_, err = conn.Write([]byte("GET / HTTP/1.0\r\n\r\n"))
Expect(err).NotTo(HaveOccurred())
defer conn.Close()
status, err := bufio.NewReader(conn).ReadString('\n')
Expect(status).To(Equal("HTTP/1.0 200 OK\r\n"))
})
Context("failure cases", func() {
Context("when it cannot parse the private key", func() {
It("returns an error", func() {
serverURL := proxy.StartTestSSHServer(httpServerHostPort, privateKey, "")
_, err := socks5Proxy.Dialer("", "some-bad-private-key", serverURL)
Expect(err).To(MatchError("parse private key: ssh: no key found"))
})
})
Context("when it cannot get the host key", func() {
BeforeEach(func() {
hostKey.GetCall.Returns.Error = errors.New("banana")
})
It("returns an error", func() {
serverURL := proxy.StartTestSSHServer(httpServerHostPort, privateKey, "")
_, err := socks5Proxy.Dialer("", privateKey, serverURL)
Expect(err).To(MatchError("get host key: banana"))
})
})
Context("when it cannot dial the jumpbox url", func() {
It("returns an error", func() {
_, err := socks5Proxy.Dialer("", privateKey, "some-bad-url")
Expect(err).To(MatchError("ssh dial: dial tcp: address some-bad-url: missing port in address"))
})
})
})
})
Context("when a custom username is given", func() {
JustBeforeEach(func() {
signer, err := ssh.ParsePrivateKey([]byte(privateKey))
Expect(err).NotTo(HaveOccurred())
hostKey = &fakes.HostKey{}
hostKey.GetCall.Returns.PublicKey = signer.PublicKey()
socks5Proxy = proxy.NewSocks5Proxy(hostKey, nil, time.Millisecond) //sock5 server defaults to a stdout logger for us
})
It("returns a dialer that proxies to the jumpbox with a custom user", func() {
serverURL := proxy.StartTestSSHServer(httpServerHostPort, privateKey, "custom-username")
dialer, err := socks5Proxy.Dialer("custom-username", privateKey, serverURL)
Expect(err).NotTo(HaveOccurred())
Expect(hostKey.GetCall.CallCount).To(Equal(1))
Expect(hostKey.GetCall.Receives.Username).To(Equal("custom-username"))
Expect(hostKey.GetCall.Receives.PrivateKey).To(Equal(privateKey))
Expect(hostKey.GetCall.Receives.ServerURL).To(Equal(serverURL))
conn, err := dialer("tcp", httpServerHostPort)
Expect(err).NotTo(HaveOccurred())
_, err = conn.Write([]byte("GET / HTTP/1.0\r\n\r\n"))
Expect(err).NotTo(HaveOccurred())
defer conn.Close()
status, err := bufio.NewReader(conn).ReadString('\n')
Expect(status).To(Equal("HTTP/1.0 200 OK\r\n"))
})
})
})
Describe("Addr", func() {
Context("when the proxy has been started", func() {
BeforeEach(func() {
})
It("returns a valid address of the socks5 proxy", func() {
serverURL := proxy.StartTestSSHServer(httpServerHostPort, privateKey, "")
err := socks5Proxy.Start("", privateKey, serverURL)
Expect(err).NotTo(HaveOccurred())
addr, err := socks5Proxy.Addr()
Expect(err).NotTo(HaveOccurred())
Expect(addr).To(MatchRegexp("127.0.0.1:\\d+"))
})
})
Context("when no proxy has been started", func() {
It("returns an error", func() {
_, err := socks5Proxy.Addr()
Expect(err).To(MatchError("socks5 proxy is not running"))
})
})
})
Describe("SetListenPort", func() {
It("listens on the provided port", func() {
serverURL := proxy.StartTestSSHServer(httpServerHostPort, privateKey, "")
socks5Proxy.SetListenPort(1337)
err := socks5Proxy.Start("", privateKey, serverURL)
Expect(err).NotTo(HaveOccurred())
socks5Addr, err := socks5Proxy.Addr()
Expect(err).NotTo(HaveOccurred())
Expect(strings.Contains(socks5Addr, ":1337")).To(Equal(true))
socks5Client, err := goproxy.SOCKS5("tcp", socks5Addr, nil, goproxy.Direct)
Expect(err).NotTo(HaveOccurred())
Expect(hostKey.GetCall.CallCount).To(Equal(1))
Expect(hostKey.GetCall.Receives.Username).To(Equal("jumpbox"))
Expect(hostKey.GetCall.Receives.PrivateKey).To(Equal(privateKey))
Expect(hostKey.GetCall.Receives.ServerURL).To(Equal(serverURL))
conn, err := socks5Client.Dial("tcp", httpServerHostPort)
Expect(err).NotTo(HaveOccurred())
_, err = conn.Write([]byte("GET / HTTP/1.0\r\n\r\n"))
Expect(err).NotTo(HaveOccurred())
defer conn.Close()
status, err := bufio.NewReader(conn).ReadString('\n')
Expect(status).To(Equal("HTTP/1.0 200 OK\r\n"))
})
})
})