WireGuard 如何直接使用局域网的 IP 访问

48 天前
 eunrui
本地 阿里云服务器 公司内网
虚拟 IP(wireguard) 10.0.8.2 10.0.8.1(服务端) 10.0.8.3
实际 IP 192.168.1.47 x.x.x.x 192.168.11.222

阿里云上部署用的wg-easy,以下是默认的配置

# Note: Do not edit this file directly.
# Your changes will be overwritten!

# Server
[Interface]
PrivateKey = KA8QwuT3CzNNGGWD9iXl7YxSqtI5urW89+k8oO5bDWM=
Address = 10.0.8.1/24
ListenPort = 51820
PreUp = 
PostUp =  iptables -t nat -A POSTROUTING -s 10.0.8.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; 
PreDown = 
PostDown =  iptables -t nat -D POSTROUTING -s 10.0.8.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; 


# Client: test (6a258866-5164-4ecc-9873-2b1cc4dc6806)
[Peer]
PublicKey = 1jGq3SNpQhm0ucRGpNevW0dnAVYk3y5aFTdb5mk4EHg=
PresharedKey = V9iL5IfalLvZiuGeXRZe3EUtutLnBXJYs5R90RNsbSs=
AllowedIPs = 10.0.8.2/32

# Client: server (9d0012cd-0248-4ee8-b845-c3cbf07172d7)
[Peer]
PublicKey = UqJD/AaTXBLi1kAMZn99TT4eapO8hdtAmyUeK9TdkhY=
PresharedKey = r2WVnILyhEsZNhRhDN5dcfP84xLrQ6/Z9ohGSq0rNLs=
AllowedIPs = 10.0.8.3/32

目前按照上面的虚拟 ip ,可以互相 ping 通,但是没办法直接访问局域网中的 IP ,看过这个帖子/t/857269,按照 1 楼 V 友说的配置的 iptables ,添加到了内网服务器上 wireguard 配置,如下

[Interface]
PrivateKey = qGn5f2GtvQm/5GmudGGgdzb2IBt4nzZYudYDvVfd0H8=
Address = 10.0.8.3/24
DNS = 114.114.114.114

PreUp=iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -j SNAT --to-source 192.168.11.222
PostDown=iptables -t nat -D POSTROUTING -s 10.0.0.0/24 -j SNAT --to-source 192.168.11.222

[Peer]
PublicKey = cXPP7xJXo/QDDiEiwEfuDsUq1ytjSXsulawokP4eg3Q=
PresharedKey = r2WVnILyhEsZNhRhDN5dcfP84xLrQ6/Z9ohGSq0rNLs=
AllowedIPs = 10.0.8.0/24
PersistentKeepalive = 25
Endpoint = xx.xx.xx.xx:51820

加完虚拟 ip 都无法 ping 通了,请教下大家,这个该怎么加呢。

以下是 wg-easy 分配给我本地的客户端

[Interface]
PrivateKey = IPh0aFShR2hw+53TPLiXvt7kLofdjkxYSPLhG4yvMnc=
Address = 10.0.8.2/24
DNS = 114.114.114.114

[Peer]
PublicKey = cXPP7xJXo/QDDiEiwEfuDsUq1ytjSXsulawokP4eg3Q=
PresharedKey = V9iL5IfalLvZiuGeXRZe3EUtutLnBXJYs5R90RNsbSs=
AllowedIPs = 10.0.8.0/24, 192.168.11.0/24
Endpoint = xx.xx.xx.xx:51820
PersistentKeepalive = 25
2455 次点击
所在节点    程序员
34 条回复
1423
48 天前
搞不定的话可以远程协助
taygetus
48 天前
docker run -d --restart unless-stopped \
--name=wg-easy \
-e WG_HOST=xxxx \
-e PASSWORD=xxxx \
-e WG_DEFAULT_DNS=223.5.5.5 \
-e WG_PERSISTENT_KEEPALIVE=10 \
-e WG_ALLOWED_IPS=192.168.124.0/24,10.8.0.0/24 \
-e WG_MTU=1420 \
-v /DATA/appdata/wg-easy:/etc/wireguard \
-p 51820:51820/udp \
-p 51821:51821/tcp \
--cap-add=NET_ADMIN \
--cap-add=SYS_MODULE \
--sysctl="net.ipv4.conf.all.src_valid_mark=1" \
--sysctl="net.ipv4.ip_forward=1" \
-e TZ=Asia/Shanghai \
weejewel/wg-easy

[Interface]
PrivateKey = xx
Address = 10.8.0.9/24
DNS = 223.5.5.5
MTU = 1420

[Peer]
PublicKey = x
PresharedKey = x
AllowedIPs = 192.168.124.0/24, 10.8.0.0/2
PersistentKeepalive = 10
Endpoint = xxx


我这个配置可以随便访问 124 网段
taotaolin
48 天前
我也遇到过这个问题,貌似是 docker 的什么转发没开。我没折腾出来,最后没用 docker 起 wg 服务,直接用宿主机就可以了。使用 wg-easy 的 docker 环境没搞出来,等个大佬帮看看
eunrui
48 天前
@taygetus 我的和你的除了 DNS 不一样,没加 MTU ,其他的没区别
eunrui
48 天前
@shiyuu 感谢,转发的 server 和内网服务器都开启了,但是 server 那边的配置貌似 AllowedIPs 没办法改,进入容器看了 wg0.conf 的注释,说不要直接在里面改,会被覆盖。server 的 Peer 都是 wg-easy 自动写入的。我先消化一下。
taygetus
48 天前
@eunrui #24 我的 124 就是 wg-easy server 的机器网段 跟你的情况不一样 我是直接用 natter 打 udp 洞直连的 没中间云服务器
V2fishWE
48 天前
提供个思路,最简单的方法使用 tailscale ,将想要做转发的机器设置成 exit - node
ovoo
48 天前
@eunrui 我的思路是一定要修改这个自动生成的配置文件,也就是
```
# Client: server (9d0012cd-0248-4ee8-b845-c3cbf07172d7)
[Peer]
PublicKey = UqJD/AaTXBLi1kAMZn99TT4eapO8hdtAmyUeK9TdkhY=
PresharedKey = r2WVnILyhEsZNhRhDN5dcfP84xLrQ6/Z9ohGSq0rNLs=
AllowedIPs = 10.0.8.3/32, 192.168.11.0/24 # 修改这里,这样 wireguard 会自动在系统里添加一条路由,192.168.11.0/24 的路由都走这个 Peer 传输过去。
```
同时需要 打开 IP 转发 以及 MASQUERADE 。
ovoo
48 天前
@eunrui https://www.procustodibus.com/blog/2022/06/multi-hop-wireguard/

看看这篇文章,和你的场景完美契合 :)
ovoo
48 天前
实在不能修改文件,云服务器上手动执行添加路由的命令也是一样的效果。
```
ip route add 192.168.11.0/24 via 10.0.8.3
```
eunrui
48 天前
@ovoo 最新战况,要在 docker 里把内网服务器的 AllowedIPs 里加上 192.168.11.0/24 ,https://github.com/wg-easy/wg-easy/issues/921 这个 issues 里有说,我自己进容器手动加上了,虽然新加设备或重启的时候会被重置。


现在我可以连通内网服务器 192.168.11.222 了,但是没办法访问其他内网设备的资源,比如说 192.168.11.215 上的 9098 的 web 服务,但是又能 ping 通 215 的 ip ,ssh 也连不上,
ovoo
48 天前
@eunrui
内外服务器 配置文件 添加:

```

PreUp = iptables -t mangle -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu

PreUp = sysctl -w net.ipv4.ip_forward=1

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


```

注意替换 eth0 为实际的 物理网卡名

再不行就在 云服务器也添加:
```
PreUp = iptables -t mangle -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
```
eunrui
48 天前
@ovoo 感谢,重启了下,莫名其妙就可以了,
eunrui
48 天前
结贴,配置追加到正文了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1054742

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX