Adds remove_by_name and new_identity_by_name methods to TorPool

This commit is contained in:
Zachary Boyd 2018-05-10 22:08:38 -07:00
parent 5f8fde9039
commit 38bf4d7c6a
4 changed files with 58 additions and 9 deletions

View file

@ -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()

View file

@ -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() {

View file

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

View file

@ -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)),