永远使用 netcat 代理

Alo*_*nez 17 linux proxy tcp service netcat

我正在用 netcat 代理一个 VNC TCP 服务器端口。代理机器运行linux。

这是我使用的命令:

mkfifo backpipe
nc -l 5902  0<backpipe | nc 10.1.1.116 5902 1>backpipe
Run Code Online (Sandbox Code Playgroud)

10.1.1.116 是“远程”机器,原始 VNC 服务运行在端口 5902 上。执行此命令后,本地主机上的 VNC 服务可用于其他机器。

但是在每次 VNC 会话之后,netcat“代理服务器”都会停止,这就是 netcat 的工作原理。

在 VNC 会话终止后,如何让 netcat 保持“代理服务”运行?


作为一种解决方法,我将 netcat 命令行置于无限循环中:

mkfifo backpipe
while true; do   nc -l 5902  0<backpipe | nc 10.1.1.116 5902 1>backpipe; done
Run Code Online (Sandbox Code Playgroud)

但我更喜欢“官方”的 netcat 解决方案,它根本不会中断服务。


我已经阅读了“-”参数,但我不确定这是否适合这种情况,而且我还无法正确应用它。


补充说明:

当然,我可以通过 ssh 隧道以不同的方式做到这一点,但我想要一个没有加密开销的解决方案,使其尽可能响应 VNC 客户端。否则,可以使用不同的代理解决方案。

客户端必须是 VNC,没有其他协议是可能的。

ger*_*ijk 25

-k选项应该可以解决问题。

从联机帮助页nc(1)

 -k      Forces nc to stay listening for another connection after its
         current connection is completed.  It is an error to use this
         option without the -l option.
Run Code Online (Sandbox Code Playgroud)

我注意到netcat-traditionalDebian/Ubuntu 上的软件包没有像它应该的那样继续监听。在这种情况下,请改用该netcat-openbsd软件包并重试!

或者,使用socat,它更适合您的代理服务器用例。来自联机帮助页的随机 TCP 转发器示例socat当然需要一些修改。

   socat -d -d -lmlocal2 \
   TCP4-LISTEN:80,bind=myaddr1,reuseaddr,fork,su=nobody,range=10.0.0.0/8 \
   TCP4:www.domain.org:80,bind=myaddr2

          TCP  port  forwarder,  each  side  bound to another local IP
          address (bind). This example  handles  an  almost  arbitrary
          number  of parallel or consecutive connections by fork'ing a
          new process after each accept() . It provides a little secu?
          rity by su'ing to user nobody after forking; it only permits
          connections from the private  10  network  (range);  due  to
          reuseaddr,   it   allows   immediate  restart  after  master
          process's termination, even if some child  sockets  are  not
          completely  shut down.  With -lmlocal2, socat logs to stderr
          until successfully reaching the accept loop. Further logging
          is directed to syslog with facility local2.
Run Code Online (Sandbox Code Playgroud)