Node.js - forward all traffic from port A to port B

后端 未结 3 1432
小鲜肉
小鲜肉 2020-12-02 17:29

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 th

3条回答
  •  离开以前
    2020-12-02 18:14

    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);
    

提交回复
热议问题