我有几个华硕 RT-AX58U 路由器,它们之间有 WireGuard VPN。路由器“B”连接到路由器“A”。路由器“B”的 LAN 地址是 192.168.50.1/24,而路由器“A”的 LAN 地址是 192.168.0.1/24。WireGuard VPN 隧道网络是 10.5.0.0/8,其中“B”位于 10.5.0.2,“A”位于 10.5.0.1。VPN 运行良好 – 连接到路由器“B”LAN 端的机器可以看到路由器“A”LAN 端的机器,反之亦然。从路由器“A”LAN 端无法访问的唯一机器是路由器“B”的管理接口,这使得远程管理路由器“B”非常困难。在路由器“B”中手动添加一条返回 192.168.0.0/24 的路由可以实现这一点。

我可以在路由器“B”中创建一条路由,以便将来自 192.168.0.0/24 的请求正确路由回源。如果我通过 SSH 进入路由器“B”并添加以下路由,则从 192.168.0.0/24 上的计算机发送到路由器“B”的数据包将正确返回源计算机:

route add -net 192.168.0.0 netmask 255.255.255.0 gw 10.5.0.1

但是,如果路由器“B”重新启动,则重新启动时路由会丢失。

我该如何保留这条路线?

我知道/jffs文件系统是可写且持久的,所以我可以在那里创建一个脚本,但是当 WireGuard VPN 在启动时自动启动后,如何让该脚本在启动时运行?


仅供参考,/Advanced/GWStaticRoute_Content.asp路由器“B”的 Web 管理页面允许添加持久路由,但是指定 WireGuard 接口不是一个选项 – 唯一的选项是LANMANWAN– 这些都不允许路由通过 WireGuard 虚拟接口正确路由数据包。


最佳答案
1

看起来我可以将PostUp脚本添加到客户端的 WireGuard 配置中,详细信息请参阅

PostUp

Optionally run a command after the interface is brought up. This option can appear multiple times, as with PreUp

Examples

    Read in a config value from a file or some command's output
    PostUp = wg set %i private-key /etc/wireguard/wg0.key <(some command here)

    Log a line to a file
    PostUp = echo "$(date +%s) WireGuard Started" >> /var/log/wireguard.log

    Hit a webhook on another server
    PostUp = curl https://events.example.dev/wireguard/started/?key=abcdefg

    Add a route to the system routing table
    PostUp = ip rule add ipproto tcp dport 22 table 1234

    Add an iptables rule to enable packet forwarding on the WireGuard interface
    PostUp   = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

    Force WireGuard to re-resolve IP address for peer domain
    PostUp = resolvectl domain %i "~."; resolvectl dns %i 192.0.2.1; resolvectl dnssec %i yes

因此,理论上,如果我创建一个/jffs/add_route.sh包含所需route add命令的脚本,则 WireGuard 客户端应该在 VPN 链接出现时自动创建路由。