22

我正在尝试将所有流量从端口 6999 转发到端口 7000(我知道我可以使用 iptables,但我的想法是使用 Node.js 进行一些数据包检查)。

这是我到目前为止的代码:

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

它似乎不起作用。当我在端口 7000 上执行 tcpdump 时,什么也没有出现。有人有什么建议吗?

提前谢谢了,

4

3 回答 3

45

这是我的尝试:

支持从命令行给出“from”和“to”,并支持远程机器。

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: <from> <to>');
    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]);

(另存为proxy.js)

从 localhost:9001 => localhost:80 转发

$ node proxy.js 9001 80

或 localhost:9001 => otherhost:80

$ node proxy.js 9001 otherhost:80

(这是基于安德烈的回答,谢谢!)

于 2013-10-28T14:27:40.180 回答
16

你需要在一侧有 createConnection 。这是我用来转发流量的脚本

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);
于 2011-06-28T00:52:19.547 回答
4

你看过 Node.js 模块Hoxy吗?

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

开发人员自述文件中的快速描述:

Hoxy 是 node.js 的网络黑客代理,供网络开发人员使用。使用 hoxy,您可以充当“中间人”,并根据一组条件规则在 HTTP 请求和响应流经时更改它们。作为一个正在运行的进程,hoxy 在其他方面表现得像一个独立的代理服务器。Hoxy 的灵感是作为对 Firebug 等调试器的补充,它可以让您操作客户端运行时,但不能操作底层的 HTTP 对话。

除非您正在寻找较低级别的数据包到数据包检查,否则这应该工作得很好。

于 2011-06-27T11:09:11.270 回答