Wireguard - 如何僅隧道部分流量

Wireguard - 如何僅隧道部分流量

是否可以設定 Wireguard 伺服器,以便僅透過 Wireguard 傳輸 ips [A、B、C、...] 清單 - 而其餘流量將被忽略並透過非 Wireguard 介面?

換句話說,我正在嘗試向某些外部人員授予對 Wireguard VPN 的訪問權限,但我不希望他們能夠使用 VPN 瀏覽我指定的其他 ip/站點(同時讓他們可以訪問任何他們想要的內容)想要自己的非VPN 介面/連線。

謝謝

答案1

您可以使用 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

例如:

[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]
...

為了在客戶端獲得無縫體驗,您還必須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 = ...

文件:

相關內容