WireGuard 是一种简单快速的VPN协议,采用现代加密技术,比 IPsec、OpenVPN更快、更简单。它还有一个更小的代码库,这使得审计和维护更容易。虽然最初是为 Linux 内核设计的,但现在已经可以跨平台使用。
本教程介绍在OPNsense上安装WireGuard 服务器,配置一个或多个客户端来进行远程连接。使用的软件为OPNsense 21.7.3,客户端为运行Windows的计算机和Android移动设备。客户端的流量将通过 Ubuntu 20.04 服务器进行路由。
图 1. WireGuard P2P VPN网络拓扑
OPNsense WireGuard 配置按照以下五个主要步骤展开:
- 服务器和客户端下载并安装 WireGuard
- 服务器和客户端生成加密密钥对(公钥和私钥)
- 服务器和客户端上配置 WireGuard 隧道接口
- OPNsense WireGuard VPN 服务器上配置防火墙规则
- 服务器和客户端上启用 WireGuard 隧道接口
1.OPNsense安装WireGuard
- 导航到
System
->Firmware
->Plugins
。 - 在搜索字段中输入
os-wireguard
。
图 2. OPNsense 上的 WireGuard 插件安装
- 在
os-wireguard
插件的右侧,单击 - 安装完成后,刷新页面,通过VPN->WireGuard菜单访问
WireGuard
。
图 3. OPNsense安装插件os-wireguard
2.OPNsense WireGuard配置
- 导航到
VPN
->WireGuard
-> 。General
- 单击复选框启用 WireGuard。
图 4. 在 OPNsense 上启用 WireGuard 服务器
- 导航到
Local
选项卡,然后单击右侧的
图 5. 在 OPNsense 上添加本地 WireGuard 配置
- 选中启用本地配置。
- 设置WireGuard VPN 服务器的名称,例如
MyWireGuard
. - 将监听端口设置为51820 或更高编号的唯一端口。
- 设置隧道地址,例如
10.0.0.1/24
隧道地址是你使用的网络的唯一 IP地址和子网。子网应该足够大以便容纳将使用隧道的所有客户端。它应该是私有 (RFC1918) 地址,例如 10.0.0.1/24。
- 其他选项保留为默认值。
图 6. 在 OPNsense 上设置本地 WireGuard VPN 服务器配置
- 单击底部的保存按钮。会自动为WireGuard VPN服务器生成公钥和私钥对。
WireGuard 的主要优势之一是它基于尖端的加密技术。它通过使用一对加密密钥对连接进行加密,提供安全的VPN隧道。为了确保双向安全通信,每个对等点都必须拥有自己的私钥和公钥。每个服务器和客户端必须生成自己的密钥对,然后交换公钥才能使用 WireGuard。
图 7. 在 OPNsense 上编辑本地 WireGuard VPN 服务器配置
- 单击
pencil
图标可编辑/查看MyWireGuard
VPN 本地配置。 - 这里的
Public Key
稍后用于配置在WireGuard VPN 客户端上。
图 8. 查看Public Key
WireGuard VPN 服务器
- 关闭
Edit Local Configuration
窗口。
现在,WireGuard服务器可以接受来自端口51820的连接,并允许客户端自动生成与私钥对应的公钥。
3.Windows客户端配置
WireGuard for Windows 支持 Windows 7、8、8.1、10、2012、2016 和 2019,并提供了 64 位和 32 位版本。:
3.1安装 Windows WireGuard客户端
从WireGuard 网站下载并安装 Windows 安装程序。
图 9. 下载 WireGuard Windows 安装程序
安装后,会在任务栏的通知区域中看到 WireGuard 图标。
图 10. 任务栏上的 WireGuard 图标
3.2配置 WireGuard Windows客户端
启动 WireGuard 应用程序,然后单击“隧道”选项卡中显示的Add Tunnel
按钮旁边的向下箭头。
图 11. 在 Windows 客户端上配置 WireGuard
点击Add empty tunnel
:
图 12. 添加空隧道
会自动创建一个公钥/私钥对,并显示在屏幕上。
图 13. 在 Windows 客户端上创建新的 WireGuard 隧道
为隧道输入一个仅包含字母数字字符(无空格或标点符号)的名称,例如MyWireGuard
,然后编辑配置如下:
[Interface]
PrivateKey = CLIENT_PRIVATE_KEY
Address = 10.0.0.11/24
DNS = 10.0.0.1
[Peer]
PublicKey = SERVER_PUBLIC_KEY
Endpoint = SERVER_IP_ADDRESS:51820
AllowedIPs = 0.0.0.0/0
接口部分的字段说明如下:
- PrivateKey:Windows 客户端的私钥。
- PublicKey:Windows 客户端的公钥。然后必须将公钥复制到OPNsense上相关客户端对等点的的端点配置中。
- Address:此客户端的VPN IP地址。它在所有客户端中必须是唯一的。是在 OPNsense的端点配置中指定为允许 IP 的 IP。
- DNS:客户端用于隧道的DNS服务器的IP 地址。在本例中,使用在OPNsense WireGuard服务器上运行的 DNS 服务器。
peer部分的字段说明如下:
- PublicKey:OPNsense WireGuard服务器的公钥(本例中如图8所示,为:
fyKJ4c6sXTVRTJla6zQ9wi4okRPRd/GsMbTMszjhAgA=
)。 - Endpoint: OPNsense 服务器的WAN接口IP地址,后跟冒号和 WireGuard 端口 (
51820
)。 - AllowedIPs:
0.0.0.0/0
: 指定应通过 VPN 路由的 IP 地址。0.0.0.0/0
允许所有通信,并通过 VPN 路由所有内容。
3.3阻止非隧道流量选项
在编辑隧道窗口中,有一个Block untunneled traffic
选项。当 AllowedIPs 设置为0.0.0.0/0
时,可以启用此选项。启用该选项,WireGuard 客户端会添加 Windows 防火墙规则来阻止所有不进出隧道接口的流量。这样它可以防止在VPN之外意外发送IP数据包。
图 14.Windows 客户端上的 WireGuard Tunnel 配置
完成后,单击Save
按钮。
4.将Windows客户端配置添加到服务器
将Windows客户端的公钥和 IP 地址添加到服务器,按以下步骤操作:
- 导航到
VPN
->WireGuard
->Endpoints
图 15. 在 OPNsense 上添加 WireGuard 端点配置
- 单击
- 输入
Name
信息,例如MyWindows
。 - 输入上面的 Windows WireGuard 客户端配置期间生成的公钥,本示例为:
HtuNHzBxW ZcKMjOc5wbXZTFL4qKNbmzo/6XoR/cyEE=
. - 将
Allowed IPs
设置为与Windows WireGuard 客户端配置相同的IP地址,本示例为10.0.0.11/32
- 单击
Save
。
图 16.在 OPNsense上配置WireGuard Endpoint(Windows)
5.配置WireGuard Android客户端
您可以按照以下步骤轻松配置移动设备上的 WireGuard 应用程序以连接 VPN
5.1 Android设备
从设备上获取并安装官方应用程序,国内的应用商店可能不提供该程序,可以单独下载适用于Android设备的apk程序进行安装。
图 17. 从 Playstore上安装 WireGuard Android 应用程序
5.2 Android配置WireGuard 客户端
安装 WireGuard 应用程序后,我们需要为 VPN 隧道添加一个新的配置文件。单击蓝色的+
图标按钮。
图 18.为 Android 客户端添加 WireGuard 隧道
会打开一个新页面来配置隧道。打开底部的Creating From Scratch
。
图 19. 从头开始创建隧道配置
- 输入
Name
信息,如MyWireGuard
。 - 单击右端的刷新图标,生成公钥/私钥对。
- 设置地址,如 10.0.0.12。
- 设置DNS,如 10.0.0.1。
- 点击底部的
Add Peer
。
图 20. Android 设备上的 WireGuard 客户端配置
图 21. Android 设备上的 WireGuard 客户端添加对端配置
接口部分的字段说明如下:
- PrivateKey:android客户端的私钥。
- PublicKey : android客户端的公钥。然后必须将公钥复制到OPNsense上相关客户端对等点的的端点配置中。
- Address:此客户端的VPN IP地址。它在所有客户端中必须是唯一的。是在 OPNsense的端点配置中指定为允许IP的IP。
- DNS:客户端用于隧道的DNS服务器的IP 地址。在本例中,使用在OPNsense WireGuard服务器上运行的DNS服务器。
peer部分的字段说明如下:
- PublicKey:OPNsense WireGuard 服务器的公钥。
- Endpoint::OPNsense 服务器的WAN接口地址,后跟冒号和WireGuard 端口 (51820)。
- AllowedIPs: 指定通过VPN 路由的 IP 地址。0.0.0.0/0 允许所有地址,并通过 VPN 路由所有内容。
- 点击屏幕右上角的保存图标。
6.将Android客户端配置添加到服务器
将Android客户端的公钥和 IP 地址添加到服务器,按照以下步骤操作:
- 导航到
VPN
->WireGuard
-> 。Endpoints
- 单击
- 输入
Name
信息,例如MyAndroid
。 - 输入上一步 Android WireGuard 客户端配置期间生成的公钥,本例为:
rQdjEcn7UMbIverQ4D0FKfz fkGLxClArwDsXCNf DE=
. Allowed IPs
填入上面Android WireGuard 客户端配置中的地址字段,在本例中为10.0.0.12/32
- 单击
Save
。
图 22. 在 OPNsense上配置 WireGuard Endpoint(Android)
现在,在 OPNsense VPN
-> WireGuard
->Endpoints
选项卡下,可以查看所有已配置的 WireGuard VPN 端点,MyWindows
和MyAndroid
。
图 23. OPNsense 上的 WireGuard 端点列表
7.在OPNsense上添加远程端点
在OPNsense 防火墙上定义端点后,还要按照以下步骤将每个端点添加到 WireGuard VPN 服务器本地配置。
- 导航到
VPN
->WireGuard
->Local
选项卡。 - 点击
MyWireGuard
右侧的铅笔图标编辑vpn 配置。 - 从
Peers
下拉菜单中选择新创建的两个端点。MyWindows
和MyAndroid
。 - 单击
Save
。
图 24. 将 Peers(端点)添加到 WireGuard VPN Server 本地配置
- 单击
Apply
。
8.在OPNsense上启用WireGuard
- 导航到
VPN
->WireGuard
->General
选项卡。 - 单击
Enable WireGuard
复选框。 - 单击
Apply
。
图 25. 在 OPNsense 上启用 WireGuard 服务器
9.创建 WireGuard 接口
如果只需要通过 WireGuard 访问LAN,而不是本地网络之外的 IP,例如公共互联网,可以跳过此步骤。
在 OPNsense上创建 WireGuard 接口,它可以:
- 为隧道子网创建一个别名,然后可以在防火墙规则中使用该别名。否则,就必须创建自己的别名或手动指定子网。
- 可以自动添加 IPv4 出站 NAT 规则,允许隧道访问本地网络之外的 IPv4 IP(如果需要),而无需手动添加规则。
- 它可以分离每个 WireGuard 实例的防火墙规则(每个
wgX
设备)。否则,它们都必须在OPNsense创建的默认 WireGuard 组上进行配置。
要创建 WireGuard 接口,可以按照以下步骤操作:
- 导航到
Interfaces
->Assignments
- 在旁边的下拉菜单中选择 WireGuard 设备(如
wg0
) - 添加描述,例如
MyWireGuard
- 点击
- 点击
Save
图 26. 在 OPNsense 上创建 WireGuard 接口
- 在 Interfaces 菜单下选择新接口,单击进行配置。
- 单击启用接口。
- 选中Lock选项。
- 将其他选项保留为默认值。
图 27. 在 OPNsense 上配置 WireGuard 接口
无需在WireGuard 接口上配置 IP 地址。重新启动Local
WireGuard 服务后,您的 WireGuard 服务器配置中指定的隧道地址会自动分配给接口。
- 单击
Apply changes
激活新的接口设置。 - 导航到
VPN
->WireGuard
->General
选项卡。 - 通过取消选中
Enable WireGuard
复选框禁用 WireGuard 。 - 通过选中
Enable WireGuard
复选框启用 WireGuard
10.创建防火墙规则
在OPNsense 节点上定义以下两个防火墙规则。
- WAN 接口上的防火墙规则,允许客户端连接到 OPNsense WireGuard 服务器。
- 允许客户端访问他们应该有权访问的任何IP地址的防火墙规则。此规则是可选的,可能并不需要允许客户端访问内部网络。
10.1 允许VPN客户端访问WireGuard服务器
要允许从公共网络访问WireGuard/OpenVPN VPN 服务器,需要在WAN接口上放行WireGuard所使用的端口,在本例中为51820。如下图所示(示例为多Wan,允许从Wan1连接):
10.2 允许VPN客户端访问内部网络
如果希望WireGuard VPN 客户端能够连接到网络上的任何设备,则必须创建另外的防火墙规则来允许此类访问。在防火墙规则中,导航到创建的WireGuard 接口,例如 MyWireGuard。单击+
图标添加一条新规则来允许对任意目标的访问。如下图所示:
图 28. 定义防火墙规则来允许所有 VPN 客户端访问任意网络-1
图 29. 定义防火墙规则来允许所有 VPN 客户端访问任意网络-2
- 单击
save
,并Apply Changes
来激活设置。
11.在OPNsense上验证WireGuard
WireGuard VPN 服务器配置和客户端配置完成。要测试配置,您可以按照以下步骤操作。
- 激活WireGuard Windows客户端:PC连接到VPN服务器,打开WireGuard 应用程序,找到
MyWireGuard
,通过单击Activate
按钮打开隧道。对端连接后,隧道Status
将更改为Active
。
图 30.在 Windows 客户端上激活 WireGuard 隧道
- 激活 WireGuard Android 客户端:Android 设备连接 VPN 服务器,启动 WireGuard 应用程序,单击
MyWireGuard
右侧的切换按钮打开隧道。
图 31. 在 Android 客户端上激活 WireGuard 隧道
- 在OPNsense上查看VPN连接:导航到
VPN
->WireGuard
->List Configuration
选项卡。可以看到有关已连接 VPN 客户端的信息。显示以下详细信息:
- 对等点(公钥)
- 已连接客户端的 IP 地址
- 自上次连接以来的时间
- 传输的数据量
图 32. OPNsense 上已连接的 WireGaurd VPN 客户端列表配置
- 握手:单击
Handshakes
选项卡。握手次数不应等于 0,而且还应该不断增加。
图 33. 在 OPNsense 上查看 WireGuard VPN 连接握手
- Ping 测试:可以从客户端成功ping WireGuard 服务器,反之亦然:
- 内部网络访问测试:由于我们允许 VPN 客户端不受任何限制地访问防火墙后面的内部网络,因此他们应该能够访问局域网中的任意地址,反之亦然。