From 13624d974707b67bbb4a5b31cec4c7f688399e10 Mon Sep 17 00:00:00 2001 From: zhy200308 <2258391712@qq.com> Date: Wed, 12 Aug 2026 08:59:07 +0800 Subject: [PATCH] =?UTF-8?q?fix(pairing):=20Direct=20AP=20=E7=BD=91?= =?UTF-8?q?=E5=85=B3=E5=9C=B0=E5=9D=80=E8=A2=AB=E6=8C=A4=E5=87=BA=E4=BA=8C?= =?UTF-8?q?=E7=BB=B4=E7=A0=81=EF=BC=8C=E4=BF=AE=E5=A4=8D=E6=88=91=E4=B8=8A?= =?UTF-8?q?=E4=B8=80=E7=89=88=E5=BC=95=E5=85=A5=E7=9A=84=E5=9B=9E=E9=80=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 上一版号称把 AP 网关置顶,实测二维码里依然没有 192.168.137.1, 而手机连上 AP 后网关正是它。两个独立原因叠在一起: 1. 判断条件读了还没赋值的字段 start_receiver 里 direct_ap 是局部变量,要到函数末尾才写回 self.receive.direct_ap;而生成二维码那行读的正是 self.receive.direct_ap, 此刻永远是 None,于是 direct_ap_active 恒为 false,置顶逻辑从未触发。 改为直接用局部变量。发送端同一处问题一并修正。 2. 虚拟网卡把名额占满了 实测机器上有 192.168.30.1、192.168.31.1、172.21.96.1 三个 VMware / Hyper-V / WSL 的地址。它们是真实网卡、排序里与普通局域网 同级,正好填满二维码仅有的 4 个名额,把 AP 网关顶了出去。手机 永远到不了这些地址,却要为它们付出探测时间。 新增 is_likely_virtual():按各家虚拟化产品的默认网段 + 以 .1 结尾 (宿主机在虚拟网络里通常是网关)判断,只降权不丢弃——万一某台机器 真用这些网段做局域网,仍能兜底。 新增三项用例:实测遇到的三个虚拟地址必须被识别;192.168.0.1 / 192.168.1.1 / 192.168.8.1 / 10.0.0.1 这些家用网关不得误伤; 只有 .1 结尾才参与判断,普通主机地址一律不降权。 cargo test 49/49。 --- desktop/src/main.rs | 4 ++-- desktop/src/pairing.rs | 52 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/desktop/src/main.rs b/desktop/src/main.rs index f85bff5..51ce040 100644 --- a/desktop/src/main.rs +++ b/desktop/src/main.rs @@ -1049,7 +1049,7 @@ impl QftApp { pc_name: self.local_device.clone(), transport: "http-parallel-v2".into(), port: server.port, - endpoints: pairing::qr_endpoints(server.port, self.receive.direct_ap.is_some()), + endpoints: pairing::qr_endpoints(server.port, direct_ap.is_some()), wifi, direction: PairingDirection::Upload, file_name: None, @@ -1129,7 +1129,7 @@ impl QftApp { pc_name: self.local_device.clone(), transport: "http-download-v2".into(), port: server.port, - endpoints: pairing::qr_endpoints(server.port, self.send.direct_ap.is_some()), + endpoints: pairing::qr_endpoints(server.port, direct_ap.is_some()), wifi, direction: PairingDirection::Download, file_name: Some(file_name), diff --git a/desktop/src/pairing.rs b/desktop/src/pairing.rs index 0a2276e..0ffef90 100644 --- a/desktop/src/pairing.rs +++ b/desktop/src/pairing.rs @@ -226,6 +226,25 @@ pub fn qr_endpoints(port: u16, direct_ap_active: bool) -> Vec { endpoints } +/// 判断地址是否多半来自虚拟网卡。 +/// +/// 依据的是各家虚拟化产品的默认网段,且都以 `.1` 结尾(宿主机在虚拟网络里 +/// 通常是网关)。这只是启发式,因此调用方只降权、不丢弃。 +fn is_likely_virtual(ip: Ipv4Addr) -> bool { + let o = ip.octets(); + if o[3] != 1 { + return false; + } + match (o[0], o[1]) { + // VMware Workstation 常用 192.168.(1..=254).1 里的高位段; + // 这里只挑实测遇到的与常见默认值,避免误伤家用 192.168.0/1.1 + (192, 168) => matches!(o[2], 30 | 31 | 56 | 99 | 100 | 152 | 174 | 226), + // WSL2 / Hyper-V 默认在 172.16-31 段 + (172, 16..=31) => true, + _ => false, + } +} + pub fn local_ipv4_endpoints(port: u16) -> Vec { let mut ips = Vec::::new(); if let Ok(addrs) = if_addrs::get_if_addrs() { @@ -247,6 +266,11 @@ pub fn local_ipv4_endpoints(port: u16) -> Vec { ips.sort_by_key(|ip| { if *ip == Ipv4Addr::new(192, 168, 137, 1) { 0 } else if *ip == Ipv4Addr::new(192, 168, 49, 1) { 1 } + // 虚拟网卡(VMware / Hyper-V / WSL / Docker)的地址手机永远到不了, + // 却会占满二维码里有限的名额,把真正可达的地址挤出去——实测中 + // 192.168.30.1、192.168.31.1、172.21.96.1 就是这样把 AP 网关顶掉的。 + // 不删除,只降权:万一某台机器真用这些网段做局域网,仍能兜底。 + else if is_likely_virtual(*ip) { 4 } else if ip.is_private() { 2 } else { 3 } }); @@ -417,3 +441,31 @@ mod qr_endpoint_tests { } } } + +#[cfg(test)] +mod virtual_adapter_tests { + use super::*; + + /// 实测中这三个虚拟网卡地址把 Direct AP 网关挤出了二维码。 + #[test] + fn known_virtual_ranges_are_recognised() { + for ip in ["192.168.30.1", "192.168.31.1", "172.21.96.1", "172.17.0.1"] { + assert!(is_likely_virtual(ip.parse().unwrap()), "{ip} 应被识别为虚拟网卡"); + } + } + + /// 家用路由器最常见的网关不能被误伤,否则真实局域网地址反被降权。 + #[test] + fn common_home_gateways_are_not_treated_as_virtual() { + for ip in ["192.168.0.1", "192.168.1.1", "192.168.8.1", "10.0.0.1"] { + assert!(!is_likely_virtual(ip.parse().unwrap()), "{ip} 不应被当作虚拟网卡"); + } + } + + /// 只有 .1 结尾才可能是虚拟网关;普通主机地址一律不降权。 + #[test] + fn only_gateway_addresses_are_considered() { + assert!(!is_likely_virtual("192.168.30.55".parse().unwrap())); + assert!(!is_likely_virtual("172.21.96.200".parse().unwrap())); + } +}