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
19 changes: 13 additions & 6 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ function Server(options, listener) {
for (var i = 0, len = options.auths.length; i < len; ++i)
this.useAuth(options.auths[i]);
}
if (options && options.localAddress != null) {
this.__localAddress = options.localAddress;
}
this._debug = (options && typeof options.debug === 'function'
? options.debug
: undefined);
Expand Down Expand Up @@ -115,7 +118,7 @@ Server.prototype._onConnection = function(socket) {
});
return socket;
} else
proxySocket(socket, reqInfo);
proxySocket(self.__localAddress, socket, reqInfo);
}
}
function deny() {
Expand All @@ -131,7 +134,7 @@ Server.prototype._onConnection = function(socket) {
return;
}

proxySocket(socket, reqInfo);
proxySocket(self.__localAddress, socket, reqInfo);
});

function onClose() {
Expand Down Expand Up @@ -193,7 +196,7 @@ exports.createServer = function(opts, listener) {

function onErrorNoop(err) {}

function proxySocket(socket, req) {
function proxySocket(localAddress, socket, req) {
dns.lookup(req.dstAddr, function(err, dstIP) {
if (err) {
handleProxyError(socket, err);
Expand Down Expand Up @@ -230,8 +233,12 @@ function proxySocket(socket, req) {
socket.resume();
} else if (dstSock.writable)
dstSock.end();
})
.connect(req.dstPort, dstIP);
});
if (localAddress != null) {
dstSock.connect({ port: req.dstPort, host: dstIP, localAddress: localAddress });
} else {
dstSock.connect(req.dstPort, dstIP);
}
socket.dstSock = dstSock;
});
}
Expand All @@ -257,4 +264,4 @@ function handleProxyError(socket, err) {
}
socket.end(errbuf);
}
}
}