iproxy的小优化

This commit is contained in:
2025-11-13 19:28:57 +08:00
parent b9e2d86857
commit 58d124919b
4 changed files with 66 additions and 3 deletions

View File

@@ -8,6 +8,7 @@
"""
import datetime
import os
import sys
import time
import threading
import subprocess
@@ -22,7 +23,6 @@ import tidevice
import wda
from tidevice import Usbmux, ConnectionType
from tidevice._device import BaseDevice
from Entity.DeviceModel import DeviceModel
from Entity.Variables import WdaAppBundleId, wdaScreenPort, wdaFunctionPort
from Module.FlaskSubprocessManager import FlaskSubprocessManager
@@ -487,6 +487,60 @@ class DeviceInfo:
print(f"[WDA] /status@{local_port} 等待超时 {udid}")
return False
def _device_online_via_tidevice(self, udid: str) -> bool:
try:
from tidevice import Usbmux, ConnectionType
devs = Usbmux().device_list()
return any(d.udid == udid and d.conn_type == ConnectionType.USB for d in devs)
except Exception:
# 容错tidevice 异常时,假定在线,避免误判;后续命令会再校验
return True
def _pair_if_needed_for_ios17(self, udid: str, timeout_sec: float | None = None) -> bool:
"""
iOS 17+ 配对:已信任直接 True否则触发配对并无限等待设备离线则 False
使用 “python -m pymobiledevice3 lockdown pair -u <udid>” 做配对,规避 API 版本差异。
timeout_sec=None 表示无限等待;若传入数字则为最多等待秒数。
"""
# 已信任直接过
if self._trusted(udid):
return True
print(f"[Pair][CLI] iOS17+ 需要配对,等待手机上点击“信任”… {udid}")
last_log = 0.0
# 轮询直到配对成功/超时/设备离线
while True:
# 1) 设备在线性检查(防止卡等已拔掉的设备)
if not self._device_online_via_tidevice(udid):
print(f"[Pair][CLI] 设备已离线,停止等待 {udid}")
return False
# 2) 触发一次配对尝试
cmd = [sys.executable, "-m", "pymobiledevice3", "lockdown", "pair", "-u", udid]
try:
# 不打印子进程输出,保持你现有日志风格;需要可改为 PIPE 查看
res = subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
if res.returncode == 0:
print(f"[Pair][CLI] 配对成功 {udid}")
return True
except Exception as e:
print(f"[Pair][CLI] 调用失败:{e}")
# 3) 日志节流 + 可选超时
now = time.monotonic()
if now - last_log >= 10.0:
print(f"[Pair][CLI] 仍在等待用户在手机上点“信任”… {udid}")
last_log = now
if timeout_sec is not None:
timeout_sec -= 2.0
if timeout_sec <= 0:
print(f"[Pair][CLI] 等待配对超时(达到设定时长){udid}")
return False
time.sleep(2.0)
# ---------------- 原 _add_device 实现:整体改名为 _add_device_impl ----------------
def _add_device_impl(self, udid: str):
print(f"[Add] 开始新增设备 {udid}")
@@ -514,8 +568,17 @@ class DeviceInfo:
# 直接用“正式端口”探测 /status避免再启一次临时 iproxy
if not self._wait_wda_ready_on_port(udid, local_port=port, total_timeout_sec=3.0):
# 如果还没起来,按你原逻辑拉起 WDA 再等
if major > 17:
print("进入iOS17设备的分支")
if major >= 17:
print("进入 iOS17+ 设备的分支")
if not self._pair_if_needed_for_ios17(udid, timeout_sec=None): # 无限等;传秒数则有限时
print(f"[WDA] iOS17+ 配对失败或设备离线,放弃新增 {udid}")
try:
self._kill(proc)
except Exception:
pass
self._release_port(port)
return
try:
IOSActivator().activate(udid)
print("wda启动完成")