如何在 Windows 后台启动 wireguard 客户端?

如何在 Windows 后台启动 wireguard 客户端?

我有多台计算机(Win10 pro)放置在不同的远程位置(我的合作伙伴),我需要管理它们。我需要从中心位置访问它们。所以我有一个中央 VPN 服务器,并且我希望多个 Win10 pro 实例连接到它。我正在使用 L2TP,但我想迁移到 wireguard。使用 L2TP,可以从任务计划程序在后台启动 rasdial.exe。主要目标是在计算机启动后立即启动隧道,并在隧道断开时自动重新连接。我需要在任何用户登录计算机之前(例如在后台)执行此操作。我想知道是否可以对 wireguard 执行相同的操作?

我注意到有一个名为 C:\Program Files\Wireguard\wg.exe 的程序,它的命令行选项与 wg(linux)程序非常相似:

C:\Program Files\WireGuard>wg.exe --help
Usage: wg.exe <cmd> [<args>]

Available subcommands:
  show: Shows the current configuration and device information
  showconf: Shows the current configuration of a given WireGuard interface, for use with `setconf'
  set: Change the current configuration, add peers, remove peers, or change peers
  setconf: Applies a configuration file to a WireGuard interface
  addconf: Appends a configuration file to a WireGuard interface
  syncconf: Synchronizes a configuration file to a WireGuard interface
  genkey: Generates a new private key and writes it to stdout
  genpsk: Generates a new preshared key and writes it to stdout
  pubkey: Reads a private key from stdin and writes a public key to stdout
You may pass `--help' to any of these subcommands to view usage.

但我认为它仅用于配置。我认为它不能用于激活隧道。原始文档(https://www.wireguard.com/quickstart/#command-line-interface) 解释说“该接口可以...通过 ifconfig(8) 或 ip-link(8) 激活”,但这当然只适用于 unix。在该文档中,还有另一条注释“非 Linux 用户将改为编写 wireguard-go wg0”,但我不确定他们在这里的意思。任何地方都没有程序名称“wireguard-go”。隧道的网络适配器仅在隧道已处于活动状态时才会显示在网络适配器列表中。例如,当隧道关闭时,没有我可以“启用”或“激活”的网络适配器。最后,还有 wireguard.exe。当不带参数启动时,它是一个 GUI 应用程序。它无法在没有登录用户和桌面的情况下运行,并且它不会自动激活任何隧道。它有一些有趣的命令行选项。“wireguard.exe /managerservice”用于将其作为 Windows 服务启动。“wireguard.exe /tunnelservice CONFIG_PATH”看起来很有希望,但我无法启动它。我收到此错误,“该进程无法连接到服务管理器”或类似错误。(当我以管理员身份启动它时也收到此错误。)

我没有主意了。我该怎么做?

答案1

我在以下网址找到了相关说明https://r-pufky.github.io/docs/services/wireguard/windows-setup.html

Start-Process 'C:\Program Files\WireGuard\wireguard.exe' -ArgumentList '/installtunnelservice', 'my-tunnel.conf' -Wait -NoNewWindow -PassThru | Out-Null
Start-Process sc.exe -ArgumentList 'config', 'WireGuardTunnel$my-tunnel', 'start= delayed-auto' -Wait -NoNewWindow -PassThru | Out-Null
Start-Service -Name WireGuardTunnel$my-tunnel -ErrorAction SilentlyContinue

答案2

针对此问题的其他所有解决方案都不理想。我见过的方法包括:

  1. 仅以管理员身份运行

  2. 将用户帐户添加到网络配置操作员

    • 优点:Wireguard GUI,运行流畅
    • 缺点:您的普通用户将被添加到管理员组,并会出现在 UAC 提示中。这非常烦人。
  3. 将 WireGuard 隧道作为 Windows 服务运行(如建议的那样回答)

    • 优点:运行完美
    • 缺点:没有 GUI,需要升高的控制台。

解决方案:

  1. 安装最新的 MSI:https://download.wireguard.com/windows-client/

  2. 然后使用 .conf 文件在提升的控制台中运行此命令:

    wireguard /installtunnelservice C:\path\to\some\myconfname.conf

这将创建一个名为 的服务WireGuardTunnel$myconfname,可以使用标准 Windows 服务管理实用程序(例如 services.msc 或 sc)进行控制。—来源

  1. 使用“ServiceTray”控制服务:https://www.coretechnologies.com/products/ServiceTray/

    (这会在系统托盘上给您一个漂亮的图标,显示 WireGuard 隧道服务的启动状态。绿色 = 已连接,红色 = 未连接,您可以通过右键单击来启动和停止它。见下图)

    注意:创建服务控制器时,将快捷方式保存到桌面而不是启动项(似乎不起作用,您可以稍后复制到启动项)

  2. (可选)如果您不想在启动时连接到隧道,请将服务的启动类型更改为手动。

在 Win 10 上的示例. 将鼠标悬停在图标上会显示隧道的名称。

答案3

我发现也许更简单的选择是:https://git.zx2c4.com/wireguard-windows/about/docs/enterprise.md

wireguard /installtunnelservice C:\path\to\some\myconfname.conf

答案4

我喜欢 bn-l 的答案,但启动或停止连接仍然需要管理员覆盖。我决定更进一步。我编写了一个 PowerShell 脚本,如果文件不存在,则在临时目录中创建一个文件;如果文件已存在,则删除它。此脚本无需额外权限即可运行。

然后我编写了另一个脚本,该脚本根据该文件的存在与否以一秒为周期启动或停止服务。我将其作为计划任务以 SYSTEM 身份运行,并由系统启动触发。

现在我使用该脚本作为启动和停止服务的按钮,但不需要任何管理员登录或覆盖,只需打开或关闭 VPN。系统托盘图标非常方便地显示红色或绿色指示器。

还有一件值得注意的事情是,我使用了管理 GUI 创建的配置文件。在我的系统上,它位于“C:\Program Files\WireGuard\Data\Configurations[连接名称].conf.dpapi”

相关内容