代理与netcat永远

我使用netcat代理VNC TCP服务器端口。 代理机器运行linux

这是我使用的命令:

mkfifo backpipe nc -l 5902 0<backpipe | nc 10.1.1.116 5902 1>backpipe 

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

但是,在每个VNC会话之后,netcat“代理服务器”将停止,这是netcat的工作方式。

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


作为一种解决方法,我把netcat命令行放在一个无限循环中:

 mkfifo backpipe while true; do nc -l 5902 0<backpipe | nc 10.1.1.116 5902 1>backpipe; done 

但我更喜欢一个“官方”的netcat解决scheme,根本不中断服务。


我已阅读有关“ – ”参数,但我不知道这是否适合的情况下,我还没有能够正确应用它。


补充说明:

当然,我可以通过不同的方式使用ssh隧道来实现这一点,但是我想要一个没有encryption开销的解决scheme,以尽可能地为VNC客户端做出响应。 否则,一个不同的代理解决scheme是可以的

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

-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. 

我注意到Debian / Ubuntu上的netcat-traditional包不会像应该那样继续监听。 在这种情况下,请使用netcat-openbsd软件包,然后重试!

或者,使用socat ,它更加针对您的代理服务器的用例。 一个随机的TCP转发器例子来自socat的manpage,当然需要一些修改。

  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.