2017-01-31 23:11:36 +00:00
|
|
|
const socks = require('socksv5');
|
2018-09-09 19:42:34 +00:00
|
|
|
const Promise = require('bluebird');
|
2018-09-09 05:45:12 +00:00
|
|
|
const { Server } = socks;
|
2017-01-31 23:11:36 +00:00
|
|
|
|
2018-09-09 05:45:12 +00:00
|
|
|
class SOCKSServer extends Server{
|
2018-09-09 19:42:34 +00:00
|
|
|
async listen() {
|
|
|
|
return await new Promise((resolve, reject) => {
|
|
|
|
let args = Array.from(arguments);
|
|
|
|
let inner_func = super.listen;
|
|
|
|
args.push(() => {
|
|
|
|
let args = Array.from(arguments);
|
|
|
|
resolve.apply(args);
|
|
|
|
});
|
|
|
|
inner_func.apply(this, args);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-10 02:26:55 +00:00
|
|
|
constructor(tor_pool, logger, proxy_by_name) {
|
2017-02-26 21:07:47 +00:00
|
|
|
let handleConnection = (info, accept, deny) => {
|
2017-01-31 23:11:36 +00:00
|
|
|
let inbound_socket = accept(true);
|
|
|
|
var outbound_socket;
|
|
|
|
let buffer = [];
|
|
|
|
|
2017-03-26 01:14:46 +00:00
|
|
|
let onInboundData = (data) => buffer.push(data)
|
|
|
|
|
|
|
|
let onClose = (error) => {
|
|
|
|
inbound_socket && inbound_socket.end();
|
|
|
|
outbound_socket && outbound_socket.end();
|
2017-03-26 01:02:56 +00:00
|
|
|
|
2017-03-26 01:14:46 +00:00
|
|
|
inbound_socket = outbound_socket = buffer = void(0);
|
2017-03-26 01:02:56 +00:00
|
|
|
|
2017-03-26 01:14:46 +00:00
|
|
|
if (error)
|
|
|
|
this.logger.error(`[socks]: an error occured: ${error.message}`)
|
|
|
|
};
|
2017-03-26 01:02:56 +00:00
|
|
|
|
2017-03-26 01:14:46 +00:00
|
|
|
if (!inbound_socket) return;
|
2017-03-26 01:02:56 +00:00
|
|
|
|
2017-03-26 01:14:46 +00:00
|
|
|
inbound_socket.on('close', onClose);
|
|
|
|
inbound_socket.on('data', onInboundData);
|
|
|
|
inbound_socket.on('error', onClose);
|
2017-03-26 01:02:56 +00:00
|
|
|
|
2017-03-26 01:14:46 +00:00
|
|
|
let connect = (tor_instance) => {
|
|
|
|
let socks_port = tor_instance.socks_port;
|
2018-09-09 19:42:34 +00:00
|
|
|
this.logger.verbose(`[socks]: ${info.srcAddr}:${info.srcPort} → 127.0.0.1:${socks_port}${tor_instance.definition.Name ? ' ('+tor_instance.definition.Name+')' : '' } → ${info.dstAddr}:${info.dstPort}`)
|
2017-03-26 01:02:56 +00:00
|
|
|
|
2018-08-09 17:42:11 +00:00
|
|
|
socks.connect({
|
|
|
|
host: info.dstAddr,
|
|
|
|
port: info.dstPort,
|
|
|
|
proxyHost: '127.0.0.1',
|
|
|
|
proxyPort: socks_port,
|
|
|
|
localDNS: false,
|
|
|
|
auths: [ socks.auth.None() ]
|
|
|
|
}, ($outbound_socket) => {
|
|
|
|
outbound_socket = $outbound_socket;
|
|
|
|
outbound_socket && outbound_socket.on('close', onClose);
|
|
|
|
|
|
|
|
inbound_socket && inbound_socket.removeListener('data', onInboundData);
|
|
|
|
inbound_socket && inbound_socket.on('data', (data) => {
|
|
|
|
outbound_socket && outbound_socket.write(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
outbound_socket && outbound_socket.on('data', (data) => {
|
|
|
|
inbound_socket && inbound_socket.write(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
outbound_socket && outbound_socket.on('error', onClose);
|
|
|
|
|
|
|
|
while (buffer && buffer.length && outbound_socket) {
|
|
|
|
outbound_socket.write(buffer.shift());
|
|
|
|
}
|
2017-03-26 01:02:56 +00:00
|
|
|
});
|
2017-01-31 23:11:36 +00:00
|
|
|
};
|
2017-03-26 01:02:56 +00:00
|
|
|
if (tor_pool.instances.length) {
|
|
|
|
connect(tor_pool.next());
|
|
|
|
} else {
|
2018-09-09 19:42:34 +00:00
|
|
|
this.logger.debug(`[socks]: a connection has been attempted, but no tor instances are live... waiting for an instance to come online`);
|
2017-03-26 01:02:56 +00:00
|
|
|
tor_pool.once('instance_created', connect);
|
|
|
|
}
|
2017-02-26 21:07:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
super(handleConnection);
|
2017-01-31 23:11:36 +00:00
|
|
|
|
2018-09-09 05:45:12 +00:00
|
|
|
this.logger = logger || require('./winston-silent-logger');;
|
2018-09-10 02:26:55 +00:00
|
|
|
if (!proxy_by_name) {
|
|
|
|
this.logger.debug(`[socks]: connecting to a specific instance by name has ben turned off`);
|
|
|
|
let auth = socks.auth.None();
|
|
|
|
} else {
|
|
|
|
this.logger.debug(`[socks]: connecting to a specific instance by name has ben turned on`);
|
|
|
|
let auth = socks.auth.UserPassword(
|
|
|
|
(username, password, cb) => {
|
|
|
|
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.useAuth(auth);
|
2017-01-31 23:11:36 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = SOCKSServer;
|