forked from avwo/whistle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
287 lines (281 loc) · 8.59 KB
/
index.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
var express = require('express');
var http = require('http');
var https = require('https');
var socks = require('sockx');
var extend = require('extend');
var EventEmitter = require('events');
var util = require('./util');
var logger = require('./util/logger');
var rules = require('./rules');
var setupHttps = require('./https').setup;
var httpsUtil = require('./https/ca');
var rulesUtil = require('./rules/util');
var initDataServer = require('./util/data-server');
var initLogServer = require('./util/log-server');
var pluginMgr = require('./plugins');
var config = require('./config');
var loadService = require('./service');
var initSocketMgr = require('./socket-mgr');
var tunnelProxy = require('./tunnel');
var upgradeProxy = require('./upgrade');
var proc = require('./util/process');
var perf = require('./util/perf');
var loadCert = require('./https/load-cert');
var common = require('./util/common');
function handleClientError(err, socket) {
if (!socket.writable) {
return socket.destroy(err);
}
var errCode = err && err.code;
var statusCode = errCode === 'HPE_HEADER_OVERFLOW' ? 431 : 400;
var stack = util.getErrorStack(
'clientError: Bad request' + (errCode ? ' (' + errCode + ')' : '')
);
socket.end('HTTP/1.1 ' + statusCode + ' Bad Request\r\n\r\n' + stack);
}
function proxy(callback, _server) {
var app = express();
var server = _server || http.createServer();
var proxyEvents = new EventEmitter();
var middlewares = ['./init', '../biz']
.concat(require('./inspectors'))
.concat(config.middlewares)
.concat(require('./handlers'));
server.timeout = config.timeout;
proxyEvents.config = config;
proxyEvents.server = server;
app.disable('x-powered-by');
app.logger = logger;
middlewares.forEach(function (mw) {
mw && app.use((typeof mw == 'string' ? require(mw) : mw).bind(proxyEvents));
});
server.on('clientError', handleClientError);
pluginMgr.setProxy(proxyEvents);
perf.setProxy(proxyEvents);
initSocketMgr(proxyEvents);
setupHttps(server, proxyEvents);
exportInterfaces(proxyEvents);
tunnelProxy(server, proxyEvents);
upgradeProxy(server);
initDataServer(proxyEvents);
initLogServer(proxyEvents);
rulesUtil.setup(proxyEvents);
var properties = rulesUtil.properties;
if (config.disableAllRules) {
properties.set('disabledAllRules', true);
} else if (config.disableAllRules === false) {
properties.set('disabledAllRules', false);
}
if (config.disableAllPlugins) {
properties.set('disabledAllPlugins', true);
} else if (config.disableAllPlugins === false) {
properties.set('disabledAllPlugins', false);
}
if (config.allowMultipleChoice) {
properties.set('allowMultipleChoice', true);
} else if (config.allowMultipleChoice === false) {
properties.set('allowMultipleChoice', false);
}
rulesUtil.addValues(config.values, config.replaceExistValue);
rulesUtil.addRules(config.rules, config.replaceExistRule);
config.debug && rules.disableDnsCache();
var count = _server ? 1 : 2;
var execCallback = function () {
if (--count === 0) {
process.whistleStarted = true;
process.emit('whistleStarted');
typeof callback === 'function' && callback.call(server, proxyEvents);
}
};
!_server && util.getBoundIp(config.host, function (host) {
util.checkPort(!config.INADDR_ANY && !host && config.port, function () {
config.host = host;
server.listen(config.port, host, execCallback);
});
});
var createNormalServer = function (port, httpModule, opts) {
if (!port) {
return;
}
++count;
var optionServer = httpModule.createServer(opts);
var isHttps = !!opts;
proxyEvents[isHttps ? 'httpsServer' : 'httpServer'] = optionServer;
optionServer.on('request', function (req, res) {
req.isHttps = isHttps;
app.handle(req, res);
});
optionServer.isHttps = isHttps;
tunnelProxy(optionServer, proxyEvents, isHttps ? 1 : 2);
upgradeProxy(optionServer);
optionServer.on('clientError', handleClientError);
util.getBoundIp(
config[isHttps ? 'httpsHost' : 'httpHost'],
function (host) {
util.checkPort(!config.INADDR_ANY && !host && port, function () {
optionServer.listen(port, host, execCallback);
});
}
);
};
createNormalServer(config.httpPort, http);
createNormalServer(
config.httpsPort,
https,
extend(
{
SNICallback: function (servername, callback) {
var curUrl = 'https://' + servername;
loadCert(
{
isHttpsServer: true,
fullUrl: curUrl,
curUrl: curUrl,
useSNI: true,
headers: {},
servername: servername,
serverName: servername,
commonName: httpsUtil.getDomain(servername)
},
function () {
httpsUtil.SNICallback(servername, callback);
}
);
}
},
httpsUtil.createCertificate('*.wproxy.org')
)
);
if (config.socksPort) {
++count;
var boundHost;
var socksServer = socks.createServer(function (info, accept, deny) {
var dstPort = info.dstPort;
var dstAddr = info.dstAddr;
var connPath = dstAddr + ':' + dstPort;
var headers = { host: connPath };
headers['x-whistle-server'] = 'socks';
if (
config.socksMode ||
(dstPort != 80 &&
dstPort != 443 &&
(dstPort != config.port ||
(!util.isLocalAddress(dstAddr) && !config.isLocalUIUrl(dstAddr))))
) {
headers[config.WHISTLE_POLICY_HEADER] = 'weakTunnel';
}
var client = http.request({
method: 'CONNECT',
agent: false,
path: connPath,
host: boundHost,
port: config.port,
headers: headers
});
var destroy = function () {
if (client) {
client.abort();
client = null;
deny();
}
};
client.on('error', destroy);
client.on('connect', function (res, socket) {
socket.on('error', destroy);
if (res.statusCode != 200) {
return destroy();
}
var reqSock = accept(true);
if (reqSock) {
reqSock.pipe(socket).pipe(reqSock);
} else {
destroy();
}
});
client.end();
});
proxyEvents.socksServer = socksServer;
util.getBoundIp(config.socksHost, function (host) {
boundHost = host || '127.0.0.1';
util.checkPort(
!config.INADDR_ANY && !host && config.socksPort,
function () {
socksServer.listen(config.socksPort, host, execCallback);
}
);
socksServer.useAuth(socks.auth.None());
});
}
require('../biz/init')(proxyEvents, function () {
server.on('request', app);
execCallback();
});
return proxyEvents;
}
function exportInterfaces(obj) {
obj.getWhistlePath = common.getWhistlePath;
obj.rules = rules;
obj.util = util;
obj.rulesUtil = rulesUtil;
obj.rulesMgr = rules;
obj.httpsUtil = httpsUtil;
obj.pluginMgr = pluginMgr;
obj.logger = logger;
obj.loadService = loadService;
obj.setAuth = config.setAuth;
obj.setUIHost = config.setUIHost;
obj.setPluginUIHost = config.setPluginUIHost;
obj.socketMgr = initSocketMgr;
obj.getRuntimeInfo = function () {
return proc;
};
obj.getShadowRules = function () {
return config.shadowRules;
};
obj.setShadowRules = function (shadowRules) {
if (typeof shadowRules === 'string') {
config.shadowRules = shadowRules;
rulesUtil.parseRules();
}
};
return obj;
}
var HTTP2_ERR_RE = /^ERR_HTTP2_/;
function handleGlobalException(err) {
var code = err && err.code;
if (
!config.diagnose &&
(code === 'EPIPE' ||
HTTP2_ERR_RE.test(code) ||
code === 'ENETUNREACH' ||
code === 'ERR_HTTP_TRAILER_INVALID' ||
code === 'ERR_INTERNAL_ASSERTION' ||
code === 'ERR_INVALID_ARG_TYPE' ||
(err && /finishwrite/i.test(err.message)))
) {
return;
}
if (
!err ||
(code !== 'ERR_IPC_CHANNEL_CLOSED' && code !== 'ERR_IPC_DISCONNECTED')
) {
var stack = util.getErrorStack(err);
common.writeLogSync('\r\n' + stack + '\r\n');
/*eslint no-console: "off"*/
console.error(stack);
if (
typeof process.handleUncauthtWhistleErrorMessage === 'function' &&
process.handleUncauthtWhistleErrorMessage(stack, err) === false
) {
return;
}
}
setTimeout(function () {
process.exit(1);
}, 360);
}
process.on('unhandledRejection', handleGlobalException);
process.on('uncaughtException', handleGlobalException);
rulesUtil.setPluginMgr(pluginMgr);
rulesUtil.parseRules();
module.exports = exportInterfaces(proxy);