Javascript Node.js - 将所有流量从端口 A 转发到端口 B
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6490898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js - forward all traffic from port A to port B
提问by Eamorr
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).
我正在尝试将所有流量从端口 6999 转发到端口 7000(我知道我可以使用 iptables,但想法是使用 Node.js 进行一些数据包检查)。
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?
它似乎不起作用。当我在端口 7000 上执行 tcpdump 时,没有任何显示。有人有什么建议吗?
Many thanks in advance,
提前谢谢了,
回答by joonas.fi
Here's my go at it:
这是我的做法:
Supports giving the "from" and "to" from command line, and supports remote machines.
支持从命令行给出“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]);
(save as proxy.js)
(另存为proxy.js)
To forward from localhost:9001 => localhost:80
从 localhost:9001 => localhost:80 转发
$ node proxy.js 9001 80
Or localhost:9001 => otherhost:80
或 localhost:9001 => otherhost:80
$ node proxy.js 9001 otherhost:80
(This was based on Andrey's answer, thanks!)
(这是基于安德烈的回答,谢谢!)
回答by Andrey Sidorov
you need to have createConnection on one side. Here is the script I use to forward traffic
您需要在一侧有 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);
回答by slickplaid
Have you looked at the Node.js module Hoxy?
你看过 Node.js 模块Hoxy吗?
http://www.youtube.com/watch?v=2YLfBTrVgZU
http://www.youtube.com/watch?v=2YLfBTrVgZU
Quick description from the developer's README:
来自开发者 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.
Hoxy 是 node.js 的网络黑客代理,供网络开发人员使用。使用 hoxy,您可以充当“中间人”并根据一组条件规则在 HTTP 请求和响应流过时更改它们。作为一个正在运行的进程,hoxy 的行为就像一个独立的代理服务器。Hoxy 的灵感来自于补充调试器(如 Firebug)的一种方式,它允许您操纵客户端运行时而不是底层的 HTTP 对话。
This should work pretty well unless you're looking for a lower level, packet to packet inspection of the data.
除非您正在寻找较低级别的数据包到数据包检查,否则这应该可以很好地工作。