了解和配置WireGuard(EdgeRoutor)
Updated
May 14, 2022 08:38 AM
Sort
一级标签
tags
Tool
Created
Feb 22, 2022 09:14 AM
URL
Tags
最近入手了一只er-x作为主路由,考虑可以随时漫游回家,er-x支持的vpn协议很多,也尝试配置了L2TP和OpenVPN,但是都不成功,且配置太难懂,经历了无数天的头疼和尝试之后,转向了wireguard。
WireGuard介绍
了解wiredguard的概念,可以更好的理解他的配置。
WG 先定义了一个很重要的概念 —— WireGuard Interface(以下简称 wgi)。为什么要有 wgi?为什么现有的 tunnel 接口不适合?一个 wgi 是这么一个特殊的接口:
- 有一个自己的私钥(curve25519)
- 有一个用于监听数据的 UDP 端口
- 有一组 peer(peer 是另一个重要的概念),每个 peer 通过该 peer 的公钥确认身份
通过定义这样一个新的接口,wgi 把它和一般的 tunnel 接口区分开。有了这样一个接口的定义,其它数据结构的挂载,以及数据的收发都很清晰明了了。
我们看 WG 的接口配置:
[Interface]
Address = 10.1.1.1/24
ListenPort = 12345
PrivateKey = blablabla
[Peer]
PublicKey = IWNVZYx0EacOpmWJq6lE8RfcFBd8EeUliOi+uYKQfG8=
AllowedIPs = 0.0.0.0/0,::/0
Endpoint = 1.1.1.1:54321
WG 的 VPN 隧道的发起者(initiator)/ 接收者(responder)是对等的,所以也没有一般 VPN 的客户端/服务器端或者 spoke/hub 的区别。因而配置也是对等的。
在这个配置中,我们进一步了解了 peer 这个概念:它是 WG 节点的对端,有静态配置的公钥,peer 背后的网络的白名单(AllowedIPs),以及 peer 的地址和端口(这个并不一定需要,并且随着网络的漫游,可能会自动更改)
我的理解:interface是当前wireguad端,peer是与其配对的另一方端
每一端的interface声明了当前端的地址,监听的端口,和私钥
peer配置对方的公钥和对方的地址
配置WireGuard(EdgeRoutor)
虽然WireGuard概念上不区分服务端客户端,但是总是有个端来提供服务,其他一堆客户端链接他。所以我们还是按照传统的概念描述吧
安装
- 从WireGuard Github下载官方安装包
curl -OL https://github.com/WireGuard/wireguard-vyatta-ubnt/releases/download/1.0.20210606-2/e50-v2-v1.0.20210606-v1.0.20210914.deb
- 安装deb文件
dpkg -i e50-v2-v1.0.20210606-v1.0.20210914.deb
通过执行
show interfaces
可以确认是否安装成功配置
- 生成服务端的密钥对
mkdir server_keys
cd server_keys
wg genkey | tee privatekey | wg pubkey > publickey
- 相似的,生成多个客户端的密钥对
mkdir my_phone
cd my_phone
wg genkey | tee privatekey | wg pubkey > publickey
- 配置wg0 interface
# Enter configure mode
configure
# The location of the server's private key, previously generated
set interfaces wireguard wg0 private-key [your server private key data]
# Creates the Gateway IP for the VPN and the subnet
# This subnet can be any private IP range, through check for conflicts
set interfaces wireguard wg0 address 10.6.69.1/24
# Creates entries in the route table for the VPN subnet
set interfaces wireguard wg0 route-allowed-ips true
# Port for WG (that peers will use)
set interfaces wireguard wg0 listen-port 51820
commit ; save
- 添加一个客户端peer
# Remote User Peer
set interfaces wireguard wg0 peer [your client public key here]
# set the client allocate ip
set interfaces wireguard wg0 peer [your client public key here] allowed-ips 10.6.69.2/32
commit ; save
- 开启防火墙
# Creates an accept rule in the WAN_LOCAL list (WAN_LOCAL - wan to router)
# Accepts all incoming UDP connections, from port 51820
set firewall name WAN_LOCAL rule 20 action accept
set firewall name WAN_LOCAL rule 20 protocol udp
set firewall name WAN_LOCAL rule 20 destination port 51820
set firewall name WAN_LOCAL rule 20 description 'WireGuard'
commit ; save
家宽还要开启端口转发
最后生成的配置类似如下
user@ER-X$ show configuration
}
}
wireguard wg0 {
address 10.6.69.1/24
description WG_VPN
listen-port 51820
peer XzXsLlbdFDGzK10jFxySz3Qbk8ekY7YsASPAb+QprAc= {
allowed-ips 10.6.69.2/32
description my_phone
}
private-key ****************
route-allowed-ips true
}
}
}
rule 20 {
action accept
description WG_IN
destination {
port 51820
}
log enable
protocol udp
source {
}
}
客户端配置
新建wg.conf文件,写入如下内容
[Interface]
PrivateKey = <my_phone private key>
ListenPort = 51820
Address = 10.6.69.2/32 # The allowed IPs value set on the server
DNS = 192.168.99.1 # Optional, I set and my er-x routor ip
[Peer]
PublicKey = <pubkey of server>
AllowedIPs = 0.0.0.0/0 # Currently set as a wildcard to route all packets through VPN
Endpoint = server.com:51820 # PubIP or DNS record of server
下载wire官方客户端,导入上述配置文件,大功告成。