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:
1 parent
4ab4504425
commit
52b811d0cb
5 files changed
+317
-35
No files matched your search
+128
-3
@@ -117,7 +117,8 @@ class TestScanOpenPorts:
|
||||
side_effect=lambda ip, port=5555, timeout=0.3: ip in {"192.168.5.2", "192.168.5.4"},
|
||||
):
|
||||
opens = scan.scan_open_ports(ips, max_workers=4)
|
||||
assert opens == ["192.168.5.2", "192.168.5.4"]
|
||||
# 现在返回 (ip, port) 元组列表
|
||||
assert opens == [("192.168.5.2", 5555), ("192.168.5.4", 5555)]
|
||||
|
||||
def test_progress_callback(self):
|
||||
progress = []
|
||||
@@ -134,6 +135,96 @@ class TestScanOpenPorts:
|
||||
def test_empty_ips(self):
|
||||
assert scan.scan_open_ports([]) == []
|
||||
|
||||
def test_multiple_ports_per_ip(self):
|
||||
"""同一个 IP 多个端口都开 → 都应被列出。"""
|
||||
ips = ["192.168.5.2"]
|
||||
# 5555 和 37001 都开,5556 关
|
||||
def fake_probe(ip, port, timeout=0.3):
|
||||
return port in (5555, 37001)
|
||||
|
||||
with patch.object(scan, "probe_adb_port", side_effect=fake_probe):
|
||||
opens = scan.scan_open_ports(
|
||||
ips, ports=[5555, 5556, 37001], max_workers=4
|
||||
)
|
||||
assert opens == [("192.168.5.2", 5555), ("192.168.5.2", 37001)]
|
||||
|
||||
def test_ports_overrides_legacy_port_arg(self):
|
||||
"""同时传 port 和 ports 时 ports 优先。"""
|
||||
with patch.object(scan, "probe_adb_port", return_value=True):
|
||||
opens = scan.scan_open_ports(
|
||||
["10.0.0.1"], port=5555, ports=[12345], max_workers=2
|
||||
)
|
||||
assert opens == [("10.0.0.1", 12345)]
|
||||
|
||||
def test_sort_by_ip_then_port(self):
|
||||
"""结果按 (IP 数值序, port 升序) 排序。"""
|
||||
with patch.object(scan, "probe_adb_port", return_value=True):
|
||||
opens = scan.scan_open_ports(
|
||||
["10.0.0.10", "10.0.0.2"],
|
||||
ports=[5556, 5555],
|
||||
max_workers=4,
|
||||
)
|
||||
assert opens == [
|
||||
("10.0.0.2", 5555),
|
||||
("10.0.0.2", 5556),
|
||||
("10.0.0.10", 5555),
|
||||
("10.0.0.10", 5556),
|
||||
]
|
||||
|
||||
|
||||
# ============================================================
|
||||
# parse_port_spec
|
||||
# ============================================================
|
||||
|
||||
class TestParsePortSpec:
|
||||
def test_int(self):
|
||||
assert scan.parse_port_spec(5555) == [5555]
|
||||
|
||||
def test_str_single(self):
|
||||
assert scan.parse_port_spec("5555") == [5555]
|
||||
|
||||
def test_comma(self):
|
||||
assert scan.parse_port_spec("5555,5556,5557") == [5555, 5556, 5557]
|
||||
|
||||
def test_range(self):
|
||||
assert scan.parse_port_spec("37000-37003") == [37000, 37001, 37002, 37003]
|
||||
|
||||
def test_mixed(self):
|
||||
assert scan.parse_port_spec("5555,37000-37002,40000") == [
|
||||
5555, 37000, 37001, 37002, 40000
|
||||
]
|
||||
|
||||
def test_dedup_preserves_order(self):
|
||||
assert scan.parse_port_spec([5555, 5556, 5555, 5557]) == [5555, 5556, 5557]
|
||||
|
||||
def test_none_returns_default(self):
|
||||
assert scan.parse_port_spec(None) == [scan.DEFAULT_ADB_PORT]
|
||||
|
||||
def test_empty_returns_default(self):
|
||||
assert scan.parse_port_spec("") == [scan.DEFAULT_ADB_PORT]
|
||||
assert scan.parse_port_spec(" ") == [scan.DEFAULT_ADB_PORT]
|
||||
assert scan.parse_port_spec([]) == [scan.DEFAULT_ADB_PORT]
|
||||
|
||||
def test_out_of_range_int(self):
|
||||
# 超过 65535 / <=0 都退化为默认
|
||||
assert scan.parse_port_spec(0) == [scan.DEFAULT_ADB_PORT]
|
||||
assert scan.parse_port_spec(70000) == [scan.DEFAULT_ADB_PORT]
|
||||
|
||||
def test_invalid_strings(self):
|
||||
assert scan.parse_port_spec("abc") == [scan.DEFAULT_ADB_PORT]
|
||||
# 单个非法 + 一个有效
|
||||
assert scan.parse_port_spec("abc,5555") == [5555]
|
||||
|
||||
def test_inverted_range_skipped(self):
|
||||
# lo > hi 跳过
|
||||
result = scan.parse_port_spec("5555,40000-30000")
|
||||
assert result == [5555]
|
||||
|
||||
def test_huge_range_skipped(self):
|
||||
# > 1024 个端口的单个范围不展开
|
||||
result = scan.parse_port_spec("5555,1-2000")
|
||||
assert result == [5555]
|
||||
|
||||
|
||||
# ============================================================
|
||||
# verify_adb_target
|
||||
@@ -172,7 +263,7 @@ class TestDiscoverAdbDevices:
|
||||
patch.object(
|
||||
scan,
|
||||
"scan_open_ports",
|
||||
return_value=["192.168.5.2"],
|
||||
return_value=[("192.168.5.2", 5555)],
|
||||
), \
|
||||
patch.object(
|
||||
scan,
|
||||
@@ -190,7 +281,7 @@ class TestDiscoverAdbDevices:
|
||||
|
||||
def test_unauthorized_still_listed(self):
|
||||
with patch.object(scan, "get_local_subnet", return_value="192.168.5.0/24"), \
|
||||
patch.object(scan, "scan_open_ports", return_value=["192.168.5.2"]), \
|
||||
patch.object(scan, "scan_open_ports", return_value=[("192.168.5.2", 5555)]), \
|
||||
patch.object(
|
||||
scan,
|
||||
"verify_adb_target",
|
||||
@@ -225,3 +316,37 @@ class TestDiscoverAdbDevices:
|
||||
patch.object(scan, "scan_open_ports", return_value=[]):
|
||||
devs = scan.discover_adb_devices("/adb")
|
||||
assert devs == []
|
||||
|
||||
def test_multi_port_passes_through(self):
|
||||
"""传入 ports=[5555,37001] 时 scan_open_ports 也收到这个列表。"""
|
||||
captured = {}
|
||||
|
||||
def fake_scan_ports(ips, **kwargs):
|
||||
captured["ports"] = kwargs.get("ports")
|
||||
return []
|
||||
|
||||
with patch.object(scan, "scan_open_ports", side_effect=fake_scan_ports):
|
||||
scan.discover_adb_devices(
|
||||
"/adb", subnet_spec="10.0.0.5", ports=[5555, 37001]
|
||||
)
|
||||
|
||||
assert captured["ports"] == [5555, 37001]
|
||||
|
||||
def test_returns_devs_with_correct_ports(self):
|
||||
"""两个 (ip, port) 分别返回各自端口的 DiscoveredDevice。"""
|
||||
with patch.object(
|
||||
scan,
|
||||
"scan_open_ports",
|
||||
return_value=[("192.168.5.2", 5555), ("192.168.5.2", 37001)],
|
||||
), patch.object(
|
||||
scan, "verify_adb_target",
|
||||
side_effect=lambda adb, target: ("connected", f"connected to {target}"),
|
||||
), patch.object(scan, "query_device_model", return_value="PEPM00"):
|
||||
devs = scan.discover_adb_devices(
|
||||
"/adb", subnet_spec="192.168.5.0/24", ports=[5555, 37001]
|
||||
)
|
||||
assert len(devs) == 2
|
||||
assert devs[0].port == 5555
|
||||
assert devs[1].port == 37001
|
||||
assert devs[0].target == "192.168.5.2:5555"
|
||||
assert devs[1].target == "192.168.5.2:37001"
|
||||
Reference in new issue
Block a user