Wireguard VPN 无法访问互联网和 LAN

Enc*_*lon 7 ubuntu vpn routing wireguard

我有一台运行Ubuntu 20.04和wireguard 1.0.20200513-1~20.04.2的服务器。我在手机(Android Samsung S20+)上安装了wireguard应用程序并禁用了WIFI并连接到4G。当 VPN 处于活动状态时,我可以访问服务器,而无法访问家庭网络 (192.168.1.X) 或互联网上的其他内容。服务器具有 10.0.0.1 (VPN) 和 192.168.1.171 (LAN) 接口。电话获得 10.0.0.2 接口。我想我需要设置一条路线。服务器防火墙(ufw 状态)处于非活动状态。任何帮助将非常感激。

/etc/wireguard/wg0.conf

[Interface]
Address = 10.0.0.1/24
Address = <MAC>::1/64
SaveConfig = true    
ListenPort = 51820
PrivateKey = <SERVER_KEY>

[Peer]
PublicKey = <CELL_PUB_KEY>
AllowedIPs = 10.0.0.2/32, 
Endpoint = <EXTERNAL_IP>:8598
Run Code Online (Sandbox Code Playgroud)

客户端配置

Cellphone config
[Interface]
PrivateKey =<CELL_KEY>
Address = 10.0.0.2/24
DNS = 1.1.1.1, 1.0.0.1

[Peer]
PublicKey = <SERVER_PUB_KEY>
AllowedIPs = 10.0.0.0/24, 192.168.1.0/24
Endpoint = <EXTERNAL_IP>:51820
Run Code Online (Sandbox Code Playgroud)

/etc/sysctl.conf

net.ipv4.ip_forward=1
net.ipv6.conf.all.forwarding=1
Run Code Online (Sandbox Code Playgroud)

路线-n

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    100    0        0 enp2s0
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 wg0
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 enp2s0
192.168.1.0     0.0.0.0         255.255.255.0   U     100    0        0 enp2s0
Run Code Online (Sandbox Code Playgroud)

在服务器上

ip route get from 10.0.0.2 iif wg0 192.168.1.1
192.168.1.1 from 10.0.0.2 dev enp2s0
    cache iif wg0
Run Code Online (Sandbox Code Playgroud)

编辑-解决方案-wireguard.conf 中需要 PostUp 和 PostDown 行:

[Interface]
Address = 10.0.0.1/24
SaveConfig = true
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o enp2s0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o enp2s0 -j MASQUERADE
ListenPort = 51820
PrivateKey = <MY_KEY>

[Peer]
PublicKey = <MY_PUB_KEY>
AllowedIPs = 10.0.0.2/32
Run Code Online (Sandbox Code Playgroud)

Enc*_*lon 6

wireguard.conf 中需要 PostUp 和 PostDown 行:

[Interface]
Address = 10.0.0.1/24
SaveConfig = true
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o enp2s0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o enp2s0 -j MASQUERADE
ListenPort = 51820
PrivateKey = <MY_KEY>

[Peer]
PublicKey = <MY_PUB_KEY>
AllowedIPs = 10.0.0.2/32
Run Code Online (Sandbox Code Playgroud)