我有一套可用的 wireguard 设置(Ubuntu 20.04 服务器、一个 Ubuntu 20.04 客户端和一个 Windows 10 客户端)。我可以连接到 VPN 服务器后面的设备。
VPN Server (EndPoint): 1.2.3.2/32
Public Subnet EndPoint sits in: 1.2.3.0/27
WireGuard Server Address: 10.2.0.1/16
Office LAN: 10.0.0.0/16
Peer Address: 10.2.0.3/16
AllowedIPs
问题是,当我在客户端配置中添加 WireGuard 公共接口的公共子网时,客户端无法连接到任何应该通过 vpn 的东西,包括 EndPoint 地址:
AllowedIPs = 10.2.0.0/16, 10.0.0.0/16, 1.2.3.0/27
或者添加EndPoint
地址:
AllowedIPs = 10.2.0.0/16, 10.0.0.0/16, 1.2.3.2/32
当我从公共子网添加其他单个地址时,它就可以正常工作。我可以通过 VPN 连接到这些服务器(traceroute 就是这样说的):
AllowedIPs = 10.2.0.0/16, 10.0.0.0/16, 1.2.3.3/32, 1.2.3.4/32
附加信息:
客户
$ sudo cat /etc/wireguard/wg1.conf
[Interface]
PrivateKey = <snipped>
Address = 10.2.0.3/16
[Peer]
PublicKey = <snipped>
AllowedIPs = 10.2.0.0/16, 10.0.0.0/16, 1.2.3.3/32, 1.2.3.4/32, 1.2.3.0/27
Endpoint = 1.2.3.2:51820
$ ip route
default via 10.25.0.1 dev wlp0s20f3 proto dhcp metric 600
10.0.0.0/16 dev wg1 scope link
10.25.0.0/16 dev wlp0s20f3 proto kernel scope link src 10.25.50.12 metric 600
10.2.0.0/16 dev wg1 proto kernel scope link src 10.2.0.3
1.2.3.0/27 dev wg1 scope link
1.2.3.3 dev wg1 scope link
1.2.3.4 dev wg1 scope link
$ sudo tcpdump -nn -i any 'udp port 51820'
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked v1), capture size 262144 bytes
23:21:43.439854 IP 10.2.0.3.34656 > 1.2.3.1.51820: UDP, length 148
23:21:48.705386 IP 10.2.0.3.34656 > 1.2.3.1.51820: UDP, length 148
23:21:54.081297 IP 10.2.0.3.34656 > 1.2.3.1.51820: UDP, length 148
23:21:59.201381 IP 10.2.0.3.34656 > 1.2.3.1.51820: UDP, length 148
跟踪路由超时。
服务器
$ ip route
default via 1.2.3.1 dev enp1s0 proto static
10.0.0.0/16 dev enp6s0 proto kernel scope link src 10.0.25.20
10.0.0.0/16 via 10.0.1.254 dev enp6s0 proto static metric 100
10.2.0.0/16 dev wg0 proto kernel scope link src 10.2.0.1
1.2.3.0/27 dev enp1s0 proto kernel scope link src 1.2.3.2
$ sudo cat /etc/wireguard/wg0.conf
[Interface]
Address = 10.2.0.1/16
SaveConfig = true
PostUp = ufw route allow in on wg0 out on enp6s0
PostUp = iptables -t nat -I POSTROUTING -o enp6s0 -j MASQUERADE
PreDown = ufw route delete allow in on wg0 out on enp6s0
PreDown = iptables -t nat -D POSTROUTING -o enp6s0 -j MASQUERADE
ListenPort = 51820
PrivateKey = <snipped>
[Peer]
PublicKey = <snipped>
AllowedIPs = 10.2.0.2/32
Endpoint = <snipped>:37785
[Peer]
PublicKey = <snipped>
AllowedIPs = 10.2.0.3/32
Endpoint = <snipped>:37950
答案1
由于您的服务器上有此路由:
1.2.3.0/27 dev enp1s0 proto kernel scope link src 1.2.3.2
您需要向服务器添加一些防火墙规则,以允许将流量1.2.3.0/27
从转发wg0
到并进行伪装enp1s0
。一些类似于您已有的规则enp6s0
可能会起作用:
ufw route allow in on wg0 out on enp1s0
iptables -t nat -I POSTROUTING -o enp1s0 -j MASQUERADE
请注意,ufw 始终允许转发 ping 数据包,因此如果您可以 ping 主机但无法访问其其他网络服务,则通常意味着您需要添加 ufw 规则来授予访问权限。