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
+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(""));
}
}