启用 OpenVPN 客户端的 Tomato 路由器上的端口转发

启用 OpenVPN 客户端的 Tomato 路由器上的端口转发

当我在基于 Tomato 的路由器上启用 OpenVPN 客户端时,端口转发似乎不再起作用。我有一台安装了此固件的华硕 RT-N16 路由器:

Tomato 固件 1.28.0000 MIPSR2-115 K26 USB VPN(shibby 构建)

我最近注册了一项 VPN 服务,并按照他们提供的指南在我的路由器上设置 VPN:http://www.ipvanish.com/visualguides/OpenVPN/Tomato/

按照指南操作后,我的所有流量都通过隧道传输,这很棒。但是,我有一个连接到此路由器的 ubuntu 服务器,该服务器托管 apache(在端口 80 和 443 上),我无法再从外部访问它。我是 VPN 世界的新手,如果我的理论有误,请纠正我。我希望当 WAN 请求进入 VPN 外部时,由于端口转发,它们会以这种方式得到响应。我的猜测是请求进来了,但响应却消失在隧道中。

我在其他地方看到过多个帖子建议需要在路由器的 iptables 中添加其他规则,如下所示:

iptables -t nat -A PREROUTING -p tcp --dport <your_port_number> -j DNAT --to-destination <your_destination_IP_address>
iptables -A FORWARD -s <your_VPN_IP> -p tcp --dport <your_port_number> -j ACCEPT

但是,我没有成功实现这一点。我可以做我正在尝试的事情吗?如果可以,我遗漏了哪些步骤?以下是启用 VPN 客户端后路由器上的 ifconfig 和 iptables 的输出。谢谢。

br0        Link encap:Ethernet  HWaddr BC:AE:C5:E8:2B:72
           inet addr:192.168.1.1  Bcast:192.168.1.255  Mask:255.255.255.0
           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
           RX packets:4906280 errors:0 dropped:0 overruns:0 frame:0
           TX packets:6593105 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:0
           RX bytes:3320899619 (3.0 GiB)  TX bytes:3055186673 (2.8 GiB)

eth0       Link encap:Ethernet  HWaddr BC:AE:C5:E8:2B:72
           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
           RX packets:12317339 errors:0 dropped:0 overruns:0 frame:0
           TX packets:11550871 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:1000
           RX bytes:2602762531 (2.4 GiB)  TX bytes:2190393333 (2.0 GiB)
           Interrupt:4 Base address:0x2000

eth1       Link encap:Ethernet  HWaddr BC:AE:C5:E8:2B:74
           UP BROADCAST RUNNING ALLMULTI MULTICAST  MTU:1500  Metric:1
           RX packets:141603 errors:0 dropped:0 overruns:0 frame:12229612
           TX packets:253818 errors:17 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:1000
           RX bytes:11473501 (10.9 MiB)  TX bytes:323350737 (308.3 MiB)
           Interrupt:3 Base address:0x1000

lo         Link encap:Local Loopback
           inet addr:127.0.0.1  Mask:255.0.0.0
           inet6 addr: ::1/128 Scope:Host
           UP LOOPBACK RUNNING MULTICAST  MTU:16436  Metric:1
           RX packets:38 errors:0 dropped:0 overruns:0 frame:0
           TX packets:38 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:0
           RX bytes:3675 (3.5 KiB)  TX bytes:3675 (3.5 KiB)

tun11      Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
           inet addr:172.20.25.181  P-t-P:172.20.25.181  Mask:255.255.248.0
           UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1500  Metric:1
           RX packets:1649 errors:0 dropped:0 overruns:0 frame:0
           TX packets:1186 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:100
           RX bytes:1808407 (1.7 MiB)  TX bytes:129251 (126.2 KiB)

vlan1      Link encap:Ethernet  HWaddr BC:AE:C5:E8:2B:72
           UP BROADCAST RUNNING ALLMULTI MULTICAST  MTU:1500  Metric:1
           RX packets:5125335 errors:0 dropped:0 overruns:0 frame:0
           TX packets:6732029 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:0
           RX bytes:3654686593 (3.4 GiB)  TX bytes:3092936664 (2.8 GiB)

vlan2      Link encap:Ethernet  HWaddr BC:AE:C5:E8:2B:73
           inet addr:98.228.254.52  Bcast:98.228.255.255  Mask:255.255.248.0
           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
           RX packets:7190398 errors:0 dropped:0 overruns:0 frame:0
           TX packets:4818842 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:0
           RX bytes:3021257256 (2.8 GiB)  TX bytes:3392423965 (3.1 GiB)

Chain INPUT (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
  119 10231 ACCEPT     all  --  tun11  *       0.0.0.0/0            0.0.0.0/0
   14  1470 DROP       all  --  br0    *       0.0.0.0/0            98.228.254.52
  393  163K DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0           state INVALID
19219 6264K ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
   11   861 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
42101 2556K ACCEPT     all  --  br0    *       0.0.0.0/0            0.0.0.0/0
 4974 1716K ACCEPT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0           udp spt:67 dpt:68

Chain FORWARD (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
 3269 2974K ACCEPT     all  --  tun11  *       0.0.0.0/0            0.0.0.0/0
  11M   11G            all  --  *      *       0.0.0.0/0            0.0.0.0/0           account: network/netmask: 192.168.1.0/255.255.255.0 name: lan
13009 5871K ACCEPT     all  --  br0    br0     0.0.0.0/0            0.0.0.0/0
  122  5288 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0           state INVALID
37051 2152K TCPMSS     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp flags:0x06/0x02 TCPMSS clamp to PMTU
  11M   11G ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
 5081  302K wanin      all  --  vlan2  *       0.0.0.0/0            0.0.0.0/0
20195 1208K wanout     all  --  *      vlan2   0.0.0.0/0            0.0.0.0/0
20300 1216K ACCEPT     all  --  br0    *       0.0.0.0/0            0.0.0.0/0

Chain OUTPUT (policy ACCEPT 3994 packets, 1258K bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain wanin (1 references)
 pkts bytes target     prot opt in     out     source               destination
 2186  125K ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.1.132       tcp dpt:49151
 1054 79129 ACCEPT     udp  --  *      *       0.0.0.0/0            192.168.1.132       udp dpt:49151
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.1.132       tcp dpt:8112
  172 10288 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.1.132       tcp dpt:443
    5   358 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.1.132       tcp dpt:993
    1    52 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.1.132       tcp dpt:5222
    2   120 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.1.132       tcp dpt:5269
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.1.132       tcp dpt:25
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.1.132       tcp dpt:110
    4   240 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.1.132       tcp dpt:26
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.1.115       tcp dpt:49491
    0     0 ACCEPT     udp  --  *      *       0.0.0.0/0            192.168.1.115       udp dpt:49491
 1610 83944 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.1.132       tcp dpt:32400
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.1.132       tcp dpt:465
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.1.132       tcp dpt:587
    3   168 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.1.132       tcp dpt:22
   29  1704 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.1.132       tcp dpt:80
    0     0 ACCEPT     udp  --  *      *       0.0.0.0/0            192.168.1.136       udp dpt:88
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.1.136       tcp dpt:3074
    0     0 ACCEPT     udp  --  *      *       0.0.0.0/0            192.168.1.136       udp dpt:3074
    1    44 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.1.136       tcp dpt:53
   14  1143 ACCEPT     udp  --  *      *       0.0.0.0/0            192.168.1.136       udp dpt:53

Chain wanout (1 references)
 pkts bytes target     prot opt in     out     source               destination

Chain PREROUTING (policy ACCEPT 625 packets, 97194 bytes)
 pkts bytes target     prot opt in     out     source               destination
 5625  364K WANPREROUTING  all  --  *      *       0.0.0.0/0            98.228.254.52
    0     0 DROP       all  --  vlan2  *       0.0.0.0/0            192.168.1.0/24

Chain POSTROUTING (policy ACCEPT 174 packets, 10820 bytes)
 pkts bytes target     prot opt in     out     source               destination
  142  9572 MASQUERADE  all  --  *      tun11   192.168.1.0/24       0.0.0.0/0
    0     0 SNAT       tcp  --  *      *       192.168.1.0/24       192.168.1.132       tcp dpt:49151 to:98.228.254.52
    0     0 SNAT       udp  --  *      *       192.168.1.0/24       192.168.1.132       udp dpt:49151 to:98.228.254.52
    0     0 SNAT       tcp  --  *      *       192.168.1.0/24       192.168.1.132       tcp dpt:8112 to:98.228.254.52
  333 17356 SNAT       tcp  --  *      *       192.168.1.0/24       192.168.1.132       tcp dpt:443 to:98.228.254.52
   63  5305 SNAT       tcp  --  *      *       192.168.1.0/24       192.168.1.132       tcp dpt:993 to:98.228.254.52
    2   230 SNAT       tcp  --  *      *       192.168.1.0/24       192.168.1.132       tcp dpt:5222 to:98.228.254.52
    0     0 SNAT       tcp  --  *      *       192.168.1.0/24       192.168.1.132       tcp dpt:5269 to:98.228.254.52
    0     0 SNAT       tcp  --  *      *       192.168.1.0/24       192.168.1.132       tcp dpt:25 to:98.228.254.52
    0     0 SNAT       tcp  --  *      *       192.168.1.0/24       192.168.1.132       tcp dpt:110 to:98.228.254.52
    0     0 SNAT       tcp  --  *      *       192.168.1.0/24       192.168.1.132       tcp dpt:26 to:98.228.254.52
    0     0 SNAT       tcp  --  *      *       192.168.1.0/24       192.168.1.115       tcp dpt:49491 to:98.228.254.52
    0     0 SNAT       udp  --  *      *       192.168.1.0/24       192.168.1.115       udp dpt:49491 to:98.228.254.52
   10   600 SNAT       tcp  --  *      *       192.168.1.0/24       192.168.1.132       tcp dpt:32400 to:98.228.254.52
    0     0 SNAT       tcp  --  *      *       192.168.1.0/24       192.168.1.132       tcp dpt:465 to:98.228.254.52
    0     0 SNAT       tcp  --  *      *       192.168.1.0/24       192.168.1.132       tcp dpt:587 to:98.228.254.52
    0     0 SNAT       tcp  --  *      *       192.168.1.0/24       192.168.1.132       tcp dpt:22 to:98.228.254.52
  111  6084 SNAT       tcp  --  *      *       192.168.1.0/24       192.168.1.132       tcp dpt:80 to:98.228.254.52
    0     0 SNAT       udp  --  *      *       192.168.1.0/24       192.168.1.136       udp dpt:88 to:98.228.254.52
    0     0 SNAT       tcp  --  *      *       192.168.1.0/24       192.168.1.136       tcp dpt:3074 to:98.228.254.52
    0     0 SNAT       udp  --  *      *       192.168.1.0/24       192.168.1.136       udp dpt:3074 to:98.228.254.52
    0     0 SNAT       tcp  --  *      *       192.168.1.0/24       192.168.1.136       tcp dpt:53 to:98.228.254.52
    0     0 SNAT       udp  --  *      *       192.168.1.0/24       192.168.1.136       udp dpt:53 to:98.228.254.52
29907 1817K MASQUERADE  all  --  *      vlan2   0.0.0.0/0            0.0.0.0/0

Chain OUTPUT (policy ACCEPT 180 packets, 11182 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain WANPREROUTING (1 references)
 pkts bytes target     prot opt in     out     source               destination
    4   172 DNAT       icmp --  *      *       0.0.0.0/0            0.0.0.0/0           to:192.168.1.1
 2103  120K DNAT       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:49151 to:192.168.1.132
  834 57491 DNAT       udp  --  *      *       0.0.0.0/0            0.0.0.0/0           udp dpt:49151 to:192.168.1.132
    0     0 DNAT       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:8112 to:192.168.1.132
  505 27636 DNAT       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:443 to:192.168.1.132
   68  5663 DNAT       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:993 to:192.168.1.132
    3   282 DNAT       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:5222 to:192.168.1.132
    2   120 DNAT       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:5269 to:192.168.1.132
    0     0 DNAT       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:25 to:192.168.1.132
    0     0 DNAT       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:110 to:192.168.1.132
    4   240 DNAT       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:26 to:192.168.1.132
    0     0 DNAT       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:49491 to:192.168.1.115
    0     0 DNAT       udp  --  *      *       0.0.0.0/0            0.0.0.0/0           udp dpt:49491 to:192.168.1.115
 1633 85204 DNAT       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:32400 to:192.168.1.132
    0     0 DNAT       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:465 to:192.168.1.132
    0     0 DNAT       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:587 to:192.168.1.132
    3   168 DNAT       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:22 to:192.168.1.132
  140  7788 DNAT       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:80 to:192.168.1.132
    0     0 DNAT       udp  --  *      *       0.0.0.0/0            0.0.0.0/0           udp dpt:88 to:192.168.1.136
    0     0 DNAT       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:3074 to:192.168.1.136
    0     0 DNAT       udp  --  *      *       0.0.0.0/0            0.0.0.0/0           udp dpt:3074 to:192.168.1.136
    1    40 DNAT       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:53 to:192.168.1.136
   14  1143 DNAT       udp  --  *      *       0.0.0.0/0            0.0.0.0/0           udp dpt:53 to:192.168.1.136

答案1

端口转发不再起作用的说法并不正确。相反,实际情况是来自 Ubuntu 服务器的回复通过 VPN 路由,因此,当它到达试图联系 Ubuntu 服务器的 PC 时,它将来自与初始消息写入的 IP 地址不同的 IP 地址。出于明显的安全问题,所有 PC 都被指示放弃这些伪造的(?) 回复包。

因此,基本思路是强制您的 LAN 允许 VPN 之外的回复(来自 Ubuntu 服务器)。这需要策略路由IE,根据源(而不是目标!)IP 地址同时使用两个路由表。策略路由有时也称为源路由

您可以在番茄路线上找到有关如何执行此操作的明确说明服务器故障地点,这里。这是以以下内容开头的贡献:我终于做到了。我认为该帖子被不公正地关闭了,但贡献却非常有用。如果您在遵循说明时遇到具体问题,请回来。

答案2

感谢 MariusMatutiae 的上述回答,我能够使用提供的链接中的答案解决这个问题。我将包含的脚本的所有功劳都归功于 grdnkln 的 serverfault 答案。我调整了他的脚本以更好地满足我的需求,因为他希望默认情况下从 VPN 中排除所有流量,而我想要做相反的事情。所以我的最终解决方案是默认情况下包括 VPN 中的所有流量,并添加来自我的服务器的端口 80/443 响应的排除。

此外,我在底部为 DynDns 的 IP 查找工具 (checkip.dyndns.com) 添加了一些例外。我一度陷入困境,因为我的动态 IP 解析器工具 (ddclient) 正在获取新的 VPN IP 并设置我的所有域名主机记录以解析该 IP。

# This code goes in the WAN UP section of the Tomato GUI.
#
# This script configures "selective" VPN routing. Normally Tomato will route ALL traffic out
# the OpenVPN tunnel. These changes to iptables allow some outbound traffic to use the VPN, and some
# traffic to bypass the VPN and use the regular Internet instead.
#
#  To list the current rules on the router, issue the command:
#      iptables -t mangle -L PREROUTING
#
#  Flush/reset all the rules to default by issuing the command:
#      iptables -t mangle -F PREROUTING
#

#
# First it is necessary to disable Reverse Path Filtering on all
# current and future network interfaces:
#
for i in /proc/sys/net/ipv4/conf/*/rp_filter ; do
      echo 0 > $i
done

#
# Delete and table 100 and flush any existing rules if they exist.
#
ip route flush table 100
ip route del default table 100
ip rule del fwmark 1 table 100
ip route flush cache
iptables -t mangle -F PREROUTING

#
# Copy all non-default and non-VPN related routes from the main table into table 100.
# Then configure table 100 to route all traffic out the WAN gateway and assign it mark "1"
#
# NOTE: Here I assume the OpenVPN tunnel is named "tun11".
#
#
ip route show table main | grep -Ev ^default | grep -Ev tun11 \
  | while read ROUTE ; do
      ip route add table 100 $ROUTE
done
ip route add default table 100 via $(nvram get wan_gateway)
ip rule add fwmark 1 table 100
ip route flush cache

#
# Define the routing policies for the traffic. The rules will be applied in the order that they
# are listed. In the end, packets with MARK set to "0" will pass through the VPN. If MARK is set
# to "1" it will bypass the VPN.
#
# EXAMPLES:
#
#  All LAN traffic will bypass the VPN (Useful to put this rule first, so all traffic bypasses the VPN and you can configure exceptions afterwards)
#    iptables -t mangle -A PREROUTING -i br0 -j MARK --set-mark 1
#  Ports 80 and 443 will bypass the VPN
#    iptables -t mangle -A PREROUTING -i br0 -p tcp -m multiport --dport 80,443 -j MARK --set-mark 1
#  All traffic from a particular computer on the LAN will use the VPN
#    iptables -t mangle -A PREROUTING -i br0 -m iprange --src-range 192.168.1.2 -j MARK --set-mark 0
#  All traffic to a specific Internet IP address will use the VPN
#    iptables -t mangle -A PREROUTING -i br0 -m iprange --dst-range 216.146.38.70 -j MARK --set-mark 0
#  All UDP and ICMP traffic will bypass the VPN
#    iptables -t mangle -A PREROUTING -i br0 -p udp -j MARK --set-mark 1
#    iptables -t mangle -A PREROUTING -i br0 -p icmp -j MARK --set-mark 1

# Whitelist ports for server
iptables -t mangle -A PREROUTING -i br0 -p tcp -m multiport --sport 80,443 -s 192.168.1.132 -j MARK --set-mark 1
# DynDNS lockup IPs
iptables -t mangle -A PREROUTING -i br0 -m iprange --dst-range 216.146.38.1-216.146.43.254 -j MARK --set-mark 1
iptables -t mangle -A PREROUTING -i br0 -m iprange --dst-range 91.198.22.1-91.198.22.254 -j MARK --set-mark 1

相关内容