forked from alibaba/anyproxy
-
Notifications
You must be signed in to change notification settings - Fork 5
/
bin.js
executable file
·163 lines (145 loc) · 6.16 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/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('-I, --no-webinterface', 'disable WebInterface')
.option('-P, --no-persistence', 'disable data persistence, will also disable WebInterface')
.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('-A, --auth', 'enable proxy authorization')
.option('-a, --adduser <user> <password> [<user> <password>]...', 'add proxy user')
.option('-m, --moduser <user> <password>', 'modify proxy user password')
.option('-d, --deluser <user> [user]...', 'delete proxy user')
.option('-F, --authFile [value]', 'save proxy auth data to a specified file, if not specified use __dirname/auth.db')
.option('install', '[alpha] install node modules')
.parse(process.argv);
var authFile = program.authFile ? path.resolve(process.cwd(), program.authFile) : path.resolve(__dirname, 'auth.db');
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 if (program.adduser || program.deluser || program.moduser){
var db, Datastore = require('nedb'),
args = program.args,
users = [];
try {
db = new Datastore({filename: authFile, autoload: true});
db.ensureIndex({ fieldName: 'username', unique: true});
db.persistence.setAutocompactionInterval(5001);
console.log("proxy auth file : " + authFile);
} catch (e) {
console.log('create proxy auth database file failed, ' + e);
process.exit(-1);
}
if (program.adduser) {
args.unshift(program.adduser);
if (args.length % 2 !== 0) {
console.log('add user failed, every user must be set a password.');
process.exit(-1);
}
for (var i = 0; i < args.length; i += 2) {
users.push({username: args[i], password: args[i + 1]});
}
db.insert(users, function (err) {
err && console.log('create proxy user failed: ' + err.message);
});
} else if (program.deluser) {
args.push(program.deluser);
args.forEach(function (user) {
db.remove({username: user}, {}, function (err) {
err && console.log('delete user failed: ' + err.message);
});
});
} else {
if (args.length <= 0) {
console.log('user password must be set.');
process.exit(-1);
}
db.update({username: program.moduser}, {username: program.moduser, password: args[0]}, {upsert: true}, function (err) {
err && console.log('modify proxy user failed: ' + err.message);
});
}
// db.persistence.compactDatafile();
db.persistence.stopAutocompaction();
}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,
rule : ruleModule,
disableWebInterface : !program.persistence || !program.webinterface,
disablePersistence : !program.persistence,
setAsGlobalProxy : program.global,
interceptHttps : program.intercept,
silent : program.silent,
auth : program.auth,
authFile : authFile
});
}