2017-03-22 16:44:08 +00:00
|
|
|
const HTTPServer = require('http').Server;
|
|
|
|
const TorPool = require('./TorPool');
|
|
|
|
const SOCKSServer = require('./SOCKSServer');
|
|
|
|
const DNSServer = require('./DNSServer');
|
2017-12-08 01:21:42 +00:00
|
|
|
const HTTPProxyServer = require('./HTTPServer');
|
2017-03-22 16:44:08 +00:00
|
|
|
|
|
|
|
class ControlServer {
|
|
|
|
constructor(logger) {
|
|
|
|
this.server = new HTTPServer();
|
|
|
|
this.io = require('socket.io')(this.server);
|
|
|
|
|
|
|
|
this.torPool = new TorPool(null, null, logger);
|
|
|
|
this.logger = logger;
|
|
|
|
|
|
|
|
this.io.use(this.handleConnection.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
listen() { this.server.listen.apply(this.server, arguments); }
|
|
|
|
close() { this.server.close.apply(this.server, arguments); }
|
|
|
|
|
|
|
|
handleConnection(socket, next) {
|
|
|
|
socket.on('createTorPool', this.createTorPool.bind(this));
|
|
|
|
socket.on('createSOCKSServer', this.createSOCKSServer.bind(this));
|
|
|
|
socket.on('createDNSServer', this.createDNSServer.bind(this));
|
2017-12-08 01:21:42 +00:00
|
|
|
socket.on('createHTTPServer', this.createHTTPServer.bind(this));
|
2017-11-12 22:54:47 +00:00
|
|
|
socket.on('queryInstances', (callback) => {
|
|
|
|
if (!this.torPool)
|
|
|
|
return callback({ message: 'No pool created' });
|
|
|
|
|
|
|
|
callback(null, this.torPool.instances.map((i) => ( { dns_port: i.dns_port, socks_port: i.socks_port, process_id: i.process.pid } )) );
|
|
|
|
});
|
2017-03-22 16:44:08 +00:00
|
|
|
|
2017-03-22 21:25:18 +00:00
|
|
|
socket.on('createInstances', (instances, callback) => { this.torPool.create(instances, (error, instances) => {
|
2017-04-06 03:41:41 +00:00
|
|
|
callback && callback(error)
|
2017-03-22 21:25:18 +00:00
|
|
|
}); });
|
2017-03-25 00:11:06 +00:00
|
|
|
socket.on('removeInstances', (instances, callback) => { this.torPool.remove(instances, callback); });
|
2017-03-22 16:44:08 +00:00
|
|
|
socket.on('newIps', () => { this.torPool.new_ips(); });
|
|
|
|
socket.on('nextInstance', () => { this.torPool.next(); });
|
|
|
|
socket.on('closeInstances', () => { this.torPool.exit(); });
|
|
|
|
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
|
|
|
createTorPool(options) {
|
|
|
|
this.torPool = new TorPool(null, options, this.logger);
|
|
|
|
return this.torPool;
|
|
|
|
}
|
|
|
|
|
|
|
|
createSOCKSServer(port) {
|
|
|
|
this.socksServer = new SOCKSServer(this.torPool, this.logger);
|
|
|
|
this.socksServer.listen(port || 9050);
|
|
|
|
this.logger && this.logger.info(`[socks]: Listening on ${port}`);
|
|
|
|
return this.socksServer;
|
|
|
|
}
|
|
|
|
|
2017-12-08 01:21:42 +00:00
|
|
|
createHTTPServer(port) {
|
|
|
|
this.httpServer = new HTTPProxyServer(this.torPool, this.logger);
|
|
|
|
this.httpServer.listen(port || 9080);
|
|
|
|
this.logger && this.logger.info(`[http]: Listening on ${port}`);
|
|
|
|
return this.httpServer;
|
|
|
|
}
|
|
|
|
|
2017-03-22 16:44:08 +00:00
|
|
|
createDNSServer(port) {
|
|
|
|
this.dnsServer = new DNSServer(this.torPool, this.logger);
|
|
|
|
this.dnsServer.serve(port || 9053);
|
|
|
|
this.logger && this.logger.info(`[dns]: Listening on ${port}`);
|
|
|
|
return this.dnsServer;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = ControlServer;
|