ARP 代理 VPS 的第二个 IP,通过 Wireguard 进行路由

ARP 代理 VPS 的第二个 IP,通过 Wireguard 进行路由

我已经在我的 VPS 上设置了 ARP 代理。通过此设置,我可以通过 WireGuard 将传入流量路由到我的 VPS 的第二个 IP。这应该允许我家里的 Raspberry Pi 使用第二个公共 IP。

我得到了这样的工作。传入的 Ping 通过 WireGuard 隧道转发到 Pi。但 Pi 随后尝试通过 eth0 应答 Ping。有没有办法解决这个问题,让它也通过 WireGuard 接口发送回复数据包?

为了展示这个问题(在 Raspberry Pi 上)

WireGuard 接口:

    # tcpdump -i wg_pub
    tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
    listening on wg_pub, link-type RAW (Raw IP), capture size 262144 bytes
    01:35:02.796522 IP <Public ip of ping PC> > <Second VPS IP>: ICMP echo request, id 14, seq 1, length 64
    01:35:03.795359 IP <Public ip of ping PC> > <Second VPS IP>: ICMP echo request, id 14, seq 2, length 64
    01:35:04.810613 IP <Public ip of ping PC> > <Second VPS IP>: ICMP echo request, id 14, seq 3, length 64

以太网接口:

    # tcpdump -i eth0 icmp
    tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
    listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
    01:37:11.477589 IP <Second VPS IP> > <Public ip of ping PC>: ICMP echo reply, id 14, seq 128, length 64
    01:37:12.491045 IP <Second VPS IP> > <Public ip of ping PC>: ICMP echo reply, id 14, seq 129, length 64
    01:37:13.505965 IP <Second VPS IP> > <Public ip of ping PC>: ICMP echo reply, id 14, seq 130, length 64

我想防止在 WireGuard 隧道上使用私有子网。

我实现此目的的一种方法是添加静态路由

ip route add <First VPS IP>/32 dev eth0

然后覆盖默认路由

ip route add 0.0.0.0/0 dev wg_pub

但这样做的缺点是所有的互联网流量都会通过 VPS 进行路由。

答案1

我认为你应该能够使用策略路由来做到这一点。设置新路由表的默认路由(123例如)以使用你的 WireGuard 接口(wg_pub):

ip route add default dev wg_pub table 123

然后添加一个策略规则,将该新表用于所有源为您的第二个 VPS IP 的数据包(例如192.0.2.2):

ip rule add from 192.0.2.2 table 123 priority 456

优先级(456)可以是任何值,但只有当您有多个匹配规则(通过列出ip rule list)时才重要。

相关内容