-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebproxy2.js
More file actions
358 lines (319 loc) · 10.7 KB
/
webproxy2.js
File metadata and controls
358 lines (319 loc) · 10.7 KB
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
#!/usr/bin/env node
import http from 'node:http';
import https from 'node:https';
import net from 'node:net';
import { readFileSync } from 'node:fs';
import { createRequire } from 'node:module';
import { dirname, join } from 'node:path';
import { HttpsProxyAgent } from 'https-proxy-agent';
import { QuickJS } from 'quickjs-wasi';
import { createPacResolver } from 'pac-resolver';
// Intentionally disabled TLS certificate verification so the proxy can relay traffic through
// corporate proxies that use self-signed or private CA certificates. Only enable this tool
// in a trusted network environment. Do NOT use this on untrusted networks.
// nosemgrep: nodejs_security.audit.security-tls-client-insecure
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; // lgtm[js/disabling-certificate-validation]
const require = createRequire(import.meta.url);
const wasmPath = join(dirname(require.resolve('quickjs-wasi')), '..', 'quickjs.wasm');
let debugging = false;
let FindProxyForURL;
let authHeader;
let httpsDecode = false;
const REGEX_HOSTPORT = /^([^:]+)(:([0-9]+))?$/;
const REGEX_PAC_PROXY = /\S+\b(\S+)/;
/**
* Extract the proxy host:port string from a PAC result such as "PROXY host:port".
* @param {string} data - PAC result string
* @returns {string}
*/
function getUrlHeader(data) {
return REGEX_PAC_PROXY.exec(data)[0];
}
/**
* Parse a "host" or "host:port" string into [host, port].
* @param {string} hostString
* @param {number} defaultPort
* @returns {[string, string|number]}
*/
function getHostPortFromString(hostString, defaultPort) {
let host = hostString;
let port = defaultPort;
const result = REGEX_HOSTPORT.exec(hostString);
if (result !== null) {
host = result[1];
if (result[2] != null) {
port = result[3];
}
}
return [host, port];
}
function printHeaderRequestHttp(userRequest) {
console.log(`${userRequest.method} ${userRequest.url} HTTP/${userRequest.httpVersion}\r\n`);
console.log(JSON.stringify(userRequest.headers, null, 4));
}
function printHeaderResponseHttp(response) {
console.log(`HTTP/${response.httpVersion} ${response.statusCode} ${response.statusMessage}\r\n`);
console.log(JSON.stringify(response.headers, null, 4));
}
/**
* Handle plain HTTPS requests forwarded through the proxy.
* @param {http.IncomingMessage} userRequest
* @param {http.ServerResponse} userResponse
*/
async function httpsRequest(userRequest, userResponse) {
try {
printHeaderRequestHttp(userRequest);
const hostport = getHostPortFromString(userRequest.headers['host'], 443);
const res = await FindProxyForURL(userRequest.url, hostport[0]);
let options;
if (res === 'DIRECT') {
let path = userRequest.url;
const match = /^[a-zA-Z]+:\/\/[^/]+(\/.*)?$/.exec(userRequest.url);
if (match) {
path = match[1]?.length > 0 ? match[1] : '/';
}
delete userRequest.headers['Proxy-Authorization'];
options = {
host: hostport[0],
port: hostport[1],
method: userRequest.method,
path,
agent: userRequest.agent,
auth: userRequest.auth,
headers: userRequest.headers,
};
} else {
const overHeader = { ...userRequest.headers, 'Proxy-Authorization': authHeader };
const proxyHostport = getHostPortFromString(getUrlHeader(res), 443);
const agent = new HttpsProxyAgent(`http://${proxyHostport[0]}:${proxyHostport[1]}`);
options = {
host: hostport[0],
port: hostport[1],
path: userRequest.url,
agent,
headers: overHeader,
};
}
const proxyRequest = https.request(options, (proxyResponse) => {
printHeaderResponseHttp(proxyResponse);
userResponse.writeHead(proxyResponse.statusCode, proxyResponse.headers);
proxyResponse.pipe(userResponse);
});
proxyRequest.on('error', (error) => {
userResponse.writeHead(500);
userResponse.end(
`<h1>500 Error</h1>\r\n<p>Error was <pre>${error}</pre></p>\r\n</body></html>\r\n`,
);
});
userRequest.pipe(proxyRequest);
} catch (err) {
console.error('httpsRequest error:', err);
if (!userResponse.headersSent) {
userResponse.writeHead(500);
}
userResponse.end();
}
}
/**
* Handle plain HTTP requests forwarded through the proxy.
* @param {http.IncomingMessage} userRequest
* @param {http.ServerResponse} userResponse
*/
async function httpUserRequest(userRequest, userResponse) {
try {
printHeaderRequestHttp(userRequest);
const hostport = getHostPortFromString(userRequest.headers['host'], 80);
const res = await FindProxyForURL(userRequest.url, hostport[0]);
let options;
if (res === 'DIRECT') {
let path = userRequest.url;
const match = /^[a-zA-Z]+:\/\/[^/]+(\/.*)?$/.exec(userRequest.url);
if (match) {
path = match[1]?.length > 0 ? match[1] : '/';
}
delete userRequest.headers['Proxy-Authorization'];
options = {
host: hostport[0],
port: hostport[1],
method: userRequest.method,
path,
agent: userRequest.agent,
auth: userRequest.auth,
headers: userRequest.headers,
};
} else {
const overHeader = { ...userRequest.headers, 'Proxy-Authorization': authHeader };
const hp = getHostPortFromString(getUrlHeader(res), 80);
options = {
host: hp[0],
port: hp[1],
path: userRequest.url,
headers: overHeader,
};
}
const proxyRequest = http.request(options, (proxyResponse) => {
printHeaderResponseHttp(proxyResponse);
userResponse.writeHead(proxyResponse.statusCode, proxyResponse.headers);
proxyResponse.pipe(userResponse);
});
proxyRequest.on('error', (error) => {
userResponse.writeHead(500);
userResponse.end(
`<h1>500 Error</h1>\r\n<p>Error was <pre>${error}</pre></p>\r\n</body></html>\r\n`,
);
});
userRequest.pipe(proxyRequest);
} catch (err) {
console.error('httpUserRequest error:', err);
if (!userResponse.headersSent) {
userResponse.writeHead(500);
}
userResponse.end();
}
}
/**
* Fetch the PAC file contents from a URL (supports http:// and https://).
* @param {string} url
* @returns {Promise<string>}
*/
function fetchPacFile(url) {
const transport = url.startsWith('https://') ? https : http;
return new Promise((resolve, reject) => {
const req = transport.get(url, (response) => {
const chunks = [];
response.on('data', (chunk) => chunks.push(chunk));
response.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
response.on('error', reject);
});
req.on('error', reject);
});
}
async function main() {
let port = 5555;
let urlProxyPac = '';
let password;
let login;
let certificateKey = 'selfsigned.key';
let certificate = 'selfsigned.crt';
for (let argn = 2; argn < process.argv.length; argn++) {
switch (process.argv[argn]) {
case '-p':
port = parseInt(process.argv[++argn], 10);
break;
case '-P':
urlProxyPac = process.argv[++argn];
break;
case '-l':
login = process.argv[++argn];
break;
case '-pass':
password = process.argv[++argn];
break;
case '-cert':
certificate = process.argv[++argn];
break;
case '-certKey':
certificateKey = process.argv[++argn];
break;
case '-d':
debugging = true;
break;
case '-https':
httpsDecode = true;
break;
default:
console.warn(`Unknown argument: ${process.argv[argn]}`);
}
}
if (!urlProxyPac) {
console.error('Error: -P <url> (PAC file URL) is required.');
process.exit(1);
}
if (!login || !password) {
console.error('Error: -l <login> and -pass <password> are required.');
process.exit(1);
}
authHeader = `Basic ${Buffer.from(`${login}:${password}`).toString('base64')}`;
if (debugging) {
console.log(`webproxy server listening on port ${port}`);
}
// Initialize QuickJS WASM runtime and load the PAC file
const wasmBytes = readFileSync(wasmPath);
const qjs = await QuickJS.create(wasmBytes);
const pacContent = await fetchPacFile(urlProxyPac);
FindProxyForURL = createPacResolver(qjs, pacContent);
console.log(`FindProxyForURL OK for ${urlProxyPac}`);
// Start HTTP proxy server
const server = http.createServer(httpUserRequest);
// Start HTTPS decoding server (port + 1) only when -https flag is set
if (httpsDecode) {
const optionsCertificate = {
key: readFileSync(certificateKey),
cert: readFileSync(certificate),
};
const httpsServer = https.createServer(optionsCertificate, httpsRequest);
httpsServer.listen(port + 1);
console.log(`TCP server accepting connection on port: ${port + 1}`);
}
// Handle HTTPS CONNECT tunnelling
server.on('connect', async (request, socketRequest, bodyhead) => {
const { url, httpVersion } = request;
let hp = getHostPortFromString(url, 443);
let res;
try {
res = await FindProxyForURL(request.url, hp[0]);
} catch (err) {
console.error('PAC resolver error:', err);
socketRequest.write(`HTTP/${httpVersion} 500 Connection error\r\n\r\n`);
socketRequest.end();
return;
}
if (res !== 'DIRECT') {
hp = getHostPortFromString(getUrlHeader(res), 80);
}
if (httpsDecode) {
hp = ['localhost', String(port + 1)];
res = 'DIRECT';
}
const proxySocket = new net.Socket();
proxySocket.connect(parseInt(hp[1], 10), hp[0], () => {
if (debugging) {
console.log(' < connected to %s/%s', hp[0], hp[1]);
console.log(' > writing head of length %d', bodyhead.length);
}
if (res === 'DIRECT') {
proxySocket.write(bodyhead);
socketRequest.write(`HTTP/${httpVersion} 200 Connection established\r\n\r\n`);
} else {
let httpConnect = `CONNECT ${request.url} HTTP/${httpVersion}\r\n`;
for (const [h, v] of Object.entries(request.headers)) {
httpConnect += `${h}: ${v}\r\n`;
}
httpConnect += `Proxy-Authorization: ${authHeader}\r\n\r\n`;
proxySocket.write(httpConnect);
proxySocket.write(bodyhead);
}
});
proxySocket.pipe(socketRequest);
socketRequest.pipe(proxySocket);
proxySocket.on('error', (err) => {
socketRequest.write(`HTTP/${httpVersion} 500 Connection error\r\n\r\n`);
if (debugging) {
console.log(' < ERR: %s', err);
}
socketRequest.end();
});
socketRequest.on('error', (err) => {
proxySocket.end();
if (debugging) {
console.log(' > ERR: %s', err);
}
});
});
server.listen(port);
console.log(`TCP server accepting connection on port: ${port}`);
}
main().catch((err) => {
console.error('Fatal error:', err);
process.exit(1);
});