Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kfox
Created April 5, 2012 20:03
Show Gist options
  • Star 71 You must be signed in to star a gist
  • Fork 22 You must be signed in to fork a gist
  • Save kfox/2313683 to your computer and use it in GitHub Desktop.
Save kfox/2313683 to your computer and use it in GitHub Desktop.
A basic TCP proxy written in node.js
var net = require("net");
process.on("uncaughtException", function(error) {
console.error(error);
});
if (process.argv.length != 5) {
console.log("usage: %s <localport> <remotehost> <remoteport>", process.argv[1]);
process.exit();
}
var localport = process.argv[2];
var remotehost = process.argv[3];
var remoteport = process.argv[4];
var server = net.createServer(function (localsocket) {
var remotesocket = new net.Socket();
remotesocket.connect(remoteport, remotehost);
localsocket.on('connect', function (data) {
console.log(">>> connection #%d from %s:%d",
server.connections,
localsocket.remoteAddress,
localsocket.remotePort
);
});
localsocket.on('data', function (data) {
console.log("%s:%d - writing data to remote",
localsocket.remoteAddress,
localsocket.remotePort
);
var flushed = remotesocket.write(data);
if (!flushed) {
console.log(" remote not flushed; pausing local");
localsocket.pause();
}
});
remotesocket.on('data', function(data) {
console.log("%s:%d - writing data to local",
localsocket.remoteAddress,
localsocket.remotePort
);
var flushed = localsocket.write(data);
if (!flushed) {
console.log(" local not flushed; pausing remote");
remotesocket.pause();
}
});
localsocket.on('drain', function() {
console.log("%s:%d - resuming remote",
localsocket.remoteAddress,
localsocket.remotePort
);
remotesocket.resume();
});
remotesocket.on('drain', function() {
console.log("%s:%d - resuming local",
localsocket.remoteAddress,
localsocket.remotePort
);
localsocket.resume();
});
localsocket.on('close', function(had_error) {
console.log("%s:%d - closing remote",
localsocket.remoteAddress,
localsocket.remotePort
);
remotesocket.end();
});
remotesocket.on('close', function(had_error) {
console.log("%s:%d - closing local",
localsocket.remoteAddress,
localsocket.remotePort
);
localsocket.end();
});
});
server.listen(localport);
console.log("redirecting connections from 127.0.0.1:%d to %s:%d", localport, remotehost, remoteport);
@moeiscool
Copy link

GLORIOUS! thank you :)

@ilsubyeega
Copy link

thanks <3

@deepakkoirala
Copy link

Does this work for https?

@ResearcherOne
Copy link

You rock! It works like charm.

@mekb-turtle
Copy link

epic

@shamsdev
Copy link

fabulous

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment