2017-08-09 14:44:24 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
2017-08-16 04:58:13 +00:00
|
|
|
const os = require('os');
|
2017-08-09 14:44:24 +00:00
|
|
|
const chalk = require('chalk');
|
|
|
|
const init = require('./init');
|
|
|
|
const bootstrap = require('./bootstrap');
|
|
|
|
const modules = require('./modules');
|
2017-10-15 05:01:51 +00:00
|
|
|
const version = require('../package.json').version;
|
2017-08-09 14:44:24 +00:00
|
|
|
|
|
|
|
const examples = [
|
2017-08-16 05:09:59 +00:00
|
|
|
['Generate json file with minimal options', '$ blinksocks init --minimal'],
|
2017-08-09 14:44:24 +00:00
|
|
|
['Start blinksocks client', '$ blinksocks --config blinksocks.client.json'],
|
2017-09-05 09:56:00 +00:00
|
|
|
['Start blinksocks server', '$ blinksocks --config blinksocks.server.json'],
|
|
|
|
['List all built-in presets', '$ blinksocks --list-presets']
|
2017-08-09 14:44:24 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
const usage = `
|
2017-09-05 09:56:00 +00:00
|
|
|
${chalk.bold.underline(`blinksocks v${version}`)}
|
|
|
|
|
2017-08-09 14:44:24 +00:00
|
|
|
Usage: blinksocks [command] [options] ...
|
|
|
|
|
|
|
|
Commands:
|
|
|
|
|
2017-09-05 09:56:00 +00:00
|
|
|
init generate configuration pair
|
2017-08-09 14:44:24 +00:00
|
|
|
|
|
|
|
Options:
|
|
|
|
|
2017-08-16 04:58:13 +00:00
|
|
|
-h, --help output usage information
|
|
|
|
-v, --version output blinksocks version
|
2017-09-05 09:56:00 +00:00
|
|
|
-c, --config file json file with configuration
|
2017-08-16 05:09:59 +00:00
|
|
|
-m, --minimal generate minimal json files
|
2017-09-05 09:56:00 +00:00
|
|
|
--list-presets list all built-in presets
|
2017-08-09 14:44:24 +00:00
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
2017-09-05 09:56:00 +00:00
|
|
|
${examples.map(([description, example]) => ` ${chalk.gray('-')} ${description}${os.EOL} ${chalk.blue(example)}`).join(os.EOL)}
|
|
|
|
|
|
|
|
About & Help: ${chalk.underline('https://github.com/blinksocks/blinksocks')}
|
2017-08-09 14:44:24 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
const argv = process.argv;
|
2017-09-06 01:34:46 +00:00
|
|
|
const options = argv.slice(2);
|
|
|
|
|
|
|
|
function hasOption(opt) {
|
|
|
|
return options.indexOf(opt) !== -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getOptionValue(opt) {
|
|
|
|
const index = options.indexOf(opt);
|
|
|
|
if (index !== -1) {
|
|
|
|
return options[index + 1];
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
2017-08-09 14:44:24 +00:00
|
|
|
|
2017-10-14 14:53:02 +00:00
|
|
|
function main() {
|
2017-08-14 07:53:01 +00:00
|
|
|
if (argv.length < 3) {
|
|
|
|
return console.log(usage);
|
|
|
|
}
|
|
|
|
|
2017-09-06 01:34:46 +00:00
|
|
|
// try to treat the first argument as conf
|
|
|
|
const opt0 = options[0];
|
|
|
|
const maybeConfPath = path.resolve(process.cwd(), opt0);
|
|
|
|
if (fs.existsSync(maybeConfPath) && path.extname(maybeConfPath) === '.json') {
|
|
|
|
return bootstrap(maybeConfPath, modules);
|
2017-08-14 07:53:01 +00:00
|
|
|
}
|
2017-08-09 14:44:24 +00:00
|
|
|
|
2017-09-06 01:34:46 +00:00
|
|
|
// parse Commands
|
|
|
|
|
|
|
|
if (options[0] === 'init') {
|
|
|
|
const isMinimal = hasOption('-m') || hasOption('--minimal');
|
|
|
|
return init({isMinimal});
|
2017-08-14 07:53:01 +00:00
|
|
|
}
|
2017-08-09 14:44:24 +00:00
|
|
|
|
2017-09-06 01:34:46 +00:00
|
|
|
// parse options
|
|
|
|
|
2017-08-14 07:53:01 +00:00
|
|
|
if (hasOption('-h') || hasOption('--help')) {
|
|
|
|
return console.log(usage);
|
2017-08-09 14:44:24 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 07:53:01 +00:00
|
|
|
if (hasOption('-v') || hasOption('--version')) {
|
|
|
|
return console.log(version);
|
|
|
|
}
|
2017-08-09 14:44:24 +00:00
|
|
|
|
2017-08-14 07:53:01 +00:00
|
|
|
if (hasOption('-c') || hasOption('--config')) {
|
|
|
|
let configPath = getOptionValue('-c') || getOptionValue('--config');
|
2017-08-09 14:44:24 +00:00
|
|
|
|
2017-08-14 07:53:01 +00:00
|
|
|
if (configPath === undefined) {
|
|
|
|
return console.log(chalk.red('config file must be provided'));
|
|
|
|
}
|
2017-08-09 14:44:24 +00:00
|
|
|
|
2017-08-14 07:53:01 +00:00
|
|
|
configPath = path.resolve(process.cwd(), configPath);
|
2017-08-09 14:44:24 +00:00
|
|
|
|
2017-08-14 07:53:01 +00:00
|
|
|
if (!fs.existsSync(configPath)) {
|
|
|
|
return console.log(chalk.red('config file is not found'));
|
|
|
|
}
|
2017-08-09 14:44:24 +00:00
|
|
|
|
2017-08-14 07:53:01 +00:00
|
|
|
return bootstrap(configPath, modules);
|
2017-08-09 14:44:24 +00:00
|
|
|
}
|
|
|
|
|
2017-08-18 07:09:38 +00:00
|
|
|
if (hasOption('--list-presets')) {
|
2017-10-14 05:02:34 +00:00
|
|
|
const {presets: {presets, legacyPresets}} = modules;
|
2017-10-20 02:56:03 +00:00
|
|
|
console.log(chalk.bold.underline('Installed'));
|
2017-10-14 05:02:34 +00:00
|
|
|
console.log(presets.join(os.EOL));
|
2017-10-20 02:56:03 +00:00
|
|
|
console.log('');
|
|
|
|
console.log(chalk.bold.underline('Deprecated'));
|
|
|
|
console.log(legacyPresets.map((name) => `${chalk.gray(name)}`).join(os.EOL));
|
2017-09-20 05:34:46 +00:00
|
|
|
return;
|
2017-08-16 04:58:13 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 07:53:01 +00:00
|
|
|
// other cases
|
|
|
|
console.log(usage);
|
2017-10-14 14:53:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// libsodium-wrappers need to be loaded asynchronously
|
|
|
|
// so we must wait for it ready before run main().
|
|
|
|
// https://github.com/jedisct1/libsodium.js#usage-as-a-module
|
|
|
|
const _sodium = require('libsodium-wrappers');
|
|
|
|
_sodium.ready
|
|
|
|
// a handy way to access libsodium without fighting with Promise
|
|
|
|
.then(() => global.libsodium = _sodium)
|
|
|
|
.then(main);
|