OPNsense 系列七:WireGuard 安装重整理

说明

接着过去文章 IPv4<->Wireguard<->IPv4组网服务: LANraragi 实例 内容,现更新OPNsense 作为 WireGuard 客户端连接公网服务器的记录。

插件方式

获取客户端文件

WireGuard 服务器获取配置文件,示例:

[Interface]
PrivateKey = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=
Address = 10.10.10.2/24
DNS = 114.114.114.114


[Peer]
PublicKey = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=
PresharedKey = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=
AllowedIPs = 10.10.10.0/24
PersistentKeepalive = 25
Endpoint = [DOMAIN]:[PORT]

安装插件

系统 -> 固件 -> 插件 中安装 os-wireguardwireguard-go

安装完成后刷新网页,在 VPN : WireGuard 进行配置。

配置 端点

VPN : WireGuard : 端点 ,“+”添加端点。

项目 备注
启用 勾选
名称 Example_Client 任意取
公钥 …… 来自配置文件 PublicKey 内容
共享密钥 …… 来自配置文件 PresharedKey 内容
允许的IP 10.10.10.0/24 意思是划分的网段,若掩码为 24 位,则左后的 0 任意取值
端点地址 [DOMAIN] 来自配置文件 Endpoint 中的 [DOMAIN] 内容
端点的端口 [PORT] 来自配置文件 Endpoint 中的 [PORT] 内容(52822)
Keepalive Interval 25 来自配置文件 PersistentKeepalive 内容

端点设置

保存并应用。

端点列表

配置 本地(Local)

VPN : WireGuard : 本地 ,“+”添加端点。

项目 备注
启用 勾选
名称 Example_Local 任意取
实例 1 此项不可修改,显示已创建端点数量
公钥 …… 来自配置文件 PublicKey 内容
私钥 …… 来自配置文件 PrivateKey 内容
侦听端口 52820 来自配置文件 Endpoint 中的 [PORT] 内容,建议与其相同,否则延迟 +10ms 左右
隧道地址 10.10.10.2/24 来自配置文件 Address 内容,为分发的固定内网 IP
对等 Example_Client 选择创建的端点
禁用路由 不勾选

本地设置

保存并应用。

本地列表

启动服务

VPN : WireGuard 勾选“启用 WireGuard”,应用。

本地列表

分配并配置接口

在“启用 WireGuard”服务后,在 接口 -> 分配 中添加接口,并保存。

分配接口

选中 wg1 进入配置并保存 。

项目 备注
启用 勾选
锁定 勾选
描述 wg1
阻止私有网络 不勾选
拦截bogon网络 不勾选
IPv4配置类型 静态IPv4
IPv4地址 10.10.10.2/24 来自配置文件 Address 内容,下拉栏选择 24

wg0配置

wg0配置

创建防火墙规则

防火墙 : 规则 : wg1 中,添加规则。

建议规则独立通行,每个端口或服务都独立规则。

进(in)规则

项目 备注
操作 通过
接口 wg1
方向 in
TCP/IP版本 IPv4
协议 any 建议针对端口或服务单独选择
源 / 反转 不勾选
wg1 网络
目标 / 反转 不勾选
目标 wg1 地址

防火墙规则配置

出(out)规则

出(out)规则其实就是 目标 对调了。

项目 备注
操作 通过
接口 wg1
方向 out
TCP/IP版本 IPv4
协议 any 建议针对端口或服务单独选择
源 / 反转 不勾选
wg1 地址
目标 / 反转 不勾选
目标 wg1 网络

防火墙规则配置

保存并应用更改。

防火墙规则列表

查看状态和握手

VPN : WireGuard : 状态VPN : WireGuard : 握手 查看情况。

WireGuard状态

WireGuard握手

计划任务

因为重启(或者 IPv6 地址变化、防火墙重启)后 ,Wireguard 客户端默认非开机启动,建议添加到计划任务中。

参考 OPNsense 防火墙系列三:阿里云 aliddns + 计划任务 - Yogile - 博客园 (cnblogs.com) 的计划任务的固定命令配置。

在 大厅 -> 服务 中,重启 configd 服务。

在 系统 -> 设置 -> 调度任务 中,点击 + 添加,选择 命令 为 actions_ping_wireguard 中的 description ,其他自定义即可。

示例 /usr/local/opnsense/service/conf/actions.d/actions_ping_wireguard.conf

[restart]
command:python3 /home/wireguard/ping_wireguard.py
parameters:
type:script
description:ping_wireguard
message:ping adn wireguard restart

python 程序在 Yogile / opnsense_tools ,我只是改了 ping_subprocess 的程序名。

config.ini 设置为:

[HOST]
host = 10.0.1.1

[PORT]
port = 80

[RESULT]
result = False

[COMMAND]
command = pluginctl -s wireguard restart

在执行程序前,通过 pluginctl -S wireguard 查看运行情况。若显示 [] ,则在 OPNsense 中开启 Wireguard 服务。

参考:

命令行安装

安装配置

22.7 版本的 OPNsense 只提供了 wireguard-go 这样一个服务器的插件,若仅实现客户端的功能可以通过下载 FreeBSD 源的软件。

pkg install wireguard

更多请参阅官方: Installation - WireGuard

在 OPNsense 中, Wireguard 客户端默认配置路径为 /usr/local/etc/wireguard ,于该文件夹下创建 wg0.conf 配置文件,并将从 wg-easy 容器 Web 服务中的配置文件下载粘贴。

[Interface]
PrivateKey = xxxxxxxxxxxxxxxxxxxxxx
Address = 10.0.1.2/24
DNS = 114.114.114.114


[Peer]
PublicKey = xxxxxxxxxxxxxxxxxxxxxxxxx
PresharedKey = xxxxxxxxxxxxxxxxxxxxxxxxxx
AllowedIPs = 10.0.1.0/24
PersistentKeepalive = 25
Endpoint = xxxxxxxxxxxxxxxx:51822

保存后执行:

wg-quick up <默认配置路径>/wg0.conf

防火墙设置

参考 插件方式配置 即可。

posted @ 2023-06-17 15:12  Yogile  阅读(1390)  评论(0编辑  收藏  举报