Organizational Research By

Surprising Reserch Topic

node js forward all traffic from port a to port b


node js forward all traffic from port a to port b  using -'javascript,node.js,portforwarding'

I'm trying to forward all traffic from port 6999 to port 7000 (I know I could use iptables, but the idea is to use Node.js to do some packet inspection).

Here is the code I have sofar:

var net=require('net');
var compress=require('./node-compress/compress');

var ip='172.16.1.224';
var ipPort=6999;
var opPort=7000;

var output=net.createServer(function(connOut){
        var input=net.createServer(function(connIn){
                connIn.pipe(connOut);
        });
        input.listen(ipPort,ip);
});
output.listen(opPort,ip);


It just does not seem to work. When I do a tcpdump on port 7000, nothing shows up. Anyone have any suggestions?

Many thanks in advance,
    

asked Sep 18, 2015 by shegokar.anjeet
0 votes
43 views



Related Hot Questions

3 Answers

0 votes

Here's my go a it:

Supports giving the "from" and "to" from command line, and supports remote machines.

var net = require('net');

// parse "80" and "localhost:80" or even "42mEANINg-life.com:80"
var addrRegex = /^(([a-zA-Z\-\.0-9]+):)?(\d+)$/;

var addr = {
    from: addrRegex.exec(process.argv[2]),
    to: addrRegex.exec(process.argv[3])
};

if (!addr.from || !addr.to) {
    console.log('Usage:  ');
    return;
}

net.createServer(function(from) {
    var to = net.createConnection({
        host: addr.to[2],
        port: addr.to[3]
    });
    from.pipe(to);
    to.pipe(from);
}).listen(addr.from[3], addr.from[2]);

(save as proxy.js)

To forward from localhost:9001 => localhost:80

$ node proxy.js 9001 80

Or localhost:9001 => otherhost:80

$ node proxy.js 9001 otherhost:80

(This was based on Andrey's answer, thanks!)

answered Sep 18, 2015 by 20shahi
0 votes

you need to have createConnection on one side. Here is the script I use to forward traffic

var net = require('net');

var sourceport = 1234;
var destport = 1235;

net.createServer(function(s)
{
    var buff = "";
    var connected = false;
    var cli = net.createConnection(destport);
    s.on('data', function(d) {
        if (connected)
        {
           cli.write(d);
        } else {
           buff += d.toString();
        }
    });
    cli.on('connect', function() {
        connected = true;
        cli.write(buff);
    });
    cli.pipe(s);
}).listen(sourceport);
answered Sep 18, 2015 by vimaldas2005
0 votes

Have you looked at the Node.js module Hoxy?

http://www.youtube.com/watch?v=2YLfBTrVgZU

Quick description from the developer's README:

Hoxy is a web-hacking proxy for node.js, intended for use by web developers. Using hoxy, you can act as a "man in the middle" and alter HTTP requests and responses as they flow through, based on a set of conditional rules. As a running process, hoxy otherwise behaves like a standalone proxy server. Hoxy was inspired as a way to complement debuggers like Firebug, which let you manipulate the client runtime but not the underlying HTTP conversation.

This should work pretty well unless you're looking for a lower level, packet to packet inspection of the data.

answered Sep 18, 2015 by deepak

...