feat: Direct AP 直连提速 + 剔除虚拟网卡 + 版本号提到 0.4.9 便于确认构建

三项一起处理。

1. 所有 PC→手机的二维码页面都有防火墙放行
   「Android → 电脑」页在 dec132c 已加,「电脑 → Android」页在 7b18c6a
   补上,两处都是「防火墙面板 + 二维码广播的地址」并在启动时自动检测。
   本次把版本号从 0.4.8 提到 0.4.9:此前用户反馈"看不到防火墙功能",
   实为未拉取新提交,而界面上没有任何可据以判断构建新旧的标识。

2. Direct AP 下先直连网关,UDP 发现退为兜底
   Direct AP 场景里网关必然就是那台电脑——手机的 IP 正是它的 DHCP 发的。
   此前却把阻塞式 UDP 发现排在最前,白白等掉一两秒才开始真正能成功的
   那一步。现在配网一就绪立刻并发探测「网关 + 二维码携带的地址」
   (400ms 连接 / 600ms 读取),不通才回落到 UDP 广播与子网扫描。

3. Direct AP 下彻底剔除虚拟网卡地址
   二维码只放得下 4 个地址,而实测机器上有 192.168.30.1、192.168.31.1、
   172.21.96.1 三个 VMware/Hyper-V/WSL 地址。上一版只做降权,名额紧张时
   救不了场。Direct AP 启用时改为直接剔除——此刻手机连的是 AP 网段,
   这些地址永远到不了,占着名额纯属浪费。
   新增 is_virtual_endpoint(),对解析不了的字符串一律不判为虚拟,
   避免误删真实地址。

cargo test 51/51。Kotlin 侧本机无法编译,仅结构检查。
This commit is contained in:
zhy200308
2026-08-12 09:22:24 +08:00
parent 7b18c6a661
commit 4c294d4270
5 changed files with 76 additions and 16 deletions
@@ -661,26 +661,42 @@ class MainActivity : ComponentActivity() {
if (connectedBaseUrl != null) return@available
transferExecutor.execute {
if (attempt != pairConnectionAttempt) return@execute
runOnUiThread { updateConnectingStatus("网络路由已就绪 · 正在 UDP 快速发现电脑…") }
val discovered = UploadClient.discoverUdp(
val gateways = wifiConnector.gatewayEndpoints(network, pair.port)
// 先直接打网关。Direct AP 场景下网关**必然**就是那台电脑
// (手机的 IP 是电脑的 DHCP 发的),根本不需要先做发现。
// 此前把阻塞式 UDP 发现排在最前,白白等掉一两秒才开始
// 真正能成功的那一步。
runOnUiThread { updateConnectingStatus("网络已就绪 · 正在直连网关…") }
val direct = UploadClient.probeEndpoints(
network = network,
pairing = pair,
hosts = wifiConnector.discoveryHosts(network),
deviceName = receiverDeviceLabel,
endpoints = (gateways + pair.endpoints).distinct(),
connectMs = 400,
readMs = 600,
)
runOnUiThread {
updateConnectingStatus(
if (discovered != null) "已通过 UDP 找到电脑 · 正在验证接收服务…"
else "UDP 未响应 · 正在并行探测网关与本地 IP…"
// 直连不通才退回发现流程:UDP 广播 → 子网扫描
var discovered: String? = null
var subnet: List<String> = emptyList()
if (direct == null) {
runOnUiThread { updateConnectingStatus("网关未应答 · 正在 UDP 快速发现电脑…") }
discovered = UploadClient.discoverUdp(
network = network,
pairing = pair,
hosts = wifiConnector.discoveryHosts(network),
)
}
val gateways = wifiConnector.gatewayEndpoints(network, pair.port)
val subnet = wifiConnector.subnetEndpoints(network, pair.port)
if (discovered == null) {
subnet = wifiConnector.subnetEndpoints(network, pair.port)
runOnUiThread {
updateConnectingStatus("正在并行探测网关;必要时自动扫描当前 QFT 子网…")
updateConnectingStatus(
if (discovered != null) "已通过 UDP 找到电脑 · 正在验证接收服务…"
else "正在并行探测网关;必要时自动扫描当前 QFT 子网…"
)
}
}
val base = UploadClient.probeRobust(
val base = direct ?: UploadClient.probeRobust(
network = network,
pairing = pair,
deviceName = receiverDeviceLabel,
+1 -1
View File
@@ -2587,7 +2587,7 @@ checksum = "d55d956fa96f5ec02be2e13af0e20391a5aa83d6a074e3ad368959d0fab299ea"
[[package]]
name = "qft-desktop"
version = "0.4.8"
version = "0.4.9"
dependencies = [
"anyhow",
"base64",
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "qft-desktop"
version = "0.4.8"
version = "0.4.9"
edition = "2024"
rust-version = "1.95"
+1 -1
View File
@@ -28,7 +28,7 @@ use std::{
// a nominally larger page that is only decoded 10~18 times per second.
const DEFAULT_CHUNK_SIZE: u16 = 320;
const UI_FONT_NAME: &str = "qft-ui";
const APP_VERSION: &str = "0.4.8";
const APP_VERSION: &str = "0.4.9";
fn main() -> eframe::Result {
let options = eframe::NativeOptions {
+44
View File
@@ -215,6 +215,10 @@ pub fn local_ipv4_addresses() -> Vec<Ipv4Addr> {
pub fn qr_endpoints(port: u16, direct_ap_active: bool) -> Vec<String> {
let mut endpoints = local_ipv4_endpoints(port);
if direct_ap_active {
// Direct AP 场景下二维码名额尤其紧张(只放得下 4 个),而手机此刻
// 连的是 AP 网段,虚拟网卡地址永远到不了,却会挤掉真正可用的地址。
// 这里直接剔除而不只是降权——降权在名额少时救不了场。
endpoints.retain(|endpoint| !is_virtual_endpoint(endpoint));
for ap in [
format!("http://192.168.137.1:{port}"),
format!("http://192.168.49.1:{port}"),
@@ -230,6 +234,15 @@ pub fn qr_endpoints(port: u16, direct_ap_active: bool) -> Vec<String> {
///
/// 依据的是各家虚拟化产品的默认网段,且都以 `.1` 结尾(宿主机在虚拟网络里
/// 通常是网关)。这只是启发式,因此调用方只降权、不丢弃。
/// 判断 `http://IP:port` 形式的端点是否指向虚拟网卡。
fn is_virtual_endpoint(endpoint: &str) -> bool {
endpoint
.strip_prefix("http://")
.and_then(|rest| rest.split(':').next())
.and_then(|host| host.parse::<Ipv4Addr>().ok())
.is_some_and(is_likely_virtual)
}
fn is_likely_virtual(ip: Ipv4Addr) -> bool {
let o = ip.octets();
if o[3] != 1 {
@@ -469,3 +482,34 @@ mod virtual_adapter_tests {
assert!(!is_likely_virtual("172.21.96.200".parse().unwrap()));
}
}
#[cfg(test)]
mod direct_ap_filter_tests {
use super::*;
#[test]
fn virtual_endpoints_are_dropped_when_direct_ap_is_on() {
let endpoints = qr_endpoints(39393, true);
for endpoint in &endpoints {
assert!(
!is_virtual_endpoint(endpoint),
"Direct AP 下不应出现虚拟网卡端点:{endpoint}(完整列表 {endpoints:?})"
);
}
// 剔除之后 AP 网关仍须在最前
assert_eq!(
endpoints.first().map(String::as_str),
Some("http://192.168.137.1:39393")
);
}
#[test]
fn endpoint_parser_handles_malformed_input() {
assert!(is_virtual_endpoint("http://192.168.30.1:39393"));
assert!(!is_virtual_endpoint("http://192.168.8.68:39393"));
// 解析不了的一律不当作虚拟网卡,避免误删真实地址
assert!(!is_virtual_endpoint("garbage"));
assert!(!is_virtual_endpoint("http://:39393"));
assert!(!is_virtual_endpoint(""));
}
}