Pool getPort race condition

It appears that when requesting multiple ports asynchronously,
that a port is given out multiple times, which crashes single
Tor processes. Such a crash brings down the whole system as of
now, which might or might not be desired.
This is caused by a race condition on the port numbers. This
commit fixes the issue if a number of Tor instances for the pool
is requested. If the pool is populated with an array of definitions,
the user has to make sure to include the ports in the definition,
otherwise the race condition still persists. If this should still be
allowed is up for discussion or if a check should be included in the
TorPool create function.
This commit is contained in:
Roman Brunner 2019-11-21 16:11:28 +01:00
parent 15408206a3
commit f89e4b20c5
5 changed files with 893 additions and 753 deletions

5
.gitignore vendored
View File

@ -1,5 +1,6 @@
node_modules
npm-debug.log
*.log
.env
.vscode
.DS_Store
.DS_Store

1611
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -31,6 +31,8 @@
"socks-proxy-agent": "^4.0.1"
},
"dependencies": {
"arrify": "^2.0.1",
"async-mutex": "^0.1.4",
"bluebird": "^3.5.2",
"del": "^3.0.0",
"eventemitter3": "^3.1.0",

View File

@ -27,7 +27,9 @@ const Promise = require("bluebird");
const _ = require('lodash');
const WeightedList = require('js-weighted-list');
const getPort = require('get-port');
const TorProcess = require('./TorProcess');
const { Mutex } = require('async-mutex');
Promise.promisifyAll(fs);
@ -462,6 +464,19 @@ class TorPool extends EventEmitter {
if (typeof(instances) === 'number') {
instances = Array.from(Array(instances)).map(() => ({}));
const lock = new Mutex();
instances = await Promise.all(instances.map(async(instance) => {
instance.ports = {};
let release = await lock.acquire();
try {
instance.ports.dns_port = await getPort();
instance.ports.socks_port = await getPort();
instance.ports.control_port = await getPort();
} finally {
release();
}
return instance;
}));
}
return await this.add(instances);
}

View File

@ -52,6 +52,7 @@ class TorProcess extends EventEmitter {
definition.Config = definition.Config || {};
this._definition = definition;
this._ports = definition.ports || {};
/**
* Path to the Tor executable.
@ -263,10 +264,12 @@ class TorProcess extends EventEmitter {
* @returns {Promise<ChildProcess>} - The process that has been created.
*/
async create() {
this._ports = {};
let dnsPort = this._ports.dns_port = await getPort();
let socksPort = this._ports.socks_port = await getPort();
let controlPort = this._ports.control_port = await getPort();
let dnsPort = this._ports.dns_port || await getPort();
let socksPort = this._ports.socks_port || await getPort();
let controlPort = this._ports.control_port || await getPort();
this.logger.info(`[tor-${this.instance_name}]: DNS PORT = ${dnsPort}`);
this.logger.info(`[tor-${this.instance_name}]: SOCKS PORT = ${socksPort}`);
this.logger.info(`[tor-${this.instance_name}]: CONTROL PORT = ${controlPort}`);
Object.freeze(this._ports);
let options = {
@ -403,7 +406,7 @@ class TorProcess extends EventEmitter {
* @returns {Error}
*/
this.emit('error', new Error(msg));
this.logger.error(`[tor-${this.instance_name}]: ${msg}`);
this.logger.error(`[tor-${this.instance_name}]: ${text}`);
}
else if (text.indexOf('[notice]') !== -1) {