feat: 分发包改为字节码,并补上 MuMu 探测和下载列表滚动
This commit is contained in:
@@ -9,3 +9,8 @@ router = APIRouter(prefix="/api/adb")
|
||||
@router.get("/devices")
|
||||
def devices():
|
||||
return call(lambda: {"devices": Adb.locate().list_devices()})
|
||||
|
||||
|
||||
@router.post("/probe-mumu")
|
||||
def probe_mumu():
|
||||
return call(lambda: Adb.locate().probe_mumu())
|
||||
|
||||
@@ -59,6 +59,14 @@ class Adb:
|
||||
})
|
||||
return items
|
||||
|
||||
def probe_mumu(self) -> dict:
|
||||
ports = sorted(_listening_local_ports() & _mumu_ports())
|
||||
if not ports:
|
||||
raise RuntimeError("没有发现正在运行的 MuMu。请先打开模拟器,再探测。")
|
||||
for port in ports:
|
||||
self._run(["connect", f"127.0.0.1:{port}"], timeout=8)
|
||||
return {"devices": self.list_devices(), "ports": ports}
|
||||
|
||||
def read_profile(self, serial: str) -> DeviceProfile:
|
||||
serial = self._ready(serial)
|
||||
profile = DeviceProfile.builtin()
|
||||
@@ -192,3 +200,44 @@ class Adb:
|
||||
|
||||
def _run(self, args: list[str], timeout: int = 20) -> subprocess.CompletedProcess:
|
||||
return subprocess.run([self.executable, *args], capture_output=True, timeout=timeout)
|
||||
|
||||
|
||||
def _mumu_ports() -> set[int]:
|
||||
ports = {7555}
|
||||
for index in range(8):
|
||||
base = 16384 + index * 32
|
||||
ports.add(base)
|
||||
ports.add(base + 1)
|
||||
port = 5555
|
||||
while port <= 5599:
|
||||
ports.add(port)
|
||||
port += 2
|
||||
return ports
|
||||
|
||||
|
||||
def _listening_local_ports() -> set[int]:
|
||||
try:
|
||||
proc = subprocess.run(
|
||||
["netstat", "-ano", "-p", "tcp"],
|
||||
capture_output=True,
|
||||
timeout=8,
|
||||
check=False,
|
||||
)
|
||||
except (OSError, subprocess.TimeoutExpired):
|
||||
return set()
|
||||
text = proc.stdout.decode("utf-8", "replace")
|
||||
if "LISTENING" not in text.upper() and "监听" not in text:
|
||||
text = proc.stdout.decode("gbk", "replace")
|
||||
ports = set()
|
||||
for line in text.splitlines():
|
||||
upper = line.upper()
|
||||
if "LISTENING" not in upper and "监听" not in line:
|
||||
continue
|
||||
parts = line.split()
|
||||
if len(parts) < 2 or ":" not in parts[1]:
|
||||
continue
|
||||
host, _, port_text = parts[1].rpartition(":")
|
||||
if host not in ("127.0.0.1", "0.0.0.0") or not port_text.isdigit():
|
||||
continue
|
||||
ports.add(int(port_text))
|
||||
return ports
|
||||
|
||||
Reference in New Issue
Block a user