/
test.ts
362 lines (306 loc) · 9.4 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import assert from 'assert';
import * as fs from 'fs';
import * as http from 'http';
import * as https from 'https';
// @ts-expect-error no types
import socks from 'socksv5';
import { listen } from 'async-listen';
import { ProxyServer, createProxy } from 'proxy';
import { req, json } from 'agent-base';
import { PacProxyAgent } from '../src';
const sslOptions = {
key: fs.readFileSync(`${__dirname}/ssl-cert-snakeoil.key`),
cert: fs.readFileSync(`${__dirname}/ssl-cert-snakeoil.pem`),
};
describe('PacProxyAgent', () => {
// target servers
let httpServer: http.Server;
let httpServerUrl: URL;
let httpsServer: https.Server;
let httpsServerUrl: URL;
// proxy servers
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let socksServer: any;
let socksServerUrl: URL;
let proxyServer: ProxyServer;
let proxyServerUrl: URL;
let proxyHttpsServer: ProxyServer;
let proxyHttpsServerUrl: URL;
beforeAll(async () => {
// setup target HTTP server
httpServer = http.createServer();
httpServerUrl = await listen(httpServer);
});
beforeAll(async () => {
// setup target SSL HTTPS server
httpsServer = https.createServer(sslOptions);
httpsServerUrl = await listen(httpsServer);
});
beforeAll(async () => {
// setup SOCKS proxy server
// @ts-expect-error no types for `socksv5`
socksServer = socks.createServer(function (_info, accept) {
accept();
});
await listen(socksServer);
const port = socksServer.address().port;
socksServerUrl = new URL(`socks://127.0.0.1:${port}`);
socksServer.useAuth(socks.auth.None());
});
beforeAll(async () => {
// setup HTTP proxy server
proxyServer = createProxy();
proxyServerUrl = await listen(proxyServer);
});
beforeAll(async () => {
// setup SSL HTTPS proxy server
proxyHttpsServer = createProxy(https.createServer(sslOptions));
proxyHttpsServerUrl = await listen(proxyHttpsServer);
});
afterAll(() => {
socksServer.close();
httpServer.close();
httpsServer.close();
proxyServer.close();
proxyHttpsServer.close();
});
beforeEach(() => {
httpServer.removeAllListeners('request');
httpsServer.removeAllListeners('request');
});
it('should allow a `sandbox` to be passed in', async () => {
//this.slow(1000);
function FindProxyForURL() {
// @ts-expect-error `bar()` is passed in via `sandbox`
throw new Error(foo() + bar());
}
function foo() {
return 'hi';
}
function asyncBar() {
return new Promise((r) => r('fooooo'));
}
asyncBar.async = true;
const agent = new PacProxyAgent(
`data:,${encodeURIComponent(FindProxyForURL.toString())}`,
{
sandbox: {
foo,
bar: asyncBar,
},
}
);
let err: Error | undefined;
try {
await req(httpServerUrl, { agent });
} catch (_err) {
err = _err as Error;
}
assert(err);
assert.equal(err.message, 'hifooooo');
});
describe('constructor', () => {
it('should accept a "string" proxy argument', () => {
const agent = new PacProxyAgent('pac+ftp://example.com/proxy.pac');
assert.equal('ftp://example.com/proxy.pac', agent.uri.href);
});
it('should accept a `URL` instance proxy argument', () => {
const agent = new PacProxyAgent(
new URL('pac+ftp://example.com/proxy.pac')
);
assert.equal('ftp://example.com/proxy.pac', agent.uri.href);
});
});
describe('"http" module', () => {
it('should work over an HTTP proxy', async () => {
httpServer.once('request', function (req, res) {
res.end(JSON.stringify(req.headers));
});
function FindProxyForURL() {
return 'PROXY localhost:PORT;';
}
const uri = `data:,${encodeURIComponent(
FindProxyForURL.toString().replace('PORT', proxyServerUrl.port)
)}`;
const agent = new PacProxyAgent(uri);
const res = await req(new URL('/test', httpServerUrl), { agent });
const data = await json(res);
assert.equal(httpServerUrl.host, data.host);
assert('via' in data);
});
it('should work over an HTTPS proxy', async () => {
httpServer.once('request', function (req, res) {
res.end(JSON.stringify(req.headers));
});
function FindProxyForURL() {
return 'HTTPS localhost:PORT;';
}
const uri = `data:,${encodeURIComponent(
FindProxyForURL.toString().replace(
'PORT',
proxyHttpsServerUrl.port
)
)}`;
const agent = new PacProxyAgent(uri, { rejectUnauthorized: false });
const res = await req(new URL('/test', httpServerUrl), { agent });
const data = await json(res);
assert.equal(httpServerUrl.host, data.host);
assert('via' in data);
});
it('should work over a SOCKS proxy', async () => {
httpServer.once('request', function (req, res) {
res.end(JSON.stringify(req.headers));
});
function FindProxyForURL() {
return 'SOCKS localhost:PORT;';
}
const uri = `data:,${encodeURIComponent(
FindProxyForURL.toString().replace('PORT', socksServerUrl.port)
)}`;
const agent = new PacProxyAgent(uri);
const res = await req(new URL('/test', httpServerUrl), { agent });
const data = await json(res);
assert.equal(httpServerUrl.host, data.host);
});
it('should fall back to the next proxy after one fails', async () => {
let gotReq = false;
httpServer.once('request', function (req, res) {
res.end(JSON.stringify(req.headers));
gotReq = true;
});
function FindProxyForURL() {
return 'SOCKS bad-domain:8080; HTTP bad-domain:8080; HTTPS bad-domain:8080; DIRECT;';
}
const uri = `data:,${encodeURIComponent(String(FindProxyForURL))}`;
const agent = new PacProxyAgent(uri);
const r = req(new URL('/test', httpServerUrl), { agent });
let proxyCount = 0;
r.on('proxy', function ({ proxy, error, socket }) {
proxyCount++;
if (proxy === 'DIRECT') {
assert(socket);
} else {
assert(error);
}
});
const res = await r;
const data = await json(res);
assert.equal(httpServerUrl.host, data.host);
assert.equal(proxyCount, 4);
assert(gotReq);
}, 10000); // This test is slow on Windows :/
it('should support `fallbackToDirect` option', async () => {
let gotReq = false;
httpServer.once('request', function (req, res) {
res.end(JSON.stringify(req.headers));
gotReq = true;
});
function FindProxyForURL() {
return 'SOCKS 127.0.0.1:4';
}
const uri = `data:,${encodeURIComponent(String(FindProxyForURL))}`;
const agent = new PacProxyAgent(uri, { fallbackToDirect: true });
const res = await req(new URL('/test', httpServerUrl), { agent });
const data = await json(res);
assert.equal(httpServerUrl.host, data.host);
assert(gotReq);
}, 10000); // This test is slow on Windows :/
});
describe('"https" module', () => {
it('should work over an HTTP proxy', async () => {
httpsServer.once('request', function (req, res) {
res.end(JSON.stringify(req.headers));
});
function FindProxyForURL() {
return 'PROXY localhost:PORT;';
}
const uri = `data:,${encodeURIComponent(
FindProxyForURL.toString().replace('PORT', proxyServerUrl.port)
)}`;
const agent = new PacProxyAgent(uri);
const res = await req(new URL('/test', httpsServerUrl), {
agent,
rejectUnauthorized: false,
});
const data = await json(res);
assert.equal(httpsServerUrl.host, data.host);
});
it('should work over an HTTPS proxy', async () => {
let gotReq = false;
httpsServer.once('request', function (req, res) {
gotReq = true;
res.end(JSON.stringify(req.headers));
});
function FindProxyForURL() {
return 'HTTPS localhost:PORT;';
}
const uri = `pac+data:,${encodeURIComponent(
FindProxyForURL.toString().replace(
'PORT',
proxyHttpsServerUrl.port
)
)}`;
const agent = new PacProxyAgent(uri, {
rejectUnauthorized: false,
});
const res = await req(new URL('/test', httpsServerUrl), {
agent,
rejectUnauthorized: false,
});
const data = await json(res);
assert.equal(httpsServerUrl.host, data.host);
assert(gotReq);
});
it('should work over a SOCKS proxy', async () => {
let gotReq = false;
httpsServer.once('request', function (req, res) {
gotReq = true;
res.end(JSON.stringify(req.headers));
});
function FindProxyForURL() {
return 'SOCKS localhost:PORT;';
}
const uri = `data:,${encodeURIComponent(
FindProxyForURL.toString().replace('PORT', socksServerUrl.port)
)}`;
const agent = new PacProxyAgent(uri);
const res = await req(new URL('/test', httpsServerUrl), {
agent,
rejectUnauthorized: false,
});
const data = await json(res);
assert.equal(httpsServerUrl.host, data.host);
assert(gotReq);
});
it('should fall back to the next proxy after one fails', async () => {
let gotReq = false;
httpsServer.once('request', function (req, res) {
gotReq = true;
res.end(JSON.stringify(req.headers));
});
function FindProxyForURL() {
return 'SOCKS bad-domain:8080; HTTP bad-domain:8080; HTTPS bad-domain:8080; DIRECT;';
}
const uri = `data:,${encodeURIComponent(String(FindProxyForURL))}`;
const agent = new PacProxyAgent(uri);
const r = req(new URL('/test', httpsServerUrl), {
agent,
rejectUnauthorized: false,
});
let proxyCount = 0;
r.on('proxy', function ({ proxy, error, socket }) {
proxyCount++;
if (proxy === 'DIRECT') {
assert(socket);
} else {
assert(error);
}
});
const res = await r;
const data = await json(res);
assert.equal(httpsServerUrl.host, data.host);
assert.equal(proxyCount, 4);
assert(gotReq);
}, 10000); // This test is slow on Windows :/
});
});