Wireguard - 如何仅隧道部分流量

Sym*_*eof 8 networking security vpn ip wireguard

是否可以设置 Wireguard 服务器,以便仅通过 Wireguard 传输 ips [A、B、C、...] 列表 - 而其余流量将被忽略并通过非 Wireguard 接口?

换句话说,我试图向某些外部人员授予对 Wireguard VPN 的访问权限,但我不希望他们能够使用 VPN 浏览我指定的其他 ip/站点(同时让他们可以访问任何他们想要的内容)想要自己的非 VPN 接口/连接。

谢谢

Tri*_*gus 6

您可以使用 iptables。
替换eth0为连接到 Internet 的网络接口和10.6.0.1/24您的客户端子网。

将其插入 Wireguard 配置中 [INTERFACE] 下方的某个位置

# Drop all outgoing packets from the client subnet
PreUp = iptables -I FORWARD -s 10.6.0.1/24 -o eth0 -j DROP
## Add your exceptions here
Run Code Online (Sandbox Code Playgroud)

例如:

[Interface]
PrivateKey = ...
Address = 10.6.0.1/24
MTU = 1420
ListenPort = 51820

## Before interface wg0 is up
# Drop all outgoing packets from the client subnet
PreUp = iptables -I FORWARD -s 10.6.0.1/24 -o eth0 -j DROP
# Allow clients to connect to the local network 192.168.0.1/24
PreUp = iptables -I FORWARD -s 10.6.0.1/24 -d 192.168.0.1/24 -j ACCEPT
# Allow clients to connect to tcp port 80 (usually http) on 10.10.0.5
PreUp = iptables -I FORWARD -s 10.6.0.1/24 -d 10.10.0.5 -p tcp --dport 80 -j ACCEPT

## After interface wg0 is down
PostDown = iptables -D FORWARD -s 10.6.0.1/24 -o eth0 -j DROP
PostDown = iptables -D FORWARD -s 10.6.0.1/24 -d 192.168.0.1/24 -j ACCEPT
PostDown = iptables -D FORWARD -s 10.6.0.1/24 -d 10.10.0.5 -p tcp --dport 80 -j ACCEPT

[Peer]
...
Run Code Online (Sandbox Code Playgroud)

为了在客户端获得无缝体验,您还必须AllowedIPs在客户端配置中进行配置。否则,客户端将尝试使用 VPN 访问互联网,而这些请求将会超时。

按照上面的示例,客户端的配置可能如下所示:

[Interface]
PrivateKey = ...
Address = 10.6.0.2/24
DNS = 10.6.0.1

[Peer]
PublicKey = ...
AllowedIPs = 192.168.0.1/24, 10.10.0.5
Endpoint = ...
PresharedKey = ...
Run Code Online (Sandbox Code Playgroud)

文档: