在 Linux 下将 TCP 流量重定向到 UNIX 域套接字
假设旧版 Linux 应用程序正在侦听 UNIX 域套接字 /tmp/foo
。
除了通过 UNIX 域套接字机制与此遗留应用程序进行通信之外,我希望能够通过端口 1234 上的 TCP 连接来连接到它。
绑定到 TCP 端口 1234 然后重定向所有传入连接的最简单方法是什么到 UNIX 域套接字 /tmp/foo
?
Assume a legacy Linux application listening on a UNIX domain socket /tmp/foo
.
In addition to communicating with this legacy application over the UNIX domain socket mechanism I want to be able to connect to it via a TCP-connection on port say 1234.
What is the easiest way to bind to TCP port 1234 and then redirect all incoming connections to the UNIX domain socket /tmp/foo
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
事实证明 socat 可用于实现此目的:
并且增加了一些安全性:
这些示例已经过测试并按预期工作。
Turns out socat can be used to achieve this:
And with a bit of added security:
These examples have been tested and work as expected.
最简单?可能是 Netcat(又名
nc
):第一个命令监听端口 1234 用于传入连接,并将结果数据通过管道传输到第二个命令。第二个连接到 Unix 域套接字
/tmp/foo
,并将其输入写入该套接字。请注意,这将只接受单个连接,并在该连接断开后立即退出。如果您想继续侦听更多连接,请使用-k
选项:您可以通过在一个终端中为该套接字设置侦听器来测试它是否正常工作:
并在另一个终端中写入它:
< a href="http://www.dest-unreach.org/socat/" rel="noreferrer">socat,如 knorv推荐,功能更强大,但使用起来更复杂。
Easiest? Probably Netcat (aka
nc
):The first command listens on port 1234 for incoming connections, and pipes the resulting data to the second command. The second connects to the Unix domain socket
/tmp/foo
, and writes its input to that socket. Note that this will only accept a single connection, and exit as soon as that connection is dropped. If you want to keep listening for more connections, use the-k
option:You can test that this is working by setting up a listener for that socket in one terminal:
And writing to it in another:
socat, as recommended by knorv, is more capable, but more complicated to use.
您应该能够绑定到 TCP 1234,获取 /tmp/foo 的套接字 fd,并使用 select 调用“监听”1234 和 /tmp/foo 上的数据。任何写入 1234 的数据都会重写到 /tmp/foo,反之亦然。
您现在充当代理并来回传输数据。
这是一个可能有帮助的网页: http://osr507doc.sco .com/en/netguide/dusockC.io_ Multiplexing.html
You should be able to bind to TCP 1234, get a socket fd for /tmp/foo and use the select call to 'listen' for data on both 1234, and /tmp/foo. Any data written to 1234, you rewrite to /tmp/foo and vice-versa.
You now act as a proxy and transfer data back and forth.
And here is a web-page which might help: http://osr507doc.sco.com/en/netguide/dusockC.io_multiplexing.html
除了@knorv的答案:使用
xinetd
它可以像守护进程一样工作In additons to @knorv's answer: with
xinetd
it can work like a daemon没有尝试过:但看起来“lighttpd”可以为您做到这一点:
http:// /redmine.lighttpd.net/wiki/lighttpd/Docs:ModProxyCore
Not tried it : but it looks like 'lighttpd' can do this for you:
http://redmine.lighttpd.net/wiki/lighttpd/Docs:ModProxyCore