如何只允许隧道连接端口?

我想通过一个永久的ssh隧道来做一个git-daemon。 我完成了这个任务。 如何阻止任何远程未连接到GIT_DAEMON端口(在我的情况下是9418)?

我已经尝试了iptables中的简单规则(阻止除localhost之外的所有内容):

$ iptables -A INPUT -p tcp -d ! localhost --destination-port 9418 -j DROP 

但是它也阻塞了一个隧道(因为它保存了源IP地址)。 如果我还有一台防火墙主机,可以通过阻止任何远程连接到这个端口来完成,但是我需要这个主机来完成这项工作。

隧道是以两种方式之一创build的:

对于Windows:

 plink.exe -N -i <key> -L 127.0.0.1:9418:192.168.1.69:9418 tunnel@192.168.1.69 

对于Linux:

 ssh -N -i <key> -L 127.0.0.1:9418:192.168.1.69:9418 tunnel@192.168.1.69 

你可以试试这个( 未经测试 ):

 # accept localhost iptables -A INPUT -p tcp -d localhost --destination-port 9418 -j ACCEPT # send everyone else packing iptables -A INPUT -p tcp --destination-port 9418 -j DROP 

使用该iptables -L说:

 ACCEPT tcp -- anywhere localhost.localdomain tcp dpt:git DROP tcp -- anywhere anywhere tcp dpt:git 

编辑

这(可能)是如何设置隧道的:

 ssh -N -i <key> -L 127.0.0.1:9418:127.0.0.1:9418 tunnel@192.168.1.69 

重要的是后半部分是127.0.0.1而不是一个正常的IP

你实际上可以实现这一点,而不用使用iptables,只需简单地把git-daemon绑定到loopback接口,例如。

 git daemon --listen=127.0.0.1 

这将使它只能从本地主机连接,并且不需要root权限来设置。