forked from uku/Unblock-Youku
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
executable file
·269 lines (233 loc) · 8.75 KB
/
server.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
#!/usr/bin/env node
/*
* Copyright (C) 2012 - 2014 Bo Zhu http://zhuzhu.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var net = require('net');
var url = require('url');
var util = require('util');
var domain = require('domain');
var cluster = require('cluster');
var http = require('http');
http.globalAgent.maxSockets = Infinity;
// mikeal/request might have a bug
// always obtain TLS errors when proxying over https
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
var colors = require('colors');
var request = require('request');
var validator = require('validator');
var shared_tools = require('../shared/tools');
var server_utils = require('./utils');
function check_url_format(str) {
if (!validator.isURL(str, {
protocols: ['http', 'https'],
require_tld: true,
require_protocol: true
})) {
return 'Input Error: Invalid URL format'.red;
}
// return; // return nothing
}
var opts = require("nomnom")
// .help('Run a redirect server for the Chrome extension')
.option('nolog', {
flag: true,
help: 'Do not print request logs'
})
.option('proxy', {
type: 'string',
metavar: 'HTTP://IP_ADDRESS:PORT',
help: 'Send requests through a remote proxy',
callback: check_url_format
})
.option('bak_proxy', {
type: 'string',
help: 'Set another proxy server for backup',
metavar: 'HTTP://IP_ADDRESS:PORT',
callback: check_url_format
})
.option('pac_proxy', {
type: 'string',
help: 'Use a different proxy in the PAC file',
metavar: 'HTTP://IP_ADDRESS:PORT',
callback: check_url_format
})
.parse();
// console.log(opts);
var local_port = process.env.PORT || 8888;
if ((!opts.proxy) && process.env.PROXY_ADDR) {
opts.proxy = process.env.PROXY_ADDR;
}
if ((!opts.bak_proxy) && process.env.BAK_PROXY_ADDR) {
opts.bak_proxy = process.env.BAK_PROXY_ADDR;
}
if (!opts.pac_proxy) {
if (process.env.PAC_PROXY_ADDR) {
opts.pac_proxy = process.env.PAC_PROXY_ADDR;
} else if (opts.proxy) {
opts.pac_proxy = opts.proxy;
}
}
//opts.proxy = 'http://abc.com:8888';
if ((opts.proxy && (check_url_format(opts.proxy) !== undefined))
|| (opts.bak_proxy && (check_url_format(opts.bak_proxy) !== undefined))
|| (opts.pac_proxy && (check_url_format(opts.pac_proxy) !== undefined))) {
util.error('ENV Error: Invalid URL format'.red);
process.exit(400);
}
//console.log(opts);
var pac_file_content = null;
var pac_proxy_obj = null;
if (opts.pac_proxy) {
pac_proxy_obj = url.parse(opts.pac_proxy);
if (!pac_proxy_obj.host || !pac_proxy_obj.protocol) {
util.error('Invalid URL format parsed'.red);
process.exit(401);
}
pac_file_content = server_utils.generate_pac_file(pac_proxy_obj.host, pac_proxy_obj.protocol);
} else {
pac_file_content = 'function FindProxyForURL(url, host) {return "DIRECT";}';
}
//console.log(pac_file_content);
function http_req_handler(client_request, client_response) {
if (!opts.nolog) {
console.log(
'[ub.uku.js] '
+ client_request.connection.remoteAddress + ': '
+ client_request.method + ' ' + client_request.url.underline);
}
if ((client_request.url === '/proxy.pac' || client_request.url === '/pac.pac')
|| (!shared_tools.string_starts_with(client_request.url, '/proxy'))) {
server_utils.static_responses(client_request, client_response, pac_file_content);
}
var target = server_utils.get_real_target(client_request.url);
if (!target.host) {
client_response.writeHead(403, {
'Cache-Control': 'public, max-age=14400'
});
client_response.end();
return;
}
// access control
if (!server_utils.is_valid_url(target.href)) {
client_response.writeHead(403, {
'Cache-Control': 'public, max-age=14400'
});
client_response.end();
return;
}
var proxy_request_headers = server_utils.filter_request_headers(client_request.headers);
proxy_request_headers.Host = target.host;
var proxy_request_options = {
encoding: null, // disable auto `buffer.toString()` in package "request"
url: target.href,
// url: 'http://httpbin.org/status/400',
method: client_request.method,
headers: proxy_request_headers
};
if (opts.proxy) {
proxy_request_options.proxy = opts.proxy;
}
function handle_proxy_request_error() {
try {
client_response.writeHead(500);
client_response.end('Error occurred, sorry.');
} catch (er) {
util.error('[ub.uku.js] Error sending 500', er, client_request.url);
}
}
function handle_proxy_response_data(response, payload) {
var filtered_headers = server_utils.filter_response_headers(response.headers);
client_response.writeHead(response.statusCode, filtered_headers);
client_response.end(payload);
}
request(proxy_request_options, function(err, resp, body) {
if (err) {
util.error('[ub.uku.js] first proxy_request error: (' + err.code + ') ' + err.message, client_request.url);
if (opts.bak_proxy) { // retry the request
setTimeout(function() { // run it in 1s
proxy_request_options.proxy = opts.bak_proxy;
request(proxy_request_options, function(err, resp, body) {
if (err) {
util.error('[ub.uku.js] second proxy_request error: (' + err.code + ') ' + err.message, client_request.url, err.stack);
handle_proxy_request_error();
} else {
handle_proxy_response_data(resp, body);
}
});
}, 1000);
} else {
handle_proxy_request_error();
}
} else {
handle_proxy_response_data(resp, body);
}
});
}
if (cluster.isMaster) {
var num_CPUs = require('os').cpus().length;
// num_CPUs = 1;
var i;
for (i = 0; i < num_CPUs; i++) {
cluster.fork();
// one note here
// the fork() in nodejs is not the same as the fork() in C
// fork() in nodejs will run the whole code from beginning
// not from where it is invoked
}
cluster.on('listening', function(worker, addr_port) {
// use ub.uku.js as keyword for searching in log files
util.log('[ub.uku.js] Worker ' + worker.process.pid
+ ' is now connected to ' + addr_port.address + ':' + addr_port.port);
});
cluster.on('exit', function(worker, code, signal) {
if (signal) {
util.log('[ub.uku.js] Worker ' + worker.process.pid
+ ' was killed by signal: ' + signal);
} else if (code !== 0) {
util.error('[ub.uku.js] Worker ' + worker.process.pid
+ ' exited with error code: ' + code);
// respawn a worker process when one dies
cluster.fork();
} else {
util.error('[ub.uku.js] Worker ' + worker.process.pid + ' exited.');
}
});
console.log('The redirect server is running...'.green);
if (opts.proxy) {
console.log(('Using the remote proxy: ' + opts.proxy).green);
} else {
console.log('Running locally without remote proxies.'.green);
}
if (opts.bak_proxy) {
console.log(('Using the backup proxy: ' + opts.bak_proxy).green);
}
if (opts.pac_proxy) {
console.log(('The PAC file uses the proxy: ' + opts.pac_proxy).green);
}
} else if (cluster.isWorker) {
var ubuku_server = http.createServer();
ubuku_server.on('request', http_req_handler);
ubuku_server.listen(local_port, '0.0.0.0').on('error', function(err) {
if (err.code === 'EADDRINUSE') {
util.error('[ub.uku.js] Port number is already in use! Exiting now...');
process.exit();
}
});
}
process.on('uncaughtException', function(err) {
util.error('[ub.uku.js] Caught uncaughtException: ' + err, err.stack);
process.exit(213);
});