-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
proxy-login-automator.js
executable file
·250 lines (221 loc) · 10.8 KB
/
proxy-login-automator.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
#!/usr/bin/env node
'use strict';
var net = require('net'), tls = require('tls');
var HTTPParser = process.binding('http_parser').HTTPParser = require('http-parser-js').HTTPParser;
var http = require('http'), https = require('https');
var url = require('url');
function main() {
//convert `-key value` to cfg[key]=value
var cfg = process.argv.slice(2/*skip ["node", "xxx.js"]*/).reduce(function (cfg, arg, i, argv) {
return (i % 2 === 0 && (arg.slice(0, 1) === '-' && (cfg[arg.slice(1)] = argv[i + 1])), cfg);
}, {local_host: '', local_port: 0, remote_host: '', remote_port: 0, usr: '', pwd: '', as_pac_server: 0});
cfg.local_host = cfg.local_host || 'localhost';
cfg.local_port = (cfg.local_port & 0xffff) || 8080;
cfg.remote_port = (cfg.remote_port & 0xffff) || 8080;
cfg.as_pac_server = cfg.as_pac_server === 'true';
cfg.is_remote_https = cfg.is_remote_https === 'true';
cfg.ignore_https_cert = cfg.ignore_https_cert === 'true';
cfg.are_remotes_in_pac_https = cfg.are_remotes_in_pac_https === 'true';
if (!cfg.local_host || !cfg.local_port || !cfg.remote_host || !cfg.remote_port || !cfg.usr || !cfg.pwd)
return console.error('Usage of parameters:\n'
+ '-local_host host\t' + 'Listening address. Default: localhost. (* means all interfaces)\n'
+ '-local_port port\t' + 'Listening port. Default: 8080\n'
+ '-remote_host host\t' + 'Real proxy/PAC server address\n'
+ '-remote_port port\t' + 'Real proxy/PAC server port. Default: 8080\n'
+ '-usr user\t\t' + 'Real proxy/PAC server user id\n'
+ '-pwd password\t\t' + 'Real proxy/PAC user password\n'
+ '-as_pac_server true/false\t' + 'Treat real proxy/PAC server as a PAC server. Default: false\n'
+ '\n'
+ '-is_remote_https true/false\t' + 'Talk to real proxy/PAC server with HTTPS. Default: false\n'
+ '-ignore_https_cert true/false\t' + 'ignore error when verify certificate of real proxy/PAC server. Default: false\n'
+ '-are_remotes_in_pac_https true/false\t' + 'Talk to proxy servers defined in PAC with HTTPS. Default: false\n'
);
if (cfg.as_pac_server && (cfg.local_host === '*' || cfg.local_host === '0.0.0.0' || cfg.local_host === '::')) {
return console.error('when use as a PAC server, the local_host parameter must be a definite address');
}
console.log('Using parameters: ' + JSON.stringify(cfg, null, ' '));
cfg.buf_proxy_basic_auth = Buffer.from('Proxy-Authorization: Basic ' + Buffer.from(cfg.usr + ':' + cfg.pwd).toString('base64'));
if (cfg.as_pac_server) {
createPacServer(cfg.local_host, cfg.local_port, cfg.remote_host, cfg.remote_port, cfg.buf_proxy_basic_auth, cfg.is_remote_https, cfg.ignore_https_cert, cfg.are_remotes_in_pac_https);
} else {
createPortForwarder(cfg.local_host, cfg.local_port, cfg.remote_host, cfg.remote_port, cfg.buf_proxy_basic_auth, cfg.is_remote_https, cfg.ignore_https_cert);
}
}
var CR = 0xd, LF = 0xa, BUF_CR = Buffer.from([0xd]), BUF_CR_LF_CR_LF = Buffer.from([0xd, 0xa, 0xd, 0xa]),
BUF_LF_LF = Buffer.from([0xa, 0xa]), BUF_PROXY_CONNECTION_CLOSE = Buffer.from('Proxy-Connection: close');
var STATE_NONE = 0, STATE_FOUND_LF = 1, STATE_FOUND_LF_CR = 2;
function createPortForwarder(local_host, local_port, remote_host, remote_port, buf_proxy_basic_auth, is_remote_https, ignore_https_cert) {
net.createServer({allowHalfOpen: true}, function (socket) {
var realCon = (is_remote_https ? tls : net).connect({
port: remote_port, host: remote_host, allowHalfOpen: true,
rejectUnauthorized: !ignore_https_cert /*not used when is_remote_https false*/
});
realCon.on('data', function (buf) {
//console.log('<<<<' + (Date.t=new Date()) + '.' + Date.t.getMilliseconds() + '\n' + buf.toString('ascii'));
socket.write(buf);
realCon.__haveGotData = true;
}).on('end', function () {
socket.end();
if (!realCon.__haveGotData && !realCon.__haveShownError) {
console.error('[LocalProxy(:' + local_port + ')][Connection to ' + remote_host + ':' + remote_port + '] Error: ended by remote peer');
realCon.__haveShownError = true;
}
}).on('close', function () {
socket.end();
if (!realCon.__haveGotData && !realCon.__haveShownError) {
console.error('[LocalProxy(:' + local_port + ')][Connection to ' + remote_host + ':' + remote_port + '] Error: reset by remote peer');
realCon.__haveShownError = true;
}
}).on('error', function (err) {
console.error('[LocalProxy(:' + local_port + ')][Connection to ' + remote_host + ':' + remote_port + '] ' + err);
realCon.__haveShownError = true;
});
var parser = new HTTPParser(HTTPParser.REQUEST);
parser[HTTPParser.kOnHeadersComplete] = function (versionMajor, versionMinor, headers, method,
url, statusCode, statusMessage, upgrade,
shouldKeepAlive) {
//console.log('---- kOnHeadersComplete----');
//console.log(arguments);
parser.__is_headers_complete = true;
parser.__upgrade = upgrade;
parser.__method = method;
};
//parser[HTTPParser.kOnMessageComplete] = function () {
// console.log('---- kOnMessageComplete----');
// console.log(arguments);
//};
var state = STATE_NONE;
socket.on('data', function (buf) {
if (!parser) {
realCon.write(buf);
return
}
//console.log('[' + remote_host + ':' + remote_port + ']>>>>' + (Date.t = new Date()) + '.' + Date.t.getMilliseconds() + '\n' + buf.toString('ascii'));
//var ret = parser.execute(buf);
//console.log('\n\n----parser result: ' + ret + ' buf len:' + buf.length);
//realCon.write(buf);
//return;
var buf_ary = [], unsavedStart = 0, buf_len = buf.length;
for (var i = 0; i < buf_len; i++) {
//find first LF
if (state === STATE_NONE) {
if (buf[i] === LF) {
state = STATE_FOUND_LF;
}
continue;
}
//find second CR LF or LF
if (buf[i] === LF) {
parser.__is_headers_complete = false;
parser.execute(buf.slice(unsavedStart, i + 1));
if (parser.__is_headers_complete) {
buf_ary.push(buf.slice(unsavedStart, buf[i - 1] === CR ? i - 1 : i));
//console.log('insert auth header');
buf_ary.push(buf_proxy_basic_auth);
buf_ary.push(state === STATE_FOUND_LF_CR ? BUF_CR_LF_CR_LF : BUF_LF_LF);
// stop intercepting packets if encountered TLS and WebSocket handshake
if (parser.__method === 5 /*CONNECT*/ || parser.__upgrade) {
parser.close();
parser = null;
buf_ary.push(buf.slice(i + 1));
realCon.write(Buffer.concat(buf_ary));
state = STATE_NONE;
return;
}
unsavedStart = i + 1;
state = STATE_NONE;
}
else {
state = STATE_FOUND_LF;
}
}
else if (buf[i] === CR && state === STATE_FOUND_LF) {
state = STATE_FOUND_LF_CR;
} else {
state = STATE_NONE;
}
}
if (unsavedStart < buf_len) {
buf = buf.slice(unsavedStart, buf_len);
parser.execute(buf);
buf_ary.push(buf);
}
realCon.write(Buffer.concat(buf_ary));
}).on('end', cleanup).on('close', cleanup).on('error', function (err) {
if (!socket.__cleanup) {
console.error('[LocalProxy(:' + local_port + ')][Incoming connection] ' + err);
}
});
function cleanup() {
socket.__cleanup = true;
if (parser) {
parser.close();
parser = null;
}
realCon.end();
}
}).on('error', function (err) {
console.error('[LocalProxy(:' + local_port + ')] ' + err);
process.exit(1);
}).listen(local_port, local_host === '*' ? undefined : local_host, function () {
console.log('[LocalProxy(:' + local_port + ')] OK: forward http://' + local_host + ':' + local_port + ' to ' + ' to http' + (is_remote_https ? 's' : '') + '://' + remote_host + ':' + remote_port);
});
}
var proxyAddrMap = {};
function createPacServer(local_host, local_port, remote_host, remote_port, buf_proxy_basic_auth, is_remote_https, ignore_https_cert, are_remotes_in_pac_https) {
http.createServer(function (req, res) {
var internal_req = url.parse(req.url);
internal_req.host = remote_host;
internal_req.port = remote_port;
req.headers['host'] = remote_host + ':' + remote_port;
if (!req.headers['authorization']) {
req.headers['authorization'] = buf_proxy_basic_auth.slice('Proxy-Authorization: '.length).toString();
}
internal_req.headers = req.headers;
internal_req.rejectUnauthorized = !ignore_https_cert; //only used for SSL
(is_remote_https ? https : http).get(internal_req, function (internal_res) {
delete internal_res.headers['content-length'];
delete internal_res.headers['transfer-encoding'];
res.writeHead(internal_res.statusCode, internal_res.headers);
res.__haveWrittenData = true;
var buf_ary = [];
internal_res.on('data', function (buf) {
// console.log('<<<<' + (Date.t=new Date()) + '.' + Date.t.getMilliseconds() + '\n' + buf.toString('ascii'));
buf_ary.push(buf);
}).on('end', function () {
var s = Buffer.concat(buf_ary).toString();
buf_ary = [];
s = s.replace(/\bPROXY\s+([^'":;\s]+):(\d+)/g, function (_, host, port) {
var remoteAddr = host + ':' + port;
var _local_port = proxyAddrMap[remoteAddr];
if (!_local_port) {
_local_port = local_port + Object.keys(proxyAddrMap).length + 1;
proxyAddrMap[remoteAddr] = _local_port;
createPortForwarder(local_host, _local_port, host, Number(port), buf_proxy_basic_auth, are_remotes_in_pac_https, ignore_https_cert);
}
return 'PROXY ' + local_host + ':' + _local_port;
});
//console.log('return patched pac');
res.end(s);
}).on('error', function (err) {
res.end();
console.error('[LocalPAC][Reading response from ' + remote_host + ':' + remote_port + '] ' + err);
});
}).on('error', function (err) {
if (!res.__haveWrittenData) {
res.statusCode = 500;
res.end();
}
console.error('[LocalPAC][Connection to ' + remote_host + ':' + remote_port + '] ' + err);
});
res.on('error', function (err) {
console.error('[LocalPAC][Writing response] ' + err);
});
}).on('error', function (err) {
console.error('[LocalPAC] ' + err);
process.exit(1);
}).listen(local_port, local_host === '*' ? undefined : local_host, function () {
console.log('[LocalPAC] OK: forward http://' + local_host + ':' + local_port + ' to http' + (is_remote_https ? 's' : '') + '://' + remote_host + ':' + remote_port);
});
}
main();