除了端口80和22(入站和出站),如何使用OpenVPN进行所有通信?

我使用dynamicDNS服务,这是从我的路由器发送IP地址更新。 在路由器设置中,端口22和80被转发到我的服务器192.168.1.10。 我有我的服务器上运行的Web服务器和sshd。 从我的本地networking外,我可以去mydomain.dyndns.org,或ssh到我的服务器。 这工作正常,直到我连接到OpenVPN。 当我连接我的服务器到我的OpenVPN提供商时,我不能再从我的本地networkingssh到我的服务器。 http和ssh超时。 即使我删除了dyndns的东西,只是使用广域网IP,它仍然超时与OpenVPN连接,并停止OpenVPN时工作。

我的OpenVPN提供者(我不控制)将路由推给我(客户端)。 具体来说,当连接到openvpn时,运行以下命令:

/sbin/ifconfig tun0 10.15.0.74 pointopoint 10.15.0.73 mtu 1500 /sbin/route add -net 141.255.164.66 netmask 255.255.255.255 gw 192.168.1.1 /sbin/route add -net 0.0.0.0 netmask 128.0.0.0 gw 10.15.0.73 /sbin/route add -net 128.0.0.0 netmask 128.0.0.0 gw 10.15.0.73 /sbin/route add -net 10.15.0.1 netmask 255.255.255.255 gw 10.15.0.73 

当vpn连接closures时,添加的路由将被删除。

问题是,这通过VPN路由一切 。 相反,我希望我的服务器对除端口80和22(入站和出站)以外的所有通信使用openvpn。 即我想能够使用http和ssh连接到我的服务器。

我相信类似的问题已经被问到,但他们似乎都通过VPN和其他一切路由特定的端口,而不是通过vpn。 我无法适应他们的目的。

编辑:

我的路由器是192.168.1.1(dd-wrt路由器)。 我的猜测是,应该标记ssh / httpstream量并将其设置为使用192.168.1.1作为默认网关。 但是,我不知道如何。

编辑2:

根据leucos的build议,我尝试了以下内容:

 root@FILESERVER:~# curl ipecho.net/plain; echo ###.###.135.118 root@FILESERVER:~# echo "Connecting to OpenVPN in other terminal" Connecting to OpenVPN in other terminal root@FILESERVER:~# curl ipecho.net/plain; echo ###.###.164.93 root@FILESERVER:~# echo "1 NOVPN" >> /etc/iproute2/rt_tables root@FILESERVER:~# ip route add default via 192.168.1.1 dev p4p1 table NOVPN root@FILESERVER:~# ip route show table NOVPN default via 192.168.1.1 dev p4p1 root@FILESERVER:~# ip rule add from all fwmark 0x1 lookup NOVPN root@FILESERVER:~# iptables -t mangle -I PREROUTING -p tcp --dport 22 -j MARK --set-mark 1 root@FILESERVER:~# iptables -t mangle -I PREROUTING -p tcp --dport 80 -j MARK --set-mark 1 root@FILESERVER:~# curl ipecho.net/plain; echo ###.###.164.93 

现在我不太确定ipecho.net 如何工作的,但是我的猜测是它已经收到来自###。###。164.93,这(我猜)意味着端口80的stream量不是通过p4p1(我的本地接口),而是通过tun0路由(由OpenVPN设置)。 试图从我的networking外部连接也继续超时。

“正常”路由决策基于目的地和路由表前缀和掩码。 如果您想根据其他数据(例如TCP端口)作出路由决定,则必须使用ip rule

使用ip rule ,基本上可以要求内核在发生某种情况时使用替代路由表。 你通常在iptables中使用数据包标记。

下面是一个未经testing的例子,您可以试试这个(如果您只有远程连接到您的服务器,请不要玩这个)。

 # Create an alternate routing table echo "1 NOVPN" >> /etc/iproute2/rt_tables # Create the routes for this table # Actually, you just want to set the default gateway ip route add default via 192.168.1.1 dev eth0 table NOVPN # Check results with ip route show table NOVPN # Now tell the kernel that this routing table should be used when # a packet waiting to be routed has a specific "mark" ip rule add from all fwmark 0x1 lookup NOVPN # Then mark all the required packets with the same mark use above iptables -t mangle -I OUTPUT -p tcp --sport 22 -j MARK --set-mark 1 iptables -t mangle -I OUTPUT -p tcp --sport 80 -j MARK --set-mark 1 

祝你好运。

编辑:

您可能还需要调整您的源地址:

 iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to-source 192.168.1.10 

如果不起作用,请检查以下情况:

 tcpdump -enieth0 port 80 

(我在我的回复中假设正确的outbount以太网接口是eth0)

这导致所有stream量通过VPN

 /sbin/route add -net 0.0.0.0 netmask 128.0.0.0 gw 10.15.0.73 /sbin/route add -net 128.0.0.0 netmask 128.0.0.0 gw 10.15.0.73 

你有没有在configuration文件中指定? 根据OpenVPN文档 ,相关的configuration指令是redirect-gateway