fix(firewall): 规则必须用 profile=any,否则在公用网络下完全不生效

实测发现的真 bug。用户机器上的规则列表:

  qft-windows  Allow Inbound Public           (旧版,能用)
  QFT Desktop  Allow Inbound Domain, Private  (新加,不生效)

Windows 对没见过的 Wi-Fi 默认归类为「公用网络」。规则只覆盖
Private,Domain 时,Get-NetFirewallRule 照样显示 Enabled: True,
但对当前网络根本不适用,连接依旧被拦——这种「配置看起来是对的却没有
效果」的失败最难排查。旧版之所以一直能用,是因为当初点系统安全警报的
「允许」时,Windows 按当时的 Public 配置自动建了规则。

改动
- grant() 生成的 netsh 规则改为 profile=any
- status() 不再只看「有没有同名规则」,还要求它是 any profile,
  否则同样报 Missing。避免出现「检测说已放行、实际仍被拦」
- README 补充排查步骤:先看 NetworkCategory 与规则的 Profile 两列,
  以及 Block 规则优先于 Allow 这个常见坑

关于收窄 profile 的取舍:限定到 private 并不能带来实际安全收益。
QFT 的入站真正靠的是连接码里的秘密加失败计数,没有秘密的连接一律被拒;
而按 profile 收窄只会在最常见的家用/公共 Wi-Fi 场景下静默失效。

验证:cargo test 35/35。Windows 分支仍无法在本机执行,
netsh 与 UAC 那条路径需要在 Windows 上实测。
This commit is contained in:
zhy200308
2026-08-11 18:48:41 +08:00
parent 0f0dbaef47
commit bcadcf0b53
2 changed files with 49 additions and 7 deletions
+28 -2
View File
@@ -41,8 +41,34 @@ Caused by: 拒绝访问。 (os error 5)
「放行防火墙(需要管理员授权)」按钮——**提权只在你点击时发生**,
不会在启动时自动弹 UAC。
也可以自己加规则。注意二进制已从 `qft-windows.exe` 改名为
`qft-desktop.exe`,**此前给旧名字放行的规则对新程序不生效**。
也可以自己加规则。两个必须注意的点:
1. 二进制已从 `qft-windows.exe` 改名为 `qft-desktop.exe`,**此前给旧名字
放行的规则对新程序不生效**。
2. **profile 必须是 `Any`**。Windows 对没见过的 Wi-Fi 默认归类为
「公用网络」,此时只覆盖 `Private,Domain` 的规则根本不适用——
规则建了、`Get-NetFirewallRule` 也显示 Enabled,但连接照样被拦。
```powershell
$exe = "<完整路径>\qft-desktop.exe"
# 先清掉可能存在的阻止规则:Windows 里 Block 永远优先于 Allow
Get-NetFirewallApplicationFilter -Program $exe -ErrorAction SilentlyContinue |
Get-NetFirewallRule | Where-Object Action -eq 'Block' | Remove-NetFirewallRule
New-NetFirewallRule -DisplayName "QFT Desktop" -Direction Inbound `
-Program $exe -Action Allow -Profile Any
```
排查时先看这两列:
```powershell
Get-NetConnectionProfile | Format-List Name,NetworkCategory
Get-NetFirewallApplicationFilter | Where-Object Program -like "*qft*" |
ForEach-Object { $r = $_ | Get-NetFirewallRule
[PSCustomObject]@{ 规则=$r.DisplayName; 动作=$r.Action; 配置=$r.Profile } } | Format-Table
```
若 `NetworkCategory` 是 `Public` 而规则的 `配置` 只有 `Domain, Private`,
就是这个原因。
管理员 PowerShell: