tor-router/bin/tor-router
2018-05-09 22:42:30 -07:00

189 lines
4.8 KiB
JavaScript
Executable file

#!/usr/bin/env node
var nconf = require('nconf');
var yargs = require('yargs');
var fs = require('fs');
var TorRouter = require('../');
var SOCKSServer = TorRouter.SOCKSServer;
var DNSServer = TorRouter.DNSServer;
var TorPool = TorRouter.TorPool;
var ControlServer = TorRouter.ControlServer;
var winston = require('winston');
var async = require('async');
process.title = 'tor-router';
let package_json = JSON.parse(fs.readFileSync(`${__dirname}/../package.json`, 'utf8'));
let argv_config = require('yargs')
.version(package_json.version)
.usage('Usage: tor-router [arguments]')
.options({
f: {
alias: 'config',
describe: 'Path to a config file to use',
demand: false
},
c: {
alias: 'controlPort',
describe: 'Port the control server will bind to [default: 9077]',
demand: false
// ,default: 9077
},
j: {
alias: 'instances',
describe: 'Number of instances using the default config',
demand: false
// ,default: 1
},
s: {
alias: 'socksPort',
describe: 'Port the SOCKS5 Proxy server will bind to',
demand: false,
// ,default: 9050
},
d: {
alias: 'dnsPort',
describe: 'Port the DNS Proxy server will bind to',
demand: false
},
h: {
alias: 'httpPort',
describe: 'Port the HTTP Proxy server will bind to',
demand: false
},
l: {
alias: 'logLevel',
describe: 'Controls the verbosity of console log output. Default level is "info". Set to "verbose" to see all network traffic logged or "null" to disable logging completely [default: info]',
demand: false
// ,default: "info"
},
p: {
alias: 'parentDataDirectory',
describe: 'Parent directory that will contain the data directories for the instances',
demand: false
},
b: {
alias: "loadBalanceMethod",
describe: 'Method that will be used to sort the instances between each request. Currently supports "round_robin" and "weighted". [default: round_robin]',
demand: false
},
t: {
alias: "torPath",
describe: "Provide the path for the Tor executable that will be used",
demand: false
}
});
let env_whitelist = [ "CONTROL_PORT", "TOR_PATH", "INSTANCES", "SOCKS_PORT", "DNS_PORT", "HTTP_PORT", "LOG_LEVEL", 'PARENT_DATA_DIRECTORIES', 'LOAD_BALANCE_METHOD', "controlPort", "torPath", "instances", "socksPort", "dnsPort", "httpPort", "logLevel", 'parentDataDirectories', 'loadBalanceMethod'];
nconf
.env({
whitelist: env_whitelist,
parseValues: true,
transform: (obj) => {
if (env_whitelist.includes(obj.key)) {
if (obj.key.indexOf('_') !== -1) {
let a = obj.key.toLowerCase().split('_');
i = 1;
while (i < a.length) {
a[i] = a[i][0].toUpperCase() + a[i].substr(1);
i++;
}
obj.key = a.join('');
}
}
return obj;
}
})
.argv(argv_config);
let nconf_config = nconf.get('config');
if (nconf_config) {
nconf.file(nconf_config);
}
nconf.defaults(require(`${__dirname}/../src/default_config.js`))
let logLevel = nconf.get('logLevel')
var logger = null;
if (logLevel !== 'null') {
logger = winston.createLogger({
level: logLevel,
format: winston.format.simple(),
transports: [
new (winston.transports.Console)({ level: logLevel })
]
});
}
let instances = nconf.get('instances');
let log_level = logLevel;
let socks_port = nconf.get('socksPort');
let dns_port = nconf.get('dnsPort');
let http_port = nconf.get('httpPort');
let control_port = nconf.get('controlPort');
if (typeof(control_port) === 'boolean') {
control_port = 9077;
nconf.set('controlPort', 9077);
}
let control = new ControlServer(logger, nconf);
process.on('SIGHUP', () => {
control.torPool.new_ips();
});
if (socks_port) {
if (typeof(socks_port) === 'boolean') {
socks_port = 9050;
nconf.set('socksPort', 9077);
}
let socks = control.createSOCKSServer(socks_port);
}
if (http_port) {
if (typeof(http_port) === 'boolean') {
http_port = 9080;
nconf.set('httpPort', 9077);
}
let http = control.createHTTPServer(http_port);
}
if (dns_port) {
if (typeof(dns_port) === 'boolean') {
dns_port = 9053;
nconf.set('dnsPort', 9077);
}
let dns = control.createDNSServer(dns_port);
}
if (instances) {
logger && logger.info(`[tor]: starting ${Array.isArray(instances) ? instances.length : instances} tor instances...`)
control.torPool.create(instances, (err) => {
logger && logger.info('[tor]: tor started');
});
}
control.listen(control_port, () => {
logger && logger.info(`[control]: Control Server listening on ${control_port}`);
})
function cleanUp(error) {
async.each(control.torPool.instances, (instance, next) => {
instance.exit(next);
}, (exitError) => {
if (error) {
console.error(error.stack);
}
if (exitError){
console.error(exitError);
}
process.exit(Number(Boolean(error || exitError)));
});
}
process.on('exit', cleanUp);
process.on('SIGINT', cleanUp);
process.on('uncaughtException', cleanUp);