WireGuard 配置指南:OpenWRT 路由器与 VPS 全局代理

本指南将帮助您在 GL.iNet MT6000 (ImmortalWRT) 上配置 WireGuard,使指定 LAN 口或 WiFi 通过 VPS 全局代理上网,特别适用于 TikTok Live Studio 推流等场景。

目标

一、VPS 端配置(WireGuard 服务端)

假设 VPS 系统为 Ubuntu 20.04+ / Debian 11+,公网 IP 为 1.2.3.4

1. 安装 WireGuard

sudo apt update && sudo apt install -y wireguard

2. 生成密钥对

umask 077
wg genkey | tee server_private.key | wg pubkey > server_public.key

查看生成结果:

cat server_private.key
cat server_public.key

3. 创建配置文件

路径:/etc/wireguard/wg0.conf

[Interface]
Address = 10.0.0.1/24
ListenPort = 51820
PrivateKey = <这里填你的 server_private.key>

# NAT 转发(出接口名称改为 VPS 的实际外网网卡)
PostUp = iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE

⚠️ 如果你的 VPS 网卡是 ens3 或 enp1s0,就把上面的 eth0 改成对应名称。可用命令 ip addr 查看。

4. 开启 IP 转发

echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
sysctl -p

5. 启动 WireGuard

systemctl enable wg-quick@wg0 --now
systemctl status wg-quick@wg0

6. 添加客户端 (OpenWRT)

编辑 /etc/wireguard/wg0.conf 文件,追加:

[Peer]
PublicKey = <CLIENT_PUBLIC_KEY>
AllowedIPs = 10.0.0.2/32

重启服务:

systemctl restart wg-quick@wg0

7. 防火墙放行端口

ufw allow 51820/udp

或使用 iptables:

iptables -A INPUT -p udp --dport 51820 -j ACCEPT

✅ 至此 VPS 端已配置完成。

二、OpenWRT 端配置(客户端)

目标:让路由器的一个 LAN 口(例如 LAN4)或 WiFi 单独走 WireGuard 出口。

1. 安装 WireGuard 组件

SSH 登录你的 MT6000:

opkg update
opkg install luci-app-wireguard wireguard-tools kmod-wireguard

2. 生成客户端密钥

umask 077
wg genkey | tee /etc/wireguard/client_private.key | wg pubkey > /etc/wireguard/client_public.key

查看:

cat /etc/wireguard/client_private.key
cat /etc/wireguard/client_public.key

3. 在 OpenWRT 上创建 WireGuard 接口

执行以下命令:

uci set network.wg0=interface
uci set network.wg0.proto='wireguard'
uci set network.wg0.private_key='<CLIENT_PRIVATE_KEY>'
uci add_list network.wg0.addresses='10.0.0.2/24'

uci add network.wireguard_wg0
uci set network.@wireguard_wg0[-1].public_key='<SERVER_PUBLIC_KEY>'
uci set network.@wireguard_wg0[-1].endpoint_host='1.2.3.4'
uci set network.@wireguard_wg0[-1].endpoint_port='51820'
uci add_list network.@wireguard_wg0[-1].allowed_ips='0.0.0.0/0'
uci set network.@wireguard_wg0[-1].persistent_keepalive='25'

uci commit network
/etc/init.d/network restart

4. 确定 LAN 口对应接口

执行:

cat /etc/board.json | grep port

假设你想让 LAN4 走代理,对应接口一般为 eth0.4(GL.iNet MT6000 上常见)。

5. 创建独立的 LAN 子网(供 TikTok 用)

uci set network.lan4_proxy=interface
uci set network.lan4_proxy.proto='static'
uci set network.lan4_proxy.device='eth0.4'
uci set network.lan4_proxy.ipaddr='192.168.4.1'
uci set network.lan4_proxy.netmask='255.255.255.0'
uci commit network
/etc/init.d/network restart

6. 添加 DHCP 服务

uci set dhcp.lan4_proxy=dhcp
uci set dhcp.lan4_proxy.interface='lan4_proxy'
uci set dhcp.lan4_proxy.start='100'
uci set dhcp.lan4_proxy.limit='150'
uci set dhcp.lan4_proxy.leasetime='12h'
uci commit dhcp
/etc/init.d/dnsmasq restart

7. 设置防火墙区域

uci set firewall.wgzone=zone
uci set firewall.wgzone.name='wgzone'
uci add_list firewall.wgzone.network='wg0'
uci set firewall.wgzone.input='ACCEPT'
uci set firewall.wgzone.output='ACCEPT'
uci set firewall.wgzone.forward='ACCEPT'
uci set firewall.wgzone.masq='1'

uci set firewall.lan4_proxy=zone
uci set firewall.lan4_proxy.name='lan4_proxy'
uci add_list firewall.lan4_proxy.network='lan4_proxy'
uci set firewall.lan4_proxy.input='ACCEPT'
uci set firewall.lan4_proxy.output='ACCEPT'
uci set firewall.lan4_proxy.forward='ACCEPT'

uci add firewall.forwarding
uci set firewall.@forwarding[-1].src='lan4_proxy'
uci set firewall.@forwarding[-1].dest='wgzone'

uci commit firewall
/etc/init.d/firewall restart

8. DNS 设置防泄漏

在 dnsmasq 中添加:

uci add_list dhcp.@dnsmasq[0].server='1.1.1.1'
uci add_list dhcp.@dnsmasq[0].server='8.8.8.8'
uci commit dhcp
/etc/init.d/dnsmasq restart

9. WiFi 独立绑定(可选)

进入 LuCI → 网络 → 无线 → 编辑某个 SSID
在 "网络" 下拉选单中选择 lan4_proxy,保存即可。

这样连接此 WiFi 的设备都会自动通过 WireGuard 出口。

10. 测试连通性

执行:

wg show

应看到:

peer: <SERVER_PUBLIC_KEY>
endpoint: 1.2.3.4:51820
allowed ips: 0.0.0.0/0
latest handshake: x seconds ago
transfer: x bytes received, x bytes sent

然后:

curl https://ipinfo.io

若返回 VPS 的公网 IP(比如 1.2.3.4),则表示全局代理成功。

流程回顾

步骤 操作端 内容
1 VPS 安装 WireGuard
2 VPS 创建 wg0.conf,设置 NAT
3 VPS 添加 OpenWRT 公钥
4 OpenWRT 安装 luci-app-wireguard
5 OpenWRT 生成密钥、配置 wg0
6 OpenWRT 创建 lan4_proxy 独立 LAN
7 OpenWRT DHCP、防火墙、DNS
8 OpenWRT 绑定 WiFi(可选)
9 测试 wg show 与 curl ipinfo.io 验证

常用排错命令

功能 命令
检查 WireGuard 状态 wg show
检查接口 ip addr
检查路由表 ip route show
查看防火墙规则 iptables -t nat -L -n -v
查看 WireGuard 日志 logread
重启网络 /etc/init.d/network restart

适用场景