2018-09-10 02:12:31 +00:00
|
|
|
|
const rpc = require('jrpc2');
|
2018-09-11 21:44:32 +00:00
|
|
|
|
const Promise = require('bluebird');
|
2018-09-10 02:12:31 +00:00
|
|
|
|
|
2017-03-22 16:44:08 +00:00
|
|
|
|
const SOCKSServer = require('./SOCKSServer');
|
2017-12-08 02:53:16 +00:00
|
|
|
|
const HTTPServer = require('./HTTPServer');
|
2018-09-10 02:12:31 +00:00
|
|
|
|
const DNSServer = require('./DNSServer');
|
|
|
|
|
const TorPool = require('./TorPool');
|
|
|
|
|
const default_ports = require('./default_ports');
|
2017-03-22 16:44:08 +00:00
|
|
|
|
|
2018-09-14 23:54:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* @typedef ControlServer~InstanceInfo
|
|
|
|
|
*
|
|
|
|
|
* @property {string} name - Name of the instance.
|
|
|
|
|
* @property {string[]} group - Groups the instance belongs to.
|
|
|
|
|
* @property {number} dns_port - Port Tor is listening on for DNS Traffic.
|
|
|
|
|
* @property {number} socks_port - Port Tor is listening on for SOCKS Traffic.
|
|
|
|
|
* @property {number} process_id - Process ID for the Tor process.
|
|
|
|
|
* @property {Object} config - Configuration (torrc) set when starting the process.
|
|
|
|
|
* @property {number} [weight] - Weight of the instance for weighted load balancing.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A server which exposes an RPC interface that can control the application.
|
|
|
|
|
*/
|
2017-03-22 16:44:08 +00:00
|
|
|
|
class ControlServer {
|
2018-09-14 23:54:14 +00:00
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param {Provider} nconf - Instance of nconf.Provider that will be used to obtain configuration values.
|
|
|
|
|
* @param {Logger} [logger] - Winston logger that will be used for logging. If not provided will disable logging.
|
|
|
|
|
*/
|
|
|
|
|
constructor(nconf, logger) {
|
|
|
|
|
/**
|
|
|
|
|
* Pool of Tor instances
|
|
|
|
|
*
|
|
|
|
|
* @type {TorPool}
|
|
|
|
|
*/
|
|
|
|
|
this.tor_pool = new TorPool(nconf.get('torPath'), (() => nconf.get('torConfig')), nconf.get('parentDataDirectory'), nconf.get('loadBalanceMethod'), nconf.get('granaxOptions'), logger);
|
|
|
|
|
/**
|
|
|
|
|
* Winston Logger for logging
|
|
|
|
|
*
|
|
|
|
|
* @type {Logger}
|
|
|
|
|
*/
|
|
|
|
|
this.logger = logger || require('./winston_silent_logger');
|
|
|
|
|
/**
|
|
|
|
|
* Nconf Provider for configuration
|
|
|
|
|
*
|
|
|
|
|
* @type {Provider}
|
|
|
|
|
*/
|
2018-05-10 04:09:38 +00:00
|
|
|
|
this.nconf = nconf;
|
2017-03-22 16:44:08 +00:00
|
|
|
|
|
2018-09-14 23:54:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* RPC Server instance
|
|
|
|
|
*
|
|
|
|
|
* @type {rpc.Server}
|
|
|
|
|
*/
|
2017-12-08 02:53:16 +00:00
|
|
|
|
let server = this.server = new rpc.Server();
|
2018-09-14 23:54:14 +00:00
|
|
|
|
|
2017-12-08 02:53:16 +00:00
|
|
|
|
server.expose('createTorPool', this.createTorPool.bind(this));
|
|
|
|
|
server.expose('createSOCKSServer', this.createSOCKSServer.bind(this));
|
|
|
|
|
server.expose('createDNSServer', this.createDNSServer.bind(this));
|
|
|
|
|
server.expose('createHTTPServer', this.createHTTPServer.bind(this));
|
2018-09-11 17:39:49 +00:00
|
|
|
|
|
2018-09-14 23:54:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns a list of all instances currently in the pool.
|
|
|
|
|
*/
|
|
|
|
|
server.expose('queryInstances', (() => {
|
|
|
|
|
return this.tor_pool.instances.map(ControlServer.instance_info);
|
2018-08-10 17:06:40 +00:00
|
|
|
|
}).bind(this));
|
|
|
|
|
|
2018-09-14 23:54:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns information on an instance identified by the {@link TorProcess#instance_name} field.
|
|
|
|
|
*/
|
|
|
|
|
server.expose('queryInstanceByName', ((instance_name) => {
|
|
|
|
|
let instance = this.tor_pool.instance_by_name(instance_name);
|
2018-08-10 17:06:40 +00:00
|
|
|
|
|
2018-09-09 05:45:12 +00:00
|
|
|
|
if (!instance)
|
|
|
|
|
throw new Error(`Instance "${instance_name}"" does not exist`);
|
2018-08-10 17:06:40 +00:00
|
|
|
|
|
2018-09-14 23:54:14 +00:00
|
|
|
|
return ControlServer.instance_info(instance);
|
2018-08-10 17:06:40 +00:00
|
|
|
|
}).bind(this));
|
|
|
|
|
|
2018-09-14 23:54:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns information on an instance identified by its index in the pool.
|
|
|
|
|
*/
|
|
|
|
|
server.expose('queryInstanceAt', ((index) => {
|
|
|
|
|
if (!this.tor_pool)
|
2018-09-09 05:45:12 +00:00
|
|
|
|
throw new Error('No pool created');
|
2018-08-10 17:06:40 +00:00
|
|
|
|
|
2018-09-14 23:54:14 +00:00
|
|
|
|
let instance = this.tor_pool.instance_at(index);
|
2018-08-10 17:06:40 +00:00
|
|
|
|
|
2018-09-09 05:45:12 +00:00
|
|
|
|
if (!instance)
|
|
|
|
|
throw new Error(`Instance at "${i}"" does not exist`);
|
2017-12-08 02:53:16 +00:00
|
|
|
|
|
2018-09-14 23:54:14 +00:00
|
|
|
|
return ControlServer.instance_info(this.tor_pool.instance_at(index));
|
2018-05-11 04:44:30 +00:00
|
|
|
|
}).bind(this));
|
|
|
|
|
|
2018-09-14 23:54:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns a list of the names of all of the instances in the pool.
|
|
|
|
|
*/
|
|
|
|
|
server.expose('queryInstanceNames', (() => this.tor_pool.instance_names).bind(this));
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a list of the names of all of the current groups.
|
|
|
|
|
*/
|
|
|
|
|
server.expose('queryGroupNames', (() => Array.from(this.tor_pool.group_names)).bind(this));
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a list of the instances that exist in a given group.
|
|
|
|
|
*/
|
|
|
|
|
server.expose('queryInstancesByGroup', ((group) => this.tor_pool.instances_by_group(group).map(ControlServer.instance_info)).bind(this));
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates instances from a number, an array of instance definitions or a single instance definition.
|
|
|
|
|
* If a number is provided, creates n many instances.
|
|
|
|
|
*/
|
|
|
|
|
server.expose('createInstances', (async (instances_to_create) => {
|
|
|
|
|
let instances = await this.tor_pool.create(instances_to_create);
|
|
|
|
|
|
|
|
|
|
return instances.map(ControlServer.instance_info);
|
2018-09-09 19:42:34 +00:00
|
|
|
|
}).bind(this));
|
|
|
|
|
|
2018-09-14 23:54:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* Creates instances from an array of instance definitions or a single instance definition.
|
|
|
|
|
*/
|
2018-09-09 19:42:34 +00:00
|
|
|
|
server.expose('addInstances', (async (defs) => {
|
2018-09-14 23:54:14 +00:00
|
|
|
|
let instances = await this.tor_pool.add(defs);
|
2018-09-09 19:42:34 +00:00
|
|
|
|
|
2018-09-14 23:54:14 +00:00
|
|
|
|
return instances.map(ControlServer.instance_info);
|
2018-09-09 19:42:34 +00:00
|
|
|
|
}).bind(this));
|
2018-05-11 04:44:30 +00:00
|
|
|
|
|
2018-09-14 23:54:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* Removes a number of instances from the pool.
|
|
|
|
|
*/
|
|
|
|
|
server.expose('removeInstances', this.tor_pool.remove.bind(this.tor_pool));
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove an instance at the index provided from the pool.
|
|
|
|
|
*/
|
|
|
|
|
server.expose('removeInstanceAt', this.tor_pool.remove_at.bind(this.tor_pool));
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove an instance from the pool by the {@link TorProcess#instance_name} field.
|
|
|
|
|
*/
|
|
|
|
|
server.expose('removeInstanceByName', this.tor_pool.remove_by_name.bind(this.tor_pool));
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets new identities for all instances in the pool.
|
|
|
|
|
*/
|
|
|
|
|
server.expose('newIdentites', this.tor_pool.new_identites.bind(this.tor_pool));
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a new identity for the instance at the index provided in the pool.
|
|
|
|
|
*/
|
|
|
|
|
server.expose('newIdentityAt', this.tor_pool.new_identity_at.bind(this.tor_pool));
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a new identity for the instance by the {@link TorProcess#instance_name} field.
|
|
|
|
|
*/
|
|
|
|
|
server.expose('newIdentityByName', this.tor_pool.new_identity_by_name.bind(this.tor_pool));
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets new identities for all instances in the group.
|
|
|
|
|
*/
|
|
|
|
|
server.expose('newIdentitiesByGroup', this.tor_pool.new_identites_by_group.bind(this.tor_pool));
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the next instance in the pool using the load balance method.
|
|
|
|
|
*/
|
|
|
|
|
server.expose('nextInstance', (() => ControlServer.instance_info(this.tor_pool.next())).bind(this));
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the next instance in the group using the load balance method.
|
|
|
|
|
*/
|
2018-09-11 17:39:49 +00:00
|
|
|
|
server.expose('nextInstanceByGroup', ((group) => {
|
2018-09-14 23:54:14 +00:00
|
|
|
|
return ControlServer.instance_info(this.tor_pool.next_by_group(group));
|
2018-09-11 17:39:49 +00:00
|
|
|
|
}).bind(this));
|
|
|
|
|
|
2018-09-14 23:54:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* Kills the processes of all instances in the pool.
|
|
|
|
|
*/
|
|
|
|
|
server.expose('closeInstances', this.tor_pool.exit.bind(this.tor_pool));
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets a property in the application configuration.
|
|
|
|
|
*/
|
2018-09-11 21:44:32 +00:00
|
|
|
|
server.expose('setConfig', ((key, value) => {
|
2018-09-14 23:54:14 +00:00
|
|
|
|
this.nconf.set(key, value);
|
2018-09-11 21:44:32 +00:00
|
|
|
|
}).bind(this));
|
|
|
|
|
|
2018-09-14 23:54:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* Gets a property in the application configuration.
|
|
|
|
|
*/
|
2018-09-11 21:44:32 +00:00
|
|
|
|
server.expose('getConfig', ((key) => {
|
|
|
|
|
return this.nconf.get(key);
|
|
|
|
|
}).bind(this));
|
|
|
|
|
|
2018-09-14 23:54:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* Saves the application configuration to the underlying store (usually a JSON file).
|
|
|
|
|
*/
|
2018-09-11 21:44:32 +00:00
|
|
|
|
server.expose('saveConfig', (async () => {
|
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
|
this.nconf.save((err) => {
|
|
|
|
|
if (err) return reject(err);
|
|
|
|
|
resolve();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}).bind(this));
|
|
|
|
|
|
2018-09-14 23:54:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* Loads the application configuration from the underlying store (usually a JSON file).
|
|
|
|
|
*/
|
2018-09-11 21:44:32 +00:00
|
|
|
|
server.expose('loadConfig', (async () => {
|
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
|
this.nconf.load((err) => {
|
|
|
|
|
if (err) return reject(err);
|
|
|
|
|
resolve();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}).bind(this));
|
|
|
|
|
|
2018-09-14 23:54:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* Sets a configuration property on all instances in the pool.
|
|
|
|
|
*/
|
2018-09-09 05:45:12 +00:00
|
|
|
|
server.expose('setTorConfig', (async (config) => {
|
|
|
|
|
await Promise.all(Object.keys(config).map((key) => {
|
2018-09-09 19:42:34 +00:00
|
|
|
|
let value = config[key];
|
2018-09-09 05:45:12 +00:00
|
|
|
|
|
2018-09-14 23:54:14 +00:00
|
|
|
|
return this.tor_pool.set_config_all(key, value);
|
2018-09-09 05:45:12 +00:00
|
|
|
|
}));
|
2018-05-10 04:09:38 +00:00
|
|
|
|
}).bind(this));
|
2018-05-10 04:11:05 +00:00
|
|
|
|
|
2018-09-14 23:54:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* Sets a configuration property on all instances in a group.
|
|
|
|
|
*/
|
2018-09-11 06:48:56 +00:00
|
|
|
|
server.expose('setTorConfigByGroup', (async (group, config) => {
|
|
|
|
|
await Promise.all(Object.keys(config).map((key) => {
|
|
|
|
|
let value = config[key];
|
|
|
|
|
|
2018-09-14 23:54:14 +00:00
|
|
|
|
return this.tor_pool.set_config_by_group(group, key, value);
|
2018-09-11 06:48:56 +00:00
|
|
|
|
}));
|
|
|
|
|
}).bind(this));
|
|
|
|
|
|
2018-09-14 23:54:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* Retrieves the current load balance method for the pool
|
|
|
|
|
*/
|
|
|
|
|
server.expose('getLoadBalanceMethod', (() => {
|
|
|
|
|
return this.tor_pool.load_balance_method;
|
2018-05-10 04:11:05 +00:00
|
|
|
|
}).bind(this));
|
|
|
|
|
|
2018-09-14 23:54:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* Sets the current load balance method for the pool
|
|
|
|
|
*/
|
2018-09-11 21:44:32 +00:00
|
|
|
|
server.expose('setLoadBalanceMethod', ((loadBalanceMethod) => {
|
2018-09-14 23:54:14 +00:00
|
|
|
|
this.tor_pool.load_balance_method = loadBalanceMethod;
|
2018-09-11 21:44:32 +00:00
|
|
|
|
this.nconf.set('loadBalanceMethod', loadBalanceMethod);
|
2018-05-10 04:11:05 +00:00
|
|
|
|
}).bind(this));
|
2018-05-11 05:25:26 +00:00
|
|
|
|
|
2018-09-14 23:54:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* Retrieve a configuration property for an instance identified by the {@link TorProcess#instance_name} field.
|
|
|
|
|
*/
|
|
|
|
|
server.expose('getInstanceConfigByName', this.tor_pool.get_config_by_name.bind(this.tor_pool));
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retrieves a configuration property for an instance by its index in the pool.
|
|
|
|
|
*/
|
|
|
|
|
server.expose('getInstanceConfigAt', this.tor_pool.get_config_at.bind(this.tor_pool));
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets a configuration property for an instance identified by the {@link TorProcess#instance_name} field.
|
|
|
|
|
*/
|
|
|
|
|
server.expose('setInstanceConfigByName', this.tor_pool.set_config_by_name.bind(this.tor_pool));
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets a configuration property for an instance identified by its index in the pool.
|
|
|
|
|
*/
|
|
|
|
|
server.expose('setInstanceConfigAt', this.tor_pool.set_config_at.bind(this.tor_pool));
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sends a signal to all instances in the pool
|
|
|
|
|
*/
|
|
|
|
|
server.expose('signalAllInstances', this.tor_pool.signal_all.bind(this.tor_pool));
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sends a signal to an instance identified by its index in the pool.
|
|
|
|
|
*/
|
|
|
|
|
server.expose('signalInstanceAt', this.tor_pool.signal_at.bind(this.tor_pool));
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sends a signal to an instance identified by the {@link TorProcess#instance_name} field.
|
|
|
|
|
*/
|
|
|
|
|
server.expose('signalInstanceByName', this.tor_pool.signal_by_name.bind(this.tor_pool));
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sends a singal to all instances in a group.
|
|
|
|
|
*/
|
|
|
|
|
server.expose('signalInstancesByGroup', this.tor_pool.signal_by_group.bind(this.tor_pool));
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds an instance to a group identified by its {@link TorProcess#instance_name} field.
|
|
|
|
|
*/
|
|
|
|
|
server.expose('addInstanceToGroupByName', this.tor_pool.add_instance_to_group_by_name.bind(this.tor_pool));
|
2018-09-11 06:48:56 +00:00
|
|
|
|
|
2018-09-14 23:54:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* Adds an instance to a group identified by its index in the pool.
|
|
|
|
|
*/
|
|
|
|
|
server.expose('addInstanceToGroupAt', this.tor_pool.add_instance_to_group_at.bind(this.tor_pool));
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Removes an instance from a group identified by its {@link TorProcess#instance_name} field.
|
|
|
|
|
*/
|
|
|
|
|
server.expose('removeInstanceFromGroupByName', this.tor_pool.remove_instance_from_group_by_name.bind(this.tor_pool));
|
2018-09-11 06:48:56 +00:00
|
|
|
|
|
2018-09-14 23:54:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* Remove an instance from a group identified by its index in the pool.
|
|
|
|
|
*/
|
|
|
|
|
server.expose('removeInstanceFromGroupAt', this.tor_pool.remove_instance_from_group_at .bind(this.tor_pool));
|
2017-12-08 02:53:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-14 23:54:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns a summary of information on the running instance
|
|
|
|
|
* @param {TorProcess} instance
|
|
|
|
|
* @static
|
|
|
|
|
* @returns {ControlServer~InstanceInfo}
|
|
|
|
|
*/
|
|
|
|
|
static instance_info(instance) {
|
|
|
|
|
return {
|
|
|
|
|
name: instance.instance_name,
|
|
|
|
|
group: instance.instance_group,
|
|
|
|
|
dns_port: instance.dns_port,
|
|
|
|
|
socks_port: instance.socks_port,
|
|
|
|
|
process_id: instance.process.pid,
|
|
|
|
|
config: instance.definition.Config,
|
|
|
|
|
weight: instance.definition.weight
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Binds the server to a host and port and begins listening for TCP traffic
|
|
|
|
|
* @param {number} port - Port the server should bind to
|
|
|
|
|
* @param {string} [hostname] - Host the server should bind to
|
|
|
|
|
* @async
|
|
|
|
|
* @returns {Promise}
|
|
|
|
|
*/
|
2018-09-10 02:12:31 +00:00
|
|
|
|
async listenTcp(port, hostname) {
|
|
|
|
|
this.tcpTransport = new rpc.tcpTransport({ port, hostname });
|
2017-12-08 02:53:16 +00:00
|
|
|
|
this.tcpTransport.listen(this.server);
|
2018-09-10 02:12:31 +00:00
|
|
|
|
this.logger.info(`[control]: control server listening on tcp://${hostname}:${port}`);
|
2017-12-08 02:53:16 +00:00
|
|
|
|
}
|
2017-03-22 16:44:08 +00:00
|
|
|
|
|
2018-09-14 23:54:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* Binds the server to a host and port and begins listening for WebSocket traffic
|
|
|
|
|
* @param {number} port - Port the server should bind to
|
|
|
|
|
* @param {string} [hostname] - Host the server should bind to
|
|
|
|
|
* @async
|
|
|
|
|
* @returns {Promise}
|
|
|
|
|
*/
|
2018-09-10 02:12:31 +00:00
|
|
|
|
async listenWs(port, hostname) {
|
|
|
|
|
this.wsTransport = new rpc.wsTransport({ port, hostname });
|
|
|
|
|
this.wsTransport.listen(this.server);
|
|
|
|
|
this.logger.info(`[control]: control server listening on ws://${hostname}:${port}`);
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-14 23:54:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* Calls {@link ControlServer#listenTcp} with the same arguments
|
|
|
|
|
* @param {number} port - Port the server should bind to
|
|
|
|
|
* @param {string} [hostname] - Host the server should bind to
|
|
|
|
|
* @async
|
|
|
|
|
* @returns {Promise}
|
|
|
|
|
*/
|
|
|
|
|
async listen(port, hostname) { return await this.listenTcp(port, hostname); }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Closes the TCP and/or WebSocket servers
|
|
|
|
|
*/
|
2017-12-08 02:53:16 +00:00
|
|
|
|
close() {
|
2018-09-14 23:54:14 +00:00
|
|
|
|
if (this.tcpTransport && this.tcpTransport.tcpServer)
|
|
|
|
|
this.tcpTransport.tcpServer.close();
|
|
|
|
|
if (this.wsTransport && this.wsTransport.httpServer)
|
|
|
|
|
this.wsTransport.httpServer.close();
|
2017-03-22 16:44:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-14 23:54:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* Creates a new {@link TorPool} instance
|
|
|
|
|
* @param {Object} [tor_config] - Default Tor config to be used for the pool of instances.
|
|
|
|
|
* @param {string} [load_balance_method] - Load balance method to be used for the pool. Will default to the global configuration if not provided.
|
|
|
|
|
*
|
|
|
|
|
* @returns {TorPool} - The {@link TorPool} that was created.
|
|
|
|
|
*/
|
|
|
|
|
createTorPool(tor_config, load_balance_method) {
|
|
|
|
|
this.tor_pool = new TorPool(this.nconf.get('torPath'), tor_config, this.nconf.get('parentDataDirectory'), (load_balance_method || this.nconf.get('loadBalanceMethod')), this.nconf.get('granaxOptions'), this.logger);
|
|
|
|
|
return this.tor_pool;
|
2017-03-22 16:44:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-14 23:54:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* Creates an instance of {@link SOCKSServer} and begins listening for traffic
|
|
|
|
|
* @param {number} port - Port the server should bind to
|
|
|
|
|
* @param {string} [hostname] - Host the server should bind to
|
|
|
|
|
* @async
|
|
|
|
|
* @returns {Promise}
|
|
|
|
|
*/
|
2018-09-10 02:12:31 +00:00
|
|
|
|
async createSOCKSServer(port, hostname) {
|
2018-09-14 23:54:14 +00:00
|
|
|
|
this.socksServer = new SOCKSServer(this.tor_pool, this.logger, (this.nconf.get('proxyByName') ? { mode: this.nconf.get('proxyByName'), deny_unidentified_users: this.nconf.get('denyUnidentifiedUsers') } : ""));
|
2018-09-10 02:12:31 +00:00
|
|
|
|
await this.socksServer.listen(port || default_ports.socks, hostname);
|
|
|
|
|
this.logger.info(`[socks]: listening on socks5://${hostname}:${port}`);
|
2017-03-22 16:44:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-14 23:54:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* Creates an instance of {@link HTTPServer} and begins listening for traffic
|
|
|
|
|
* @param {number} port - Port the server should bind to
|
|
|
|
|
* @param {string} [hostname] - Host the server should bind to
|
|
|
|
|
* @async
|
|
|
|
|
* @returns {Promise}
|
|
|
|
|
*/
|
2018-09-10 02:12:31 +00:00
|
|
|
|
async createHTTPServer(port, hostname) {
|
2018-09-14 23:54:14 +00:00
|
|
|
|
this.httpServer = new HTTPServer(this.tor_pool, this.logger, (this.nconf.get('proxyByName') ? { mode: this.nconf.get('proxyByName'), deny_unidentified_users: this.nconf.get('denyUnidentifiedUsers') } : ""));
|
2018-09-10 02:12:31 +00:00
|
|
|
|
await this.httpServer.listen(port || default_ports.http, hostname);
|
|
|
|
|
this.logger.info(`[http]: listening on http://${hostname}:${port}`);
|
2017-12-08 01:21:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-14 23:54:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* Creates an instance of {@link DNSServer} and begins listening for traffic
|
|
|
|
|
* @param {number} port - Port the server should bind to
|
|
|
|
|
* @param {string} [hostname] - Host the server should bind to
|
|
|
|
|
* @async
|
|
|
|
|
* @returns {Promise}
|
|
|
|
|
*/
|
2018-09-10 02:12:31 +00:00
|
|
|
|
async createDNSServer(port, hostname) {
|
2018-09-14 23:54:14 +00:00
|
|
|
|
this.dnsServer = new DNSServer(this.tor_pool, this.nconf.get('dns:options'), this.nconf.get('dns:timeout'), this.logger);
|
2018-09-10 02:12:31 +00:00
|
|
|
|
await this.dnsServer.serve(port || default_ports.dns, hostname);
|
|
|
|
|
this.logger.info(`[dns]: listening on dns://${hostname}:${port}`);
|
2017-03-22 16:44:08 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-09-14 23:54:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* Module that contains the {@link ControlServer} class.
|
|
|
|
|
* @module tor-router/ControlServer
|
|
|
|
|
* @see ControlServer
|
|
|
|
|
*/
|
2017-03-22 16:44:08 +00:00
|
|
|
|
module.exports = ControlServer;
|