技巧杂烩

wireguard 异地组网自动重连

WireGuard 异地组网自动重连脚本(Windows 版)解决时常因网络波动导致连接中断,提升连接稳定性,建议搭配远程桌面工具作为备用方案。

Windowswireguard

wireguard 异地组网自动重连

在使用 WireGuard 进行异地组网时,这里的使用场景是一端有固定 IP 做为中心点,其他端通过连接中心点 IP 进行远程桌面

graph TD
    A[节点 A<br/>Central Node]

    B[节点 B]
    C[节点 C]
    D[节点 D]

    B <--> A
    C <--> A
    D <--> A

    style A fill:#cce5ff,stroke:#007bff,stroke-width:2px
    style B fill:#e8ffe8,stroke:#34a853
    style C fill:#e8ffe8,stroke:#34a853
    style D fill:#e8ffe8,stroke:#34a853
    linkStyle 0 stroke:#1a73e8,stroke-width:2px
    linkStyle 1 stroke:#1a73e8,stroke-width:2px
    linkStyle 2 stroke:#1a73e8,stroke-width:2px

各个段 Address 和 AllowedIPs 配置为 10.8.0.0/24 的 IP 段,使节点 A - D 处在同一内网网段。

在实际运行过程中,节点出现丢失连接的现象,通过手动重新连接后,连接恢复正常。

配置文件中已有 PersistentKeepalive 参数的配置:

PersistentKeepalive = 25

怀疑到网络中断的问题上,因为某些原因,比如 IP 变动、网络切换等,导致网络中断后,wg0 网卡无法自动重新连接导致的。

由于使用场景以 Windows 为主,这里使用 Windows PowerShell 脚本实现自动重连:

# 设置服务器 IP 和 WireGuard 配置路径
$serverIp = "10.8.0.1"
# $serverPort = 3000
$path = "C:\Program Files\WireGuard\Data\Configurations\"
$config = "wg0"

# 检查 WireGuard 隧道是否启用
$tunnelEnabled = !!(wg show $config 2> $null)

while ($true)
{
    # 使用 TCP 检查指定端口是否连通
    # $tcpConn = Test-NetConnection -ComputerName $serverIp -Port $serverPort
    # $ping = $tcpConn.TcpTestSucceeded
    # Ping 服务器 IP 检查连接是否正常
    $ping = Test-Connection -ComputerName $serverIp -Count 1 -Quiet

    if (-not $ping)
    {
        # Write-Host "$( Get-Date ): 掉线(端口 $serverPort 不通),尝试重启 WireGuard 隧道服务..." -ForegroundColor Red
        Write-Host "$( Get-Date ): 掉线,尝试重启 WireGuard 隧道服务..." -ForegroundColor Red

        # 如果当前隧道已启用,卸载 WireGuard 隧道服务
        if ($tunnelEnabled)
        {
            wireguard.exe /uninstalltunnelservice "$path$config.conf.dpapi"
        }

        # 等待几秒钟后重新安装隧道服务
        Start-Sleep -Seconds 2
        wireguard.exe /installtunnelservice "$path$config.conf.dpapi"

        Write-Host "$( Get-Date ): 重连完成" -ForegroundColor Green
    }
    else
    {
        # Write-Host "$( Get-Date ): 连接正常(端口 $serverPort 可达)" -ForegroundColor Cyan
        Write-Host "$( Get-Date ): 连接正常" -ForegroundColor Cyan
    }

    # 每 10 秒检查一次
    Start-Sleep -Seconds 10

    # 更新隧道状态
    $tunnelEnabled = !!(wg show $config 2> $null)
}

将脚步内容保存为 .ps1 文件,比如 wg-automatic-reconnect.ps1

修改 $serverIp 为你的对端内网 IP 地址,$path 为 WireGuard 配置文件的路径,$config 为你的 WireGuard 配置文件名称

以管理员身份运行 PowerShell 程序,使用 cd 命令切换到包含 .ps1 脚本的文件夹:

cd "C:\path\to\your\script"

默认情况下 PowerShell 会阻止执行未经签名的脚本,通过以下命令来修改执行策略:

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

RemoteSigned 允许执行本地脚本,而从网络下载的脚本必须有有效签名。CurrentUser 只会影响当前用户,不会改变系统级别的设置

在 PowerShell 中,使用以下命令运行 .ps1 脚本,若脚本的文件是 wg-auto-reconnect.ps1

.\wg-auto-reconnect.ps1

脚本运行后,将 PowerShell 的执行策略恢复到默认配置(可省略):

Set-ExecutionPolicy Restricted -Scope CurrentUser

Restricted 是 PowerShell 的默认策略,表示禁止执行任何脚本,只允许交互式命令

针对所有用户恢复为系统默认:

Set-ExecutionPolicy Restricted -Scope LocalMachine

验证执行策略是否更改:

Get-ExecutionPolicy -List

恢复到默认配置后,正在运行脚本不会中断,后续再次运行需要重新调整配置

步骤到这里结束了,在使用 wireguard 做异地组网时,可以安装向日葵ToDesk 等作为备用远程连接方式,为避免 wireguard 中断导致连接不上设备

文章标题:wireguard 异地组网自动重连

文章作者:浅小沫

文章链接:https://blog.truimo.com/posts/wireguard-windows-auto-reconnect


您可以自由在任何媒介以任何形式分享本作品,但需署名,且不得用于商业目的或改编。若分发衍生作品,须采用相同的许可协议。

本博客的所有原创内容采用 CC BY-NC-ND 4.0 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议 进行许可。