From 38bf4d7c6acb7e927753e152ed35481fc7b2bc56 Mon Sep 17 00:00:00 2001 From: Zachary Boyd Date: Thu, 10 May 2018 22:08:38 -0700 Subject: [PATCH] Adds remove_by_name and new_identity_by_name methods to TorPool --- docs/rpc-methods.md | 18 +++++++++++++----- src/ControlServer.js | 18 ++++++++++++++++++ src/TorPool.js | 23 +++++++++++++++++++---- src/TorProcess.js | 8 ++++++++ 4 files changed, 58 insertions(+), 9 deletions(-) diff --git a/docs/rpc-methods.md b/docs/rpc-methods.md index 7131d67..1eb7e29 100644 --- a/docs/rpc-methods.md +++ b/docs/rpc-methods.md @@ -40,15 +40,23 @@ Removes a number of instances # removeInstanceAt(Integrer) -Remove a specific instance from the pool +Remove a specific instance from the pool by it's index -# newIps() +# removeInstanceByName(String) -Change the Tor Circuit on all instances +Remove a specific instance from the pool by it's name -# newIpAt(Integrer) +# newIdentites() -Change the Tor Circuit on a specific instance +Get new identites for all instances + +# newIdentityAt(Integrer) + +Get a new identity for a specific instance by it's index + +# newIdentityByName(String) + +Get a new identity for a specific instance by it's name # nextInstance() diff --git a/src/ControlServer.js b/src/ControlServer.js index 78ea938..8b5c5c2 100644 --- a/src/ControlServer.js +++ b/src/ControlServer.js @@ -61,6 +61,15 @@ class ControlServer { }); }).bind(this) ); + server.expose('removeInstanceByName', (function (instance_name) { + return new Promise((resolve, reject) => { + this.torPool.remove_by_name(instance_name, (error) => { + if (error) reject(error); + else resolve(); + }); + }); + }).bind(this) ); + server.expose('newIdentites', (function() { return new Promise((resolve, reject) => { this.torPool.new_identites((error) => { @@ -79,6 +88,15 @@ class ControlServer { }); }).bind(this)); + server.expose('newIdentityByName', (function(name) { + return new Promise((resolve, reject) => { + this.torPool.new_identity_by_name(name, (error) => { + if (error) reject(error); + else resolve(); + }); + }); + }).bind(this)); + /* Begin Deprecated */ server.expose('newIps', (function() { diff --git a/src/TorPool.js b/src/TorPool.js index eaa1ebd..9409de9 100644 --- a/src/TorPool.js +++ b/src/TorPool.js @@ -97,6 +97,10 @@ class TorPool extends EventEmitter { return this.add(instances, callback); } + instance_by_name(name) { + return this.instances.filter((i) => i.definition.Name === name)[0]; + } + remove(instances, callback) { let instances_to_remove = this._instances.splice(0, instances); async.each(instances_to_remove, (instance, next) => { @@ -105,10 +109,15 @@ class TorPool extends EventEmitter { } remove_at(instance_index, callback) { - let instance = this._instances.slice(instance_index, 1); - instance.exit(() => { - callback(); - }); + let instance = this._instances.splice(instance_index, 1); + instance.exit(callback); + } + + remove_by_name(instance_name, callback) { + let instance = this.instance_by_name(instance_name); + if (!instance) return callback && callback(new Error(`Instance "${name}" not found`)); + let instance_index = (this.instances.indexOf(instance));; + return this.remove_at(instance_index, callback); } next() { @@ -136,6 +145,12 @@ class TorPool extends EventEmitter { this.instances[index].new_identity(callback); } + new_identity_by_name(name, callback) { + let instance = this.instance_by_name(name); + if (!instance) return callback && callback(new Error(`Instance "${name}" not found`)); + instance.new_identity(callback); + } + /* Begin Deprecated */ new_ips(callback) { diff --git a/src/TorProcess.js b/src/TorProcess.js index 6b88bce..665080c 100644 --- a/src/TorProcess.js +++ b/src/TorProcess.js @@ -68,6 +68,14 @@ class TorProcess extends EventEmitter { return this._controller || null; } + getConfig(callback) { + if (!this.controller) { + return callback(new Error(`Controller is not connected`)); + } + + + } + create(callback) { async.auto({ dnsPort: (callback) => getPort().then(port => callback(null, port)),