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 ..``
|
||||
|
||||
### 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);
|
||||
|
||||
}
|
64
index.js
64
index.js
@ -9,47 +9,29 @@ var http = require('http'),
|
||||
createCert= require("./lib/createCert"),
|
||||
program = require('commander');
|
||||
|
||||
program
|
||||
.option('-u, --url [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);
|
||||
var T_TYPE_HTTP = 0,
|
||||
T_TYPE_HTTPS = 1,
|
||||
DEFAULT_PORT = 8001,
|
||||
DEFAULT_HOST = "localhost",
|
||||
DEFAULT_TYPE = T_TYPE_HTTP;
|
||||
|
||||
var PROXY_PORT = program.port || 8001,
|
||||
T_PROXY_HTTP = 0,
|
||||
T_PROXY_HTTPS = 1,
|
||||
PROXY_TYPE = /https/i.test(program.type)? T_PROXY_HTTPS : T_PROXY_HTTP;
|
||||
HOSTNAME = program.host || "localhost";
|
||||
|
||||
|
||||
var count = 0;
|
||||
if(program.clear){
|
||||
exec("rm -rf ./cert/tmpCert",function(){
|
||||
console.log("certificates cleared");
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
}else if(program.help && false){ //TODO
|
||||
program.help();
|
||||
|
||||
}else{
|
||||
var serverMgrInstance = new serverMgr(),
|
||||
httpProxyServer;
|
||||
|
||||
function startServer(type, port, hostname){
|
||||
var proxyType = /https/i.test(type || DEFAULT_TYPE) ? T_TYPE_HTTPS : T_TYPE_HTTP ,
|
||||
proxyPort = port || DEFAULT_PORT,
|
||||
proxyHost = hostname || DEFAULT_HOST;
|
||||
|
||||
async.series([
|
||||
//creat server
|
||||
function(callback){
|
||||
if(PROXY_TYPE == T_PROXY_HTTP){
|
||||
httpProxyServer = http.createServer(dealProxyUserHttpReq);
|
||||
callback(null);
|
||||
}else{
|
||||
|
||||
var keyFile = "./cert/tmpCert/__hostname.key".replace(/__hostname/,HOSTNAME),
|
||||
crtFile = "./cert/tmpCert/__hostname.crt".replace(/__hostname/,HOSTNAME);
|
||||
if(proxyType == T_TYPE_HTTPS){
|
||||
var keyFile = "./cert/tmpCert/__hostname.key".replace(/__hostname/,proxyHost),
|
||||
crtFile = "./cert/tmpCert/__hostname.crt".replace(/__hostname/,proxyHost);
|
||||
|
||||
if(!fs.existsSync(keyFile) || !fs.existsSync(crtFile)){
|
||||
createCert(HOSTNAME,function(){
|
||||
createCert(proxyHost,function(){
|
||||
httpProxyServer = https.createServer({
|
||||
key : fs.readFileSync(keyFile),
|
||||
cert: fs.readFileSync(crtFile)
|
||||
@ -63,19 +45,26 @@ if(program.clear){
|
||||
},dealProxyUserHttpReq);
|
||||
callback(null);
|
||||
}
|
||||
|
||||
}else{
|
||||
httpProxyServer = http.createServer(dealProxyUserHttpReq);
|
||||
callback(null);
|
||||
|
||||
}
|
||||
},
|
||||
|
||||
//
|
||||
function(callback){
|
||||
//listen CONNECT method for https over http
|
||||
httpProxyServer.on('connect',dealProxyConnectReq);
|
||||
httpProxyServer.listen(PROXY_PORT);
|
||||
httpProxyServer.listen(proxyPort);
|
||||
callback(null);
|
||||
|
||||
}],function(err,result){
|
||||
}],
|
||||
|
||||
//final callback
|
||||
function(err,result){
|
||||
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{
|
||||
console.log("err when start proxy server :(");
|
||||
console.log(err);
|
||||
@ -108,7 +97,6 @@ function dealProxyUserHttpReq(req,res){
|
||||
directReq.end();
|
||||
}
|
||||
|
||||
|
||||
function dealProxyConnectReq(req, socket, head){
|
||||
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