wireguard在家庭网络的妙用

概览:

介绍

众所周知,你需要的时候就知道了。

安装

若linux内核版本高于5.x的话。

1
yum install wireguard-tools

网络架构

请注意⚠️wireguard本身是不存在server和client的概念,每一个wireguard节点都是平等的。为了方便说明,我们假定下图的pc作为访问方,HomeLabs作为被访问方,ECS作为中转方。

sequenceDiagram
    participant HomeLabs
    participant ECS
    HomeLabs->>ECS: HomeLabs in there?
    loop ->ECS route
        HomeLabs->>ECS: TCP/IP & UDP
    end
    loop HomeLabs route
        ECS->>HomeLabs: TCP/IP & UDP
    end
    participant PC
    PC-->>ECS: I`am online!
    ECS->>PC: ok!
    loop PC->ECS route
        PC->>ECS: TCP/IP & UDP
    end
    loop ECS->PC route
        ECS->>PC: TCP/IP & UDP
    end
    PC-->>HomeLabs: PC可以访问HomeLabs啦!

安装以及配置可以参考wireguard 笔记

高阶

若家庭中不止一台设备需要访问,但是设备又很多,不方便各安装一个。那么此时就需要有一台wireguard的主机去做转发。我选择的是openwrt路由去做转发,因为设置起来比较快捷,pve搞起来也快。

sequenceDiagram
    participant ECS
    participant openwrt
    participant HomeLabs
    HomeLabs->>ECS: HomeLabs in there?
    loop ->ECS route
        openwrt->>ECS: TCP/IP & UDP
        HomeLabs->>ECS: TCP/IP & UDP
    end
    loop HomeLabs route
        ECS->>openwrt: TCP/IP & UDP
        ECS->>HomeLabs: TCP/IP & UDP
    end
    participant PC
    PC-->>ECS: I`am online!
    ECS->>PC: ok!
    loop PC->ECS route
        PC->>ECS: TCP/IP & UDP
    end
    loop ECS->PC route
        ECS->>PC: TCP/IP & UDP
    end
    PC-->>openwrt: PC可以访问openwrt啦!
    PC-->>HomeLabs: PC可以访问HomeLabs啦!

安装

1
2
opkg update
opkg install wireguard-tools

生成钥匙

建议统一在ECS操作,再分发到openwrt来。

1
wg genkey | tee wrt-privatekey | wg pubkey > wrt-publickey

配置虚拟网卡

建议统一在ECS操作,再分发到openwrt来。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
cat >> /etc/wireguard/wrt.conf <<EOF
# openwrt
[Peer]
PublicKey = $(cat wrt-publickey)
AllowedIPs = 10.3.6.4/32, 192.168.6.0/24
EOF

cat > openwrt.conf <<EOF
[Interface]
PrivateKey = $(cat wrt-privatekey)
Address = 10.3.6.4/24 #wg之前通信组网的内网ip和段,主机位每个得不一样

[Peer]
PublicKey = $(cat gw-publickey) # gateway的公钥
AllowedIPs = 10.3.6.0/24
Endpoint = $(curl -s ip.sb):28000 # gateway 公网ip和端口
PersistentKeepalive = 10 # 心跳时间
EOF

安装依赖

在界面 系统>>软件 搜索安装以下软件即可

  • luci-app-wireguard
  • luci-proto-wireguard

然后重启软路由。

配置网卡以及转发

重启成功后,在界面 网络>>网口,下方选择新增网卡。

协议选择Wireguard,可以在下方看到导入配置,导入先前的/etc/wireguard/wrt.conf配置,剩下的再根据自己的需求填写。ip地址哪里,需要填写允许访问的ip列表。

然后在网络>>防火墙>>NAT规则 新增一条规则开启转发。

enjoy