更正配置

更正配置

我正在尝试设置链式/双跳 VPN,其中所有客户端流量在到达互联网之前都要经过 2 个服务器:

Client → Server1 → Server2 → Public Internet

所有对等点都位于这些私有地址块上:10.103.213.0/24(IPv4) 和fd6f:9403:2887:9cd6:10:103:213:0/112(IPv6)。以下是这些对等点的配置和现有的 IPTables 规则。(另请记下配置文件的名称,以防万一。)


Server2 配置

文件:/etc/wireguard/wg0.conf

# Server2

[Interface]
PrivateKey = SERVER2_PRIVATE_KEY
Address = 10.103.213.2/24, fd6f:9403:2887:9cd6:10:103:213:2/112
ListenPort = 53701
SaveConfig = false

# CLIENTS

[Peer] # Server1
PublicKey = SERVER1_PUBLIC_KEY
PresharedKey = SERVER1_PRESHARED_KEY
# ↓ to allow traffic from client (10.103.213.11/32) via Server1 (10.103.213.1/32), allow both
AllowedIPs = 10.103.213.0/24, fd6f:9403:2887:9cd6:10:103:213:0/112

防火墙配置命令:

ufw allow 53701/udp comment 'WireGuard VPN'

iptables -A FORWARD -i wg0 -j ACCEPT &&
iptables -A FORWARD -o wg0 -j ACCEPT &&
ip6tables -A FORWARD -i wg0 -j ACCEPT &&
ip6tables -A FORWARD -o wg0 -j ACCEPT

iptables -t nat -A POSTROUTING -s 10.103.213.0/24 -o enp8s0 -j MASQUERADE
ip6tables -t nat -A POSTROUTING -s fd6f:9403:2887:9cd6:10:103:213:0/112 -o enp8s0 -j MASQUERADE

Server1 配置

文件:/etc/wireguard/wg0.conf

# Server1

[Interface]
PrivateKey = SERVER1_PRIVATE_KEY
Address = 10.103.213.1/24, fd6f:9403:2887:9cd6:10:103:213:1/112
ListenPort = 53701
SaveConfig = false

# CLIENTS

[Peer] # Server2
PublicKey = SERVER2_PUBLIC_KEY
PresharedKey = SERVER1_PRESHARED_KEY
Endpoint = SERVER2_PUBLIC_IP:53701
AllowedIPs = 10.103.213.2/32, fd6f:9403:2887:9cd6:10:103:213:2/128
#PersistentKeepalive = 25

[Peer] # PC
PublicKey = CLIENT_PUBLIC_KEY
PresharedKey = CLIENT_PRESHARED_KEY
AllowedIPs = 10.103.213.11/32, fd6f:9403:2887:9cd6:10:103:213:11/128

防火墙配置命令:

ufw allow 53701/udp comment 'WireGuard VPN'

iptables -A FORWARD -i wg0 -j ACCEPT &&
iptables -A FORWARD -o wg0 -j ACCEPT &&
ip6tables -A FORWARD -i wg0 -j ACCEPT &&
ip6tables -A FORWARD -o wg0 -j ACCEPT

客户端配置

# CLIENT: PC

[Interface]
PrivateKey = CLIENT_PRIVATE_KEY
Address = 10.103.213.11/24, fd6f:9403:2887:9cd6:10:103:213:11/112
DNS = 10.103.213.1, fd6f:9403:2887:9cd6:10:103:213:1

[Peer] # Server1
PublicKey = SERVER1_PUBLIC_KEY
PresharedKey = CLIENT_PRESHARED_KEY
Endpoint = SERVER1_PUBLIC_IP:53701
AllowedIPs = 0.0.0.0/0, ::0/0
#PersistentKeepalive = 25

现在,就像我说的,当所有对等点都启用 VPN 时,我能够从私有网络 (WireGuard) 上的客户端 ( ) ping 服务器 1 ( 10.103.213.2) 和服务器 2 ( ),10.103.213.110.103.213.11但我无法访问互联网

我知道我缺少一些急需的 IP 路由或 IPTables 规则,但尽管我努力寻找我能理解的解决方案几天来,我都没有成功。我发现如果不真正阅读有关 Linux 网络/防火墙的书,我的希望就很小了。

现在,我希望有人能帮我找到解决方案,并解释我们正在做什么以及为什么这样做,以便我能够更好地理解并做笔记。

非常感谢!


笔记:

  • 如果我遗漏了任何有用的信息,请询问,我会很乐意获取它。

  • 客户端运行 macOS。服务器 1 和服务器 2 运行 Debian 11“Bullseye”。仅供参考,以防万一。

  • Server1 和 Server2 已安装 Unbound 并设置为本地 DNS 解析。这就是您DNS = 10.103.213.1, fd6f:9403:2887:9cd6:10:103:213:1在客户端配置中看到的原因。如果不是这样,我会在那里使用 CloudFlare 或 Google 的 IP。

  • #wireguard IRC 上有人建议我尝试在 Server1 的 WireGuard 配置中添加Table = 123[Interface]然后运行命令ip rule add iif wg0 table 123。但这不起作用,我也不明白它应该做什么(我看不懂手册页或技术细节)。

  • 从我的阅读中,我得出结论,iptablesufw可以一起使用——但使用时必须小心。即使在运行命令后,iptables-persistent您仍需要运行防火墙规则,以便在重新启动后保持不变netfilter-persistent saveufw即使 ufw status表示它们已到位。如果出于某种原因您在保存之前重新启动,请删除 UFW 规则并再次添加它们,然后运行netfilter-persistent save

答案1

善良的人们#wireguardLibera.Chat 上的 IRC 频道帮了我大忙!

我的配置存在以下问题:

  • Server1 的配置中 Server2AllowedIPs只包含 Server2 的 IP 地址。这样就无法访问“Internet”。需要访问0.0.0.0/0, ::0/0

  • 必须配置 Server1 的 WireGuard 接口,以将路由(针对 中的所有条目AllowedIPs)添加到自定义 IP 路由表(我们称之为wireguard2x)而不是main表。然后添加一条 IP 策略规则,规定具有输入接口 (iif) 的流量wg0转到自定义表 — 这也意味着其他类型的流量将照常采用默认路由。


更正配置

Server2 配置

文件:/etc/wireguard/wg0.conf

# Server2

[Interface]
PrivateKey = SERVER2_PRIVATE_KEY
Address = 10.103.213.2/24, fd6f:9403:2887:9cd6:10:103:213:2/112
ListenPort = 53701
SaveConfig = false

# CLIENTS

[Peer] # Server1
PublicKey = SERVER1_PUBLIC_KEY
PresharedKey = SERVER1_PRESHARED_KEY
# ↓ to allow traffic from client (10.103.213.11/32) via Server1 (10.103.213.1/32), allow both
AllowedIPs = 10.103.213.0/24, fd6f:9403:2887:9cd6:10:103:213:0/112

防火墙配置命令:

ufw allow 53701/udp comment 'WireGuard VPN'

iptables -A FORWARD -i wg0 -j ACCEPT &&
iptables -A FORWARD -o wg0 -j ACCEPT &&
ip6tables -A FORWARD -i wg0 -j ACCEPT &&
ip6tables -A FORWARD -o wg0 -j ACCEPT

iptables -t nat -A POSTROUTING -s 10.103.213.0/24 -o enp7s0 -j MASQUERADE
ip6tables -t nat -A POSTROUTING -s fd6f:9403:2887:9cd6:10:103:213:0/112 -o enp7s0 -j MASQUERADE

Server1 配置

文件:/etc/wireguard/wg0.conf

# Server1

[Interface]
PrivateKey = SERVER1_PRIVATE_KEY
Address = 10.103.213.1/32, fd6f:9403:2887:9cd6:10:103:213:1/128
ListenPort = 53701
Table = wireguard2x
# ↓ should only be set if resolvconf or openresolv is installed on the system, otherwise let the system use defaults
# ↓ is unncessary if local DNS resolution is set up
#DNS = 1.1.1.1, 1.0.0.1, 2606:4700:4700::1111, 2606:4700:4700::1001
DNS = 10.103.213.1, fd6f:9403:2887:9cd6:10:103:213:1
SaveConfig = false

# CLIENTS

[Peer] # Server2
PublicKey = SERVER2_PUBLIC_KEY
PresharedKey = SERVER1_PRESHARED_KEY
Endpoint = SERVER2_PUBLIC_IP:53701
AllowedIPs = 0.0.0.0/0, ::0/0
#PersistentKeepalive = 25

[Peer] # PC
PublicKey = CLIENT_PUBLIC_KEY
PresharedKey = CLIENT_PRESHARED_KEY
AllowedIPs = 10.103.213.11/32, fd6f:9403:2887:9cd6:10:103:213:11/128

防火墙配置命令:

ufw allow 53701/udp comment 'WireGuard VPN'

iptables -A FORWARD -i wg0 -j ACCEPT &&
iptables -A FORWARD -o wg0 -j ACCEPT &&
ip6tables -A FORWARD -i wg0 -j ACCEPT &&
ip6tables -A FORWARD -o wg0 -j ACCEPT

echo 123 wireguard2x >> /etc/iproute2/rt_tables
ip rule add iif wg0 table wireguard2x

客户端配置

# CLIENT: PC

[Interface]
PrivateKey = CLIENT_PRIVATE_KEY
Address = 10.103.213.11/32, fd6f:9403:2887:9cd6:10:103:213:11/128
DNS = 10.103.213.1, fd6f:9403:2887:9cd6:10:103:213:1

[Peer] # Server1
PublicKey = SERVER1_PUBLIC_KEY
PresharedKey = CLIENT_PRESHARED_KEY
Endpoint = SERVER1_PUBLIC_IP:53701
AllowedIPs = 0.0.0.0/0, ::0/0
#PersistentKeepalive = 25

笔记:

  • 您可以使用此命令查看当前有效的所有策略路由规则:ip rule listip rule

  • ip route show table wireguard2x例如,使用以下命令查看路由表:ip route list table wireguard2x.

  • 刷新路由缓存:ip route flush cache

  • 进一步阅读:Linux 高级路由和流量控制 (LARTC) 指南

  • 您可以监控客户端的网络流量——如果在 Linux 上,使用sudo iptraf-ng;在 macOS 上使用sudo iftop。(在客户端上运行。)

    • 我发现运行起来很难解析,因为实时更改太多。因此,有人建议我尝试route get 192.0.2.0(显示正在使用的接口,例如interface: utun2),然后在 macOS 上运行ifconfig <interface>(例如ifconfig utun2)。后者应该显示客户端的私有(WireGuard 对等)IP 地址,例如10.103.213.11,确认流量正在通过 WG 接口路由。虽然不多,但这是一个开始。

    • 更新: traceroute非常适合这个!例如,traceroute 8.8.8.8. (向 Chrispus Kamau 致敬

  • curl ipinfo.io在启用 VPN 的情况下检查您的 IP 地址。(在客户端上运行。)

有关的:

答案2

its_me 的答案对我来说非常有用。以下是我为自动创建/删除路由表、iptables 和 ip 规则所做的一些更改。自动化发生在 wireguard 接口启动和关闭时。请注意,我在此配置中未使用 ipv6 或 ufw:

服务器1:

# Add a route table for this interface
PreUp = echo 1 wireguard2x >> /etc/iproute2/rt_tables
# Add ip rule to point this interface at the new route table
PreUp = ip rule add iif %i table wireguard2x
# Setup iptables
PostUp = iptables -A FORWARD -i %i -j ACCEPT
PostUp = iptables -A FORWARD -o %i -j ACCEPT

# Remove the iptables
PostDown = iptables -D FORWARD -i %i -j ACCEPT
PostDown = iptables -D FORWARD -o %i -j ACCEPT
# Find and remove the ip rule
PostDown = ip rule | grep "from all iif %i" | cut -d: -f1 | xargs -L1 ip rule del prio
# Remove the ip table
PostDown = sed -i '/wireguard2x/d' /etc/iproute2/rt_tables

服务器2:

PostUp = iptables -A FORWARD -i %i -j ACCEPT
PostUp = iptables -A FORWARD -o %i -j ACCEPT
PostUp = iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT
PostDown = iptables -D FORWARD -o %i -j ACCEPT
PostDown = iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE

答案3

(根据 Chrispus Kamau 的优秀Wireguard VPN教程典型设置链式设置,看来我可能有一个解决方案(未经测试!) 以一种展示如何处理一些不寻常/复杂的设置的格式——或者至少给你一个想法。非常感谢 CK!)

通过:https://github.com/iamckn/chained-wireguard-ansible


怎么运行的:

  • 客户端 ( vpn0) → 10.200.200.0/24→ ( wg0) → 中间人 ( gate0) → 10.100.100.0/24→ ( wg0) 网关 ( wg0) → 公共互联网

  • 假设 Middleman 和 Gate 上都设置了 Unbound 以进行本地 DNS 解析。

门配置

配置门的 VPN 接口 ( wg0)。

文件:/etc/wireguard/wg0.conf

# SERVER

[Interface] # Gate
PrivateKey = GATE_PRIVATE_KEY
Address = 10.100.100.1/24
ListenPort = 53701
SaveConfig = false

# CLIENTS

[Peer] # Middleman
PublicKey = MIDDLEMAN_PUBLIC_KEY
PresharedKey = MIDDLEMAN_PRESHARED_KEY
AllowedIPs = 10.0.0.0/8

防火墙配置命令:

# Track VPN connection
## Track input chain
iptables -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
## Track forward chain
iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT

# Allow incoming WireGuard connections/VPN traffic on the listening port
iptables -A INPUT -p udp -m udp --dport 53701 -m conntrack --ctstate NEW -j ACCEPT

# Allow both TCP and UDP recursive DNS traffic
iptables -A INPUT -s 10.100.100.0/24 -p tcp -m tcp --dport 53 -m conntrack --ctstate NEW -j ACCEPT
iptables -A INPUT -s 10.100.100.0/24 -p udp -m udp --dport 53 -m conntrack --ctstate NEW -j ACCEPT

# Allow forwarding of packets that stay in the VPN tunnel
iptables -A FORWARD -i wg0 -o wg0 -m conntrack --ctstate NEW -j ACCEPT

# Set up NAT
iptables -t nat -A POSTROUTING -s 10.100.100.0/24 -o enp7s0 -j MASQUERADE

启动门的 VPN 接口,并启用 WireGuard 服务在启动时自动重启:

wg-quick up wg0
systemctl enable wg-quick@wg0

中间人配置

配置中间人的面向门接口(gate0)。这里中间人充当客户端。

文件:/etc/wireguard/gate0.conf

[Interface] # Middleman
PrivateKey = MIDDLEMAN_PRIVATE_KEY
Address = 10.100.100.2/32
DNS = 10.100.100.1
SaveConfig = false

# PEERS

[Peer] # Gate
PublicKey = GATE_PUBLIC_KEY
PresharedKey = MIDDLEMAN_PRESHARED_KEY
Endpoint = GATE_PUBLIC_IP:53701
AllowedIPs = 0.0.0.0/0
#PersistentKeepalive = 21

配置中间人的面向客户端的接口(wg0)。这里中间人充当服务器。

文件:/etc/wireguard/wg0.conf

# SERVER

[Interface] # Middleman
PrivateKey = MIDDLEMAN_PRIVATE_KEY
Address = 10.200.200.1/24
ListenPort = 53701
SaveConfig = false

# CLIENTS

[Peer] # PC
PublicKey = CLIENT_PUBLIC_KEY
PresharedKey = CLIENT_PRESHARED_KEY
AllowedIPs = 10.200.200.2/32

防火墙配置命令:

iptables -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT

iptables -A INPUT -p udp -m udp --dport 53701 -m conntrack --ctstate NEW -j ACCEPT

iptables -A INPUT -s 10.200.200.0/24 -p tcp -m tcp --dport 53 -m conntrack --ctstate NEW -j ACCEPT
iptables -A INPUT -s 10.200.200.0/24 -p udp -m udp --dport 53 -m conntrack --ctstate NEW -j ACCEPT

iptables -A FORWARD -i wg0 -o wg0 -m conntrack --ctstate NEW -j ACCEPT

iptables -t nat -A POSTROUTING -s 10.200.200.0/24 -o enp41s0 -j MASQUERADE

# Set up VPN chain NAT
iptables -t nat -A POSTROUTING -s 10.200.200.0/24 -j SNAT --to-source 10.100.100.2

在中间人上配置策略路由,将流量从客户端路由到网关。

echo "1 middleman" >> /etc/iproute2/rt_tables

# Forward all traffic to the gate
ip route add 0.0.0.0/0 dev gate0 table middleman

# OR only forward traffic to 4.2.2.2 (for e.g.), to the gate
#ip route add 4.2.2.2/32 dev gate0 table middleman

ip rule add from 10.200.200.0/24 lookup middleman

启动中间人的 WireGuard 接口,并启用 WireGuard 服务在启动时自动重启:

wg-quick up gate0
systemctl enable wg-quick@gate0

wg-quick up wg0
systemctl enable wg-quick@wg0

客户端配置

文件:/etc/wireguard/vpn0.conf

[Interface]
PrivateKey = CLIENT_PRIVATE_KEY
Address = 10.200.200.2/32
DNS = 10.200.200.1

# PEERS

[Peer] # Middleman
PublicKey = MIDDLEMAN_PUBLIC_KEY
PresharedKey = CLIENT_PRESHARED_KEY
Endpoint = MIDDLEMAN_PUBLIC_IP:53701
AllowedIPs = 0.0.0.0/0
#PersistentKeepalive = 21

最后在客户端上激活 WireGuard。

相关内容