-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin.js
88 lines (75 loc) · 2.46 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
#!/usr/bin/env node
var program = require('commander'),
color = require('colorful'),
fs = require('fs'),
rule = require('./rules/rule.js'),
package = require('./package.json'),
proxy = require('./lib/proxy');
program
.version(package.version)
.option('-r, --rule [path]', 'path for rule file')
.option('-p, --port [int]', 'use specify port')
.parse(process.argv);
program.on('--help', function () {
console.log(' Examples:');
console.log('webproxy start');
console.log('webproxy start --rule rule.js');
console.log('webproxy start --port 9000');
});
var firstArgs = program.args[0];
var ruleFile = program.rule || null;
var port = program.port || 9000;
var absRulePath, ruleModule;
if (ruleFile) {
switch (ruleFile[0]) {
// /rule.js
case '/':
absRulePath = ruleFile;
break;
// ./rule.js
case '.':
absRulePath = process.cwd() + '/' + ruleFile.slice(2);
break;
// rule.js
default:
absRulePath = process.cwd() + '/' + ruleFile;
break;
}
if (fs.existsSync(ruleFile)) {
ruleModule = require(absRulePath);
}
}
if (!ruleModule) {
ruleModule = {};
ruleModule.request = function (req) {
return req;
};
ruleModule.response = function (req, res) {
return res;
}
}
if (firstArgs == 'start') {
proxy.start({
port:port,
//staticPort:9001,
//websocketPort:9002,
//useWebUI:true,
//printConsoleInfo:true,
useWebUI:false,
printConsoleInfo:false,
ruleModule:ruleModule
});
return;
}
//magenta', 'cyan'
console.log('\nWelcome to use '+ color.magenta('Webproxy') + ' ' + package.version);
console.log(color.yellow('*You can modify your http request/response in javascript file easily') + '\n');
console.log('Usage:\n');
console.log(' -p, --port [int] use specify port');
console.log(' -r, --rule [javascript file path] path for rule file');
console.log('\nExamples:\n');
console.log(' webproxy start');
console.log(' webproxy start --rule rule.js');
console.log(' webproxy start --port 9000');
console.log('\n\n'+ color.yellow('Author:human huang <[email protected]>'));
console.log(color.yellow('QQ交流群:415719701') +'\n');