mirror of
https://github.com/alibaba/anyproxy.git
synced 2025-05-10 14:58:27 +00:00
update bin.js
This commit is contained in:
parent
c93fe5a596
commit
5e269eac91
@ -33,7 +33,7 @@ What this proxy do is to generate and replace a temporary cert for any domain if
|
|||||||
* ``cd ..``
|
* ``cd ..``
|
||||||
|
|
||||||
### start server
|
### start server
|
||||||
* ``node index.js``
|
* ``node bin.js`` , or use ``node bin.js --help`` for help
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
20
bin.js
Normal file
20
bin.js
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
var program = require('commander'),
|
||||||
|
mainProxy = require("./index.js");
|
||||||
|
|
||||||
|
program
|
||||||
|
.option('-u, --host [value]', 'hostname for https proxy, localhost for default')
|
||||||
|
.option('-t, --type [value]', 'http|https,http for default')
|
||||||
|
.option('-p, --port [value]', 'proxy port, 8001 for default')
|
||||||
|
.option('-c, --clear', 'clear all the tmp certificates')
|
||||||
|
.parse(process.argv);
|
||||||
|
|
||||||
|
if(program.clear){
|
||||||
|
exec("rm -rf ./cert/tmpCert",function(){
|
||||||
|
console.log("certificates cleared");
|
||||||
|
process.exit(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
}else{
|
||||||
|
mainProxy.startServer(program.type,program.port, program.host);
|
||||||
|
|
||||||
|
}
|
68
index.js
68
index.js
@ -9,47 +9,29 @@ var http = require('http'),
|
|||||||
createCert= require("./lib/createCert"),
|
createCert= require("./lib/createCert"),
|
||||||
program = require('commander');
|
program = require('commander');
|
||||||
|
|
||||||
program
|
var T_TYPE_HTTP = 0,
|
||||||
.option('-u, --url [value]', 'hostname for https proxy, localhost for default')
|
T_TYPE_HTTPS = 1,
|
||||||
.option('-t, --type [value]', 'http|https,http for default')
|
DEFAULT_PORT = 8001,
|
||||||
.option('-p, --port [value]', 'proxy port, 8001 for default')
|
DEFAULT_HOST = "localhost",
|
||||||
.option('-c, --clear', 'clear all the tmp certificates')
|
DEFAULT_TYPE = T_TYPE_HTTP;
|
||||||
.parse(process.argv);
|
|
||||||
|
|
||||||
var PROXY_PORT = program.port || 8001,
|
var serverMgrInstance = new serverMgr(),
|
||||||
T_PROXY_HTTP = 0,
|
httpProxyServer;
|
||||||
T_PROXY_HTTPS = 1,
|
|
||||||
PROXY_TYPE = /https/i.test(program.type)? T_PROXY_HTTPS : T_PROXY_HTTP;
|
|
||||||
HOSTNAME = program.host || "localhost";
|
|
||||||
|
|
||||||
|
function startServer(type, port, hostname){
|
||||||
var count = 0;
|
var proxyType = /https/i.test(type || DEFAULT_TYPE) ? T_TYPE_HTTPS : T_TYPE_HTTP ,
|
||||||
if(program.clear){
|
proxyPort = port || DEFAULT_PORT,
|
||||||
exec("rm -rf ./cert/tmpCert",function(){
|
proxyHost = hostname || DEFAULT_HOST;
|
||||||
console.log("certificates cleared");
|
|
||||||
process.exit(0);
|
|
||||||
});
|
|
||||||
|
|
||||||
}else if(program.help && false){ //TODO
|
|
||||||
program.help();
|
|
||||||
|
|
||||||
}else{
|
|
||||||
var serverMgrInstance = new serverMgr(),
|
|
||||||
httpProxyServer;
|
|
||||||
|
|
||||||
async.series([
|
async.series([
|
||||||
//creat server
|
//creat server
|
||||||
function(callback){
|
function(callback){
|
||||||
if(PROXY_TYPE == T_PROXY_HTTP){
|
if(proxyType == T_TYPE_HTTPS){
|
||||||
httpProxyServer = http.createServer(dealProxyUserHttpReq);
|
var keyFile = "./cert/tmpCert/__hostname.key".replace(/__hostname/,proxyHost),
|
||||||
callback(null);
|
crtFile = "./cert/tmpCert/__hostname.crt".replace(/__hostname/,proxyHost);
|
||||||
}else{
|
|
||||||
|
|
||||||
var keyFile = "./cert/tmpCert/__hostname.key".replace(/__hostname/,HOSTNAME),
|
|
||||||
crtFile = "./cert/tmpCert/__hostname.crt".replace(/__hostname/,HOSTNAME);
|
|
||||||
|
|
||||||
if(!fs.existsSync(keyFile) || !fs.existsSync(crtFile)){
|
if(!fs.existsSync(keyFile) || !fs.existsSync(crtFile)){
|
||||||
createCert(HOSTNAME,function(){
|
createCert(proxyHost,function(){
|
||||||
httpProxyServer = https.createServer({
|
httpProxyServer = https.createServer({
|
||||||
key : fs.readFileSync(keyFile),
|
key : fs.readFileSync(keyFile),
|
||||||
cert: fs.readFileSync(crtFile)
|
cert: fs.readFileSync(crtFile)
|
||||||
@ -63,25 +45,32 @@ if(program.clear){
|
|||||||
},dealProxyUserHttpReq);
|
},dealProxyUserHttpReq);
|
||||||
callback(null);
|
callback(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}else{
|
||||||
|
httpProxyServer = http.createServer(dealProxyUserHttpReq);
|
||||||
|
callback(null);
|
||||||
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
//
|
|
||||||
function(callback){
|
function(callback){
|
||||||
//listen CONNECT method for https over http
|
//listen CONNECT method for https over http
|
||||||
httpProxyServer.on('connect',dealProxyConnectReq);
|
httpProxyServer.on('connect',dealProxyConnectReq);
|
||||||
httpProxyServer.listen(PROXY_PORT);
|
httpProxyServer.listen(proxyPort);
|
||||||
callback(null);
|
callback(null);
|
||||||
|
|
||||||
}],function(err,result){
|
}],
|
||||||
|
|
||||||
|
//final callback
|
||||||
|
function(err,result){
|
||||||
if(!err){
|
if(!err){
|
||||||
console.log( (PROXY_TYPE == T_PROXY_HTTP ? "Http" : "Https") + " proxy started at port " + PROXY_PORT);
|
console.log( (proxyType == T_TYPE_HTTP ? "Http" : "Https") + " proxy started at port " + proxyPort);
|
||||||
}else{
|
}else{
|
||||||
console.log("err when start proxy server :(");
|
console.log("err when start proxy server :(");
|
||||||
console.log(err);
|
console.log(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function dealProxyUserHttpReq(req,res){
|
function dealProxyUserHttpReq(req,res){
|
||||||
@ -108,7 +97,6 @@ function dealProxyUserHttpReq(req,res){
|
|||||||
directReq.end();
|
directReq.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function dealProxyConnectReq(req, socket, head){
|
function dealProxyConnectReq(req, socket, head){
|
||||||
var hostname = req.url.split(":")[0];
|
var hostname = req.url.split(":")[0];
|
||||||
|
|
||||||
@ -131,3 +119,5 @@ function dealProxyConnectReq(req, socket, head){
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.exports.startServer = startServer;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user