-
Notifications
You must be signed in to change notification settings - Fork 140
/
proxy.js
231 lines (199 loc) · 7.36 KB
/
proxy.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
'use strict';
const http = require('http'),
https = require('https'),
async = require('async'),
color = require('colorful'),
certMgr = require('./lib/certMgr'),
Recorder = require('./lib/recorder'),
logUtil = require('./lib/log'),
util = require('./lib/util'),
events = require('events'),
ThrottleGroup = require('stream-throttle').ThrottleGroup;
const T_TYPE_HTTP = 'http',
T_TYPE_HTTPS = 'https',
DEFAULT_CONFIG_PORT = 8088,
DEFAULT_TYPE = T_TYPE_HTTP;
const PROXY_STATUS_INIT = 'INIT';
const PROXY_STATUS_READY = 'READY';
const PROXY_STATUS_CLOSED = 'CLOSED';
/**
*
* @class ProxyServer
* @extends {events.EventEmitter}
*/
class ProxyServer extends events.EventEmitter {
/**
* Creates an instance of ProxyServer.
*
* @param {object} config - configs
* @param {number} config.port - port of the proxy server
* @param {object} [config.rule=null] - rule module to use
* @param {string} [config.type=http] - type of the proxy server, could be 'http' or 'https'
* @param {strign} [config.hostname=localhost] - host name of the proxy server, required when this is an https proxy
* @param {object} [config.webInterface] - config of the web interface
* @param {boolean} [config.webInterface.enable=false] - if web interface is enabled
* @param {number} [config.webInterface.webPort=8002] - http port of the web interface
* @param {number} [config.webInterface.wsPort] - web socket port of the web interface
* @param {number} [config.throttle] - speed limit in kb/s
* @param {boolean} [config.forceProxyHttps=false] - if proxy all https requests
* @param {boolean} [config.silent=false] - if keep the console silent
* @param {boolean} [config.dangerouslyIgnoreUnauthorized=false] - if ignore unauthorized server response
*
* @memberOf ProxyServer
*/
constructor(config) {
super();
config = config || {};
this.status = PROXY_STATUS_INIT;
this.proxyPort = config.port;
this.proxyType = /https/i.test(config.type || DEFAULT_TYPE) ? T_TYPE_HTTPS : T_TYPE_HTTP;
this.proxyHostName = config.hostname || 'localhost';
this.proxyWebinterfaceConfig = config.webInterface;
this.proxyConfigPort = config.webConfigPort || DEFAULT_CONFIG_PORT; //TODO : port to ui config server
if (config.forceProxyHttps && !certMgr.ifRootCAFileExists()) {
throw new Error('root CA not found. can not intercept https'); // TODO : give a reference to user
} else if (this.proxyType === T_TYPE_HTTPS && !config.hostname) {
throw new Error('hostname is required in https proxy');
} else if (!this.proxyPort) {
throw new Error('proxy port is required');
}
// ??
// currentRule.setInterceptFlag(true);
// logUtil.printLog(color.blue("The WebSocket will not work properly in the https intercept mode :("), logUtil.T_TIP);
this.httpProxyServer = null;
this.requestHandler = null;
// copy the rule to keep the original proxyRule independent
this.proxyRule = config.rule || {};
if (config.silent) {
logUtil.setPrintStatus(false);
}
if (config.throttle) {
logUtil.printLog('throttle :' + config.throttle + 'kb/s');
const rate = parseInt(config.throttle, 10);
if (rate < 1) {
throw new Error('Invalid throttle rate value, should be positive integer');
}
global._throttle = new ThrottleGroup({ rate: 1024 * rate }); // rate - byte/sec
}
// init recorder
this.recorder = new Recorder();
global.recorder = this.recorder; // TODO 消灭这个global
// init request handler
const RequestHandler = util.freshRequire('./requestHandler');
this.requestHandler = new RequestHandler({
forceProxyHttps: !!config.forceProxyHttps,
dangerouslyIgnoreUnauthorized: !!config.dangerouslyIgnoreUnauthorized
}, this.proxyRule, this.recorder);
}
/**
* start the proxy server
*
* @returns ProxyServer
*
* @memberOf ProxyServer
*/
start() {
const self = this;
if (self.status !== PROXY_STATUS_INIT) {
throw new Error('server status is not PROXY_STATUS_INIT, can not run start()');
}
async.series(
[
//creat proxy server
function (callback) {
if (self.proxyType === T_TYPE_HTTPS) {
certMgr.getCertificate(self.proxyHostName, (err, keyContent, crtContent) => {
if (err) {
callback(err);
} else {
self.httpProxyServer = https.createServer({
key: keyContent,
cert: crtContent
}, self.requestHandler.userRequestHandler);
callback(null);
}
});
} else {
self.httpProxyServer = http.createServer(self.requestHandler.userRequestHandler);
callback(null);
}
},
//handle CONNECT request for https over http
function (callback) {
self.httpProxyServer.on('connect', self.requestHandler.connectReqHandler);
callback(null);
},
//start proxy server
function (callback) {
self.httpProxyServer.listen(self.proxyPort);
callback(null);
},
//start web socket service
// function(callback){
// self.ws = new wsServer({ port : self.proxyWsPort }, self.recorder);
// callback(null);
// },
//set proxy rule
// function(callback){
// if (self.interceptHttps) {
// self.proxyRule.setInterceptFlag(true);
// }
// callback(null);
// },
//start web interface
function (callback) {
if (self.proxyWebinterfaceConfig && self.proxyWebinterfaceConfig.enable) {
const webInterface = require('./lib/webInterface');
self.webServerInstance = new webInterface(self.proxyWebinterfaceConfig, self);
}
callback(null);
},
],
//final callback
(err, result) => {
if (!err) {
const tipText = (self.proxyType === T_TYPE_HTTP ? 'Http' : 'Https') + ' proxy started on port ' + self.proxyPort;
logUtil.printLog(color.green(tipText));
if (self.webServerInstance) {
const webTip = 'web interface started on port ' + self.webServerInstance.webPort;
logUtil.printLog(color.green(webTip));
}
self.status = PROXY_STATUS_READY;
self.emit('ready');
} else {
const tipText = 'err when start proxy server :(';
logUtil.printLog(color.red(tipText), logUtil.T_ERR);
logUtil.printLog(err, logUtil.T_ERR);
self.emit('error', {
error: err
});
}
}
);
return self;
}
/**
* close the proxy server
*
* @returns ProxyServer
*
* @memberOf ProxyServer
*/
close() {
// clear recorder cache
this.recorder && this.recorder.clear();
this.httpProxyServer && this.httpProxyServer.close();
this.webServerInstance && this.webServerInstance.close();
this.recorder = null;
this.httpProxyServer = null;
this.webServerInstance = null;
this.status = PROXY_STATUS_CLOSED;
logUtil.printLog('server closed ' + this.proxyHostName + ':' + this.proxyPort);
return this
}
}
module.exports.ProxyServer = ProxyServer;
module.exports.utils = {
systemProxyMgr: require('./lib/systemProxyMgr'),
certMgr,
};