2014-08-11 17:54:19 +08:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2014-08-14 10:59:49 +08:00
|
|
|
var program = require('commander'),
|
|
|
|
proxy = require("./proxy.js"),
|
2014-08-13 11:51:39 +08:00
|
|
|
color = require('colorful'),
|
|
|
|
fs = require("fs");
|
2014-08-11 10:28:16 +08:00
|
|
|
|
|
|
|
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')
|
2014-08-13 11:51:39 +08:00
|
|
|
.option('-r, --rule [value]', 'rule file to map localfiles')
|
|
|
|
.option('-g, --root [value]', 'generate root CA')
|
2014-08-11 10:28:16 +08:00
|
|
|
.option('-c, --clear', 'clear all the tmp certificates')
|
|
|
|
.parse(process.argv);
|
|
|
|
|
|
|
|
if(program.clear){
|
2014-08-11 16:43:14 +08:00
|
|
|
require("./lib/certMgr").clearCerts(function(){
|
2014-08-13 11:51:39 +08:00
|
|
|
console.log( color.green("all certs cleared") );
|
2014-08-11 10:28:16 +08:00
|
|
|
process.exit(0);
|
|
|
|
});
|
|
|
|
|
2014-08-13 11:51:39 +08:00
|
|
|
}else if(program.root){
|
|
|
|
require("./lib/certMgr").generateRootCA(function(){
|
|
|
|
process.exit(0);
|
|
|
|
});
|
2014-08-11 10:28:16 +08:00
|
|
|
}else{
|
2014-09-04 11:22:35 +08:00
|
|
|
var ruleModule;
|
|
|
|
|
|
|
|
if(program.rule){
|
|
|
|
if(fs.existsSync(program.rule)){
|
|
|
|
try{ //for abs path
|
|
|
|
ruleModule = require(program.rule);
|
|
|
|
}catch(e){ //for relative path
|
2014-09-06 10:38:47 +08:00
|
|
|
ruleModule = require(process.cwd() + '/' + program.rule.replace(/^\.\//,''));
|
2014-09-04 11:22:35 +08:00
|
|
|
}
|
|
|
|
console.log(color.green("rule file loaded"));
|
|
|
|
}else{
|
|
|
|
console.log(color.red("can not find rule file"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
new proxy.proxyServer({
|
|
|
|
type : program.type,
|
|
|
|
port : program.port,
|
|
|
|
hostname : program.hostname,
|
|
|
|
rule : ruleModule
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-08-29 09:16:48 +08:00
|
|
|
|