feat(scan): WiFi-ADB 扫描支持自定义/多端口(覆盖 Android 11+ 随机端口)

之前只扫 5555。Android 11+ "无线调试" 用 37000-44999 随机端口,
固定的 adb tcpip 也可改端口。新接口让扫描适配任意端口规格。

新增 parse_port_spec:解析端口规格字符串
  - 5555            单端口
  - "5555,5556"     逗号分隔
  - "37000-37010"   范围(最大 1024 个)
  - 混合格式与序列输入
  - 非法/越界 → 退化为默认 5555
  - 单个范围 > 1024 端口直接忽略,避免误扫整个端口段

scan_open_ports 重构:
  - 接受 ports 参数(list[int]),传 ports 时优先于旧 port=int
  - 返回 list[(ip, port)] 而不是 list[ip],按 (IP 数值序, port 升序) 稳定排序

discover_adb_devices 支持 ports 参数,按 (ip, port) 逐个 verify_adb_target

接入 GUI 与 webui:
  - gui_app.py 扫描对话框新增"端口"输入框 + 提示
  - gui_app.py 启动重连:上次端口非 5555 时把它一并扫描
  - web_gui.py /api/wifi/scan 接受 ports;/api/wifi/auto-reconnect 同上次端口逻辑

新增 13 个 parse_port_spec 单测 + 4 个 scan_open_ports 多端口测试 +
2 个 discover_adb_devices 多端口集成测试。
真机验证:6 端口 (5555 + 37000-37005) × 254 IP,~8.5s 扫完,
正确发现 192.168.5.2:5555 (PEPM00)。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
CodexandClaude Opus 4.7 committed 2026-05-23 01:02:48 +08:00
1 parent 4ab4504425
commit 52b811d0cb
5 files changed
+317 -35

No files matched your search

+24 -3
View File
@@ -2111,16 +2111,27 @@ class Handler(BaseHTTPRequestHandler):
POST body 可选字段:
- subnet: 192.168.5.0/24 / 192.168.5.1-100 / 192.168.5.5;
留空则自动用本机子网
- ports: 端口规格(int / str / list)。"5555,37000-40000" 等。
留空 → 仅扫 5555。
- probe_timeout: 单 IP 探测超时秒(默认 0.3)
"""
body = self._read_json()
adb = find_adb_executable()
subnet = str(body.get("subnet") or "").strip() or None
probe_timeout = float(body.get("probe_timeout") or 0.3)
ports_raw = body.get("ports")
try:
from gui.gui_core_pkg import discover_adb_devices, get_local_subnet
from gui.gui_core_pkg import (
discover_adb_devices,
get_local_subnet,
parse_port_spec,
)
port_list = parse_port_spec(ports_raw)
devs = discover_adb_devices(
adb, subnet_spec=subnet, probe_timeout=probe_timeout
adb,
subnet_spec=subnet,
ports=port_list,
probe_timeout=probe_timeout,
)
payload = [
{
@@ -2137,6 +2148,7 @@ class Handler(BaseHTTPRequestHandler):
{
"ok": True,
"subnet": subnet or get_local_subnet() or "",
"ports": port_list,
"devices": payload,
}
)
@@ -2265,8 +2277,17 @@ class Handler(BaseHTTPRequestHandler):
)
# 不可达 → 扫描 + 智能匹配
# 如果上次记的端口非 5555,把它加进扫描列表(覆盖 Android 11+ 随机端口的场景)
try:
devs = discover_adb_devices(adb)
from gui.gui_core_pkg import parse_port_spec
last_port = 5555
if ":" in target:
try:
last_port = int(target.rsplit(":", 1)[1])
except ValueError:
pass
scan_ports = [5555] if last_port == 5555 else [5555, last_port]
devs = discover_adb_devices(adb, ports=scan_ports)
except Exception as e:
return self._send_json(
{