iptables 规则通过 SSH 隧道传输 UDP

iptables 规则通过 SSH 隧道传输 UDP

问题

我有两个程序在两台远程计算机上运行并通过特定协议进行通信。我所知道的是,该协议仅通过 UDP 进行通信,并使用端口 1101 和 1102,并且两台计算机需要位于同一本地网络上。

我已经设法使用 VPN (p2p) 来做到这一点,但我想知道是否可以通过 SSH 隧道使用 iptables(此处 SSH 隧道是强制性的)。

我使用两台 Linux Debian wheezy 服务器在两个程序之间通过互联网打开隧道。我设法创建一个 SSH 隧道,将 UDP 流量从一侧重定向到另一侧(对于所有端口),但这不起作用。为此,我使用 socat(此处表示端口 1101):

transmission from computer1 --> port 1101/server1 --> socat to port 61101 --->
---> ssh tunnel ---> 
---> socat from port 61101--> port 1101/server2 --> reception on computer2

我的目标是使“server2”的行为从“computer2”的角度来看与“computer1”完全相同(反之亦然)。传输数据包时必须正确转换 IP 地址,并且传输的数据必须相应更改。

SSH 配置为使用密钥文件自动登录。

我的隧道配置

这就是我配置隧道的方式,以便在两个方向上重定向两个端口。

#From server1's shell 
$ ssh -f -N -T -R51101:localhost:51101 login@server2 # reverse tunnel for port 1101
$ ssh -f -N -T -R51102:localhost:51102 login@server2 # reverse tunnel for port 1102
$ ssh -f -N -T -L61101:localhost:61101 login@server2 # tunnel for port 1101
$ ssh -f -N -T -L61102:localhost:61102 login@server2 # tunnel for port 1102

#socat redirection on server1
$ socat TCP4-LISTEN:51101,fork UDP4:server1:1101 #redirects from the tunnel to the local 1101 port
$ socat TCP4-LISTEN:51102,fork UDP4:server1:1102 #redirects from the tunnel to the local 1102 port
$ sudo socat -d -d UDP4-LISTEN:1101,fork,bind=1101 TCP4:localhost:61101  #redirects to both tunnel and local UDP port 1101
$ sudo socat -d -d UDP4-LISTEN:1102,fork,bind=1102 TCP4:localhost:61102  #redirects to both tunnel and local UDP port 1102

#From server2's shell 
#socat redirection on server2
$ socat TCP4-LISTEN:61101,fork UDP4:server1:1101 #redirects from the tunnel to the local 1101 port
$ socat TCP4-LISTEN:61102,fork UDP4:server1:1102 #redirects from the tunnel to the local 1102 port
$ sudo socat -d -d UDP4-LISTEN:1101,fork,bind=1101 TCP4:localhost:51101  #redirects to both tunnel and local UDP port 1101
$ sudo socat -d -d UDP4-LISTEN:1102,fork,bind=1102 TCP4:localhost:51102  #redirects to both tunnel and local UDP port 1102

我认为可以使用 iptables 来做到这一点,但我不知道如何做到这一点。

有人有解决办法吗?或者关于如何使用 iptables 进行正确地址转换的秘诀?

相关内容