forked from ashi009/bestroutetb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·290 lines (279 loc) · 7.9 KB
/
cli.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#! /usr/bin/env node
require('apollojs');
var fs = require('fs');
var util = require('util');
var pathUtil = require('path');
var packageInfo = require('./package');
var async = require('async');
var yargs = require('yargs');
var chalk = require('chalk');
var Db = require('./lib/db');
var Logger = require('./lib/logger');
var Minifier = require('./lib/minifier');
var Profile = require('./lib/profile');
var Evaluator = require('./lib/evaluator');
var argv = yargs
.usage('Usage: $0 [options]')
.options('route.net', {
string: true,
default: 'CN',
describe: 'Subnets that should be routed to ISP gateway'
})
.options('route.vpn', {
string: true,
default: 'US,GB,JP,HK',
describe: 'Subnets that should be routed to VPN gateway'
})
.options('p', {
alias: 'profile',
string: true,
describe: 'Output format profile: `' +
Profile.getAvailableNames().join('`, `') + '`'
})
.config('c')
.options('c', {
alias: 'config',
describe: 'Configuration file path'
})
.options('o', {
alias: 'output',
string: true,
describe: 'Output file path'
})
.options('r', {
alias: 'report',
string: true,
describe: 'Report file path'
})
.options('header', {
string: true,
describe: 'Header of the output file'
})
.options('footer', {
string: true,
describe: 'Footer of the output file'
})
.options('rule-format', {
string: true,
describe: 'String used to format a rule when `--profile=custom`'
})
.options('gateway.net', {
string: true,
describe: 'Substitute for `%gw` when rule is using ISP gateway'
})
.options('gateway.vpn', {
string: true,
describe: 'Substitute for `%gw` when rule is using VPN gateway'
})
.options('default-gateway', {
boolean: true,
default: true,
describe: 'Output directive for default route (0.0.0.0/0)'
})
.options('group-gateway', {
boolean: true,
default: undefined,
describe: 'Group rules by gateway'
})
.options('group-header', {
string: true,
describe: 'Header of each group'
})
.options('group-footer', {
string: true,
describe: 'Footer of each group'
})
.options('group-name.net', {
string: true,
describe: 'Substitute for `%name` when group is route to ISP gateway'
})
.options('group-name.vpn', {
string: true,
describe: 'Substitute for `%name` when group is route to VPN gateway'
})
.options('f', {
alias: 'force',
boolean: true,
describe: 'Force to overwrite existing files'
})
.options('update', {
boolean: true,
default: undefined,
describe: 'Force update delegation data, `--no-update` to use stale data'
})
.options('v', {
alias: 'verbose',
count: true,
describe: 'Verbose output'
})
.options('s', {
alias: 'silent',
boolean: true,
describe: 'Silent mode'
})
.addHelpOpt('h')
.alias('h', 'help')
.version(util.format('%s %s (%s)\n', packageInfo.name, packageInfo.version,
Db.getInstance().version), 'V')
.alias('V', 'version')
.check(function(argv) {
if (argv.verbose && argv.silent)
throw '`--verbose` conflicts with `--silent`';
if (argv.output && !argv.profile)
throw '`--profile` must be specified when generating output';
})
.check(function(argv) {
if (argv.profile &&
Profile.getAvailableNames().indexOf(argv.profile) === -1) {
var path = pathUtil.resolve(argv.profile);
if (!fs.existsSync(path))
throw 'Cannot find given profile.';
argv.profile = path;
}
})
.check(function(argv) {
for (var gateway in argv.route)
argv.route[gateway] = []
.concat(argv.route[gateway])
.join(',')
.split(',');
})
.wrap(process.stdout.columns)
.argv;
var logger = new Logger({
verbose: argv.verbose,
silent: argv.silent
});
function writeFileSync(scope, path, data) {
logger.info(scope, 'generating %s', chalk.cyan(path));
if (fs.existsSync(path)) {
if (!argv.force)
return logger.error(scope,
'%s already exists, use `-f` to continue', chalk.cyan(path));
logger.warn(scope, 'will overwrite %s', chalk.cyan(path));
}
if (Function.isFunction(data))
data = data();
fs.writeFileSync(path, data.content || data);
if (data.mode) {
logger.info(scope, 'setting mode of %s to %s',
chalk.cyan(path), chalk.cyan(data.mode.toString(8)));
fs.chmodSync(path, data.mode);
}
logger.info(scope, 'created %s', chalk.cyan(path));
}
var jobs = {
// db
db: function(callback) {
var scope = 'db';
logger.info(scope, 'loading');
if (argv.update === false)
logger.warn(scope, 'using stale data');
else if (argv.update === true)
logger.info(scope, 'force update');
Db.update({
force: argv.update === true,
useStale: argv.update === false,
progressBar: logger.getProgressBar(scope,
'updating... :percent :currentB/:totalB :etas')
}, function(err) {
if (err) {
err.scope = scope;
return callback(err);
}
logger.info(scope, 'version %s', Db.getInstance().version);
callback();
});
},
// minify
rules: ['db', function(callback) {
var scope = 'minify';
logger.info(scope, 'generating route table');
logger.verbose(scope, 'options %j', argv.route);
try {
var rules = Minifier.minify(argv.route);
logger.info(scope, '%d rules generated', rules.length);
callback(null, rules);
} catch(err) {
err.scope = scope;
callback(err);
}
}],
// profile
profile: ['db', function(callback) {
if (!argv.profile)
return callback();
var scope = 'profile';
logger.info(scope, 'loading');
logger.verbose(scope, 'using %s', argv.profile);
try {
var profile = Profile.load(argv.profile);
logger.info(scope, 'loaded');
callback(null, profile);
} catch(err) {
err.scope = scope;
callback(err);
}
}],
// generate
output: ['rules', 'profile', function(callback, results) {
if (!argv.output)
return callback();
var scope = 'profile:' + argv.profile;
var options = {
header: argv.header,
footer: argv.footer,
ruleFormat: argv.ruleFormat,
gateway: argv.gateway,
defaultGateway: argv.defaultGateway,
groupGateway: argv.groupGateway,
groupHeader: argv.groupHeader,
groupFooter: argv.groupFooter,
groupName: argv.groupName
};
logger.debug(scope, 'options %j', options);
var profile = results.profile;
var resolved = profile.resolveTargets(argv.output);
logger.verbose(scope, 'resolved targets %j', resolved);
for (var name in resolved) {
try {
writeFileSync(scope, resolved[name],
profile.generate.bind(profile, name, results.rules, options));
} catch(err) {
err.scope = scope;
return callback(err);
}
}
callback();
}],
// report
report: ['rules', function(callback, results) {
if (!argv.report)
return callback();
var scope = 'eval';
logger.info(scope, 'analysing rules');
var report = Evaluator.evaluate(results.rules);
try {
writeFileSync(scope, argv.report,
Evaluator.generateCSV.bind(Evaluator, report));
} catch(err) {
err.scope = scope;
return callback(err);
}
callback();
}]
};
logger.debug('cli', 'argv parsed %j', argv);
var startTime = new Date();
logger.verbose('cli', 'start at %s', startTime);
async.auto(jobs, function(err) {
if (err) {
logger.error(err.scope, err.message);
logger.debug(err.scope, err.stack);
process.exit(1);
} else {
var endTime = new Date();
logger.info('cli', 'finished at %s, %ds', endTime,
(endTime - startTime) / 1000);
}
});