mirror of
https://github.com/alibaba/anyproxy.git
synced 2025-04-24 08:41:31 +00:00
update to async
This commit is contained in:
parent
770d495440
commit
cf77e0ec1f
49
index.js
49
index.js
@ -2,23 +2,36 @@ var http = require('http'),
|
|||||||
https = require('https'),
|
https = require('https'),
|
||||||
fs = require('fs'),
|
fs = require('fs'),
|
||||||
net = require('net'),
|
net = require('net'),
|
||||||
|
async = require("async"),
|
||||||
url = require('url'),
|
url = require('url'),
|
||||||
exec = require('child_process').exec,
|
exec = require('child_process').exec,
|
||||||
serverMgr = require("./lib/serverMgr"),
|
serverMgr = require("./lib/serverMgr"),
|
||||||
createCert= require("./lib/createCert");
|
createCert= require("./lib/createCert"),
|
||||||
|
program = require('commander');
|
||||||
|
|
||||||
var PROXY_PORT = 8001,
|
program
|
||||||
|
.option('-h, --host [value]', 'hostname,use for https proxy. localhost for default')
|
||||||
|
.option('-t, --type [value]', 'http|https,http for default')
|
||||||
|
.option('-p, --port [value]', 'proxy port , 8001 for default')
|
||||||
|
.parse(process.argv);
|
||||||
|
|
||||||
|
var PROXY_PORT = program.port || 8001,
|
||||||
T_PROXY_HTTP = 0,
|
T_PROXY_HTTP = 0,
|
||||||
T_PROXY_HTTPS = 1,
|
T_PROXY_HTTPS = 1,
|
||||||
PROXY_TYPE = T_PROXY_HTTPS,
|
PROXY_TYPE = /https/i.test(program.type)? T_PROXY_HTTPS : T_PROXY_HTTP;
|
||||||
HOSTNAME = "localhost";
|
HOSTNAME = program.host || "localhost";
|
||||||
|
|
||||||
var serverMgrInstance = new serverMgr(),
|
var serverMgrInstance = new serverMgr(),
|
||||||
httpProxyServer;
|
httpProxyServer;
|
||||||
|
|
||||||
if(PROXY_TYPE == T_PROXY_HTTP){
|
|
||||||
|
async.series([
|
||||||
|
//creat server
|
||||||
|
function(callback){
|
||||||
|
if(PROXY_TYPE == T_PROXY_HTTP){
|
||||||
httpProxyServer = http.createServer(dealProxyUserHttpReq);
|
httpProxyServer = http.createServer(dealProxyUserHttpReq);
|
||||||
}else{
|
callback(null);
|
||||||
|
}else{
|
||||||
|
|
||||||
var keyFile = "./cert/tmpCert/__hostname.key".replace(/__hostname/,HOSTNAME),
|
var keyFile = "./cert/tmpCert/__hostname.key".replace(/__hostname/,HOSTNAME),
|
||||||
crtFile = "./cert/tmpCert/__hostname.crt".replace(/__hostname/,HOSTNAME);
|
crtFile = "./cert/tmpCert/__hostname.crt".replace(/__hostname/,HOSTNAME);
|
||||||
@ -29,20 +42,34 @@ if(PROXY_TYPE == T_PROXY_HTTP){
|
|||||||
key : fs.readFileSync(keyFile),
|
key : fs.readFileSync(keyFile),
|
||||||
cert: fs.readFileSync(crtFile)
|
cert: fs.readFileSync(crtFile)
|
||||||
},dealProxyUserHttpReq);
|
},dealProxyUserHttpReq);
|
||||||
|
callback(null);
|
||||||
});
|
});
|
||||||
}else{
|
}else{
|
||||||
httpProxyServer = https.createServer({
|
httpProxyServer = https.createServer({
|
||||||
key : fs.readFileSync(keyFile),
|
key : fs.readFileSync(keyFile),
|
||||||
cert: fs.readFileSync(crtFile)
|
cert: fs.readFileSync(crtFile)
|
||||||
},dealProxyUserHttpReq);
|
},dealProxyUserHttpReq);
|
||||||
|
callback(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
|
||||||
//listen CONNECT method for https over http
|
//
|
||||||
httpProxyServer.on('connect',dealProxyConnectReq);
|
function(callback){
|
||||||
httpProxyServer.listen(PROXY_PORT);
|
//listen CONNECT method for https over http
|
||||||
console.log( (PROXY_TYPE == T_PROXY_HTTP ? "Http" : "Https") + " proxy started at port " + PROXY_PORT);
|
httpProxyServer.on('connect',dealProxyConnectReq);
|
||||||
|
httpProxyServer.listen(PROXY_PORT);
|
||||||
|
callback(null);
|
||||||
|
|
||||||
|
}],function(err,result){
|
||||||
|
if(!err){
|
||||||
|
console.log( (PROXY_TYPE == T_PROXY_HTTP ? "Http" : "Https") + " proxy started at port " + PROXY_PORT);
|
||||||
|
}else{
|
||||||
|
console.log("err when start proxy server :(");
|
||||||
|
console.log(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
function dealProxyUserHttpReq(req,res){
|
function dealProxyUserHttpReq(req,res){
|
||||||
var urlPattern = url.parse(req.url);
|
var urlPattern = url.parse(req.url);
|
||||||
|
@ -4,7 +4,8 @@
|
|||||||
"description": "https proxy over http",
|
"description": "https proxy over http",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"async": "~0.9.0"
|
"async": "~0.9.0",
|
||||||
|
"commander": "~2.3.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {},
|
"devDependencies": {},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user