anyproxy/proxy.js

93 lines
3.2 KiB
JavaScript
Raw Normal View History

2014-08-11 16:43:14 +08:00
var http = require('http'),
https = require('https'),
fs = require('fs'),
net = require('net'),
async = require("async"),
url = require('url'),
exec = require('child_process').exec,
program = require('commander'),
2014-08-13 11:51:39 +08:00
color = require('colorful'),
certMgr = require("./lib/certMgr"),
2014-08-19 15:31:28 +08:00
getPort = require("./lib/getPort"),
2014-08-11 16:43:14 +08:00
requestHandler = require("./lib/requestHandler");
var T_TYPE_HTTP = 0,
T_TYPE_HTTPS = 1,
DEFAULT_PORT = 8001,
DEFAULT_HOST = "localhost",
DEFAULT_TYPE = T_TYPE_HTTP;
2014-08-14 22:58:30 +08:00
function proxyServer(type, port, hostname,ruleFile){
var self = this,
proxyType = /https/i.test(type || DEFAULT_TYPE) ? T_TYPE_HTTPS : T_TYPE_HTTP ,
2014-08-11 16:43:14 +08:00
proxyPort = port || DEFAULT_PORT,
2014-08-14 22:58:30 +08:00
proxyHost = hostname || DEFAULT_HOST;
self.httpProxyServer = null;
2014-08-19 15:31:28 +08:00
self.close = function(){
self.httpProxyServer && self.httpProxyServer.close();
console.log(color.green("server closed :" + proxyHost + ":" + proxyPort));
}
2014-08-11 16:43:14 +08:00
2014-08-18 19:51:15 +08:00
if(ruleFile){
2014-08-14 10:59:49 +08:00
if(fs.existsSync(ruleFile)){
2014-08-18 19:51:15 +08:00
try{ //for abs path
requestHandler.setRules(require(ruleFile)); //todo : require path
}catch(e){ //for relative path
requestHandler.setRules(require("./" + ruleFile));
}
2014-08-14 10:59:49 +08:00
console.log(color.green("rule file loaded"));
}else{
console.log(color.red("can not find rule file"));
}
}
2014-08-13 11:51:39 +08:00
2014-08-14 22:58:30 +08:00
async.series(
[
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);
}
},
//listen CONNECT method for https over http
function(callback){
2014-08-14 22:58:30 +08:00
self.httpProxyServer.on('connect',requestHandler.connectReqHandler);
self.httpProxyServer.listen(proxyPort);
2014-08-11 16:43:14 +08:00
callback(null);
}
],
//final callback
function(err,result){
if(!err){
2014-08-13 11:51:39 +08:00
var tipText = (proxyType == T_TYPE_HTTP ? "Http" : "Https") + " proxy started at port " + proxyPort;
console.log(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 :(";
console.log(color.red(tipText));
2014-08-11 16:43:14 +08:00
console.log(err);
}
}
);
2014-08-14 22:58:30 +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;