VPS + 家宽NAS + WireGuard 公网IPV4方案

1. 架构说明

目标链路:

腾讯云轻量服务器
  ↓ WireGuard 隧道
家宽飞牛 OS

用途:

1. 腾讯云作为公网 IPv4 入口
2. 飞牛 OS 主动连接腾讯云
3. 家里路由器不开放任何入站端口
4. 后续由腾讯云 Caddy 反代飞牛服务

最终架构:

公网用户
  ↓
腾讯云公网 IPv4
  ↓
腾讯云 Caddy
  ↓
腾讯云 WireGuard Server:10.66.66.1
  ↓
飞牛 WireGuard Client:10.66.66.2
  ↓
飞牛 OS 服务

2. 最终采用方案

最终采用:

腾讯云:
  原生 WireGuard Server
  Docker Caddy

飞牛 OS:
  原生 WireGuard Client
  Docker 业务服务

WireGuard 端口:
  UDP 51820

WireGuard 网段:
  10.66.66.0/24

不推荐使用 Docker 运行 WireGuard。 原因是腾讯云和飞牛 Docker 环境都曾出现:

RTNETLINK answers: Operation not permitted

该错误表示容器没有权限创建或配置 WireGuard 网卡。最终两端改用原生 WireGuard 后解决。


3. 网络规划

腾讯云公网 IPv4:<PUBLIC_IPV4>
腾讯云 WireGuard IP:10.66.66.1/24
飞牛 WireGuard IP:10.66.66.2/24
WireGuard UDP 端口:51820

家里路由器:

不开放 80
不开放 443
不开放 51820
不做端口转发

飞牛主动连接腾讯云:

飞牛 OS → 腾讯云公网 IPv4:51820/udp

4. 腾讯云防火墙放行

腾讯云轻量应用服务器控制台放行:

TCP 22
TCP 80
TCP 443
UDP 51820

系统 UFW 放行:

ufw default deny incoming
ufw default allow outgoing

ufw allow 22/tcp
ufw allow 80/tcp
ufw allow 443/tcp
ufw allow 51820/udp

ufw --force enable
ufw status

5. 腾讯云安装 WireGuard

腾讯云 Debian 执行:

apt update

apt install -y wireguard wireguard-tools curl wget vim ufw tcpdump dnsutils

开启内核转发:

cat > /etc/sysctl.d/99-wireguard.conf <<'EOF'
net.ipv4.ip_forward=1
net.ipv4.conf.all.src_valid_mark=1
EOF

sysctl --system

6. 腾讯云生成 WireGuard 服务端密钥

mkdir -p /etc/wireguard
cd /etc/wireguard
umask 077

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

cat server_public.key

保存输出:

<SERVER_PUBLIC_KEY>

服务端私钥保存在:

/etc/wireguard/server_private.key

7. 腾讯云创建临时 wg0 配置

先写入服务端配置,等飞牛生成客户端公钥后再补 Peer。

SERVER_PRIVATE_KEY="$(cat /etc/wireguard/server_private.key)"

cat > /etc/wireguard/wg0.conf <<EOF
[Interface]
Address = 10.66.66.1/24
ListenPort = 51820
PrivateKey = ${SERVER_PRIVATE_KEY}

# 等飞牛生成 CLIENT_PUBLIC_KEY 后添加 Peer
EOF

启动 WireGuard:

systemctl enable wg-quick@wg0
systemctl restart wg-quick@wg0
wg

8. 腾讯云创建添加飞牛 Peer 的脚本

cat > /root/server-add-client.sh <<'EOF'
#!/usr/bin/env bash
set -euo pipefail

if [ $# -lt 1 ]; then
  echo "用法:bash /root/server-add-client.sh <CLIENT_PUBLIC_KEY>"
  exit 1
fi

CLIENT_PUBLIC_KEY="$1"
SERVER_PRIVATE_KEY="$(cat /etc/wireguard/server_private.key)"

cat > /etc/wireguard/wg0.conf <<EOF_WG
[Interface]
Address = 10.66.66.1/24
ListenPort = 51820
PrivateKey = ${SERVER_PRIVATE_KEY}

[Peer]
PublicKey = ${CLIENT_PUBLIC_KEY}
AllowedIPs = 10.66.66.2/32
PersistentKeepalive = 25
EOF_WG

systemctl restart wg-quick@wg0
sleep 2
wg
EOF

chmod +x /root/server-add-client.sh

9. 飞牛 OS 安装 WireGuard

飞牛 OS 已确认底层为 Debian 12:

Debian GNU/Linux 12 bookworm
apt 可用

飞牛执行:

apt update

apt install -y wireguard wireguard-tools curl wget vim

10. 飞牛生成 WireGuard 客户端密钥

mkdir -p /etc/wireguard
cd /etc/wireguard
umask 077

wg genkey | tee client_private.key | wg pubkey > client_public.key

cat client_public.key

保存输出:

<CLIENT_PUBLIC_KEY>

客户端私钥保存在:

/etc/wireguard/client_private.key

11. 飞牛写入 wg0 配置

<SERVER_PUBLIC_KEY> 替换成腾讯云输出的服务端公钥。

<PUBLIC_IPV4> 替换成腾讯云公网 IPv4。

CLIENT_PRIVATE_KEY="$(cat /etc/wireguard/client_private.key)"

cat > /etc/wireguard/wg0.conf <<EOF
[Interface]
Address = 10.66.66.2/24
PrivateKey = ${CLIENT_PRIVATE_KEY}

[Peer]
PublicKey = <SERVER_PUBLIC_KEY>
Endpoint = <PUBLIC_IPV4>:51820
AllowedIPs = 10.66.66.0/24
PersistentKeepalive = 25
EOF

启动飞牛 WireGuard:

systemctl enable wg-quick@wg0
systemctl restart wg-quick@wg0
wg

12. 腾讯云添加飞牛 Peer

回到腾讯云,将飞牛输出的 <CLIENT_PUBLIC_KEY> 添加进服务端配置:

bash /root/server-add-client.sh <CLIENT_PUBLIC_KEY>

查看状态:

wg

成功时应看到:

latest handshake: xx seconds ago
transfer: xxx received, xxx sent

13. 验证 WireGuard 隧道

腾讯云执行:

ping -c 4 10.66.66.2

成功结果:

4 packets transmitted, 4 received

查看路由:

ip route get 10.66.66.2

应看到:

10.66.66.2 dev wg0 src 10.66.66.1

14. 飞牛启动临时测试服务

飞牛执行:

docker run -d \
  --name test-web \
  --restart unless-stopped \
  -p 18080:80 \
  nginx:alpine

腾讯云测试:

curl -I http://10.66.66.2:18080

如果返回:

HTTP/1.1 200 OK

说明:

腾讯云 → WireGuard → 飞牛服务

已经打通。


15. 后续服务访问方式

后续腾讯云 Caddy 可以反代飞牛服务:

10.66.66.2:服务端口

例如:

飞牛管理后台:10.66.66.2:5666
WordPress:10.66.66.2:48080
MCS Web:10.66.66.2:23333
MinIO Console:10.66.66.2:9001
MinIO API:10.66.66.2:9000

16. 常用维护命令

腾讯云

wg
systemctl status wg-quick@wg0
systemctl restart wg-quick@wg0
ping -c 4 10.66.66.2

飞牛 OS

wg
systemctl status wg-quick@wg0
systemctl restart wg-quick@wg0

查看端口

ss -tulpn | grep -E '51820|80|443'

抓包排查

腾讯云抓 WireGuard 包:

tcpdump -ni any udp port 51820

飞牛重启 WireGuard:

systemctl restart wg-quick@wg0

如果腾讯云能看到飞牛来的 UDP 包,但没有握手,多半是密钥不匹配。 如果腾讯云看不到 UDP 包,多半是防火墙或 Endpoint 配置问题。


17. 常见问题

17.1 Docker WireGuard 报权限错误

错误:

RTNETLINK answers: Operation not permitted

原因:

Docker 容器无权限创建或配置 wg0 网卡

解决:

两端都改用原生 WireGuard

17.2 latest-handshake 为 0

检查:

wg
tcpdump -ni any udp port 51820

重点确认:

腾讯云控制台是否放行 UDP 51820
UFW 是否放行 UDP 51820
飞牛 Endpoint 是否为 <PUBLIC_IPV4>:51820
服务端 Peer PublicKey 是否为飞牛 client_public.key
客户端 Peer PublicKey 是否为腾讯云 server_public.key

17.3 ping 不通 10.66.66.2

腾讯云执行:

ip route get 10.66.66.2
wg

应看到:

10.66.66.2 dev wg0 src 10.66.66.1

如果有路由但 ping 不通,通常是握手未成功。


17.4 curl 飞牛服务不通

如果:

ping -c 4 10.66.66.2

通,但:

curl -I http://10.66.66.2:端口

不通,则问题在飞牛服务本身:

1. 服务未启动
2. 端口写错
3. Docker 没映射端口
4. 服务只监听 127.0.0.1
5. 服务只监听局域网 IP,没有监听 WireGuard 可访问地址

飞牛检查:

ss -tlnp
docker ps

18. 最终成功状态

成功状态应满足:

腾讯云 wg 有 latest handshake
腾讯云 ping 10.66.66.2 成功
腾讯云 curl http://10.66.66.2:18080 成功
飞牛无需开放路由器端口

最终链路:

腾讯云公网 IPv4
  ↓
腾讯云 WireGuard Server 10.66.66.1
  ↓
飞牛 WireGuard Client 10.66.66.2
  ↓
飞牛 Docker / 系统服务

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇