-
Notifications
You must be signed in to change notification settings - Fork 17
/
test.js
552 lines (474 loc) · 18 KB
/
test.js
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
const util = require('util');
const tls = require('tls');
const exec = util.promisify(require('child_process').exec);
const ProxyServer = require('./ProxyServer');
const axios = require('axios');
const forge = require('node-forge');
const HttpServer = require('./test/HttpServer');
async function test1() {
console.log('Starting TEST1 - Normal Transparent-Proxy!');
//init ProxyServer
const server = new ProxyServer({verbose: true});
const toTest = ['http://localhost:3000', 'http://localhost:3000/notFound',];
//starting server on port 10001
const PORT = 10001;
return new Promise((res, rej) => {
server.listen(PORT, '0.0.0.0', async function () {
console.log('transparent-proxy was started!', server.address());
for (const singlePath of toTest) {
const res = await axios.get(singlePath,
{
proxy: {
protocol: 'http',
host: '127.0.0.1',
port: PORT
}
})
.catch((err) => {
return err.response;
});
console.log('For:', singlePath, 'Response =>', JSON.stringify(res.data));
}
console.log('Closing transparent-proxy Server - TEST1\n');
server.close();
res(true);
});
});
}
async function test2() {
console.log('Starting TEST2 - Spoof Response!');
let ownIp = '';
const TO_SWITCH = '6.6.6.6';
const toTest = ['http://localhost:3000/ip', 'https://localhost:3001/ip'];
const PORT = 10002; //starting server on port 10001
const cmdOwnIp = 'curl ' + toTest[0];
console.log('Getting Own ip with', cmdOwnIp);
const {stdout, stderr} = await exec(cmdOwnIp);
ownIp = stdout;
console.log('Your IP is:', ownIp);
console.log('Starting Proxy Server with spoof-behaviors');
//init ProxyServer
const server = new ProxyServer({
verbose: true,
intercept: true,
injectResponse: (data, session) => {
//SPOOFING RETURNED RESPONSE
if (data.toString().match(ownIp)) {
const newData = Buffer.from(data.toString()
.replace(new RegExp('Content-Length: ' + ownIp.length, 'gmi'),
'Content-Length: ' + (TO_SWITCH.length))
.replace(ownIp, TO_SWITCH));
return newData;
}
return data;
}
});
return new Promise((res, rej) => {
server.listen(PORT, '0.0.0.0', async function () {
console.log('transparent-proxy was started!', server.address());
for (const singlePath of toTest) {
const res = await axios.get(singlePath,
{
proxy: {
protocol: 'http',
host: '127.0.0.1',
port: PORT
}
})
.catch((err) => {
return err.response;
});
console.log('For:', singlePath, 'Response =>', res.data);
if (res.data !== TO_SWITCH) {
console.error('Response must be', TO_SWITCH);
process.exit(2);
}
}
console.log('Closing transparent-proxy Server - TEST2\n');
server.close();
res(true);
});
});
}
async function test3() {
console.log('Starting TEST3 - Spoof Request!');
const toTest = ['http://localhost:3000/ua', 'https://localhost:3001/ua'];
const USER_AGENT = /axios\/.+/;
const TO_SWITCH = 'Spoofed UA!!';
const PORT = 10003; //starting server on port 10001
console.log('Starting Proxy Server with spoof-behaviors');
//init ProxyServer
const server = new ProxyServer({
verbose: true,
intercept: true,
injectData: (data, session) => {
if (!session.request.headers?.['user-agent']?.match(USER_AGENT)) {
console.error('Original User-Agent', session.request.headers['user-agent']);
process.exit(3);
}
return Buffer.from(data.toString().replace(USER_AGENT, TO_SWITCH));
}
});
return new Promise((res, rej) => {
server.listen(PORT, '0.0.0.0', async function () {
console.log('transparent-proxy was started!', server.address());
for (const singlePath of toTest) {
const res = await axios.get(singlePath,
{
proxy: {
protocol: 'http',
host: '127.0.0.1',
port: PORT
}
})
.catch((err) => {
return err.response;
});
console.log('For:', singlePath, 'Response =>', res.data);
if (res.data.trim() !== TO_SWITCH) {
console.error('Response must be', TO_SWITCH);
process.exit(3);
}
}
console.log('Closing transparent-proxy Server - TEST3\n');
server.close();
res(true);
});
})
}
async function test4() {
console.log('Starting TEST4 - Change Some Keys on runtime!');
const toTest = ['https://localhost:3001']; //TODO need https for this
const PORT = 10004; //starting server on port 10001
//init ProxyServer
const server = new ProxyServer({
verbose: true,
intercept: true,
keys: (session) => {
const tunnel = session.getTunnelStats();
console.log('\t\t=> Could change keys for', tunnel);
return false;
}
});
return new Promise((res, rej) => {
server.listen(PORT, '0.0.0.0', async function () {
console.log('transparent-proxy was started!', server.address());
for (const singlePath of toTest) {
const cmd = 'curl' + ' -x 127.0.0.1:' + PORT + ' -k ' + singlePath;
console.log(cmd);
const {stdout, stderr} = await exec(cmd);
console.log('Response =>', stdout);
}
console.log('Closing transparent-proxy Server - TEST4\n');
server.close();
res(true);
});
});
}
async function test5() {
console.log('Starting TEST5 - Proxy With Authentication!');
const singlePath = 'http://localhost:3000/';
const pwdToTest = ['bar:foo', 'wronguser:wrongpassword'];
const {AUTH_REQUIRED} = require('./lib/constants').HTTP_BODIES;
const PORT = 10005; //starting server on port 10001
//init ProxyServer
const server = new ProxyServer({
verbose: true,
auth: (username, password, session) => {
return username === 'bar' && password === 'foo';
}
});
return new Promise((res, rej) => {
server.listen(PORT, '0.0.0.0', async function () {
console.log('transparent-proxy was started!', server.address());
for (const pwd of pwdToTest) {
const [user, pass] = pwd.split(':');
const res = await axios.get(singlePath,
{
proxy: {
protocol: 'http',
host: '127.0.0.1',
port: PORT,
auth: {
username: user,
password: pass
}
}
})
.catch((err) => {
return err.response || AUTH_REQUIRED;
});
console.log('For:', user, pass, singlePath, 'Response =>', res.status);
if (pwd === pwdToTest[0]
&& res.data === AUTH_REQUIRED) {
console.error('Response must not be', 'HTTP CODE 407');
process.exit(5);
}
if (pwd === pwdToTest[1]
&& res.data !== AUTH_REQUIRED) {
console.error('Response must be', 'HTTP CODE 407');
process.exit(5);
}
}
console.log('Closing transparent-proxy Server - TEST5\n');
server.close();
res(true);
});
});
}
async function test6() {
console.log('Starting TEST6 - Async inject data');
const toTest = ['http://localhost:3000', 'https://localhost:3001'];
const ADDED_HEADER = 'x-test: my async value';
const PORT = 10006;
console.log('Starting Proxy Server with spoof-behaviors');
const getHeader = async () =>
new Promise((resolve) => setTimeout(() => resolve(ADDED_HEADER), 250));
//init ProxyServer
const server = new ProxyServer({
verbose: true,
intercept: true,
injectData: async (data, session) => {
const requestLines = data.toString().split('\r\n');
// add the new header after the request line
requestLines.splice(2, 0, `${await getHeader()}`);
return requestLines.join('\r\n');
}
});
return new Promise((res, rej) => {
server.listen(PORT, '0.0.0.0', async function () {
console.log('transparent-proxy was started!', server.address());
for (const singlePath of toTest) {
const res = await axios.get(singlePath,
{
proxy: {
protocol: 'http',
host: '127.0.0.1',
port: PORT
}
})
.catch((err) => {
return err.response;
});
console.log('For:', singlePath, 'Response =>', JSON.stringify(res.data));
if (res.data.headers['x-test'] !== 'my async value') {
console.error(`Header ${ADDED_HEADER} must have been sent`);
process.exit(6);
}
}
console.log('Closing transparent-proxy Server - TEST6\n');
server.close();
res(true);
});
})
}
async function test7() {
console.log('Starting TEST7 - Custom Logger');
const toTest = ['http://localhost:3000'];
const PORT = 10007;
console.log('Starting Proxy Server with custom logger');
let logs = [];
const loggerStub = {
log(args) {
logs.push(args)
},
error(args) {
logs.push(args)
}
};
//init ProxyServer
const server = new ProxyServer({
verbose: true,
logger: loggerStub,
});
return new Promise((res, rej) => {
server.listen(PORT, '0.0.0.0', async function () {
console.log('transparent-proxy was started!', server.address());
for (const singlePath of toTest) {
const cmd = 'curl' + ' -x 127.0.0.1:' + PORT + ' -k ' + singlePath;
logs = [];
await exec(cmd);
if (!logs.length) {
console.error('Url should have been written to logs', logs);
process.exit(7);
}
}
console.log('Closing transparent-proxy Server - TEST7\n');
server.close();
res(true);
});
})
}
async function test8() {
console.log('Starting TEST8 - Use SNICallback');
const toTest = ['ifconfig.me', 'ifconfig.io']; //protocol will be append later
const PORT = 10008;
//init ProxyServer
const server = new ProxyServer({
verbose: true,
intercept: true,
handleSni: (hostname, callback) => {
console.log(`In SNI callback for ${hostname}`);
try {
const keypair = forge.rsa.generateKeyPair({bits: 2048, e: 0x10001});
const cert = forge.pki.createCertificate();
cert.publicKey = keypair.publicKey;
const attrs = [
{
name: 'organizationName',
value: 'transparent-proxy',
}
];
cert.setIssuer(attrs);
cert.setSubject([
...attrs,
{
name: 'commonName',
value: hostname,
},
]);
cert.sign(keypair.privateKey);
callback(null, tls.createSecureContext({
key: forge.pki.privateKeyToPem(keypair.privateKey),
cert: forge.pki.certificateToPem(cert)
}
))
}
catch (err) {
callback(err)
}
}
});
return new Promise((res, rej) => {
server.listen(PORT, '0.0.0.0', async function () {
console.log('transparent-proxy was started!', server.address());
for (const domain of toTest) {
const cmd = 'curl' + ' -v -x 127.0.0.1:' + PORT + ' -k https://' + domain;
console.log(cmd);
const {stdout, stderr} = await exec(cmd);
console.log('Response =>', stdout);
console.log('Log =>', stderr);
if (!(stderr.includes('issuer: O=transparent-proxy') && stderr.includes(`CN=${domain}`))) {
console.error(`Certificate issued by O=transparent-proxy for CN=${domain} expected`);
process.exit(8);
}
}
console.log('Closing transparent-proxy Server - TEST8\n');
server.close();
res(true);
});
});
}
async function test9() {
console.log('Starting TEST9 - Use Gzip with Transfer');
const toTest = ['http://localhost:3000/gzip-chunked', 'https://localhost:3001/gzip-chunked'];
const MUST_BE = '{response:"ok"}';
const server = new ProxyServer({
verbose: true,
intercept: true,
injectResponse: (data, session) => {
if (session.response.complete) { //if response is finished
console.log('Response is completed');
const zlib = require('zlib');
zlib.gunzip(session.rawResponse, function (err, decoded) {
if (err) {
throw err;
}
console.log('Decoded is', decoded.toString());
if (decoded.toString() !== MUST_BE) {
console.error('Decoded is not', MUST_BE, 'but is', decoded.toString());
process.exit(9);
}
});
}
return data;
}
});
const port = 10009;
return new Promise((res, rej) => {
//starting server on port 10009
server.listen(port, '0.0.0.0', async function () {
console.log('transparent-proxy was started!', server.address());
for (const singlePath of toTest) {
const cmd = 'curl' + ' -x localhost:' + port + ' -k ' + singlePath;
console.log(cmd);
await exec(cmd);
}
server.close();
res(true);
});
});
}
// async function test10() {
// const puppeteer = require('puppeteer');
//
// const server = new ProxyServer({
// verbose: true,
// intercept: true,
// injectResponse: (data, session) => {
// // console.log('data', session.response.headers)
// if (session.response.complete) { //if response is finished
// console.log('Response is completed');
// const zlib = require('zlib');
// zlib.gunzip(session.rawResponse, function (err, decoded) {
// if (err) {
// throw err;
// }
// console.log('Decoded is', decoded.toString());
// // if (decoded.toString() !== MUST_BE) {
// // console.error('Decoded is not', MUST_BE, 'but is', decoded.toString());
// // process.exit(9);
// // }
// });
// }
// return data;
// }
// });
//
// const port = 10010;
// return new Promise((res, rej) => {
// //starting server
// server.listen(port, '0.0.0.0', async function () {
// console.log('transparent-proxy was started!', server.address());
//
// const browser = await puppeteer.launch({
// headless: false,
// ignoreHTTPSErrors: true,
// args: [
// `--proxy-server=localhost:` + port,
// '--ignore-certificate-errors',
// '--ignore-certificate-errors-spki-list',
// '--disable-gpu',
// '--disable-dev-shm-usage',
// '--disable-setuid-sandbox',
// '--no-first-run',
// '--no-sandbox',
// '--no-zygote',
// ]
// });
// const page = await browser.newPage();
//
// await page.goto('https://localhost:3001/gzip-chunked', {waitUntil: 'networkidle2'});
// console.log('page', await page.title());
// await page.close();
// await browser.close();
// server.close();
// res(true);
// });
// });
// }
async function main() {
const server = await HttpServer([]);
await test1();
await test2();
await test3();
await test4();
await test5();
await test6();
await test7();
// // await test8(); //TODO reactivate this, validation doesn't work with curl 7.83
await test9();
// await test10();
server.close();
}
return main();