Skip to content

Instantly share code, notes, and snippets.

@jsocol
Created March 16, 2011 01:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsocol/871845 to your computer and use it in GitHub Desktop.
Save jsocol/871845 to your computer and use it in GitHub Desktop.
slow TCP proxy
/**
* Usage: node sockolepsy.js <listen> <forward> <delay>
*
* Creates a generic TCP proxy that can introduce a controllable degree of
* delay per packet.
*
* Params:
* listen - the port to listen on.
* forward - the port to foward to.
* delay - the amount of delay to introduce, in ms.
*
* For example, to create a proxy to MySQL with 50 milliseconds of delay, you
* could run:
*
* node sockolepsy.js 9999 3306 500
*
* Then connect your MySQL client to port 9999:
*
* mysql -P9999
*
*/
var net = require('net'),
sys = require('sys');
var listen = 9999,
forward = 3306,
delay = 500,
server;
if (process.argv.length < 5) {
sys.puts('Usage: node ' + process.argv[1] + ' <listen> <forward> <delay>');
process.exit(1);
}
listen = process.argv[2];
forward = process.argv[3];
delay = process.argv[4];
server = net.createServer(function(stream) {
var client;
stream.on('connect', function() {
sys.puts('incoming connection');
client = net.createConnection(forward);
client.on('connect', function() {
sys.puts('connected through');
});
client.on('data', function(data) {
setTimeout(function() {
stream.write(data);
}, delay);
});
});
stream.on('data', function(data) {
client.write(data);
});
stream.on('end', function() {
client.end();
stream.end();
})
});
server.listen(listen);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment