Wireguard 服务器和 openvpn 客户端 - 将流量从 wg0 转发到 tun0(openvpn 隧道)

Mig*_*ira 6 openvpn wireguard

我有一个运行 OpenVPN 客户端的树莓派,它连接到 VPN 提供商和 Wireguard 服务器,因此我可以从外部连接到我的家庭局域网。我想通过wireguard连接到我的家,并通过Openvpn连接发送所有流量。

这是我的 ifconfig 输出

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.5  netmask 255.255.255.0  broadcast 192.168.1.255

wg0: flags=209<UP,POINTOPOINT,RUNNING,NOARP>  mtu 1420
        inet 172.1.1.1  netmask 255.255.255.0  destination 172.1.1.1

tun0: flags=4305<UP,POINTOPOINT,RUNNING,NOARP,MULTICAST>  mtu 1500
        inet 10.8.8.17  netmask 255.255.255.0  destination 10.8.8.17
Run Code Online (Sandbox Code Playgroud)

eth0 - 是互联网的网关(连接到我的家庭路由器)

当我在没有运行 OpenVPN 客户端的情况下连接到 wireguard 服务器时,我可以访问我的内部 LAN (192.168.1.X) 并通过树莓派 (eth0) 将我的请求转发到互联网。当我启用 OpenVPN 客户端(tun0 up)时,我无法访问内部 LAN,也无法访问 Internet。

我想要做的是通过wireguard连接到我的家,并通过openvpn连接(tun0)获得所有流量隧道。

这是我从“route -n”的输出:

在 OpenVPN 启动之前(wireguard 工作正常):

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    202    0        0 eth0
172.1.1.0       0.0.0.0         255.255.255.0   U     0      0        0 wg0
192.168.1.0     0.0.0.0         255.255.255.0   U     202    0        0 eth0
Run Code Online (Sandbox Code Playgroud)

openVPN tun0 启动后(wireguard 连接无法连接到 Internet 和 LAN 客户端):

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.8.8.1        128.0.0.0       UG    0      0        0 tun0 
0.0.0.0         192.168.1.1     0.0.0.0         UG    202    0        0 eth0 
10.8.8.0        0.0.0.0         255.255.255.0   U     0      0        0 tun0 
95.142.172.143  192.168.1.1     255.255.255.255 UGH   0      0        0 eth0 
128.0.0.0       10.8.8.1        128.0.0.0       UG    0      0        0 tun0 
172.1.1.0       0.0.0.0         255.255.255.0   U     0      0        0 wg0  
192.168.1.0     0.0.0.0         255.255.255.0   U     202    0        0 eth0 
Run Code Online (Sandbox Code Playgroud)

我的防火墙规则:

-A FORWARD -i wg0 -j ACCEPT
-A POSTROUTING -o eth0 -j MASQUERADE
Run Code Online (Sandbox Code Playgroud)

是否缺少任何防火墙规则或我必须添加任何路由才能使其正常工作?我应该吃什么?

谢谢!!

b0w*_*tie 2

简而言之:解决方案

创建新的路由表:

ip route add default via 192.168.1.5 dev eth0 table 7
ip rule add fwmark 0x55 priority 1000 table 7
ip route flush cache
Run Code Online (Sandbox Code Playgroud)

其中 192.168.1.5 是外部接口 (eth0) 的 IP。现在将其添加到您的 wg0.conf 中:

FwMark = 0x55
Run Code Online (Sandbox Code Playgroud)

现在,即使 OpenVPN 隧道打开,您也可以通过 WireGuard 连接到您的家庭服务器。

更长的解释

当您启动 OpenVPN 隧道时,一条新路由将设置到主路由表中。该路线可能如下所示:0.0.0.0/1 via 10.8.8.1 dev tun0并且意味着您的所有互联网流量都应通过隧道发送出去。

这很棒,但是每当您想通过未受保护的接口与路由计算机通信时,您的计算机的答案也会发送到隧道中。这就是为什么您无法再通过 https 访问服务器,即使您已将端口 443 转发给它。它的答案只会被发送到隧道中并丢失。

在设置第二个路由表(可以通过ip route show table 70x55 规则查看)时,我们基本上告诉您的机器通过正常的、不受保护的 eth0 接口路由每个标记的数据包。其余的仍将被送入隧道。

还能做什么?

OpenVPN-服务器

实际上,当我还没有听说过 WireGuard 时,我就找到了解决方案。当时我想通过 OpenVPN 连接到我的家庭网络,但当服务器打开隧道时无法做到这一点。然而,我自己的 OpenVPN 服务器正在侦听端口 993,因此我用“0x55”标记通过该端口的每个数据包:

sudo iptables -t mangle -A OUTPUT -p tcp -m multiport --sport 993 -j MARK --set-mark 0x55
Run Code Online (Sandbox Code Playgroud)

这使得通过 VPN 连接到我的 VPN 连接服务器成为可能。

电子邮件端口不受保护

我的 VPN 提供商不允许通过其 VPN 发送邮件,因为存在垃圾邮件问题。该规则会将连接路由到我的邮件帐户,而不通过隧道:

iptables -t mangle -A PREROUTING -p tcp --dport 25 -j MARK --set-mark 0x55
Run Code Online (Sandbox Code Playgroud)

没有 VPN 的 MAC 地址

您可能希望整个设备“不受保护”。如果您使用瑞典服务器并且不想在平板电脑上看到瑞典 YouTube 广告,您可能需要执行以下操作:

iptables -t mangle -A PREROUTING -m mac --mac-source 4c:h7:9f:0l:17:k1 -j MARK --set-mark 0x55
Run Code Online (Sandbox Code Playgroud)

当然,您必须使用平板电脑的 MAC 地址。

  • “当我尝试通过 OpenVPN 连接到我的家庭服务器时,我解决了你的问题”,这是否意味着你的帖子的一部分作为答案?如果是这样,请编辑您的帖子以使其成为答案并提出自己的问题,请参考此问题。如果没有,请将其从答案部分删除。 (2认同)