Merge pull request #14 from jogli5er/upstream_master

Pool getPort race condition
This commit is contained in:
Zachary Boyd 2019-11-23 22:40:13 -05:00 committed by GitHub
commit 37a58f929a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 887 additions and 741 deletions

5
.gitignore vendored
View file

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

1593
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) {