在 OpenWrt + Starlink 环境下使用家庭 IPv6 地址搭建 WireGuard 服务器

7 min

language: ja bn en es hi pt ru zh-cn zh-tw

你好,我是无能。

在上一篇文章中,我已经成功为家庭网络获取了公网 IPv6 地址,因此我将进行如下操作:

  • 服务器:OpenWrt + WireGuard Server

    • 接口:wg1(10.2.0.0/24)

    • 出口 IP 为家庭网络,是一个没有带宽限制(如 VPS 等的带宽上限)的环境

  • 客户端:智能手机、笔记本电脑

此外,本次的目标是让这个 WireGuard 服务器能够与托管在 ConoHa VPS 上的 WireGuard 服务器接口 wg0(10.1.0.0/24) 进行通信。

通过建立一个常驻的 wg1 接口 VPN 环境,可以实现在无需担心带宽的情况下始终接入内部网络。虽然有人担心加入这个网段本身可能会带来脆弱性,但比起直接通过公网 IP 公开要好一些,而且如果能加入 wg0 接口,那基本上就大功告成了。

通过这次调整,可以在 OpenWrt 侧使用 Dnsmasq 托管内部 DNS 服务器,从而实现常态化的广告拦截。

在 OpenWrt 上安装 WireGuard

安装 wireguard-tools

root@OpenWrt:~# apk search wireguard
kmod-wireguard-6.12.74-r1
luci-proto-wireguard-26.120.35050~a611522
prometheus-node-exporter-ucode-wireguard-2024.02.07-r2
rpcd-mod-wireguard-2
wireguard-tools-1.0.20250521-r1
root@OpenWrt:~# apk add wireguard-tools-1.0.20250521-r1

另外,安装 luci-proto-wireguard-26.120.35050~a611522 应该就可以通过 Web UI 上的 LuCI 进行 WireGuard 设置,但我没有安装它。

设置

/etc/config/network

如下所示创建服务器端接口。

~~~
config interface 'wg1'
        option proto 'wireguard'
        option private_key 'wg1 server private key'
        option listen_port '51820'
        list addresses '10.2.0.1/24'
        list addresses 'fd02::1/64'
        option mtu '1420'
~~~

下面添加允许的客户端设置。本来应该设置 PreSharedKey 来配置预共享密钥,但这里先跳过。

config wireguard_wg1
        option public_key 'wg1 client public key'
        list allowed_ips '10.2.0.2/32'
        list allowed_ips 'fd02::2/128'
        list allowed_ips 'client ipv6 addr ::2/128'
        option persistent_keepalive '25'
        option route_allowed_ips '1'

在客户端创建 wg1.conf。所有流量都将通过 OpenWrt 路由器。

[Interface]
PrivateKey = "wg1 client public key "
Address = 10.2.0.2/24, fd02::2/64, client ipv6 addr::2/64
DNS = 10.2.0.1 # 已经通过 Dnsmasq 搭建了 DNS 服务器,所以已设置完成
[Peer]
PublicKey = "wg1 server public key"
Endpoint = "wg1 ipv6 addr :51820"
AllowedIPs = 0.0.0.0/0, ::/0
PersistentKeepalive = 25

如果保持现状,会被防火墙拦截,因此需要创建 wg 区域,在 wan 侧 IPv6 开放 51820/udp 端口,并允许作为家庭环境的 LAN 访问,使 wg 能够通过 wan 访问外部。

基本上 IPv6 不需要 NAT,所以明确将其关闭。

/etc/config/firewall

config zone
        option name 'wg'
        list network 'wg1'
        list network 'wg0'
        option input 'ACCEPT'
        option forward 'ACCEPT'
        option output 'ACCEPT'
        option masq '1'
        option masq6 '0'
config rule
        option name 'Allow-WireGuard-IPv6'
        option src 'wan'
        option family 'ipv6'
        option proto 'udp'
        option dest_port '51820'
        option target 'ACCEPT'
config forwarding
        option src 'wg'
        option dest 'lan'
config forwarding
        option src 'wg'
        option dest 'wan'

在这种状态下无法与 wg0 通信,因此需要追加设置。

假设 wg0 服务器端的设置也根据以下内容进行了追加(此处省略)。

/etc/config/network

config route
        option interface 'wg0'
        option target '10.1.0.0/24'
config interface 'wg0'
        option proto 'wireguard'
        option private_key 'wg0 client private key'
        list addresses '10.1.0.99/24'
config wireguard_wg0
        option public_key 'wg0 server public key'
        option endpoint_host 'wg0 server addr'
        option endpoint_port '51820'
        list allowed_ips '10.1.0.0/24'
        list allowed_ips '10.2.0.0/24'
        option persistent_keepalive '25'

到此基本设置完成,关于 dnsmasq 如下:

在我的环境中它是预装的,所以只需启用即可。

/etc/init.d/dnsmasq enable/etc/init.d/dnsmasq start

之后,像 /etc/dnsmasq.d/adblock.conf 这样放入列表进行拦截。

/etc/init.d/dnsmasq restart

到这一步,只需重新加载即可。

/etc/init.d/network restart
/etc/init.d/firewall restart

这样就完成了双栈环境的搭建,无论是 CGNAT IP 的 IPv4 还是 IPv6 都可以正常使用。

OpenWrt 环境下的速度测试

以下是未连接 WireGuard 的有线环境

$ speedtest-go -s 48463 
    speedtest-go v1.7.10 (git-dev) @showwin
✓ ISP: 206.83.125.125 (Starlink) [35.6893, 139.6899] 
✓ Found 1 Specified Public Server(s)
✓ Test Server: [48463] 5.80km Tokyo by IPA CyberLab 400G
✓ Latency: 41.037747ms Jitter: 8.459256ms Min: 24.163074ms Max: 57.008632ms
✓ Packet Loss Analyzer: Running in background (<= 30 Secs)
✓ Download: 173.63 Mbps (Used: 211.22MB) (Latency: 35ms Jitter: 6ms Min: 25ms Max: 41ms)
✓ Upload: 45.99 Mbps (Used: 81.23MB) (Latency: 35ms Jitter: 29ms Min: 23ms Max: 136ms)
✓ Packet Loss: N/A

以下是 5GHz 频段 Wi-Fi 环境下 wg1 VPN 环境的测试结果

$ speedtest-go -s 48463 
    speedtest-go v1.7.10 (git-dev) @showwin
✓ ISP: 206.83.125.125 (Starlink) [35.6893, 139.6899] 
✓ Found 1 Specified Public Server(s)
✓ Test Server: [48463] 5.80km Tokyo by IPA CyberLab 400G
✓ Latency: 28.199611ms Jitter: 1.516613ms Min: 26.461289ms Max: 31.90411ms
✓ Packet Loss Analyzer: Running in background (<= 30 Secs)
✓ Download: 79.63 Mbps (Used: 114.99MB) (Latency: 98ms Jitter: 39ms Min: 27ms Max: 151ms)
✓ Upload: 65.86 Mbps (Used: 96.92MB) (Latency: 62ms Jitter: 73ms Min: 26ms Max: 285ms)
✓ Packet Loss: N/A

现在,即使在室外,也可以通过智能手机访问这个 wg1 VPN 环境了。

Related Posts