fix(desktop): 防火墙提权改为手动触发;构建前检测占用;清理警告
三个问题一起处理。 1. 提权改为手动 上一版会在点「启动接收」时自动弹 UAC。实际用下来太侵入——多数人 只是想收个文件,却先被要求管理员授权。现在: - 检测保持自动,但**只读**,无任何副作用 - 提权只在用户点「放行防火墙(需要管理员授权)」时发生 - 按钮无条件保留,不再只在"检测到被拦"时出现:检测可能不准, 而用户遇到「对方连不上」时需要有地方可点 - 同时移除了拒绝标记的持久化——提权已不自动发生,无需记忆 2. Windows 构建报 os error 5 error: failed to remove file `...\target\release\qft-desktop.exe` Caused by: 拒绝访问。 (os error 5) 原因是 QFT 窗口还开着,运行中的 exe 锁住了产物文件,而 cargo 的报错 完全看不出这一层。VERIFY_WINDOWS_HOST.ps1 现在会在构建前检测 qft-desktop 进程,列出 PID 并询问是否结束;README 也补了说明 (含 Defender 扫描导致的偶发同类报错)。 3. 构建警告 15 → 5 长期堆积的警告会训练人忽略所有警告,所以逐条处理: - 真实问题:if 条件多余的括号,已修 - 为后续里程碑准备但尚未接线的 API(client::ping、download_file、 platform::direct_ap_support/ensure_dir),加 #[allow(dead_code)] 并注明将在哪个阶段使用 - 仅特定平台构造的项(firewall::RULE_NAME 只用于 Windows, FirewallStatus::Unsupported 只在 Linux 返回),改用 #[cfg_attr(not(...), allow(dead_code))],避免无条件放行 剩余 5 个均为本次改动之前既有(cache_mib、v/transport、security、 path),不属本次范围,未改动。 验证:cargo test 35/35,release 构建 5 个警告且全部是既有的。
This commit is contained in:
@@ -18,10 +18,31 @@ v0.4.7 提供两套互不依赖的传输方式:
|
|||||||
|
|
||||||
## v0.5-m1:电脑 ↔ 电脑互传(新增)
|
## v0.5-m1:电脑 ↔ 电脑互传(新增)
|
||||||
|
|
||||||
### Windows 首次运行必须放行防火墙
|
### Windows 构建报 `os error 5 拒绝访问`
|
||||||
|
|
||||||
二进制已从 `qft-windows.exe` 改名为 `qft-desktop.exe`,**此前给旧名字放行的
|
```
|
||||||
防火墙规则对新程序不生效**,接收方会表现为「对方能解析连接码但连不上」。
|
error: failed to remove file `...\target\release\qft-desktop.exe`
|
||||||
|
Caused by: 拒绝访问。 (os error 5)
|
||||||
|
```
|
||||||
|
|
||||||
|
**QFT 窗口还开着**,正在运行的 exe 锁住了产物文件。关掉窗口再构建即可。
|
||||||
|
`VERIFY_WINDOWS_HOST.ps1` 会在构建前检测并询问是否结束进程。
|
||||||
|
|
||||||
|
若关掉后仍然报错,通常是 Windows Defender 正在扫描刚生成的 exe,
|
||||||
|
稍等几秒重试;也可以把 `desktop\target` 加入 Defender 排除目录。
|
||||||
|
|
||||||
|
### 防火墙放行(手动)
|
||||||
|
|
||||||
|
|
||||||
|
接收端要监听入站 TCP,Windows 防火墙默认会拦截未授权程序,表现是
|
||||||
|
「对方能解析连接码但连接一直超时」。
|
||||||
|
|
||||||
|
程序在「电脑 ↔ 电脑 → 我要接收」页会**只读检测**防火墙状态,并提供
|
||||||
|
「放行防火墙(需要管理员授权)」按钮——**提权只在你点击时发生**,
|
||||||
|
不会在启动时自动弹 UAC。
|
||||||
|
|
||||||
|
也可以自己加规则。注意二进制已从 `qft-windows.exe` 改名为
|
||||||
|
`qft-desktop.exe`,**此前给旧名字放行的规则对新程序不生效**。
|
||||||
|
|
||||||
管理员 PowerShell:
|
管理员 PowerShell:
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,25 @@ if (Get-Command python -ErrorAction SilentlyContinue) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Write-Host "[2/4] Desktop Rust compile/test/release"
|
Write-Host "[2/4] Desktop Rust compile/test/release"
|
||||||
|
|
||||||
|
# 正在运行的 qft-desktop.exe 会锁住产物文件,cargo 只会抛出
|
||||||
|
# "failed to remove file ... 拒绝访问 (os error 5)" 这种看不出原因的错误。
|
||||||
|
# 这里提前识别并说清楚,而不是让用户去猜。
|
||||||
|
$Running = Get-Process -Name 'qft-desktop' -ErrorAction SilentlyContinue
|
||||||
|
if ($Running) {
|
||||||
|
Write-Host ""
|
||||||
|
Write-Warning "检测到 qft-desktop.exe 正在运行(PID: $($Running.Id -join ', '))。"
|
||||||
|
Write-Warning "它会锁住 target\release\qft-desktop.exe,导致构建报 'os error 5 拒绝访问'。"
|
||||||
|
$Answer = Read-Host "是否结束这些进程后继续构建?(y/N)"
|
||||||
|
if ($Answer -eq 'y' -or $Answer -eq 'Y') {
|
||||||
|
$Running | Stop-Process -Force
|
||||||
|
Start-Sleep -Milliseconds 500
|
||||||
|
Write-Host "已结束,继续构建。"
|
||||||
|
} else {
|
||||||
|
throw "请先关闭 QFT 窗口再重新运行本脚本"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Push-Location "$Root\desktop"
|
Push-Location "$Root\desktop"
|
||||||
try {
|
try {
|
||||||
& cargo check
|
& cargo check
|
||||||
|
|||||||
@@ -41,6 +41,10 @@ pub struct PeerClient {
|
|||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
pub struct Progress {
|
pub struct Progress {
|
||||||
pub done: u64,
|
pub done: u64,
|
||||||
|
/// 单个文件的总量。多文件发送时 UI 用的是跨文件的累计总量,
|
||||||
|
/// 因此当前没有读者;保留是为了让回调本身自洽,
|
||||||
|
/// 单文件场景(如 P2 的目录传输逐项进度)会用到。
|
||||||
|
#[allow(dead_code)]
|
||||||
pub total: u64,
|
pub total: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,6 +83,8 @@ impl PeerClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// `GET /qft/ping`。用于发现阶段快速验证「这个地址上确实是我要找的那台 QFT」。
|
/// `GET /qft/ping`。用于发现阶段快速验证「这个地址上确实是我要找的那台 QFT」。
|
||||||
|
// 供 P1.5 的发现流程与后续的可达性诊断使用,当前 UI 尚未接线
|
||||||
|
#[allow(dead_code)]
|
||||||
pub fn ping(&self, timeout: Duration) -> Result<serde_json::Value> {
|
pub fn ping(&self, timeout: Duration) -> Result<serde_json::Value> {
|
||||||
let address = self.address()?;
|
let address = self.address()?;
|
||||||
let mut response = send_request(address, "GET", "/qft/ping", &self.auth_headers(), None, timeout)?;
|
let mut response = send_request(address, "GET", "/qft/ping", &self.auth_headers(), None, timeout)?;
|
||||||
@@ -305,6 +311,8 @@ impl PeerClient {
|
|||||||
|
|
||||||
/// 从对端的发送服务拉取文件(对端跑 `sender.rs`)。
|
/// 从对端的发送服务拉取文件(对端跑 `sender.rs`)。
|
||||||
/// 支持断点续传:`dest` 已存在的部分会作为 Range 起点。
|
/// 支持断点续传:`dest` 已存在的部分会作为 Range 起点。
|
||||||
|
// PC↔PC 目前只做「推送」,「主动拉取」在 P2 的会话模型里才用得上
|
||||||
|
#[allow(dead_code)]
|
||||||
pub fn download_file(
|
pub fn download_file(
|
||||||
&self,
|
&self,
|
||||||
dest: &Path,
|
dest: &Path,
|
||||||
@@ -399,6 +407,7 @@ fn desired_chunk_size(size: u64) -> u64 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)] // 仅被尚未接线的 download_file 使用
|
||||||
fn platform_ensure_dir(path: &Path) -> Result<()> {
|
fn platform_ensure_dir(path: &Path) -> Result<()> {
|
||||||
crate::platform::ensure_dir(path)
|
crate::platform::ensure_dir(path)
|
||||||
.with_context(|| format!("创建目录 {} 失败", path.display()))
|
.with_context(|| format!("创建目录 {} 失败", path.display()))
|
||||||
|
|||||||
+7
-32
@@ -18,6 +18,8 @@ use anyhow::{bail, Context, Result};
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
/// 规则显示名。检测与添加都用它,改名会导致旧规则失效。
|
/// 规则显示名。检测与添加都用它,改名会导致旧规则失效。
|
||||||
|
// 仅 Windows 分支使用;其余平台整段被 cfg 编译掉
|
||||||
|
#[cfg_attr(not(target_os = "windows"), allow(dead_code))]
|
||||||
pub const RULE_NAME: &str = "QFT Desktop";
|
pub const RULE_NAME: &str = "QFT Desktop";
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||||
@@ -28,7 +30,8 @@ pub enum FirewallStatus {
|
|||||||
Missing,
|
Missing,
|
||||||
/// 查不出来(命令不可用、输出无法解析等),不要据此吓唬用户
|
/// 查不出来(命令不可用、输出无法解析等),不要据此吓唬用户
|
||||||
Unknown(String),
|
Unknown(String),
|
||||||
/// 本平台不做自动放行
|
/// 本平台不做自动放行(目前只有 Linux 会返回)
|
||||||
|
#[cfg_attr(not(all(unix, not(target_os = "macos"))), allow(dead_code))]
|
||||||
Unsupported(&'static str),
|
Unsupported(&'static str),
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,27 +41,6 @@ impl FirewallStatus {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 用户拒绝过提权的标记。存在则不再自动弹窗。
|
|
||||||
fn declined_marker() -> PathBuf {
|
|
||||||
crate::platform::config_dir().join("firewall-declined")
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn was_declined() -> bool {
|
|
||||||
declined_marker().is_file()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn remember_declined() {
|
|
||||||
let path = declined_marker();
|
|
||||||
if let Some(parent) = path.parent() {
|
|
||||||
let _ = crate::platform::ensure_dir(parent);
|
|
||||||
}
|
|
||||||
let _ = std::fs::write(&path, b"1");
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn forget_declined() {
|
|
||||||
let _ = std::fs::remove_file(declined_marker());
|
|
||||||
}
|
|
||||||
|
|
||||||
fn current_exe() -> Result<PathBuf> {
|
fn current_exe() -> Result<PathBuf> {
|
||||||
std::env::current_exe().context("无法获取本程序路径")
|
std::env::current_exe().context("无法获取本程序路径")
|
||||||
}
|
}
|
||||||
@@ -286,16 +268,9 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn declined_marker_round_trips() {
|
fn status_is_read_only_and_repeatable() {
|
||||||
// 记住/遗忘的状态必须能持久化,否则每次启动都会重新弹窗
|
// 检测必须无副作用:连续调用结果一致,且不改动任何系统设置
|
||||||
let existed = was_declined();
|
assert_eq!(status(), status());
|
||||||
remember_declined();
|
|
||||||
assert!(was_declined(), "记录拒绝后应能读回");
|
|
||||||
forget_declined();
|
|
||||||
assert!(!was_declined(), "清除后应恢复");
|
|
||||||
if existed {
|
|
||||||
remember_declined();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+29
-29
@@ -1063,7 +1063,7 @@ impl QftApp {
|
|||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
if self.pc.firewall.needs_action() || !self.pc.firewall_note.is_empty() {
|
{
|
||||||
ui.add_space(10.0);
|
ui.add_space(10.0);
|
||||||
ui.group(|ui| {
|
ui.group(|ui| {
|
||||||
let blocked = self.pc.firewall.needs_action();
|
let blocked = self.pc.firewall.needs_action();
|
||||||
@@ -1078,16 +1078,21 @@ impl QftApp {
|
|||||||
ui.label(&self.pc.firewall_note);
|
ui.label(&self.pc.firewall_note);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if blocked {
|
ui.horizontal_wrapped(|ui| {
|
||||||
ui.horizontal_wrapped(|ui| {
|
// 无论检测结果如何都保留入口:检测可能不准,
|
||||||
if ui.button("放行防火墙").clicked() {
|
// 而用户遇到「对方连不上」时需要有地方可点。
|
||||||
firewall::forget_declined();
|
if ui.button("放行防火墙(需要管理员授权)").clicked() {
|
||||||
self.ensure_firewall();
|
self.request_firewall_grant();
|
||||||
}
|
}
|
||||||
if ui.small_button("复制手动放行命令").clicked() {
|
if ui.small_button("复制手动放行命令").clicked() {
|
||||||
ui.ctx().copy_text(firewall::manual_instructions(port));
|
ui.ctx().copy_text(firewall::manual_instructions(port));
|
||||||
}
|
}
|
||||||
});
|
if ui.small_button("重新检测").clicked() {
|
||||||
|
self.refresh_firewall();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if !blocked {
|
||||||
|
ui.small("检测认为入站不会被拦。若对方仍连不上,可手动放行试试。");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -1151,8 +1156,8 @@ impl QftApp {
|
|||||||
&& ui.input(|i| i.key_pressed(egui::Key::Enter));
|
&& ui.input(|i| i.key_pressed(egui::Key::Enter));
|
||||||
let parsed = connect_code::ConnectCode::decode(&self.pc.code_input);
|
let parsed = connect_code::ConnectCode::decode(&self.pc.code_input);
|
||||||
let can_connect = !self.pc.busy && parsed.is_ok();
|
let can_connect = !self.pc.busy && parsed.is_ok();
|
||||||
if (ui.add_enabled(can_connect, egui::Button::new("连接")).clicked()
|
if ui.add_enabled(can_connect, egui::Button::new("连接")).clicked()
|
||||||
|| (submitted && can_connect))
|
|| (submitted && can_connect)
|
||||||
{
|
{
|
||||||
self.pair_pc_peer();
|
self.pair_pc_peer();
|
||||||
}
|
}
|
||||||
@@ -1228,38 +1233,33 @@ impl QftApp {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 检测防火墙,必要时**自动**申请一次放行。
|
/// 只做**只读检测**,不申请任何权限。
|
||||||
///
|
///
|
||||||
/// 只在确实缺规则、且用户此前没拒绝过时才弹系统授权框——
|
/// 早先版本会在启动接收时自动弹 UAC。实际用下来这太侵入:
|
||||||
/// 每次启动都弹 UAC 属于骚扰。用户点过「否」就记住,
|
/// 多数人只是想收个文件,却先被要求管理员授权。
|
||||||
/// 之后只在界面上留一个手动按钮。
|
/// 现在提权一律由用户点按钮触发(`request_firewall_grant`)。
|
||||||
fn ensure_firewall(&mut self) {
|
fn refresh_firewall(&mut self) {
|
||||||
self.pc.firewall = firewall::status();
|
self.pc.firewall = firewall::status();
|
||||||
if !self.pc.firewall.needs_action() {
|
if !self.pc.firewall.needs_action() {
|
||||||
self.pc.firewall_note.clear();
|
self.pc.firewall_note.clear();
|
||||||
return;
|
|
||||||
}
|
|
||||||
if firewall::was_declined() {
|
|
||||||
self.pc.firewall_note =
|
|
||||||
"此前拒绝过防火墙授权。若对方连不上,请点下方按钮重新放行。".into();
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 用户显式点击后才提权。
|
||||||
|
fn request_firewall_grant(&mut self) {
|
||||||
match firewall::grant() {
|
match firewall::grant() {
|
||||||
Ok(()) => {
|
Ok(()) => {
|
||||||
self.pc.firewall = firewall::status();
|
self.pc.firewall = firewall::status();
|
||||||
self.pc.firewall_note = "已放行系统防火墙。".into();
|
self.pc.firewall_note = "已放行系统防火墙。".into();
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => self.pc.firewall_note = format!("{e}"),
|
||||||
firewall::remember_declined();
|
|
||||||
self.pc.firewall_note = format!("{e}。若对方连不上,可稍后手动放行。");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn start_pc_receiver(&mut self) {
|
fn start_pc_receiver(&mut self) {
|
||||||
let Some(dir) = self.pc.save_dir.clone() else { return };
|
let Some(dir) = self.pc.save_dir.clone() else { return };
|
||||||
self.stop_pc();
|
self.stop_pc();
|
||||||
self.ensure_firewall();
|
self.refresh_firewall();
|
||||||
let token = pairing::new_session_token();
|
let token = pairing::new_session_token();
|
||||||
let (tx, rx) = mpsc::channel();
|
let (tx, rx) = mpsc::channel();
|
||||||
match ReceiverServer::start(dir, token, tx) {
|
match ReceiverServer::start(dir, token, tx) {
|
||||||
|
|||||||
@@ -163,11 +163,14 @@ pub const fn platform_tag() -> &'static str {
|
|||||||
/// 只有 Windows 走 WiFi Direct;macOS 无公开 API,Linux 各发行版
|
/// 只有 Windows 走 WiFi Direct;macOS 无公开 API,Linux 各发行版
|
||||||
/// NetworkManager 行为差异过大,都不做——UI 据此隐藏入口,
|
/// NetworkManager 行为差异过大,都不做——UI 据此隐藏入口,
|
||||||
/// 而不是让用户点进去再失败。
|
/// 而不是让用户点进去再失败。
|
||||||
|
// UI 尚未据此隐藏 Direct AP 入口,留待 P1.7 收尾
|
||||||
|
#[allow(dead_code)]
|
||||||
pub enum DirectApSupport {
|
pub enum DirectApSupport {
|
||||||
Supported,
|
Supported,
|
||||||
Unsupported(&'static str),
|
Unsupported(&'static str),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
pub const fn direct_ap_support() -> DirectApSupport {
|
pub const fn direct_ap_support() -> DirectApSupport {
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
{
|
{
|
||||||
@@ -180,6 +183,7 @@ pub const fn direct_ap_support() -> DirectApSupport {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl DirectApSupport {
|
impl DirectApSupport {
|
||||||
|
#[allow(dead_code)]
|
||||||
pub const fn is_supported(&self) -> bool {
|
pub const fn is_supported(&self) -> bool {
|
||||||
matches!(self, DirectApSupport::Supported)
|
matches!(self, DirectApSupport::Supported)
|
||||||
}
|
}
|
||||||
@@ -241,6 +245,7 @@ fn xdg_user_dir(key: &str) -> Option<PathBuf> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// 确保目录存在,返回其路径。调用方负责处理失败。
|
/// 确保目录存在,返回其路径。调用方负责处理失败。
|
||||||
|
#[allow(dead_code)] // 由 client 的下载路径使用,该路径尚未接线
|
||||||
pub fn ensure_dir(path: &Path) -> std::io::Result<()> {
|
pub fn ensure_dir(path: &Path) -> std::io::Result<()> {
|
||||||
if path.is_dir() {
|
if path.is_dir() {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
|||||||
Reference in New Issue
Block a user