mirror of
https://github.com/alibaba/anyproxy.git
synced 2025-04-24 04:01:27 +00:00
update to async
This commit is contained in:
parent
770d495440
commit
cf77e0ec1f
83
index.js
83
index.js
@ -2,47 +2,74 @@ var http = require('http'),
|
||||
https = require('https'),
|
||||
fs = require('fs'),
|
||||
net = require('net'),
|
||||
async = require("async"),
|
||||
url = require('url'),
|
||||
exec = require('child_process').exec,
|
||||
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_HTTPS = 1,
|
||||
PROXY_TYPE = T_PROXY_HTTPS,
|
||||
HOSTNAME = "localhost";
|
||||
PROXY_TYPE = /https/i.test(program.type)? T_PROXY_HTTPS : T_PROXY_HTTP;
|
||||
HOSTNAME = program.host || "localhost";
|
||||
|
||||
var serverMgrInstance = new serverMgr(),
|
||||
httpProxyServer;
|
||||
|
||||
if(PROXY_TYPE == T_PROXY_HTTP){
|
||||
httpProxyServer = http.createServer(dealProxyUserHttpReq);
|
||||
}else{
|
||||
|
||||
var keyFile = "./cert/tmpCert/__hostname.key".replace(/__hostname/,HOSTNAME),
|
||||
crtFile = "./cert/tmpCert/__hostname.crt".replace(/__hostname/,HOSTNAME);
|
||||
async.series([
|
||||
//creat server
|
||||
function(callback){
|
||||
if(PROXY_TYPE == T_PROXY_HTTP){
|
||||
httpProxyServer = http.createServer(dealProxyUserHttpReq);
|
||||
callback(null);
|
||||
}else{
|
||||
|
||||
if(!fs.existsSync(keyFile) || !fs.existsSync(crtFile)){
|
||||
createCert(HOSTNAME,function(){
|
||||
httpProxyServer = https.createServer({
|
||||
key : fs.readFileSync(keyFile),
|
||||
cert: fs.readFileSync(crtFile)
|
||||
},dealProxyUserHttpReq);
|
||||
});
|
||||
}else{
|
||||
httpProxyServer = https.createServer({
|
||||
key : fs.readFileSync(keyFile),
|
||||
cert: fs.readFileSync(crtFile)
|
||||
},dealProxyUserHttpReq);
|
||||
var keyFile = "./cert/tmpCert/__hostname.key".replace(/__hostname/,HOSTNAME),
|
||||
crtFile = "./cert/tmpCert/__hostname.crt".replace(/__hostname/,HOSTNAME);
|
||||
|
||||
if(!fs.existsSync(keyFile) || !fs.existsSync(crtFile)){
|
||||
createCert(HOSTNAME,function(){
|
||||
httpProxyServer = https.createServer({
|
||||
key : fs.readFileSync(keyFile),
|
||||
cert: fs.readFileSync(crtFile)
|
||||
},dealProxyUserHttpReq);
|
||||
callback(null);
|
||||
});
|
||||
}else{
|
||||
httpProxyServer = https.createServer({
|
||||
key : fs.readFileSync(keyFile),
|
||||
cert: fs.readFileSync(crtFile)
|
||||
},dealProxyUserHttpReq);
|
||||
callback(null);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
//
|
||||
function(callback){
|
||||
//listen CONNECT method for https over http
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//listen CONNECT method for https over http
|
||||
httpProxyServer.on('connect',dealProxyConnectReq);
|
||||
httpProxyServer.listen(PROXY_PORT);
|
||||
console.log( (PROXY_TYPE == T_PROXY_HTTP ? "Http" : "Https") + " proxy started at port " + PROXY_PORT);
|
||||
|
||||
);
|
||||
|
||||
function dealProxyUserHttpReq(req,res){
|
||||
var urlPattern = url.parse(req.url);
|
||||
|
@ -4,7 +4,8 @@
|
||||
"description": "https proxy over http",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"async": "~0.9.0"
|
||||
"async": "~0.9.0",
|
||||
"commander": "~2.3.0"
|
||||
},
|
||||
"devDependencies": {},
|
||||
"scripts": {
|
||||
|
Loading…
x
Reference in New Issue
Block a user