-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.spec.ts
272 lines (227 loc) · 7.24 KB
/
index.spec.ts
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
import { after, afterEach, beforeEach, it } from "node:test";
import * as assert from "assert";
import * as net from "node:net";
import { once } from "node:events";
import { WebSocketServer } from "ws";
import { createProxyServer, waitForConnect } from "@e9x/simple-socks";
import { getLocal, Mockttp, MockttpOptions } from "mockttp";
import { Agent, Dispatcher, fetch, MessageEvent, WebSocket } from "undici";
import { socksConnector, socksDispatcher } from "./index.js";
const kGlobalDispatcher = Symbol.for("undici.globalDispatcher.1");
declare const global: typeof globalThis & {
[kGlobalDispatcher]?: Dispatcher;
};
function setupHttpServer(options?: MockttpOptions) {
const server = getLocal(options);
beforeEach(() => server.start());
afterEach(() => server.stop());
return server;
}
type AuthFn = NonNullable<Parameters<typeof createProxyServer>[0]>["authenticate"];
function setupSocksServer(authenticate?: AuthFn) {
let inbound: net.Socket;
let outbound: net.Socket;
const server = createProxyServer({
authenticate,
async connect(port: number, host: string) {
outbound = net.connect(port, host);
await waitForConnect(outbound);
return outbound;
},
filter(port: number, host: string, socket: net.Socket) {
inbound = socket;
return true;
},
});
server.listen();
after(() => void server.close());
return {
get inbound() { return inbound; },
get outbound() { return outbound; },
...server.address() as net.AddressInfo,
};
}
function setupWSServer() {
const server = new WebSocketServer({ port: 0 });
let inbound: net.Socket;
server.on("connection", (ws, request) => {
inbound = request.socket;
ws.on("message", (m, isBinary) => {
ws.send(m, { binary: isBinary });
});
});
after(() => void server.close());
return {
get inbound() { return inbound; },
...server.address() as net.AddressInfo,
};
}
const httpServer = setupHttpServer();
const wsServer = setupWSServer();
const secureServer = setupHttpServer({
https: {
keyPath: "fixtures/localhost.pvk",
certPath: "fixtures/localhost.pem",
},
});
const plainProxy = setupSocksServer();
const secureProxy = setupSocksServer((username, password) => {
return username === "foo" && password === "bar";
});
async function verifyFetchFailed(server: Mockttp | string, dispatcher: Dispatcher, cause?: RegExp) {
if (typeof server !== "string") {
await server.forGet("/foobar").thenReply(200, "__RESPONSE_DATA__");
server = server.urlFor("/foobar");
}
const promise = fetch(server, { dispatcher });
await assert.rejects(promise, new TypeError("fetch failed"));
if (cause) {
const actualCause = await promise.catch((e: Error) => e.cause);
assert.match((actualCause as Error).message, cause);
}
}
async function verifyFetchSuccess(server: Mockttp, dispatcher: Dispatcher) {
const mockedEndpoint = await server
.forGet("/foobar")
.thenReply(200, "__RESPONSE_DATA__");
const r = await fetch(server.urlFor("/foobar"), { dispatcher });
assert.strictEqual(await r.text(), "__RESPONSE_DATA__");
return (await mockedEndpoint.getSeenRequests()).at(-1)!;
}
it("should throw error if proxy connect timeout", async () => {
const blackHole = net.createServer();
blackHole.listen();
try {
const addr = blackHole.address() as net.AddressInfo;
const dispatcher = socksDispatcher({
type: 5,
host: addr.address,
port: addr.port,
}, {
connect: { timeout: 500 },
});
await verifyFetchFailed(httpServer, dispatcher, /Proxy connection timed out/);
} finally {
blackHole.close();
}
});
it("should throw error if the argument is invalid", async () => {
// @ts-expect-error
const dispatcher = socksDispatcher([null]);
return verifyFetchFailed(httpServer, dispatcher, /Invalid SOCKS proxy details were provided/);
});
it("should throw error if the socks server is unreachable", () => {
const dispatcher = socksDispatcher({
type: 5,
host: "::1",
port: 111,
});
return verifyFetchFailed(httpServer, dispatcher, /connect ECONNREFUSED ::1:111/);
});
it("should throw error if authenticate failed", async () => {
const dispatcher = socksDispatcher({
type: 5,
host: secureProxy.address,
port: secureProxy.port,
userId: "foo",
password: "_INVALID_",
});
// The socks package missing handing of auth failed.
return verifyFetchFailed(httpServer, dispatcher /* , message */);
});
it("should throw error if the target is unreachable", async () => {
const dispatcher = socksDispatcher({
type: 5,
host: plainProxy.address,
port: plainProxy.port,
});
return verifyFetchFailed("http://[::1]:8964", dispatcher, /Socks5 proxy rejected connection/);
});
it("should connect directly if no proxies are provided", async () => {
await verifyFetchSuccess(httpServer, socksDispatcher([]));
});
it("should connect target through socks", async () => {
const dispatcher = socksDispatcher({
type: 5,
host: plainProxy.address,
port: plainProxy.port,
});
const inbound = await verifyFetchSuccess(httpServer, dispatcher);
assert.strictEqual(inbound.remotePort, plainProxy.outbound.localPort);
});
it("should support proxy chain", async () => {
const dispatcher = socksDispatcher([{
type: 5,
host: secureProxy.address,
port: secureProxy.port,
userId: "foo",
password: "bar",
}, {
type: 5,
host: plainProxy.address,
port: plainProxy.port,
}]);
const inbound = await verifyFetchSuccess(httpServer, dispatcher);
assert.strictEqual(inbound.remotePort, plainProxy.outbound.localPort);
assert.strictEqual(plainProxy.inbound.remotePort, secureProxy.outbound.localPort);
});
it("should support TLS over socks", async () => {
const dispatcher = socksDispatcher({
type: 5,
host: plainProxy.address,
port: plainProxy.port,
}, {
connect: {
rejectUnauthorized: false,
},
});
const inbound = await verifyFetchSuccess(secureServer, dispatcher);
assert.strictEqual(inbound.remotePort, plainProxy.outbound.localPort);
});
it("should do handshake on existing socket", async () => {
const socksConnect = socksConnector({
type: 5,
host: plainProxy.address,
port: plainProxy.port,
});
const dispatcher = new Agent({
connect(options, callback) {
const socket = net.connect(plainProxy.port, plainProxy.address);
socksConnect({ ...options, httpSocket: socket }, callback);
},
});
const inbound = await verifyFetchSuccess(httpServer, dispatcher);
assert.strictEqual(inbound.remotePort, plainProxy.outbound.localPort);
});
it("should set the proxy globally", async () => {
const dispatcher = socksDispatcher({
type: 5,
host: plainProxy.address,
port: plainProxy.port,
});
global[kGlobalDispatcher] = dispatcher;
try {
const inbound = await verifyFetchSuccess(httpServer, dispatcher);
assert.strictEqual(inbound.remotePort, plainProxy.outbound.localPort);
} finally {
global[kGlobalDispatcher] = undefined;
}
});
it("should proxy WebSocket", async () => {
global[kGlobalDispatcher] = socksDispatcher({
type: 5,
host: plainProxy.address,
port: plainProxy.port,
});
const ws = new WebSocket(`ws://localhost:${wsServer.port}`);
try {
await once(ws, "open");
ws.send("Hello");
const [response]: Array<MessageEvent<string>> = await once(ws, "message");
assert.strictEqual(response.data, "Hello");
assert.strictEqual(wsServer.inbound.remotePort, plainProxy.outbound.localPort);
} finally {
ws.close();
global[kGlobalDispatcher] = undefined;
}
});