Wireguard WAN IP 伺服器可從路由器訪問,但不能從 LAN 存取

Wireguard WAN IP 伺服器可從路由器訪問,但不能從 LAN 存取

我正在使用 OpenWRT OpenWrt 19.07.8 r11364-ef56c85848 的 GL.inet 路由器。

我已在遠端電腦上設定了 Wireguard 伺服器。在未連接 VPN 的情況下,我可以使用其公用 IP 從 LAN 存取該伺服器。連接 VPN 後,我可以使用內部 IP 存取它,但無法再從 LAN 上的電腦透過外部 IP 存取它。

Traceroute 顯示封包在路由器處失敗,沒有到主機的路由:

~ % ping 35.190.161.xxx
PING 35.190.161.169 (35.190.161.xxx): 56 data bytes
92 bytes from router.local.wan (192.168.1.254): Destination Host Unreachable

但是,如果我 ssh 進入路由器,它不僅會顯示預期的路由,而且 ping 和 Traceroute 也會成功:

~# netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         100.64.0.1      0.0.0.0         UG        0 0          0 wan
10.66.66.0      *               255.255.255.0   U         0 0          0 wg0
34.120.255.244  *               255.255.255.255 UH        0 0          0 wan
35.190.161.xxx  100.64.0.1      255.255.255.255 UGH       0 0          0 wan
100.64.0.0      *               255.192.0.0     U         0 0          0 wan
192.168.0.0     *               255.255.252.0   U         0 0          0 br-lan

~# ping 35.190.161.xxx
PING 35.190.161.xxx (35.190.161.xxx): 56 data bytes
64 bytes from 35.190.161.xxx: seq=0 ttl=59 time=243.335 ms

我對此客戶端的 Wireguard 設定是:

[Interface]
PrivateKey = xxxxxxxxxxxxxxxxxxxxxxxxxxxlMxuhwtB9vV2Gpks=
Address = 10.66.66.3/32,fd42:42:42::3/128
DNS = 8.8.8.8,8.8.4.4

[Peer]
PublicKey = xxxxxxxxxxxxxxxxxxxxxxxxkIIPFsO2/EuXDNbeR3g=
PresharedKey = xxxxxxxxxxxxxxxxxxxxxxxxYnnXy4CZUMUzGBAieqU=
Endpoint = 35.190.161.xxx:60242
AllowedIPs = 10.66.66.0/24,::/0

當我使用內部IP存取遠端伺服器(例如透過ssh),必須根據是否建立VPN來選擇正確的位址,很不方便。

我的 Wireguard 配置中是否缺少某些內容或是否有其他問題?

答案1

我最近也面臨這個問題。我發現我的路由器為伺服器 IP 位址新增了 IP 規則(表 31):

LUCI 路由表的螢幕截圖

root@GL-MT300N-V2:~# ip rule
0:  from all lookup local
31: from all fwmark 0x60000/0x60000 lookup 31
1001:   from all iif eth0.2 lookup 1
2001:   from all fwmark 0x100/0x3f00 lookup 1
2061:   from all fwmark 0x3d00/0x3f00 blackhole
2062:   from all fwmark 0x3e00/0x3f00 unreachable
32766:  from all lookup main
32767:  from all lookup default

如果我啟用wireguard用戶端,然後使用ip rule del from all fwmark 0x60000/0x60000 lookup 31命令手動刪除此規則,我可以直接從LAN網路ping/ssh到Wireguard伺服器IP。

我發現有幾個地方添加了這條規則:

/etc/init.d/wireguard

/etc/vpn.user

我已經用 IP 規則新增命令註解了行,現在我可以關閉和開啟wireguard 用戶端並且仍然存取 WAN IP:

    #fix ddns conflict
    #local DDNS=$(iptables -nL -t mangle | grep WG_DDNS)
    #local lanip=$(uci get network.lan.ipaddr)
    #local gateway=${lanip%.*}.0/24
    #if [ -z "$DDNS" ];then
            #iptables -t mangle -N WG_DDNS
            #iptables -A WG_DDNS -t mangle -i br-lan -s $gateway -d $publicip -j MARK --set-mark 0x60000
            #iptables -t mangle -I PREROUTING -j WG_DDNS
            #ip rule add fwmark 0x60000/0x60000 lookup 31 pref 31
            #ip route add $publicip dev wg0 table 31
    #fi

請注意,我不是路由方面的專家,我不知道這個hack 是否會破壞任何東西(在評論中它說“修復ddns 衝突” - 不確定這意味著什麼),但對我來說它工作正常並且不會破壞任何東西(我使用wireguard連接僅訪問遠端網路)。另外,我不是 OpenWRT 的專家,所​​以我不能保證這些變更會在路由器重新啟動後儲存。

相關內容