scan wifi adb random port across subnets
This commit is contained in:
1 parent
207cf0eadb
commit
6279b129cb
3 files changed
+68
-2
No files matched your search
@@ -74,7 +74,7 @@ def open_scan_dialog(
|
||||
).grid(row=2, column=0, columnspan=4, sticky=W, pady=(4, 0))
|
||||
ttk.Label(
|
||||
top_frame,
|
||||
text="端口留空=自动完整扫候选主机 1-65535;手动示例: 5555 | 44219 | 37000-65535",
|
||||
text="端口留空=自动轻量扫 5555/上次随机端口/常见无线调试端口;手动示例: 5555 | 42047",
|
||||
font=("Arial", 8),
|
||||
foreground="gray",
|
||||
).grid(row=3, column=0, columnspan=4, sticky=W, pady=(2, 0))
|
||||
|
||||
@@ -468,6 +468,33 @@ def _build_quick_port_candidates(last_port_hint: Optional[int] = None) -> list[i
|
||||
return out
|
||||
|
||||
|
||||
def _build_same_host_candidates_for_subnets(
|
||||
last_ip: str,
|
||||
subnet_specs: list[str],
|
||||
) -> list[str]:
|
||||
"""换网段时优先尝试同主机号 IP,例如 *.102 在新局域网仍常是 *.102。"""
|
||||
try:
|
||||
last_addr = ipaddress.IPv4Address(str(last_ip or "").strip())
|
||||
last_octet = int(str(last_addr).split(".")[-1])
|
||||
except Exception:
|
||||
return []
|
||||
if not (1 <= last_octet <= 254):
|
||||
return []
|
||||
|
||||
out: list[str] = []
|
||||
seen: set[str] = set()
|
||||
for spec in subnet_specs:
|
||||
try:
|
||||
net = ipaddress.IPv4Network(str(spec or "").strip(), strict=False)
|
||||
candidate = str(ipaddress.IPv4Address(int(net.network_address) + last_octet))
|
||||
if ipaddress.IPv4Address(candidate) not in net:
|
||||
continue
|
||||
except Exception:
|
||||
continue
|
||||
_append_unique_ip(out, seen, candidate, subnet_specs)
|
||||
return out
|
||||
|
||||
|
||||
def _append_unique_ip(out: list[str], seen: set[str], ip: str, subnet_specs: list[str]) -> None:
|
||||
try:
|
||||
ipaddress.IPv4Address(ip)
|
||||
@@ -823,6 +850,7 @@ def discover_adb_devices(
|
||||
# 1.5) 利用上次成功目标加速:优先直连上次 target;并把上次端口作为补充端口。
|
||||
last_target = ""
|
||||
last_model = ""
|
||||
last_ip_for_same_host = ""
|
||||
last_ip_hint = ""
|
||||
last_port_hint: Optional[int] = None
|
||||
if auto_ports_mode:
|
||||
@@ -840,9 +868,10 @@ def discover_adb_devices(
|
||||
parsed_last = _parse_target_ip_port(last_target) if last_target else None
|
||||
if parsed_last:
|
||||
last_ip, last_port_hint_val = parsed_last
|
||||
last_ip_for_same_host = last_ip
|
||||
last_port_hint = last_port_hint_val
|
||||
if _ip_in_subnets(last_ip, subnet_specs):
|
||||
last_ip_hint = last_ip
|
||||
last_port_hint = last_port_hint_val
|
||||
if on_phase:
|
||||
try:
|
||||
on_phase(f"快速验证上次目标 {last_ip}:{last_port_hint}")
|
||||
@@ -863,12 +892,22 @@ def discover_adb_devices(
|
||||
quick_open_targets: list[tuple[str, int]] = []
|
||||
quick_hosts: list[str] = []
|
||||
if auto_ports_mode:
|
||||
same_host_candidates = _build_same_host_candidates_for_subnets(
|
||||
last_ip_for_same_host,
|
||||
subnet_specs,
|
||||
)
|
||||
quick_hosts = _build_quick_host_candidates(
|
||||
subnet_specs,
|
||||
last_ip=last_ip_hint,
|
||||
mdns_targets=mdns_targets,
|
||||
max_hosts=MAX_QUICK_HOSTS,
|
||||
)
|
||||
if same_host_candidates:
|
||||
seen_quick = set(quick_hosts)
|
||||
quick_hosts = [
|
||||
*[ip for ip in same_host_candidates if ip not in seen_quick],
|
||||
*quick_hosts,
|
||||
][:MAX_QUICK_HOSTS]
|
||||
quick_ports = _build_quick_port_candidates(last_port_hint)
|
||||
if quick_hosts and quick_ports:
|
||||
if on_phase:
|
||||
|
||||
@@ -323,6 +323,7 @@ class TestDiscoverAdbDevices:
|
||||
def test_full_flow_one_connected(self):
|
||||
"""子网推导 → 1 个 IP 开 5555 → connect connected → 取 model。"""
|
||||
with patch.object(scan, "get_local_subnet", return_value="192.168.5.0/24"), \
|
||||
patch("gui.gui_core_pkg.config.get_last_wifi_target", return_value={}), \
|
||||
patch.object(
|
||||
scan,
|
||||
"scan_open_ports",
|
||||
@@ -344,6 +345,7 @@ class TestDiscoverAdbDevices:
|
||||
|
||||
def test_unauthorized_still_listed(self):
|
||||
with patch.object(scan, "get_local_subnet", return_value="192.168.5.0/24"), \
|
||||
patch("gui.gui_core_pkg.config.get_last_wifi_target", return_value={}), \
|
||||
patch.object(scan, "scan_open_ports", return_value=[("192.168.5.2", 5555)]), \
|
||||
patch.object(
|
||||
scan,
|
||||
@@ -483,6 +485,31 @@ class TestDiscoverAdbDevices:
|
||||
assert len(captured_ports[0]) <= 8
|
||||
assert captured_ports[1] == [5555]
|
||||
|
||||
def test_changed_subnet_scans_same_host_with_last_random_port(self):
|
||||
calls = []
|
||||
|
||||
def fake_scan_ports(ips, **kwargs):
|
||||
ports = list(kwargs.get("ports") or [])
|
||||
calls.append((list(ips), ports))
|
||||
if "192.168.10.102" in ips and 42047 in ports:
|
||||
return [("192.168.10.102", 42047)]
|
||||
return []
|
||||
|
||||
with patch.object(scan, "get_local_subnets", return_value=["192.168.10.0/24"]), \
|
||||
patch("gui.gui_core_pkg.config.get_last_wifi_target", return_value={"target": "192.168.5.102:42047", "model": "PEPM00"}), \
|
||||
patch.object(scan, "verify_adb_target", return_value=("connected", "ok")), \
|
||||
patch.object(scan, "discover_mdns_targets", return_value=[]), \
|
||||
patch.object(scan, "_list_arp_ipv4_candidates", return_value=[]), \
|
||||
patch.object(scan, "scan_open_ports", side_effect=fake_scan_ports), \
|
||||
patch.object(scan, "query_device_model", return_value="PEPM00"):
|
||||
devs = scan.discover_adb_devices("/adb")
|
||||
|
||||
assert len(devs) == 1
|
||||
assert devs[0].target == "192.168.10.102:42047"
|
||||
assert calls[0][0][0] == "192.168.10.102"
|
||||
assert calls[0][1][0] == 42047
|
||||
assert 5555 in calls[0][1]
|
||||
|
||||
def test_auto_mode_lightweight_port_scan_before_base_5555(self):
|
||||
calls = []
|
||||
|
||||
|
||||
Reference in new issue
Block a user