Removes refrences to 'nconf' within classes to make testing easier

This commit is contained in:
Zachary Boyd 2018-08-09 15:03:51 -04:00
parent 41aea76a95
commit b3a03b4a3e
7 changed files with 47 additions and 55 deletions

View file

@ -105,18 +105,14 @@ if (nconf_config) {
nconf.defaults(require(`${__dirname}/../src/default_config.js`))
let logLevel = nconf.get('logLevel')
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 })
]
});
}
logger = winston.createLogger({
level: logLevel,
format: winston.format.simple(),
transports: (logLevel == 'null') ? [] : [ new (winston.transports.Console)({ level: logLevel }) ]
});
let instances = nconf.get('instances');
let log_level = logLevel;
@ -161,14 +157,14 @@ if (dns_port) {
}
if (instances) {
logger && logger.info(`[tor]: starting ${Array.isArray(instances) ? instances.length : instances} tor instances...`)
logger.info(`[tor]: starting ${Array.isArray(instances) ? instances.length : instances} tor instances...`)
control.torPool.create(instances, (err) => {
logger && logger.info('[tor]: tor started');
logger.info('[tor]: tor started');
});
}
control.listen(control_port, () => {
logger && logger.info(`[control]: Control Server listening on ${control_port}`);
logger.info(`[control]: Control Server listening on ${control_port}`);
})
function cleanUp(error) {

View file

@ -6,7 +6,7 @@ const rpc = require('jrpc2');
class ControlServer {
constructor(logger, nconf) {
this.torPool = new TorPool(null, null, logger, nconf);
this.torPool = new TorPool(nconf.get('torPath'), null, nconf.get('parentDataDirectory'), nconf.get('loadBalanceMethod'), nconf.get('granaxOptions'),logger);
this.logger = logger;
this.nconf = nconf;
@ -223,30 +223,30 @@ class ControlServer {
}
createTorPool(options) {
this.torPool = new TorPool(null, options, this.logger, this.nconf);
this.torPool = new TorPool(nconf.get('torPath'), options, nconf.get('parentDataDirectory'), nconf.get('loadBalanceMethod'), nconf.get('granaxOptions'), this.logger);
return Promise.resolve();
}
createSOCKSServer(port) {
this.socksServer = new SOCKSServer(this.torPool, this.logger, this.nconf);
this.socksServer = new SOCKSServer(this.torPool, this.logger);
this.socksServer.listen(port || 9050);
this.logger && this.logger.info(`[socks]: Listening on ${port}`);
this.logger.info(`[socks]: Listening on ${port}`);
this.socksServer;
return Promise.resolve();
}
createHTTPServer(port) {
this.httpServer = new HTTPServer(this.torPool, this.logger, this.nconf);
this.httpServer = new HTTPServer(this.torPool, this.logger);
this.httpServer.listen(port || 9080);
this.logger && this.logger.info(`[http]: Listening on ${port}`);
this.logger.info(`[http]: Listening on ${port}`);
this.httpServer;
return Promise.resolve();
}
createDNSServer(port) {
this.dnsServer = new DNSServer(this.torPool, this.logger, this.nconf);
this.dnsServer = new DNSServer(this.torPool, nconf.get('dns:options'), this.nconf.get('dns:timeout'), this.logger);
this.dnsServer.serve(port || 9053);
this.logger && this.logger.info(`[dns]: Listening on ${port}`);
this.logger.info(`[dns]: Listening on ${port}`);
this.dnsServer;
return Promise.resolve();
}

View file

@ -2,8 +2,8 @@ const dns = require('native-dns');
const UDPServer = require('native-dns').UDPServer;
class DNSServer extends UDPServer {
constructor(tor_pool, logger, nconf) {
super(nconf.get('dns:options'));
constructor(tor_pool, dns_options, dns_timeout, logger) {
super(dns_options);
this.logger = logger;
this.tor_pool = tor_pool;
@ -14,14 +14,14 @@ class DNSServer extends UDPServer {
let outbound_req = dns.Request({
question,
server: { address: '127.0.0.1', port: dns_port, type: 'udp' },
timeout: this.nconf.get('dns:timeout')
timeout: dns_timeout
});
outbound_req.on('message', (err, answer) => {
if (!err && answer) {
for (let a of answer.answer){
res.answer.push(a);
this.logger && this.logger.verbose(`[dns]: ${question.name} type ${dns.consts.QTYPE_TO_NAME[question.type]} → 127.0.0.1:${dns_port}${tor_instance.definition.Name ? ' ('+tor_instance.definition.Name+')' : '' }${a.address}`)
this.logger.verbose(`[dns]: ${question.name} type ${dns.consts.QTYPE_TO_NAME[question.type]} → 127.0.0.1:${dns_port}${tor_instance.definition.Name ? ' ('+tor_instance.definition.Name+')' : '' }${a.address}`)
}
}
});

View file

@ -6,11 +6,11 @@ const URL = require('url');
const SocksProxyAgent = require('socks-proxy-agent');
class HTTPServer extends Server {
constructor(tor_pool, logger, nconf) {
constructor(tor_pool, logger) {
let handle_http_connections = (req, res) => {
let d = domain.create();
d.on('error', (error) => {
this.logger.error(`[http-proxy]: an error occured: ${error.message}`)
logger.error(`[http-proxy]: an error occured: ${error.message}`)
res.end();
});
d.add(req);
@ -35,7 +35,7 @@ class HTTPServer extends Server {
let connect = (tor_instance) => {
let socks_port = tor_instance.socks_port;
logger && logger.verbose(`[http-proxy]: ${req.connection.remoteAddress}:${req.connection.remotePort} → 127.0.0.1:${socks_port}${url.hostname}:${url.port}`);
logger.verbose(`[http-proxy]: ${req.connection.remoteAddress}:${req.connection.remotePort} → 127.0.0.1:${socks_port}${url.hostname}:${url.port}`);
d.run(() => {
let proxy_req = http.request({
@ -154,7 +154,6 @@ class HTTPServer extends Server {
this.on('connect', handle_connect_connections);
this.logger = logger;
this.nconf = nconf;
this.tor_pool = tor_pool;
}
};

View file

@ -3,7 +3,7 @@ const SOCKS5Server = socks.Server;
const domain = require('domain');
class SOCKSServer extends SOCKS5Server{
constructor(tor_pool, logger, nconf) {
constructor(tor_pool, logger) {
let handleConnection = (info, accept, deny) => {
let d = domain.create();
@ -34,7 +34,7 @@ class SOCKSServer extends SOCKS5Server{
let connect = (tor_instance) => {
let socks_port = tor_instance.socks_port;
logger && logger.verbose(`[socks]: ${info.srcAddr}:${info.srcPort} → 127.0.0.1:${socks_port}${tor_instance.definition.Name ? ' ('+tor_instance.definition.Name+')' : '' }${info.dstAddr}:${info.dstPort}`)
logger.verbose(`[socks]: ${info.srcAddr}:${info.srcPort} → 127.0.0.1:${socks_port}${tor_instance.definition.Name ? ' ('+tor_instance.definition.Name+')' : '' }${info.dstAddr}:${info.dstPort}`)
d.on('error', onClose);
@ -79,7 +79,6 @@ class SOCKSServer extends SOCKS5Server{
super(handleConnection);
this.logger = logger;
this.nconf = nconf;
this.useAuth(socks.auth.None());
}

View file

@ -43,17 +43,17 @@ const load_balance_methods = {
};
class TorPool extends EventEmitter {
constructor(tor_path, default_config, logger, nconf, data_directory, load_balance_method) {
constructor(tor_path, default_config, data_directory, load_balance_method, granax_options, logger) {
super();
this._instances = [];
default_config = _.extend({}, (default_config || {}), nconf.get('torConfig'));
default_config = _.extend({}, (default_config || {}));
this.default_tor_config = default_config;
this.data_directory = data_directory || nconf.get('parentDataDirectory');
this.load_balance_method = load_balance_method || nconf.get('loadBalanceMethod');
this.data_directory = data_directory;
this.load_balance_method = load_balance_method;
!fs.existsSync(this.data_directory) && fs.mkdirSync(this.data_directory);
this.tor_path = tor_path || nconf.get('torPath');
this.tor_path = tor_path;
this.logger = logger;
this.nconf = nconf;
this.granax_options = granax_options;
}
get instances() {
@ -65,7 +65,7 @@ class TorPool extends EventEmitter {
instance_definition.Config = _.extend(instance_definition.Config, this.default_tor_config);
let instance_id = nanoid();
instance_definition.Config.DataDirectory = instance_definition.Config.DataDirectory || path.join(this.data_directory, (instance_definition.Name || instance_id));
let instance = new TorProcess(this.tor_path, instance_definition.Config, this.logger, this.nconf);
let instance = new TorProcess(this.tor_path, instance_definition.Config, this.granax_options, this.logger);
instance.id = instance_id;
instance.definition = instance_definition;
instance.create((error) => {
@ -121,7 +121,7 @@ class TorPool extends EventEmitter {
}
next() {
this._instances = load_balance_methods[this.nconf.get('loadBalanceMethod')](this._instances);
this._instances = load_balance_methods[this.load_balance_method](this._instances);
return this.instances[0];
}
@ -154,12 +154,12 @@ class TorPool extends EventEmitter {
/* Begin Deprecated */
new_ips(callback) {
this.logger && this.logger.warn(`TorPool.new_ips is deprecated, use TorPool.new_identites`);
this.logger.warn(`TorPool.new_ips is deprecated, use TorPool.new_identites`);
return this.new_identites(callback);
}
new_ip_at(index, callback) {
this.logger && this.logger.warn(`TorPool.new_ip_at is deprecated, use TorPool.new_identity_at`);
this.logger.warn(`TorPool.new_ip_at is deprecated, use TorPool.new_identity_at`);
return this.new_identity_at(index, callback);
}

View file

@ -13,13 +13,11 @@ const crypto = require('crypto');
temp.track();
class TorProcess extends EventEmitter {
constructor(tor_path, config, logger, nconf, granax_options) {
constructor(tor_path, config, granax_options, logger) {
super();
this.tor_path = tor_path || nconf.get('torPath');
this.nconf = nconf;
this.logger = logger;
this.granax_options = granax_options || nconf.get('granaxOptions');
this.tor_path = tor_path;
this.granax_options = granax_options;
this.control_password = crypto.randomBytes(128).toString('base64');
config.DataDirectory = config.DataDirectory || temp.mkdirSync();
@ -89,7 +87,7 @@ class TorProcess extends EventEmitter {
/* Begin Deprecated */
new_ip(callback) {
this.logger && this.logger.warn(`TorProcess.new_ip is deprecated, use TorProcess.new_identity`);
this.logger.warn(`TorProcess.new_ip is deprecated, use TorProcess.new_identity`);
return this.new_identity(callback);
}
@ -146,19 +144,19 @@ class TorProcess extends EventEmitter {
this.once('ready', () => {
this.ready = true;
this.logger && this.logger.info(`[tor-${this.instance_name}]: tor is ready`);
this.logger.info(`[tor-${this.instance_name}]: tor is ready`);
});
this.on('control_listen', () => {
this._controller = new TorController(connect(this._control_port), _.extend({ authOnConnect: false }, this.granax_options));
this.controller.on('ready', () => {
this.logger && this.logger.debug(`[tor-${this.instance_name}]: connected to tor control port`);
this.logger.debug(`[tor-${this.instance_name}]: connected to tor control port`);
this.controller.authenticate(`"${this.control_password}"`, (err) => {
if (err) {
this.logger && this.logger.error(`[tor-${this.instance_name}]: ${err.stack}`);
this.logger.error(`[tor-${this.instance_name}]: ${err.stack}`);
this.emit('error', err);
} else {
this.logger && this.logger.debug(`[tor-${this.instance_name}]: authenticated with tor instance via the control port`);
this.logger.debug(`[tor-${this.instance_name}]: authenticated with tor instance via the control port`);
this.emit('controller_ready');
}
});
@ -186,15 +184,15 @@ class TorProcess extends EventEmitter {
if (text.indexOf('[err]') !== -1) {
this.emit('error', new Error(msg));
this.logger && this.logger.error(`[tor-${this.instance_name}]: ${msg}`);
this.logger.error(`[tor-${this.instance_name}]: ${msg}`);
}
else if (text.indexOf('[notice]') !== -1) {
this.logger && this.logger.debug(`[tor-${this.instance_name}]: ${msg}`);
this.logger.debug(`[tor-${this.instance_name}]: ${msg}`);
}
else if (text.indexOf('[warn]') !== -1) {
this.logger && this.logger.warn(`[tor-${this.instance_name}]: ${msg}`);
this.logger.warn(`[tor-${this.instance_name}]: ${msg}`);
}
});