Node.js - 将所有流量从端口 A 转发到端口 B

发布于 2024-11-17 05:18:18 字数 548 浏览 6 评论 0原文

我正在尝试将所有流量从端口 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 时,没有任何显示。有人有什么建议吗?

预先非常感谢,

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,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

゛清羽墨安 2024-11-24 05:18:18

这是我的做法:

支持从命令行给出“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 ==>本地主机:80

$ node proxy.js 9001 80

或本地主机:9001 ==> otherhost:80

$ node proxy.js 9001 otherhost:80

(这是基于 Andrey 的回答,谢谢!)

Here's my go at 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: <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]);

(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!)

孤蝉 2024-11-24 05:18:18

您需要在一侧有 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);

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);
森罗 2024-11-24 05:18:18

您看过 Node.js 模块 Hoxy 吗?

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

的快速说明开发者自述文件:

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文