anyproxy/proxy.js

208 lines
7.7 KiB
JavaScript
Raw Permalink Normal View History

2014-09-03 01:10:26 +08:00
try{
GLOBAL.util = require('./lib/util');
2014-09-03 01:10:26 +08:00
}catch(e){}
2014-08-11 16:43:14 +08:00
var http = require('http'),
2015-04-20 09:39:27 +08:00
https = require('https'),
fs = require('fs'),
async = require("async"),
url = require('url'),
program = require('commander'),
color = require('colorful'),
certMgr = require("./lib/certMgr"),
getPort = require("./lib/getPort"),
requestHandler = require("./lib/requestHandler"),
Recorder = require("./lib/recorder"),
logUtil = require("./lib/log"),
2015-04-25 10:28:40 +08:00
wsServer = require("./lib/wsServer"),
webInterface = require("./lib/webInterface"),
2015-04-20 09:39:27 +08:00
inherits = require("util").inherits,
util = require("./lib/util"),
path = require("path"),
juicer = require('juicer'),
events = require("events"),
express = require("express"),
ip = require("ip"),
ThrottleGroup = require("stream-throttle").ThrottleGroup,
iconv = require('iconv-lite'),
2015-04-25 10:28:40 +08:00
Buffer = require('buffer').Buffer;
2015-04-20 09:39:27 +08:00
2014-09-04 11:22:35 +08:00
var T_TYPE_HTTP = 0,
2014-08-27 17:42:42 +08:00
T_TYPE_HTTPS = 1,
DEFAULT_PORT = 8001,
2015-02-24 15:57:27 +08:00
DEFAULT_WEB_PORT = 8002, // port for web interface
DEFAULT_WEBSOCKET_PORT = 8003, // internal web socket for web interface, not for end users
2014-09-18 17:42:01 +08:00
DEFAULT_CONFIG_PORT = 8088,
2014-08-27 17:42:42 +08:00
DEFAULT_HOST = "localhost",
DEFAULT_TYPE = T_TYPE_HTTP;
2014-08-11 16:43:14 +08:00
2014-09-06 09:50:49 +08:00
var default_rule = require('./lib/rule_default');
2014-12-08 17:13:41 +08:00
2014-09-04 11:22:35 +08:00
//option
//option.type : 'http'(default) or 'https'
//option.port : 8001(default)
//option.hostname : localhost(default)
2014-09-18 17:42:01 +08:00
//option.rule : ruleModule
2014-09-18 13:36:27 +08:00
//option.webPort : 8002(default)
//option.socketPort : 8003(default)
2014-09-18 17:42:01 +08:00
//option.webConfigPort : 8088(default)
2014-10-23 11:07:02 +08:00
//option.dbFile : null(default)
//option.throttle : null(default)
2014-11-04 17:23:52 +08:00
//option.disableWebInterface
//option.silent : false(default)
2015-01-22 10:16:19 +08:00
//option.interceptHttps ,internal param for https
2014-09-04 11:22:35 +08:00
function proxyServer(option){
option = option || {};
var self = this,
2014-11-04 17:23:52 +08:00
proxyType = /https/i.test(option.type || DEFAULT_TYPE) ? T_TYPE_HTTPS : T_TYPE_HTTP ,
proxyPort = option.port || DEFAULT_PORT,
proxyHost = option.hostname || DEFAULT_HOST,
proxyRules = option.rule || default_rule,
proxyWebPort = option.webPort || DEFAULT_WEB_PORT, //port for web interface
socketPort = option.socketPort || DEFAULT_WEBSOCKET_PORT, //port for websocket
proxyConfigPort = option.webConfigPort || DEFAULT_CONFIG_PORT, //port to ui config server
disableWebInterface = !!option.disableWebInterface,
ifSilent = !!option.silent;
if(ifSilent){
logUtil.setPrintStatus(false);
}
2014-10-23 11:07:02 +08:00
2015-01-22 10:16:19 +08:00
if(!!option.interceptHttps){
default_rule.setInterceptFlag(true);
//print a tip when using https features in Node < v0.12
var nodeVersion = Number(process.version.match(/^v(\d+\.\d+)/)[1]);
if(nodeVersion < 0.12){
logUtil.printLog(color.red("node >= v0.12 is required when trying to intercept HTTPS requests :("), logUtil.T_ERR);
}
2015-01-22 10:16:19 +08:00
}
2014-10-29 16:20:18 +08:00
if(option.throttle){
logUtil.printLog("throttle :" + option.throttle + "kb/s");
2014-10-29 16:20:18 +08:00
GLOBAL._throttle = new ThrottleGroup({rate: 1024 * parseInt(option.throttle) }); // rate - byte/sec
}
2014-09-05 13:53:12 +08:00
requestHandler.setRules(proxyRules); //TODO : optimize calling for set rule
2014-08-14 22:58:30 +08:00
self.httpProxyServer = null;
2014-08-13 11:51:39 +08:00
2014-08-14 22:58:30 +08:00
async.series(
[
//clear cache dir, prepare recorder
function(callback){
util.clearCacheDir(function(){
if(option.dbFile){
GLOBAL.recorder = new Recorder({filename: option.dbFile});
}else{
GLOBAL.recorder = new Recorder();
}
callback();
});
},
2014-08-13 11:51:39 +08:00
//creat proxy server
2014-08-11 16:43:14 +08:00
function(callback){
if(proxyType == T_TYPE_HTTPS){
2014-08-11 17:48:32 +08:00
certMgr.getCertificate(proxyHost,function(err,keyContent,crtContent){
2014-08-11 16:43:14 +08:00
if(err){
callback(err);
}else{
2014-08-14 22:58:30 +08:00
self.httpProxyServer = https.createServer({
2014-08-11 16:43:14 +08:00
key : keyContent,
cert: crtContent
2014-08-13 11:51:39 +08:00
},requestHandler.userRequestHandler);
2014-08-11 16:43:14 +08:00
callback(null);
}
});
}else{
2014-08-14 22:58:30 +08:00
self.httpProxyServer = http.createServer(requestHandler.userRequestHandler);
2014-08-11 16:43:14 +08:00
callback(null);
}
2014-08-11 16:43:14 +08:00
},
2015-04-25 10:28:40 +08:00
//handle CONNECT request for https over http
2014-08-11 16:43:14 +08:00
function(callback){
2014-08-14 22:58:30 +08:00
self.httpProxyServer.on('connect',requestHandler.connectReqHandler);
2015-04-25 10:28:40 +08:00
callback(null);
},
2015-04-25 10:28:40 +08:00
//start proxy server
function(callback){
2014-08-14 22:58:30 +08:00
self.httpProxyServer.listen(proxyPort);
2014-08-11 16:43:14 +08:00
callback(null);
2014-09-04 11:22:35 +08:00
},
2015-04-25 10:28:40 +08:00
//start web socket service
function(callback){
self.ws = new wsServer({port : socketPort});
callback(null);
2015-04-25 10:28:40 +08:00
},
2014-09-04 11:22:35 +08:00
//start web interface
function(callback){
2014-11-04 17:23:52 +08:00
if(disableWebInterface){
logUtil.printLog('web interface is disabled');
2014-11-04 17:23:52 +08:00
}else{
2015-04-25 10:28:40 +08:00
var config = {
port : proxyWebPort,
wsPort : socketPort,
2015-07-07 17:31:56 +08:00
userRule : proxyRules,
2015-04-25 10:28:40 +08:00
ip : ip.address()
2015-04-20 09:39:27 +08:00
};
self.webServerInstance = new webInterface(config);
2015-04-20 09:39:27 +08:00
}
callback(null);
},
2014-09-11 10:22:08 +08:00
2015-04-20 09:39:27 +08:00
//server status manager
function(callback){
2014-09-11 10:22:08 +08:00
2015-04-20 09:39:27 +08:00
process.on("exit",function(code){
logUtil.printLog('AnyProxy is about to exit with code: ' + code, logUtil.T_ERR);
process.exit();
});
process.on("uncaughtException",function(err){
logUtil.printLog('Caught exception: ' + (err.stack || err), logUtil.T_ERR);
2015-04-20 09:39:27 +08:00
process.exit();
});
callback(null);
2014-08-11 16:43:14 +08:00
}
],
//final callback
function(err,result){
if(!err){
2015-04-25 10:28:40 +08:00
var webTip,webUrl;
webUrl = "http://" + ip.address() + ":" + proxyWebPort +"/";
webTip = "GUI interface started at : " + webUrl;
logUtil.printLog(color.green(webTip));
2014-12-08 17:13:41 +08:00
var tipText = (proxyType == T_TYPE_HTTP ? "Http" : "Https") + " proxy started at " + color.bold(ip.address() + ":" + proxyPort);
logUtil.printLog(color.green(tipText));
2014-08-11 16:43:14 +08:00
}else{
2014-08-13 11:51:39 +08:00
var tipText = "err when start proxy server :(";
logUtil.printLog(color.red(tipText), logUtil.T_ERR);
logUtil.printLog(err, logUtil.T_ERR);
2014-08-11 16:43:14 +08:00
}
}
);
2014-08-14 22:58:30 +08:00
2014-09-04 11:22:35 +08:00
self.close = function(){
self.httpProxyServer && self.httpProxyServer.close();
self.ws && self.ws.closeAll();
self.webServerInstance && self.webServerInstance.server && self.webServerInstance.server.close();
logUtil.printLog("server closed :" + proxyHost + ":" + proxyPort);
2014-09-04 11:22:35 +08:00
}
2014-08-11 16:43:14 +08:00
}
2014-08-14 22:58:30 +08:00
module.exports.proxyServer = proxyServer;
2014-08-14 10:59:49 +08:00
module.exports.generateRootCA = certMgr.generateRootCA;
module.exports.isRootCAFileExists = certMgr.isRootCAFileExists;
2015-07-22 13:26:22 +08:00
module.exports.setRules = requestHandler.setRules;