anyproxy/bin.js

57 lines
1.8 KiB
JavaScript
Raw Normal View History

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')
2014-10-23 11:07:02 +08:00
.option('-t, --type [value]', 'http|https, http for default')
2014-08-11 10:28:16 +08:00
.option('-p, --port [value]', 'proxy port, 8001 for default')
2014-10-23 11:07:02 +08:00
.option('-f, --file [value]', 'save request data to a specified file, will use in-memory db if not specified')
.option('-r, --rule [value]', 'path for rule file,')
2014-08-13 11:51:39 +08:00
.option('-g, --root [value]', 'generate root CA')
2014-10-31 18:51:24 +08:00
.option('-l, --throttle [value]', 'throttle speed in kb/s (kbyte / sec)')
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({
2014-10-23 11:07:02 +08:00
type : program.type,
port : program.port,
2014-11-06 10:46:32 +08:00
hostname : program.host,
2014-10-23 11:07:02 +08:00
dbFile : program.file,
2014-10-29 16:20:18 +08:00
throttle : program.throttle,
2014-11-04 17:23:52 +08:00
rule : ruleModule,
disableWebInterface:false
2014-09-04 11:22:35 +08:00
});
}
2014-08-29 09:16:48 +08:00