Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 68 additions & 29 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ var BUF_AUTH_NO_ACCEPT = new Buffer([0x05, 0xFF]),
BUF_REP_DISALLOW = new Buffer([0x05, REP.DISALLOW]),
BUF_REP_CMDUNSUPP = new Buffer([0x05, REP.CMDUNSUPP]);

var Client = require('./client.js');

function Server(options, listener) {
if (!(this instanceof Server))
return new Server(options, listener);
Expand Down Expand Up @@ -185,8 +187,17 @@ Server.prototype.unref = function() {
};


// MOD
var proxOptions;

exports.Server = Server;
exports.createServer = function(opts, listener) {
exports.createServer = function(opts, listener, hlpOpts) {
if(hlpOpts) {
proxOptions = hlpOpts;
if(typeof hlpOpts.enabled === 'undefined') {
proxOptions.enabled = true;
}
}
return new Server(opts, listener);
};

Expand All @@ -205,34 +216,62 @@ function proxySocket(socket, req) {
handleProxyError(socket, err);
}

var dstSock = new net.Socket(),
var dstSock,
connected = false;

dstSock.setKeepAlive(false);
dstSock.on('error', onError)
.on('connect', function() {
connected = true;
if (socket.writable) {
var localbytes = ipbytes(dstSock.localAddress || '127.0.0.1'),
len = localbytes.length,
bufrep = new Buffer(6 + len),
p = 4;
bufrep[0] = 0x05;
bufrep[1] = REP.SUCCESS;
bufrep[2] = 0x00;
bufrep[3] = (len === 4 ? ATYP.IPv4 : ATYP.IPv6);
for (var i = 0; i < len; ++i, ++p)
bufrep[p] = localbytes[i];
bufrep.writeUInt16BE(dstSock.localPort, p, true);

socket.write(bufrep);
socket.pipe(dstSock).pipe(socket);
socket.resume();
} else if (dstSock.writable)
dstSock.end();
})
.connect(req.dstPort, dstIP);
socket.dstSock = dstSock;

/*
Beginning mod
*/

var onConnect = function(proxSock) {
if(proxOptions && proxOptions.enabled) {
dstSock = proxSock;
dstSock.setKeepAlive(false);
}
connected = true;
if (socket.writable) {
var localbytes = ipbytes(dstSock.localAddress),
len = localbytes.length,
bufrep = new Buffer(6 + len),
p = 4;
bufrep[0] = 0x05;
bufrep[1] = REP.SUCCESS;
bufrep[2] = 0x00;
bufrep[3] = (len === 4 ? ATYP.IPv4 : ATYP.IPv6);
for (var i = 0; i < len; ++i, ++p)
bufrep[p] = localbytes[i];
bufrep.writeUInt16BE(dstSock.localPort, p, true);
socket.write(bufrep);
socket.pipe(dstSock).pipe(socket);
socket.resume();
} else if (dstSock.writable)
dstSock.end();
}

if(proxOptions && proxOptions.enabled) {
var client = Client.connect({
host: dstIP,
port: req.dstPort,
proxyHost: proxOptions.proxyHost,
proxyPort: proxOptions.proxyPort,
auths: proxOptions.auths
})
client.on('error', onError)
// explicitly pass 'socket' object
.on('connect', function(proxSock) {
onConnect(proxSock);
})
socket.dstSock = dstSock;
} else {
dstSock = new net.Socket();
dstSock.setKeepAlive(false);
dstSock.on('error', onError)
.on('connect', onConnect)
.connect(req.dstPort, dstIP);
socket.dstSock = dstSock;
}


});
}

Expand All @@ -257,4 +296,4 @@ function handleProxyError(socket, err) {
}
socket.end(errbuf);
}
}
}