我在 Podman 容器中运行 WireGuard。我有一个像这样的 Hub and Spoke 配置
Client A <--> Hub <--> Client B
|
|
Internet
这是目前 Hub 上的 WireGuard 配置
[Interface]
PrivateKey = ...
Address = 10.8.0.1/24, fdcc:ad94:bacf:61a4::cafe:1/112
ListenPort = 51820
MTU = 1420
PostUp = iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE; iptables -A INPUT -p udp -m udp --dport 51820 -j ACCEPT; iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT; ip6tables -t nat -A POSTROUTING -s fdcc:ad94:bacf:61a4::cafe:0/112 -o eth0 -j MASQUERADE; ip6tables -A INPUT -p udp -m udp --dport 51820 -j ACCEPT; ip6tables -A FORWARD -i wg0 -j ACCEPT; ip6tables -A FORWARD -o wg0 -j ACCEPT;
PostDown = iptables -t nat -D POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE; iptables -D INPUT -p udp -m udp --dport 51820 -j ACCEPT; iptables -D FORWARD -i wg0 -j ACCEPT; iptables -D FORWARD -o wg0 -j ACCEPT; ip6tables -t nat -D POSTROUTING -s fdcc:ad94:bacf:61a4::cafe:0/112 -o eth0 -j MASQUERADE; ip6tables -D INPUT -p udp -m udp --dport 51820 -j ACCEPT; ip6tables -D FORWARD -i wg0 -j ACCEPT; ip6tables -D FORWARD -o wg0 -j ACCEPT;
# Client A
[Peer]
PublicKey = hcU3LDJmlsInRSDFErt+Kp2CZBRzSggvmfOkSKcGx1A=
PresharedKey = ...
AllowedIPs = 10.8.0.2/32, fdcc:ad94:bacf:61a4::cafe:2/128
但是因为 Hub 在 OCI 上运行的是 AlmaLinux 9.5,所以我不得不将 iptables 设置为使用 iptables-legacy。
现在我正尝试迁移到 nftables
这是新的配置
[Interface]
...
PostUp = nft add table inet wg_table; nft add chain inet wg_table postrouting { type nat hook postrouting priority 100 \; }; nft add rule inet wg_table postrouting ip saddr 10.8.0.0/24 oifname eth0 masquerade; nft add rule inet wg_table postrouting ip6 saddr fdcc:ad94:bacf:61a4::cafe:0/112 oifname eth0 masquerade; nft add chain inet wg_table input { type filter hook input priority 0 \; policy accept \; }; nft add rule inet wg_table input udp dport 51820 accept; nft add chain inet wg_table forward { type filter hook forward priority 0 \; policy accept \; }; nft add rule inet wg_table forward iifname "wg0" accept; nft add rule inet wg_table forward oifname "wg0" accept;
PostDown = nft delete table inet wg_table
...
我尝试暂时保持接近原始 iptables 规则。
但是用我的手机却不行,我无法用新规则连接到互联网。即使使用 iptables-nft 也不起作用
这可能是因为 nftables 也需要在预路由钩子上建立一个链来执行 nat:它不需要任何内部规则,但需要它来启用返回路径。
旁注:
这 2 条规则应该无关紧要,因为默认的钩子策略是接受。我会删除钩子链的“策略接受”,因为无论如何这 2 条规则都会允许。
我强烈建议使用 nftables“输出格式”脚本,这样可以避免重复 nft(..) 命令,并以更加结构化的视觉方式显示内容。https ://wiki.nftables.org/wiki-nftables/index.php/Scripting #File_formats
xxxxx meta nftrace set 1
然后:在控制台上:nft monitor trace
再见