在 Linux 下将 TCP 流量重定向到 UNIX 域套接字

发布于 2024-08-19 13:04:18 字数 208 浏览 6 评论 0原文

假设旧版 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 技术交流群。

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

发布评论

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

评论(5

说谎友 2024-08-26 13:04:18

事实证明 socat 可用于实现此目的:

socat TCP-LISTEN:1234,reuseaddr,fork UNIX-CLIENT:/tmp/foo

并且增加了一些安全性:

socat TCP-LISTEN:1234,bind=127.0.0.1,reuseaddr,fork,su=nobody,range=127.0.0.0/8 UNIX-CLIENT:/tmp/foo

这些示例已经过测试并按预期工作。

Turns out socat can be used to achieve this:

socat TCP-LISTEN:1234,reuseaddr,fork UNIX-CLIENT:/tmp/foo

And with a bit of added security:

socat TCP-LISTEN:1234,bind=127.0.0.1,reuseaddr,fork,su=nobody,range=127.0.0.0/8 UNIX-CLIENT:/tmp/foo

These examples have been tested and work as expected.

青衫负雪 2024-08-26 13:04:18

最简单?可能是 Netcat(又名 nc):

nc -l 1234 | nc -U /tmp/foo

第一个命令监听端口 1234 用于传入连接,并将结果数据通过管道传输到第二个命令。第二个连接到 Unix 域套接字 /tmp/foo,并将其输入写入该套接字。请注意,这将只接受单个连接,并在该连接断开后立即退出。如果您想继续侦听更多连接,请使用 -k 选项:

nc -lk 1234 | nc -U /tmp/foo

您可以通过在一个终端中为该套接字设置侦听器来测试它是否正常工作:

nc -lUk /tmp/foo

并在另一个终端中写入它:

nc localhost 1234

< a href="http://www.dest-unreach.org/socat/" rel="noreferrer">socat,如 knorv推荐,功能更强大,但使用起来更复杂。

Easiest? Probably Netcat (aka nc):

nc -l 1234 | nc -U /tmp/foo

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:

nc -lk 1234 | nc -U /tmp/foo

You can test that this is working by setting up a listener for that socket in one terminal:

nc -lUk /tmp/foo

And writing to it in another:

nc localhost 1234

socat, as recommended by knorv, is more capable, but more complicated to use.

九公里浅绿 2024-08-26 13:04:18

您应该能够绑定到 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

蓝眼泪 2024-08-26 13:04:18

除了@knorv的答案:使用xinetd它可以像守护进程一样工作

# cat /etc/xined.d/mysrv
service mysrv
{
 disable = no
 type = UNLISTED
 socket_type = stream
 protocol = tcp
 wait = no
 server = /usr/bin/socat
 server_args = STDIN UNIX-CLIENT:/tmp/mysocket.sock
 bind = 127.0.0.1
 port = 1234
}

In additons to @knorv's answer: with xinetd it can work like a daemon

# cat /etc/xined.d/mysrv
service mysrv
{
 disable = no
 type = UNLISTED
 socket_type = stream
 protocol = tcp
 wait = no
 server = /usr/bin/socat
 server_args = STDIN UNIX-CLIENT:/tmp/mysocket.sock
 bind = 127.0.0.1
 port = 1234
}
梅窗月明清似水 2024-08-26 13:04:18

没有尝试过:但看起来“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

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