easierbycode
4/21/2013 - 8:36 AM

tcpproxy.js

// Logging TCP proxy. Forwards traffic to the given host/port and logs
// everything in both directions.
//
// e.g.
//    $ node tcpproxy 4567 www.songkick.com 80
//    $ curl -H 'Host: www.songkick.com' localhost:4567/

var net  = require('net'),
    me   = process.argv[2],
    host = process.argv[3],
    port = process.argv[4];

net.createServer(function(conn) {
  var forward = net.connect({host: host, port: port});

  conn.pipe(forward);
  forward.pipe(conn);

  conn.pipe(process.stdout);
  forward.pipe(process.stdout);
})
.listen(me);