第三部分:客户端部署

9. Windows客户端配置

9.1 客户端下载地址

9.1.1 官方下载渠道

WireGuard for Windows官方下载:

官方网站:https://www.wireguard.com/install/
直接下载:https://download.wireguard.com/windows-client/wireguard-installer.exe

版本要求:v0.5.3 或更高版本
文件大小:约 3-4 MB
支持系统:Windows 10/11 (64-bit)
数字签名:WireGuard LLC

国内镜像加速(可选):

清华大学镜像:
https://mirrors.tuna.tsinghua.edu.cn/wireguard/windows-client/

阿里云镜像:
https://mirrors.aliyun.com/wireguard/windows-client/

9.1.2 下载方式

方法1:浏览器直接下载

1. 打开浏览器
2. 访问 https://www.wireguard.com/install/
3. 点击 "Windows" 链接
4. 下载 wireguard-installer.exe
5. 保存到本地(如:D:\Downloads\)

方法2:使用PowerShell下载

# 以管理员身份打开PowerShell

# 下载安装程序
Invoke-WebRequest -Uri "https://download.wireguard.com/windows-client/wireguard-installer.exe" -OutFile "C:\Temp\wireguard-installer.exe"

# 验证文件
Get-FileHash C:\Temp\wireguard-installer.exe -Algorithm SHA256

9.1.3 验证安装包完整性

查看数字签名:

# 右键点击 wireguard-installer.exe
# 属性 → 数字签名 → 详细信息
# 验证签名者:WireGuard LLC

验证SHA256哈希值:

# PowerShell命令
Get-FileHash wireguard-installer.exe -Algorithm SHA256

# 对比官方提供的哈希值

9.2 安装步骤

9.2.1 运行安装程序

标准安装流程:

1. 双击 wireguard-installer.exe
2. 如果弹出UAC提示,点击"是"
3. 阅读许可协议,点击"I Agree"
4. 选择安装路径(默认:C:\Program Files\WireGuard)
5. 点击"Install"
6. 等待安装完成(约10-30秒)
7. 点击"Close"完成安装

9.2.2 静默安装(命令行)

管理员PowerShell执行:

# 静默安装
Start-Process -FilePath "wireguard-installer.exe" -ArgumentList "/S" -Wait

# 验证安装
Get-ItemProperty "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\WireGuard"

# 查看安装路径
$env:ProgramFiles\WireGuard\wireguard.exe

9.2.3 验证安装

检查WireGuard是否正确安装:

# 方法1:查看程序列表
Get-WmiObject -Class Win32_Product | Where-Object {$_.Name -like "*WireGuard*"}

# 方法2:检查服务
Get-Service | Where-Object {$_.Name -like "*WireGuard*"}

# 方法3:检查可执行文件
Test-Path "$env:ProgramFiles\WireGuard\wireguard.exe"

# 方法4:启动WireGuard GUI
& "$env:ProgramFiles\WireGuard\wireguard.exe"

预期结果:

9.2.4 卸载WireGuard(如需要)

通过控制面板卸载:

1. 打开"设置" → "应用" → "应用和功能"
2. 搜索"WireGuard"
3. 点击"卸载"
4. 确认卸载

通过PowerShell卸载:

# 查找WireGuard产品GUID
$app = Get-WmiObject -Class Win32_Product | Where-Object {$_.Name -eq "WireGuard"}

# 卸载
$app.Uninstall()

# 或使用静默卸载
& "$env:ProgramFiles\WireGuard\uninstall.exe" /S

9.3 客户端配置文件编写

9.3.1 配置文件位置

Windows客户端配置文件路径:

默认位置:C:\Program Files\WireGuard\Data\Configurations\
配置格式:.conf 文件(INI格式)
文件名示例:wg-client.conf

注意事项:

9.3.2 创建配置文件

方法1:使用文本编辑器创建

1. 打开记事本(Notepad)
2. 复制以下配置内容(见下节)
3. 保存为:wg-client.conf
4. 确保扩展名是 .conf 而非 .txt

方法2:在WireGuard GUI中创建

1. 打开WireGuard应用
2. 点击"Add Tunnel" → "Add empty tunnel..."
3. 在编辑器中粘贴配置
4. 点击"Save"
5. 输入隧道名称(如:VPN-Server)

方法3:从文件导入

1. 打开WireGuard应用
2. 点击"Import tunnel(s) from file"
3. 选择事先准备好的 .conf 文件
4. 点击"打开"

9.3.3 基础配置内容

标准客户端配置文件:

[Interface]
# 客户端私钥(保密!)
PrivateKey = eKX7A+ZxHanItaMVdEIwB3ENUZpQhX0VdPCL1CCWCmE=

# VPN隧道内的客户端IP地址
Address = 10.8.0.2/24

# DNS服务器(可选,推荐配置)
DNS = 223.5.5.5, 114.114.114.114

[Peer]
# 服务器公钥
PublicKey = rUEB9pR7DO7qgkbo6Ylerrh/Fvl1XA8RJuALBdlkFkk=

# 服务器地址和端口
Endpoint = 192.168.1.66:51820

# 允许的IP范围(路由规则)
AllowedIPs = 10.8.0.0/24

# 保活间隔(NAT穿透)
PersistentKeepalive = 25

9.3.4 配置文件模板

模板1:分流模式(仅VPN流量走隧道)

[Interface]
PrivateKey = <客户端私钥>
Address = 10.8.0.2/24
DNS = 223.5.5.5, 114.114.114.114

[Peer]
PublicKey = <服务器公钥>
Endpoint = <服务器IP>:51820
AllowedIPs = 10.8.0.0/24  # 仅VPN网段
PersistentKeepalive = 25

模板2:全流量模式(所有流量走VPN)

[Interface]
PrivateKey = <客户端私钥>
Address = 10.8.0.2/24
DNS = 223.5.5.5, 114.114.114.114

[Peer]
PublicKey = <服务器公钥>
Endpoint = <服务器IP>:51820
AllowedIPs = 0.0.0.0/0, ::/0  # 全部流量
PersistentKeepalive = 25

模板3:多网段访问

[Interface]
PrivateKey = <客户端私钥>
Address = 10.8.0.2/24

[Peer]
PublicKey = <服务器公钥>
Endpoint = <服务器IP>:51820
# VPN网段 + 内网网段
AllowedIPs = 10.8.0.0/24, 192.168.1.0/24, 192.168.100.0/24
PersistentKeepalive = 25

9.4 配置参数说明

9.4.1 [Interface] 部分参数

PrivateKey(必需)

PrivateKey = eKX7A+ZxHanItaMVdEIwB3ENUZpQhX0VdPCL1CCWCmE=

Address(必需)

Address = 10.8.0.2/24
  Address = 10.8.0.2/24   # 标准配置
  Address = 10.8.0.2/32   # 单IP配置

DNS(可选但推荐)

DNS = 223.5.5.5, 114.114.114.114
  # 阿里云DNS
  DNS = 223.5.5.5, 223.6.6.6
  
  # 腾讯DNS
  DNS = 119.29.29.29, 182.254.116.116
  
  # Google DNS
  DNS = 8.8.8.8, 8.8.4.4
  
  # Cloudflare DNS
  DNS = 1.1.1.1, 1.0.0.1
  
  # 使用VPN服务器作为DNS
  DNS = 10.8.0.1

MTU(可选)

MTU = 1420
  # Windows PowerShell测试
  ping -f -l 1472 192.168.1.66
  # 如果成功,MTU = 1472 + 28 = 1500
  # 如果失败,逐步减小直到成功

PreUp / PostUp / PreDown / PostDown(高级,Windows较少使用)

# Windows客户端通常不使用脚本
# 如需执行脚本,需使用完整路径
PostUp = powershell.exe -ExecutionPolicy Bypass -File C:\Scripts\vpn-up.ps1

9.4.2 [Peer] 部分参数

PublicKey(必需)

PublicKey = rUEB9pR7DO7qgkbo6Ylerrh/Fvl1XA8RJuALBdlkFkk=
  # 在服务器上查看
  sudo cat /etc/wireguard/server_public.key

Endpoint(必需)

Endpoint = 192.168.1.66:51820
  # 使用IP地址
  Endpoint = 192.168.1.66:51820
  
  # 使用域名
  Endpoint = vpn.example.com:51820
  
  # 使用公网IP
  Endpoint = 203.0.113.1:51820

AllowedIPs(必需)

AllowedIPs = 10.8.0.0/24
  # 仅VPN内网(分流模式)
  AllowedIPs = 10.8.0.0/24
  
  # VPN + 特定内网
  AllowedIPs = 10.8.0.0/24, 192.168.1.0/24
  
  # 全部流量(全局代理)
  AllowedIPs = 0.0.0.0/0
  
  # 排除特定网段(使用路由表)
  AllowedIPs = 0.0.0.0/1, 128.0.0.0/1

PersistentKeepalive(推荐配置)

PersistentKeepalive = 25

PresharedKey(可选,增强安全)

PresharedKey = <预共享密钥>
  # 在服务器上生成
  wg genpsk
  [Peer]
  PublicKey = ...
  PresharedKey = xK9yL8mP2qR5sT7uV4wX6yZ8aB1cD3eF5gH7iJ9k0L=
  Endpoint = ...

9.5 完整配置示例

9.5.1 标准配置(分流模式)

文件名:wg-client.conf

# ====================================
# WireGuard客户端配置文件
# 模式:分流(仅VPN流量走隧道)
# 客户端:Windows 10/11
# ====================================

[Interface]
# 客户端私钥(请替换为实际私钥)
PrivateKey = eKX7A+ZxHanItaMVdEIwB3ENUZpQhX0VdPCL1CCWCmE=

# 客户端VPN地址
Address = 10.8.0.2/24

# DNS服务器(阿里云)
DNS = 223.5.5.5, 114.114.114.114

# MTU设置
MTU = 1420

[Peer]
# 服务器公钥(请替换为实际公钥)
PublicKey = rUEB9pR7DO7qgkbo6Ylerrh/Fvl1XA8RJuALBdlkFkk=

# 服务器地址和端口
Endpoint = 192.168.1.66:51820

# 路由规则:仅VPN网段流量走隧道
AllowedIPs = 10.8.0.0/24

# 保活间隔(NAT穿透)
PersistentKeepalive = 25

9.5.2 全流量模式配置

文件名:wg-client-full.conf

[Interface]
PrivateKey = eKX7A+ZxHanItaMVdEIwB3ENUZpQhX0VdPCL1CCWCmE=
Address = 10.8.0.2/24
DNS = 223.5.5.5, 114.114.114.114

[Peer]
PublicKey = rUEB9pR7DO7qgkbo6Ylerrh/Fvl1XA8RJuALBdlkFkk=
Endpoint = 192.168.1.66:51820

# 全部流量(IPv4 + IPv6)
AllowedIPs = 0.0.0.0/0, ::/0

PersistentKeepalive = 25

9.5.3 多网段访问配置

文件名:wg-client-multi.conf

[Interface]
PrivateKey = eKX7A+ZxHanItaMVdEIwB3ENUZpQhX0VdPCL1CCWCmE=
Address = 10.8.0.2/24
DNS = 10.8.0.1  # 使用VPN服务器作为DNS

[Peer]
PublicKey = rUEB9pR7DO7qgkbo6Ylerrh/Fvl1XA8RJuALBdlkFkk=
Endpoint = 192.168.1.66:51820

# 多个网段:VPN网段 + 公司内网
AllowedIPs = 10.8.0.0/24, 192.168.1.0/24, 192.168.100.0/24

PersistentKeepalive = 25

9.5.4 带预共享密钥的配置

文件名:wg-client-psk.conf

[Interface]
PrivateKey = eKX7A+ZxHanItaMVdEIwB3ENUZpQhX0VdPCL1CCWCmE=
Address = 10.8.0.2/24
DNS = 223.5.5.5, 114.114.114.114

[Peer]
PublicKey = rUEB9pR7DO7qgkbo6Ylerrh/Fvl1XA8RJuALBdlkFkk=

# 预共享密钥(增强安全性)
PresharedKey = xK9yL8mP2qR5sT7uV4wX6yZ8aB1cD3eF5gH7iJ9k0L=

Endpoint = 192.168.1.66:51820
AllowedIPs = 10.8.0.0/24
PersistentKeepalive = 25

9.6 配置文件导入

9.6.1 通过GUI导入

方法1:导入文件

1. 打开WireGuard应用程序
2. 点击左下角"Add Tunnel"按钮
3. 选择"Import tunnel(s) from file"
4. 浏览到配置文件位置(如:D:\wg-client.conf)
5. 选择文件,点击"打开"
6. 隧道名称自动设置为文件名(不含.conf扩展名)
7. 导入成功后,隧道出现在左侧列表

方法2:粘贴配置

1. 打开WireGuard应用
2. 点击"Add Tunnel" → "Add empty tunnel..."
3. 删除自动生成的内容
4. 粘贴完整配置文本
5. 点击"Save"
6. 输入隧道名称(如:VPN-Server)
7. 点击"OK"

方法3:扫描二维码

1. 在服务器上生成配置二维码:
   qrencode -t ansiutf8 < wg-client.conf
2. 打开WireGuard应用
3. 点击"Add Tunnel" → "Add empty tunnel..."
4. 点击右下角二维码图标
5. 扫描服务器显示的二维码
6. 保存隧道

9.6.2 通过命令行导入

使用WireGuard命令行工具:

# 进入WireGuard安装目录
cd "C:\Program Files\WireGuard"

# 安装隧道(添加配置)
.\wireguard.exe /installtunnelservice "D:\wg-client.conf"

# 验证安装
Get-Service | Where-Object {$_.Name -like "*WireGuard*"}

9.6.3 配置文件存储位置

导入后的配置文件位置:

路径:C:\Program Files\WireGuard\Data\Configurations\
文件:<隧道名称>.conf.dpapi

注意:
- .dpapi是加密后的配置文件
- 使用Windows数据保护API加密
- 与用户账户绑定
- 无法直接编辑,需通过GUI修改

查看配置文件:

# 列出所有隧道配置
Get-ChildItem "C:\Program Files\WireGuard\Data\Configurations\"

# 输出示例:
# VPN-Server.conf.dpapi
# wg-client.conf.dpapi

10. 客户端连接测试

10.1 连接激活命令

10.1.1 通过GUI激活

激活VPN隧道:

1. 打开WireGuard应用
2. 在左侧隧道列表中选择要激活的隧道
3. 点击右侧"Activate"按钮
4. 或者点击隧道左侧的切换开关

状态变化:
- Inactive(灰色)→ Activating(黄色)→ Active(绿色)

激活成功标志:
- 状态显示"Active"
- 图标变为绿色
- 显示接收/发送数据统计
- 显示最后握手时间

停用VPN隧道:

1. 选择已激活的隧道
2. 点击"Deactivate"按钮
3. 或再次点击切换开关

状态变化:
- Active(绿色)→ Deactivating(黄色)→ Inactive(灰色)

10.1.2 通过命令行激活

使用WireGuard CLI:

# 以管理员身份运行PowerShell

# 激活隧道
& "C:\Program Files\WireGuard\wireguard.exe" /installtunnelservice "C:\Program Files\WireGuard\Data\Configurations\VPN-Server.conf.dpapi"

# 或使用服务管理
Start-Service -Name "WireGuardTunnel$VPN-Server"

# 停用隧道
Stop-Service -Name "WireGuardTunnel$VPN-Server"

# 查看隧道状态
Get-Service -Name "WireGuardTunnel$*"

10.1.3 开机自动连接

设置开机启动:

1. 在WireGuard应用中选择隧道
2. 右键点击隧道名称
3. 勾选"Automatically start at boot"
4. 隧道图标旁出现小星标★

取消自动启动:
- 再次右键点击
- 取消勾选"Automatically start at boot"

通过服务管理配置:

# 设置服务为自动启动
Set-Service -Name "WireGuardTunnel$VPN-Server" -StartupType Automatic

# 设置为手动启动
Set-Service -Name "WireGuardTunnel$VPN-Server" -StartupType Manual

# 禁用自动启动
Set-Service -Name "WireGuardTunnel$VPN-Server" -StartupType Disabled

# 验证设置
Get-Service -Name "WireGuardTunnel$VPN-Server" | Select-Object Name, StartType, Status

10.2 连通性测试(ping)

10.2.1 测试VPN隧道连通性

测试VPN网关:

# 打开PowerShell或CMD

# Ping VPN服务器虚拟IP
ping 10.8.0.1

# 预期输出:
# 正在 Ping 10.8.0.1 具有 32 字节的数据:
# 来自 10.8.0.1 的回复: 字节=32 时间<1ms TTL=64
# 来自 10.8.0.1 的回复: 字节=32 时间<1ms TTL=64
# 来自 10.8.0.1 的回复: 字节=32 时间<1ms TTL=64
# 来自 10.8.0.1 的回复: 字节=32 时间<1ms TTL=64
#
# 10.8.0.1 的 Ping 统计信息:
#     数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
# 往返行程的估计时间(以毫秒为单位):
#     最短 = 0ms,最长 = 1ms,平均 = 0ms

持续测试:

# 持续ping(Ctrl+C停止)
ping -t 10.8.0.1

# 指定次数
ping -n 100 10.8.0.1

# 指定数据包大小
ping -l 1400 10.8.0.1

10.2.2 测试内网资源

测试服务器物理IP:

# 通过VPN访问服务器物理网卡
ping 192.168.1.66

# 如果AllowedIPs包含192.168.1.0/24,应该能ping通
# 如果AllowedIPs只有10.8.0.0/24,则走本地路由,不经过VPN

测试内网其他设备:

# 测试内网设备(假设AllowedIPs包含192.168.1.0/24)
ping 192.168.1.1   # 路由器
ping 192.168.1.100 # 其他设备

10.2.3 测试外网连接

验证是否所有流量走VPN:

# 如果AllowedIPs = 0.0.0.0/0(全流量模式)

# 测试公网IP
ping 8.8.8.8

# 查询公网出口IP
curl ifconfig.me
# 或
Invoke-RestMethod -Uri "http://ip-api.com/json"

# 如果显示的IP是VPN服务器的IP,说明流量经过VPN

10.3 路由验证命令

10.3.1 查看路由表

显示完整路由表:

# PowerShell命令
Get-NetRoute | Format-Table -AutoSize

# 或使用CMD命令
route print

# 输出示例(部分):
# ===========================================================================
# 接口列表
#  12...00 ff 4a 7e 8c 91 ......WireGuard Tunnel
#   3...00 50 56 c0 00 08 ......Intel(R) PRO/1000 MT Network Connection
# ===========================================================================
#
# IPv4 路由表
# ===========================================================================
# 活动路由:
# 网络目标        网络掩码          网关       接口  跃点数
#   0.0.0.0          0.0.0.0   192.168.1.1  192.168.1.100     35
#  10.8.0.0      255.255.255.0         在链路上     10.8.0.2    281
#  10.8.0.2  255.255.255.255         在链路上     10.8.0.2    281
# 192.168.1.0    255.255.255.0         在链路上  192.168.1.100    291

查看特定路由:

# 查看10.8.0.0网段的路由
Get-NetRoute -DestinationPrefix "10.8.0.0/24"

# 查看默认路由
Get-NetRoute -DestinationPrefix "0.0.0.0/0"

# 输出示例:
# DestinationPrefix : 10.8.0.0/24
# NextHop           : 0.0.0.0
# InterfaceAlias    : wg-client
# InterfaceIndex    : 12
# RouteMetric       : 0

10.3.2 跟踪路由路径

使用tracert命令:

# 跟踪到VPN服务器的路径
tracert 10.8.0.1

# 预期输出(VPN连接):
#   1    <1 ms    <1 ms    <1 ms  10.8.0.1
# 跟踪完成。

# 如果经过多跳,说明路由有问题

使用pathping命令(更详细):

# 详细路径和延迟分析
pathping -n 10.8.0.1

# 输出包括:
# - 完整路由路径
# - 每跳的丢包率
# - 平均延迟

10.3.3 验证流量走向

测试特定目标的路由:

# 查看到特定IP的路由
Find-NetRoute -RemoteIPAddress 10.8.0.1

# 输出示例:
# IPAddress         : 192.168.1.100
# InterfaceAlias    : wg-client
# NextHop           : 0.0.0.0

# 如果NextHop是0.0.0.0且InterfaceAlias是wg-client,说明走VPN

测试DNS解析路径:

# 查看当前使用的DNS服务器
Get-DnsClientServerAddress -InterfaceAlias "wg-client"

# 或查看所有接口的DNS
Get-DnsClientServerAddress | Format-Table

10.4 访问内网资源测试

10.4.1 HTTP服务访问测试

使用浏览器访问:

打开浏览器(Chrome、Edge、Firefox)

# 访问VPN服务器的Web服务
http://10.8.0.1:8000

# 预期结果:
# - 成功加载Web页面
# - 显示VPN管理界面
# - 左右侧边栏按钮可点击

使用curl命令测试:

# 测试HTTP连接
curl http://10.8.0.1:8000

# 或使用Invoke-WebRequest
Invoke-WebRequest -Uri "http://10.8.0.1:8000"

# 预期输出:
# StatusCode        : 200
# StatusDescription : OK
# Content           : <!DOCTYPE html>...

10.4.2 SSH服务访问测试

使用SSH客户端连接:

# Windows 10/11内置SSH客户端

# 连接到VPN服务器
ssh [email protected]

# 或使用PuTTY
# Host Name: 10.8.0.1
# Port: 22
# Connection Type: SSH

测试端口连通性:

# 测试SSH端口(22)
Test-NetConnection -ComputerName 10.8.0.1 -Port 22

# 预期输出:
# ComputerName     : 10.8.0.1
# RemoteAddress    : 10.8.0.1
# RemotePort       : 22
# InterfaceAlias   : wg-client
# SourceAddress    : 10.8.0.2
# TcpTestSucceeded : True

10.4.3 文件共享访问测试

访问SMB/CIFS共享:

# 映射网络驱动器
net use Z: \\10.8.0.1\share

# 或使用文件资源管理器
# 地址栏输入:\\10.8.0.1\share

访问NFS共享(需安装NFS客户端):

# 挂载NFS共享
mount \\10.8.0.1\nfs Z:

10.4.4 远程桌面连接测试

使用RDP连接内网Windows机器:

1. 打开"远程桌面连接"(mstsc.exe)
2. 计算机:10.8.0.1 或 192.168.1.66
3. 用户名:<用户名>
4. 点击"连接"
5. 输入密码

或使用命令行:
mstsc /v:10.8.0.1

10.4.5 数据库连接测试

MySQL/MariaDB连接:

# 使用MySQL客户端
mysql -h 10.8.0.1 -u root -p

# 或使用数据库管理工具(Navicat、HeidiSQL)
# Host: 10.8.0.1
# Port: 3306

PostgreSQL连接:

psql -h 10.8.0.1 -U postgres

Redis连接:

redis-cli -h 10.8.0.1 -p 6379

10.5 连接状态查看

10.5.1 通过GUI查看状态

查看连接详情:

1. 打开WireGuard应用
2. 选择已激活的隧道
3. 右侧显示详细信息:

显示内容:
- Status: Active(绿色)
- Public key: 服务器公钥(部分显示)
- Endpoint: 服务器地址:端口
- Allowed IPs: 路由规则
- Latest handshake: 最后握手时间(如:2分钟前)
- Transfer: 
  - Received: 接收数据量(如:5.16 MiB)
  - Sent: 发送数据量(如:8.92 MiB)

实时数据统计:

主界面显示:
- 上行速度:实时发送速率(如:125 KiB/s)
- 下行速度:实时接收速率(如:512 KiB/s)
- 总流量:累计收发数据量

10.5.2 通过命令行查看状态

使用PowerShell查看:

# 查看WireGuard服务状态
Get-Service | Where-Object {$_.Name -like "*WireGuard*"}

# 输出示例:
# Status   Name               DisplayName
# ------   ----               -----------
# Running  WireGuardTunne...  WireGuard Tunnel: VPN-Server

# 查看网络接口状态
Get-NetAdapter | Where-Object {$_.InterfaceDescription -like "*WireGuard*"}

# 输出示例:
# Name                      InterfaceDescription    Status
# ----                      --------------------    ------
# wg-client                 WireGuard Tunnel        Up

查看IP配置:

# 查看WireGuard接口的IP配置
Get-NetIPAddress -InterfaceAlias "wg-client"

# 输出示例:
# IPAddress         : 10.8.0.2
# InterfaceAlias    : wg-client
# AddressFamily     : IPv4
# PrefixLength      : 24

查看连接统计:

# 查看网络统计
Get-NetAdapterStatistics -Name "wg-client"

# 输出示例:
# Name                      ReceivedBytes  SentBytes
# ----                      -------------  ---------
# wg-client                 5410234        9346721

10.5.3 查看日志

通过GUI查看日志:

1. 打开WireGuard应用
2. 点击菜单栏"Log" → "View Log"
3. 或点击主界面右上角齿轮图标 → "View Log"

日志内容示例:
2025-03-23 14:30:15.123: [TUN] [wg-client] Starting WireGuard/0.5.3
2025-03-23 14:30:15.234: [TUN] [wg-client] Watching network interfaces
2025-03-23 14:30:15.345: [TUN] [wg-client] Resolving DNS names
2025-03-23 14:30:15.456: [TUN] [wg-client] Creating Wintun interface
2025-03-23 14:30:15.567: [TUN] [wg-client] Binding v4 socket to interface
2025-03-23 14:30:15.678: [TUN] [wg-client] Setting interface configuration
2025-03-23 14:30:15.789: [TUN] [wg-client] Bringing interface up
2025-03-23 14:30:15.890: [TUN] [wg-client] Startup complete

通过PowerShell查看事件日志:

# 查看WireGuard相关的系统事件
Get-EventLog -LogName Application -Source "WireGuard*" -Newest 50

# 或使用Get-WinEvent
Get-WinEvent -FilterHashtable @{LogName='Application'; ProviderName='WireGuard*'} -MaxEvents 50

10.5.4 诊断连接问题

检查握手状态:

在WireGuard GUI中查看"Latest handshake":

正常情况:
- 显示时间(如:2分钟前、30秒前)
- 时间不断更新(每1-2分钟)

异常情况:
- 显示"Never"(从未握手)
  → 检查服务器是否运行
  → 检查Endpoint地址是否正确
  → 检查防火墙是否开放51820端口

- 时间很久(如:10分钟前、1小时前)
  → 检查网络连接
  → 检查PersistentKeepalive设置
  → 可能服务器重启或网络中断

检查数据传输:

查看Transfer统计:

正常情况:
- Received和Sent都在增长
- 有网络活动时数据量变化

异常情况:
- Received=0, Sent>0
  → 服务器可能没有响应
  → 检查服务器防火墙规则
  
- Received>0, Sent=0
  → 客户端可能没有发送流量
  → 检查AllowedIPs配置

网络诊断命令集:

# 综合诊断脚本
function Test-WireGuardConnection {
    Write-Host "=== WireGuard连接诊断 ===" -ForegroundColor Cyan
    
    # 1. 检查服务状态
    Write-Host "`n[1] 服务状态:" -ForegroundColor Yellow
    Get-Service | Where-Object {$_.Name -like "*WireGuard*"} | Format-Table -AutoSize
    
    # 2. 检查网络接口
    Write-Host "`n[2] 网络接口:" -ForegroundColor Yellow
    Get-NetAdapter | Where-Object {$_.InterfaceDescription -like "*WireGuard*"} | Format-Table -AutoSize
    
    # 3. 检查IP配置
    Write-Host "`n[3] IP配置:" -ForegroundColor Yellow
    Get-NetIPAddress -InterfaceAlias "wg-client" -ErrorAction SilentlyContinue | Format-Table -AutoSize
    
    # 4. 测试VPN网关连通性
    Write-Host "`n[4] VPN网关连通性测试:" -ForegroundColor Yellow
    Test-Connection -ComputerName 10.8.0.1 -Count 4
    
    # 5. 检查路由
    Write-Host "`n[5] VPN路由:" -ForegroundColor Yellow
    Get-NetRoute -DestinationPrefix "10.8.0.0/24" -ErrorAction SilentlyContinue | Format-Table -AutoSize
    
    # 6. 测试端口
    Write-Host "`n[6] Web服务测试:" -ForegroundColor Yellow
    Test-NetConnection -ComputerName 10.8.0.1 -Port 8000
    
    Write-Host "`n=== 诊断完成 ===" -ForegroundColor Cyan
}

# 运行诊断
Test-WireGuardConnection

XBR

© 2026 XBR

bilibili bluesky discord