/
test.ts
343 lines (297 loc) · 9.52 KB
/
test.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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import * as fs from 'fs';
import * as http from 'http';
import * as https from 'https';
import { once } from 'events';
import assert from 'assert';
import WebSocket, { WebSocketServer } from 'ws';
import { json, req } from 'agent-base';
import { ProxyServer, createProxy } from 'proxy';
// @ts-expect-error no types
import socks from 'socksv5';
import { listen } from 'async-listen';
import { ProxyAgent } from '../src';
import { HttpProxyAgent } from 'http-proxy-agent';
import { HttpsProxyAgent } from 'https-proxy-agent';
const sslOptions = {
key: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.key'),
cert: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.pem'),
};
describe('ProxyAgent', () => {
// target servers
let httpServer: http.Server;
let httpWebSocketServer: WebSocketServer;
let httpServerUrl: URL;
let httpsServer: https.Server;
let httpsWebSocketServer: WebSocketServer;
let httpsServerUrl: URL;
// proxy servers
let httpProxyServer: ProxyServer;
let httpProxyServerUrl: URL;
let httpsProxyServer: ProxyServer;
let httpsProxyServerUrl: URL;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let socksServer: any;
let socksPort: number;
beforeAll(async () => {
// setup target HTTP server
httpServer = http.createServer();
httpWebSocketServer = new WebSocketServer({ server: httpServer });
httpServerUrl = await listen(httpServer);
});
beforeAll(async () => {
// setup target SSL HTTPS server
httpsServer = https.createServer(sslOptions);
httpsWebSocketServer = new WebSocketServer({ server: httpsServer });
httpsServerUrl = await listen(httpsServer);
});
beforeAll(async () => {
// setup SOCKS proxy server
// @ts-expect-error no types
socksServer = socks.createServer((_info, accept) => {
accept();
});
socksServer.useAuth(socks.auth.None());
await listen(socksServer);
socksPort = socksServer.address().port;
});
beforeAll(async () => {
// setup HTTP proxy server
httpProxyServer = createProxy();
httpProxyServerUrl = await listen(httpProxyServer);
});
beforeAll(async () => {
// setup SSL HTTPS proxy server
httpsProxyServer = createProxy(https.createServer(sslOptions));
httpsProxyServerUrl = await listen(httpsProxyServer);
});
afterAll(() => {
socksServer.close();
httpServer.close();
httpsServer.close();
httpProxyServer.close();
httpsProxyServer.close();
});
beforeEach(() => {
delete process.env.HTTP_PROXY;
delete process.env.HTTPS_PROXY;
delete process.env.WS_PROXY;
delete process.env.WSS_PROXY;
delete process.env.NO_PROXY;
httpServer.removeAllListeners('request');
httpsServer.removeAllListeners('request');
httpWebSocketServer.removeAllListeners('connection');
httpsWebSocketServer.removeAllListeners('connection');
});
describe('"http" module', () => {
it('should work with no proxy from env', async () => {
httpServer.once('request', function (req, res) {
res.end(JSON.stringify(req.headers));
});
// `NO_PROXY` should take precedence
process.env.NO_PROXY = '*';
process.env.HTTP_PROXY = httpProxyServerUrl.href;
const agent = new ProxyAgent();
const res = await req(new URL('/test', httpServerUrl), { agent });
const body = await json(res);
assert.equal(httpServerUrl.host, body.host);
assert(!('via' in body));
});
it('should work over "http" proxy', async () => {
httpServer.once('request', function (req, res) {
res.end(JSON.stringify(req.headers));
});
process.env.HTTP_PROXY = httpProxyServerUrl.href;
const agent = new ProxyAgent();
const res = await req(new URL('/test', httpServerUrl), { agent });
const body = await json(res);
assert.equal(httpServerUrl.host, body.host);
assert('via' in body);
});
it('should work over "https" proxy', async () => {
httpServer.once('request', function (req, res) {
res.end(JSON.stringify(req.headers));
});
process.env.HTTP_PROXY = httpsProxyServerUrl.href;
const agent = new ProxyAgent({ rejectUnauthorized: false });
const res = await req(new URL('/test', httpServerUrl), { agent });
const body = await json(res);
assert.equal(httpServerUrl.host, body.host);
});
it('should work over "socks" proxy', async () => {
httpServer.once('request', function (req, res) {
res.end(JSON.stringify(req.headers));
});
process.env.HTTP_PROXY = `socks://localhost:${socksPort}`;
const agent = new ProxyAgent();
const res = await req(new URL('/test', httpServerUrl), { agent });
const body = await json(res);
assert.equal(httpServerUrl.host, body.host);
});
it('should work with `keepAlive: true`', async () => {
httpServer.on('request', function (req, res) {
res.end(JSON.stringify(req.headers));
});
process.env.HTTP_PROXY = httpsProxyServerUrl.href;
const agent = new ProxyAgent({
keepAlive: true,
rejectUnauthorized: false,
});
try {
const res = await req(new URL('/test', httpServerUrl), {
agent,
});
res.resume();
expect(res.headers.connection).toEqual('keep-alive');
const s1 = res.socket;
await once(s1, 'free');
const res2 = await req(new URL('/test', httpServerUrl), {
agent,
});
res2.resume();
expect(res2.headers.connection).toEqual('keep-alive');
const s2 = res2.socket;
assert(s1 === s2);
await once(s2, 'free');
} finally {
agent.destroy();
}
});
});
describe('"https" module', () => {
it('should work over "https" proxy', async () => {
let gotReq = false;
httpsServer.once('request', function (req, res) {
res.end(JSON.stringify(req.headers));
gotReq = true;
});
process.env.HTTPS_PROXY = httpsProxyServerUrl.href;
const agent = new ProxyAgent({ rejectUnauthorized: false });
const res = await req(new URL('/test', httpsServerUrl), {
agent,
rejectUnauthorized: false,
});
const body = await json(res);
assert(gotReq);
assert.equal(httpsServerUrl.host, body.host);
});
it('should work over "socks" proxy', async () => {
let gotReq = false;
httpsServer.once('request', function (req, res) {
gotReq = true;
res.end(JSON.stringify(req.headers));
});
process.env.HTTP_PROXY = `socks://localhost:${socksPort}`;
const agent = new ProxyAgent();
const res = await req(new URL('/test', httpsServerUrl), {
agent,
rejectUnauthorized: false,
});
const body = await json(res);
assert(gotReq);
assert.equal(httpsServerUrl.host, body.host);
});
it('should use `HttpProxyAgent` for "http" and `HttpsProxyAgent` for "https"', async () => {
let gotHttpReq = false;
httpServer.once('request', function (req, res) {
res.end(JSON.stringify(req.headers));
gotHttpReq = true;
});
let gotHttpsReq = false;
httpsServer.once('request', function (req, res) {
res.end(JSON.stringify(req.headers));
gotHttpsReq = true;
});
process.env.ALL_PROXY = httpsProxyServerUrl.href;
const agent = new ProxyAgent({ rejectUnauthorized: false });
const res = await req(httpServerUrl, {
agent,
});
const body = await json(res);
assert(gotHttpReq);
assert.equal(httpServerUrl.host, body.host);
expect(agent.cache.size).toEqual(1);
expect([...agent.cache.values()][0]).toBeInstanceOf(HttpProxyAgent);
const res2 = await req(httpsServerUrl, {
agent,
});
const body2 = await json(res2);
assert(gotHttpsReq);
assert.equal(httpsServerUrl.host, body2.host);
expect(agent.cache.size).toEqual(2);
expect([...agent.cache.values()][0]).toBeInstanceOf(
HttpsProxyAgent
);
});
it('should call provided function with getProxyForUrl option', async () => {
let gotCall = false;
let urlParameter = '';
httpsServer.once('request', function (req, res) {
res.end(JSON.stringify(req.headers));
});
const agent = new ProxyAgent({
rejectUnauthorized: false,
getProxyForUrl: (u) => {
gotCall = true;
urlParameter = u;
return httpsProxyServerUrl.href;
},
});
const requestUrl = new URL('/test', httpsServerUrl);
const res = await req(requestUrl, {
agent,
rejectUnauthorized: false,
});
const body = await json(res);
assert(httpsServerUrl.host === body.host);
assert(gotCall);
assert(requestUrl.href === urlParameter);
});
});
describe('"ws" module', () => {
it('should work over "http" proxy to `ws:` URL', async () => {
let requestCount = 0;
let connectionCount = 0;
httpServer.once('request', function (req, res) {
requestCount++;
res.end();
});
httpWebSocketServer.on('connection', (ws) => {
connectionCount++;
ws.send('OK');
});
process.env.WS_PROXY = httpProxyServerUrl.href;
const agent = new ProxyAgent();
const ws = new WebSocket(httpServerUrl.href.replace('http', 'ws'), {
agent,
});
const [message] = await once(ws, 'message');
expect(connectionCount).toEqual(1);
expect(requestCount).toEqual(0);
expect(message.toString()).toEqual('OK');
ws.close();
});
it('should work over "http" proxy to `wss:` URL', async () => {
let requestCount = 0;
let connectionCount = 0;
httpsServer.once('request', function (req, res) {
requestCount++;
res.end();
});
httpsWebSocketServer.on('connection', (ws) => {
connectionCount++;
ws.send('OK');
});
process.env.WSS_PROXY = httpProxyServerUrl.href;
const agent = new ProxyAgent();
const ws = new WebSocket(httpsServerUrl.href.replace('https', 'wss'), {
agent,
rejectUnauthorized: false
});
const [message] = await once(ws, 'message');
expect(connectionCount).toEqual(1);
expect(requestCount).toEqual(0);
expect(message.toString()).toEqual('OK');
ws.close();
});
});
});