# -*- coding: utf-8 -*- """把联调跑一遍并把被控端日志打出来,定位"第二次会话"的问题。 被控端的日志写在 windows/data/silent.log(静默模式的日志文件)。 中转这边的日志也一起打。 """ from __future__ import annotations import os import shutil import subprocess import sys import tempfile import time import urllib.request from pathlib import Path ROOT = Path(__file__).resolve().parent.parent WINDOWS = ROOT / "windows" TOOLCHAIN = Path(r"C:\Users\shu\Desktop\deepseekagent\starklink\toolchain") JAVAC = TOOLCHAIN / "jdk17" / "bin" / "javac.exe" RELAY_PORT = 8092 AGENT_PORT = 8897 RELAY_URL = f"http://127.0.0.1:{RELAY_PORT}" PAIR_CODE = "135791" def main() -> int: real_data = WINDOWS / "data" backup = WINDOWS / "data_bk_dbg" if backup.exists(): shutil.rmtree(backup, ignore_errors=True) had_data = real_data.exists() if had_data: shutil.move(str(real_data), str(backup)) work = Path(tempfile.mkdtemp(prefix="dbgrelay_")) relay_proc = agent_proc = None try: real_data.mkdir(parents=True, exist_ok=True) sys.path.insert(0, str(WINDOWS)) from lanremote.config import Store store = Store(real_data) store.set_pairing_code(PAIR_CODE) store.set("setup_done", True) store.set("allow_new_pairing", True) store.set("close_pair_after_first", False) store.set("server_name", "调试电脑") store.set("relay_url", RELAY_URL) store.set("port", AGENT_PORT) env = dict(os.environ) env.update({ "RELAY_PORT": str(RELAY_PORT), "RELAY_BIND": "127.0.0.1", "RELAY_DATA": str(work / "relaydata"), "RELAY_POLL": "8", "PYTHONIOENCODING": "utf-8", "PYTHONUNBUFFERED": "1", }) (work / "relaydata").mkdir(parents=True, exist_ok=True) relay_proc = subprocess.Popen( [sys.executable, "-u", str(ROOT / "relay" / "relay.py")], env=env, stdout=open(work / "relay.log", "wb"), stderr=subprocess.STDOUT) for _ in range(40): time.sleep(0.5) try: with urllib.request.urlopen(RELAY_URL + "/v1/health", timeout=2) as r: if r.status == 200: break except Exception: # noqa: BLE001 continue agent_proc = subprocess.Popen( [sys.executable, "-u", str(WINDOWS / "run_agent.py"), "--silent", "--port", str(AGENT_PORT)], cwd=str(WINDOWS), stdout=open(work / "agent.out", "wb"), stderr=subprocess.STDOUT) for _ in range(60): time.sleep(1.0) try: with urllib.request.urlopen(RELAY_URL + "/v1/health", timeout=3) as r: import json if any(d.get("online") for d in json.loads(r.read()).get("devices", [])): break except Exception: # noqa: BLE001 continue print("被控端已在线,开始跑 Java 客户端…\n") classes = work / "classes" classes.mkdir(parents=True, exist_ok=True) sources = [ROOT / "android" / "src" / "com" / "lanremote" / "watch" / n for n in ("Json.java", "B64.java", "Transport.java", "RelayTransport.java", "RemoteClient.java", "DiscoveryCore.java")] sources.append(ROOT / "_tests" / "java" / "JavaRelayTest.java") compile_result = subprocess.run( [str(JAVAC), "--release", "8", "-encoding", "UTF-8", "-d", str(classes)] + [str(s) for s in sources], capture_output=True, text=True, encoding="utf-8", errors="replace") if compile_result.returncode != 0: print(compile_result.stderr[:1500]) return 1 result = subprocess.run( [str(TOOLCHAIN / "jdk17" / "bin" / "java.exe"), "-Dfile.encoding=UTF-8", "-cp", str(classes), "JavaRelayTest", RELAY_URL, PAIR_CODE], capture_output=True, text=True, encoding="utf-8", errors="replace") print("---- Java stdout ----") print(result.stdout[-2500:]) print("---- Java stderr(含 [dbg] 打点)----") print(result.stderr[-4000:]) print() print("=" * 70) print("被控端日志(silent.log)") print("=" * 70) silent = real_data / "silent.log" if silent.is_file(): text = silent.read_text(encoding="utf-8", errors="replace") print(text[-3000:]) else: print(" (没有 silent.log)") print() print("=" * 70) print("中转日志(relay.log)") print("=" * 70) relay_log = work / "relaydata" / "relay.log" if relay_log.is_file(): print(relay_log.read_text(encoding="utf-8", errors="replace")[-2000:]) return 0 finally: for process in (agent_proc, relay_proc): if process is not None: try: process.terminate() process.wait(timeout=8) except Exception: # noqa: BLE001 try: process.kill() except Exception: # noqa: BLE001 pass time.sleep(1) if real_data.exists(): shutil.rmtree(real_data, ignore_errors=True) if had_data and backup.exists(): shutil.move(str(backup), str(real_data)) shutil.rmtree(work, ignore_errors=True) if __name__ == "__main__": sys.exit(main())