Removes domains
This commit is contained in:
commit
a8b68ef5ec
|
@ -1,6 +1,5 @@
|
|||
const http = require('http');
|
||||
const Server = http.Server;
|
||||
const domain = require('domain');
|
||||
const socks = require('socksv5');
|
||||
const URL = require('url');
|
||||
const SocksProxyAgent = require('socks-proxy-agent');
|
||||
|
@ -8,14 +7,6 @@ const SocksProxyAgent = require('socks-proxy-agent');
|
|||
class HTTPServer extends Server {
|
||||
constructor(tor_pool, logger) {
|
||||
let handle_http_connections = (req, res) => {
|
||||
let d = domain.create();
|
||||
d.on('error', (error) => {
|
||||
logger.error(`[http-proxy]: an error occured: ${error.message}`)
|
||||
res.end();
|
||||
});
|
||||
d.add(req);
|
||||
d.add(res);
|
||||
|
||||
let url = URL.parse(req.url);
|
||||
url.port = url.port || 80;
|
||||
|
||||
|
@ -31,53 +22,53 @@ class HTTPServer extends Server {
|
|||
|
||||
req.on('data', onIncomingData);
|
||||
req.on('end', preConnectClosed);
|
||||
|
||||
req.on('error', function (err) {
|
||||
logger.error("[http-proxy]: an error occured\n"+err.stack);
|
||||
});
|
||||
|
||||
let connect = (tor_instance) => {
|
||||
let socks_port = tor_instance.socks_port;
|
||||
logger.verbose(`[http-proxy]: ${req.connection.remoteAddress}:${req.connection.remotePort} → 127.0.0.1:${socks_port} → ${url.hostname}:${url.port}`);
|
||||
|
||||
d.run(() => {
|
||||
let proxy_req = http.request({
|
||||
method: req.method,
|
||||
hostname: url.hostname,
|
||||
port: url.port,
|
||||
path: url.path,
|
||||
headers: req.headers,
|
||||
agent: new SocksProxyAgent(`socks://127.0.0.1:${socks_port}`)
|
||||
}, (proxy_res) => {
|
||||
d.add(proxy_res);
|
||||
proxy_res.on('data', (chunk) => {
|
||||
res.write(chunk);
|
||||
});
|
||||
|
||||
proxy_res.on('end', () => {
|
||||
res.end();
|
||||
});
|
||||
|
||||
res.writeHead(proxy_res.statusCode, proxy_res.headers);
|
||||
let proxy_req = http.request({
|
||||
method: req.method,
|
||||
hostname: url.hostname,
|
||||
port: url.port,
|
||||
path: url.path,
|
||||
headers: req.headers,
|
||||
agent: new SocksProxyAgent(`socks://127.0.0.1:${socks_port}`)
|
||||
}, (proxy_res) => {
|
||||
d.add(proxy_res);
|
||||
proxy_res.on('data', (chunk) => {
|
||||
res.write(chunk);
|
||||
});
|
||||
|
||||
req.removeListener('data', onIncomingData);
|
||||
proxy_res.on('end', () => {
|
||||
res.end();
|
||||
});
|
||||
|
||||
req.on('data', (chunk) => {
|
||||
proxy_req.write(chunk);
|
||||
})
|
||||
|
||||
req.on('end', () => {
|
||||
proxy_req.end();
|
||||
})
|
||||
|
||||
while (buffer.length) {
|
||||
proxy_req.write(buffer.shift());
|
||||
}
|
||||
|
||||
if (req.finished)
|
||||
proxy_req.end();
|
||||
|
||||
d.add(proxy_req);
|
||||
res.writeHead(proxy_res.statusCode, proxy_res.headers);
|
||||
});
|
||||
|
||||
req.removeListener('data', onIncomingData);
|
||||
|
||||
req.on('data', (chunk) => {
|
||||
proxy_req.write(chunk);
|
||||
})
|
||||
|
||||
req.on('end', () => {
|
||||
proxy_req.end();
|
||||
})
|
||||
|
||||
while (buffer.length) {
|
||||
proxy_req.write(buffer.shift());
|
||||
}
|
||||
|
||||
if (req.finished)
|
||||
proxy_req.end();
|
||||
|
||||
};
|
||||
|
||||
if (tor_pool.instances.length) {
|
||||
connect(tor_pool.next());
|
||||
} else {
|
||||
|
@ -87,10 +78,6 @@ class HTTPServer extends Server {
|
|||
};
|
||||
|
||||
let handle_connect_connections = (req, inbound_socket, head) => {
|
||||
let d = domain.create();
|
||||
|
||||
d.add(inbound_socket);
|
||||
|
||||
let hostname = req.url.split(':').shift();
|
||||
let port = Number(req.url.split(':').pop());
|
||||
|
||||
|
@ -107,39 +94,30 @@ class HTTPServer extends Server {
|
|||
|
||||
if (error)
|
||||
this.logger.error(`[http-connect]: an error occured: ${error.message}`)
|
||||
|
||||
d.exit();
|
||||
};
|
||||
|
||||
d.on('error', onClose);
|
||||
|
||||
d.add(inbound_socket);
|
||||
|
||||
var buffer = [head];
|
||||
let onInboundData = function (data) {
|
||||
buffer.push(data);
|
||||
};
|
||||
|
||||
d.run(() => {
|
||||
socks.connect({
|
||||
host: hostname,
|
||||
port: port,
|
||||
proxyHost: '127.0.0.1',
|
||||
proxyPort: socks_port,
|
||||
localDNS: false,
|
||||
auths: [ socks.auth.None() ]
|
||||
}, ($outbound_socket) => {
|
||||
outbound_socket = $outbound_socket;
|
||||
d.add(outbound_socket);
|
||||
outbound_socket && outbound_socket.on('close', onClose);
|
||||
outbound_socket && outbound_socket.on('error', onClose);
|
||||
socks.connect({
|
||||
host: hostname,
|
||||
port: port,
|
||||
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);
|
||||
outbound_socket && outbound_socket.on('error', onClose);
|
||||
|
||||
inbound_socket.write('HTTP/1.1 200 Connection Established\r\n'+'Proxy-agent: tor-router\r\n' +'\r\n');
|
||||
outbound_socket.write(head);
|
||||
inbound_socket.write('HTTP/1.1 200 Connection Established\r\n'+'Proxy-agent: tor-router\r\n' +'\r\n');
|
||||
outbound_socket.write(head);
|
||||
|
||||
outbound_socket.pipe(inbound_socket);
|
||||
inbound_socket.pipe(outbound_socket);
|
||||
})
|
||||
outbound_socket.pipe(inbound_socket);
|
||||
inbound_socket.pipe(outbound_socket);
|
||||
});
|
||||
};
|
||||
if (tor_pool.instances.length) {
|
||||
|
|
|
@ -1,14 +1,10 @@
|
|||
const socks = require('socksv5');
|
||||
const SOCKS5Server = socks.Server;
|
||||
const domain = require('domain');
|
||||
|
||||
class SOCKSServer extends SOCKS5Server{
|
||||
constructor(tor_pool, logger) {
|
||||
let handleConnection = (info, accept, deny) => {
|
||||
let d = domain.create();
|
||||
|
||||
let inbound_socket = accept(true);
|
||||
d.add(inbound_socket);
|
||||
var outbound_socket;
|
||||
let buffer = [];
|
||||
|
||||
|
@ -22,8 +18,6 @@ class SOCKSServer extends SOCKS5Server{
|
|||
|
||||
if (error)
|
||||
this.logger.error(`[socks]: an error occured: ${error.message}`)
|
||||
|
||||
d.exit();
|
||||
};
|
||||
|
||||
if (!inbound_socket) return;
|
||||
|
@ -36,36 +30,31 @@ class SOCKSServer extends SOCKS5Server{
|
|||
let socks_port = tor_instance.socks_port;
|
||||
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}`)
|
||||
|
||||
d.on('error', onClose);
|
||||
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);
|
||||
|
||||
d.run(() => {
|
||||
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;
|
||||
d.add(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);
|
||||
});
|
||||
|
||||
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('data', (data) => {
|
||||
inbound_socket && inbound_socket.write(data);
|
||||
});
|
||||
outbound_socket && outbound_socket.on('error', onClose);
|
||||
|
||||
outbound_socket && outbound_socket.on('error', onClose);
|
||||
|
||||
while (buffer && buffer.length && outbound_socket) {
|
||||
outbound_socket.write(buffer.shift());
|
||||
}
|
||||
})
|
||||
while (buffer && buffer.length && outbound_socket) {
|
||||
outbound_socket.write(buffer.shift());
|
||||
}
|
||||
});
|
||||
};
|
||||
if (tor_pool.instances.length) {
|
||||
|
|
Loading…
Reference in a new issue