forked from alibaba/anyproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin.js
executable file
·101 lines (93 loc) · 3.74 KB
/
bin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env node
var program = require('commander'),
color = require('colorful'),
fs = require("fs"),
path = require("path"),
npm = require("npm"),
packageInfo = require("./package.json"),
util = require("./lib/util"),
logUtil = require("./lib/log");
program
.version(packageInfo.version)
.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('-w, --web [value]' , 'web GUI port, 8002 for default')
.option('-e, --ws [value]' , 'web socket port, 8003 for default')
.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,')
.option('-g, --root [value]', 'generate root CA')
.option('-l, --throttle [value]', 'throttle speed in kb/s (kbyte / sec)')
.option('-i, --intercept', 'intercept(decrypt) https requests when root CA exists')
.option('-s, --silent', 'do not print anything into terminal')
.option('-c, --clear', 'clear all the tmp certificates')
.option('-o, --global', 'set as global proxy for system')
.option('install', '[alpha] install node modules')
.parse(process.argv);
if(program.clear){
require("./lib/certMgr").clearCerts(function(){
console.log( color.green("all certs cleared") );
process.exit(0);
});
}else if(program.root){
require("./lib/certMgr").generateRootCA(function(){
process.exit(0);
});
}else if(program.install){
npm.load({
"prefix": process.env.NODE_PATH + '/anyproxy/'
}, function (er) {
npm.commands.install(program.args || [], function (er, data) {
if(er)throw er;
});
npm.registry.log.on("log", function (message) {});
});
}else{
var proxy = require("./proxy.js");
var ruleModule;
if(program.silent){
logUtil.setPrintStatus(false);
}
if(program.rule){
var ruleFilePath = path.resolve(process.cwd(),program.rule);
try{
if(fs.existsSync(ruleFilePath)){
ruleModule = require(ruleFilePath);
logUtil.printLog("rule file loaded :" + ruleFilePath);
}else{
var logText = color.red("can not find rule file at " + ruleFilePath);
logUtil.printLog(logText, logUtil.T_ERR);
}
}catch(e){
logUtil.printLog("failed to load rule file :" + e.toString(), logUtil.T_ERR);
}
}else{
//a feature for donghua.yan
//read rule file from a specific position
(function(){
try{
var anyproxyHome = path.join(util.getAnyProxyHome());
if(fs.existsSync(path.join(anyproxyHome,"rule_default.js"))){
ruleModule = require(path.join(anyproxyHome,"rule_default"));
}
if(fs.existsSync(path.join(process.cwd(),'rule.js'))){
ruleModule = require(path.join(process.cwd(),'rule'));
}
}catch(e){}
})();
}
new proxy.proxyServer({
type : program.type,
port : program.port,
hostname : program.host,
dbFile : program.file,
throttle : program.throttle,
webPort : program.web,
socketPort : program.ws,
rule : ruleModule,
disableWebInterface : false,
setAsGlobalProxy : program.global,
interceptHttps : program.intercept,
silent : program.silent
});
}