scan wifi adb random port across subnets

This commit is contained in:
Your Name committed 2026-06-08 18:17:29 +08:00
1 parent 207cf0eadb
commit 6279b129cb
3 files changed
+68 -2

No files matched your search

+40 -1
View File
@@ -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: