fix(pairing): Direct AP 网关地址被挤出二维码,修复我上一版引入的回退
手机端诊断给出了决定性证据。二维码广播的三个地址是: 192.168.8.68、192.168.30.1、192.168.31.1 而手机连上 Direct AP 后拿到的是 192.168.137.200/24、网关 192.168.137.1 ——**码里根本没有它能到达的那个地址**。 回退是我造成的。上一版为了让真实网卡优先,把 192.168.137.1 / 192.168.49.1 这两个猜测性兜底地址挪到了列表末尾;而二维码只取前 3 个。二维码是在 点「启动接收」的瞬间生成的,此时 ICS 虚拟网卡往往还没拿到 IP,因此 不会出现在 get_if_addrs 里,只能靠那两个兜底地址补位——一挪到末尾就被 截断掉了。表现是"Wi-Fi 已连接但找不到电脑",从现象几乎无法反推到这里。 改动 - 新增 pairing::qr_endpoints(port, direct_ap_active):Direct AP 启用时 把 ICS 网关地址强制放到最前,未启用时维持真实网卡优先 - 二维码端点上限 3 → 4,以便同时容纳 AP 网关与常规局域网地址 - Windows 接收页新增可展开的「二维码广播的地址」列表。手机报"找不到 电脑"时,第一件要确认的就是码里有没有它能到达的地址——现在一眼可见, 不必等手机端诊断 测试:新增三项,分别锁住「AP 网关必须排第一」「即使网卡很多也不能被 长度上限截断」「未开 AP 时不该让猜测地址占位」。cargo test 43/43。 注意:本次修复的是"地址没进二维码"。手机此前也直接探测过网关 192.168.137.1:39393 并且失败,说明 Windows 在 ICS 虚拟网卡上仍未接受 连接——那是防火墙配置文件的问题,需要在接收页点「放行防火墙」 (规则会以 profile=any 重建,覆盖该网卡的网络类型)。
This commit is contained in:
+11
-2
@@ -604,6 +604,15 @@ impl QftApp {
|
||||
ui.vertical_centered(|ui| { let side = snap_to_physical(side, ui.ctx().pixels_per_point());
|
||||
ui.image((texture.id(), egui::vec2(side, side))); });
|
||||
}
|
||||
// 把二维码实际广播的地址摊开显示。手机报"找不到电脑"时,
|
||||
// 第一件要确认的就是码里到底有没有它能到达的那个地址——
|
||||
// 在这里一眼可见,不必等手机端的诊断。
|
||||
ui.collapsing("二维码广播的地址", |ui| {
|
||||
for endpoint in pairing::qr_endpoints(listen_port, self.receive.direct_ap.is_some()).iter().take(4) {
|
||||
ui.monospace(endpoint);
|
||||
}
|
||||
ui.small("手机连上后会依次尝试这些地址;其中必须有一个与手机同网段。");
|
||||
});
|
||||
if let Some(ap) = &self.receive.direct_ap {
|
||||
ui.label(format!("临时网络:{}", ap.ssid));
|
||||
ui.small("网络身份会在这台电脑上复用以减少重复确认;每次接收仍会生成新的会话 Token。");
|
||||
@@ -1014,7 +1023,7 @@ impl QftApp {
|
||||
pc_name: self.local_device.clone(),
|
||||
transport: "http-parallel-v2".into(),
|
||||
port: server.port,
|
||||
endpoints: pairing::local_ipv4_endpoints(server.port),
|
||||
endpoints: pairing::qr_endpoints(server.port, self.receive.direct_ap.is_some()),
|
||||
wifi,
|
||||
direction: PairingDirection::Upload,
|
||||
file_name: None,
|
||||
@@ -1094,7 +1103,7 @@ impl QftApp {
|
||||
pc_name: self.local_device.clone(),
|
||||
transport: "http-download-v2".into(),
|
||||
port: server.port,
|
||||
endpoints: pairing::local_ipv4_endpoints(server.port),
|
||||
endpoints: pairing::qr_endpoints(server.port, self.send.direct_ap.is_some()),
|
||||
wifi,
|
||||
direction: PairingDirection::Download,
|
||||
file_name: Some(file_name),
|
||||
|
||||
+91
-1
@@ -61,10 +61,12 @@ impl PairingPayload {
|
||||
// 现在无论是否开 Direct AP,都把常规局域网地址一并带上,
|
||||
// 手机可以先在当前网络直连,连不上再切 AP。多两个短地址对
|
||||
// 二维码密度的影响可以忽略。
|
||||
// 取 4 个:Direct AP 场景要同时容纳 AP 网关地址与常规局域网地址,
|
||||
// 只取 3 个时前者会被挤掉——手机于是连上了 AP 却找不到电脑。
|
||||
let endpoints: Vec<&str> = self
|
||||
.endpoints
|
||||
.iter()
|
||||
.take(3)
|
||||
.take(4)
|
||||
.map(String::as_str)
|
||||
.collect();
|
||||
let wifi = self.wifi.as_ref().map(|w| json!([w.ssid, w.passphrase]));
|
||||
@@ -203,6 +205,27 @@ pub fn local_ipv4_addresses() -> Vec<Ipv4Addr> {
|
||||
ips
|
||||
}
|
||||
|
||||
/// 供二维码使用的端点列表。
|
||||
///
|
||||
/// `direct_ap_active` 为真时,把 ICS/Wi-Fi Direct 的固定网关地址放在最前。
|
||||
/// 这一步不能省:二维码是在启动接收的瞬间生成的,而那块虚拟网卡往往
|
||||
/// **还没拿到 IP**,因此它不会出现在 get_if_addrs 的结果里。可手机连上
|
||||
/// AP 之后,网关恰恰就是这个地址——漏掉它,手机就会连上了 Wi-Fi 却
|
||||
/// 找不到电脑。
|
||||
pub fn qr_endpoints(port: u16, direct_ap_active: bool) -> Vec<String> {
|
||||
let mut endpoints = local_ipv4_endpoints(port);
|
||||
if direct_ap_active {
|
||||
for ap in [
|
||||
format!("http://192.168.137.1:{port}"),
|
||||
format!("http://192.168.49.1:{port}"),
|
||||
].into_iter().rev() {
|
||||
endpoints.retain(|e| *e != ap);
|
||||
endpoints.insert(0, ap);
|
||||
}
|
||||
}
|
||||
endpoints
|
||||
}
|
||||
|
||||
pub fn local_ipv4_endpoints(port: u16) -> Vec<String> {
|
||||
let mut ips = Vec::<Ipv4Addr>::new();
|
||||
if let Ok(addrs) = if_addrs::get_if_addrs() {
|
||||
@@ -327,3 +350,70 @@ mod endpoint_order_tests {
|
||||
assert!(json.contains("192.168.137.1"), "Direct AP 地址也应保留:{json}");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod qr_endpoint_tests {
|
||||
use super::*;
|
||||
|
||||
/// Direct AP 的网关地址必须出现在二维码里,且排在最前。
|
||||
///
|
||||
/// 这是一个真实发生过的回退:把"猜测性兜底地址"挪到列表末尾后,
|
||||
/// 二维码只取前 3 个,192.168.137.1 被挤了出去。手机连上了 AP、
|
||||
/// 拿到了 192.168.137.200/24,却在二维码里找不到 192.168.137.1,
|
||||
/// 表现为"Wi-Fi 已连接但找不到电脑"——从现象几乎无法反推到这里。
|
||||
#[test]
|
||||
fn direct_ap_gateway_is_always_advertised_first() {
|
||||
let endpoints = qr_endpoints(39393, true);
|
||||
assert_eq!(
|
||||
endpoints.first().map(String::as_str),
|
||||
Some("http://192.168.137.1:39393"),
|
||||
"Direct AP 网关必须排第一:{endpoints:?}"
|
||||
);
|
||||
assert!(
|
||||
endpoints.iter().any(|e| e == "http://192.168.49.1:39393"),
|
||||
"Wi-Fi Direct 的另一个常用网段也应保留:{endpoints:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn direct_ap_addresses_survive_the_qr_length_cap() {
|
||||
// 即使本机有很多张网卡,AP 地址也不能被截断掉
|
||||
let payload = PairingPayload {
|
||||
v: 2,
|
||||
session: "abcdefghijklmnopqrstuvwxyz012345".into(),
|
||||
pc_name: "PC".into(),
|
||||
transport: "http-parallel-v2".into(),
|
||||
port: 39393,
|
||||
endpoints: qr_endpoints(39393, true),
|
||||
wifi: Some(WifiCredentials {
|
||||
ssid: "QFT-PC-abc".into(),
|
||||
passphrase: "0123456789ab".into(),
|
||||
security: "WPA2".into(),
|
||||
}),
|
||||
direction: PairingDirection::Upload,
|
||||
file_name: None,
|
||||
file_size: None,
|
||||
sha256: None,
|
||||
};
|
||||
let text = payload.encode_text().unwrap();
|
||||
let json = String::from_utf8(
|
||||
base64::engine::general_purpose::URL_SAFE_NO_PAD
|
||||
.decode(text.trim_start_matches(PAIR_PREFIX))
|
||||
.unwrap(),
|
||||
).unwrap();
|
||||
assert!(json.contains("192.168.137.1"), "AP 网关必须进二维码:{json}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn without_direct_ap_the_guessed_addresses_stay_at_the_back() {
|
||||
let endpoints = qr_endpoints(39393, false);
|
||||
// 没开 AP 时不该把两个猜测地址顶到前面去占位
|
||||
if endpoints.len() > 2 {
|
||||
assert_ne!(
|
||||
endpoints.first().map(String::as_str),
|
||||
Some("http://192.168.137.1:39393"),
|
||||
"未开 Direct AP 时不应优先猜测地址:{endpoints:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user