# -*- coding: utf-8 -*- """补上 _selfcheck_report 定义,并让它 return 结论。 上一版补丁有顺序 bug:先把调用插进去了,导致后面"函数还没定义"的判断 失效,函数体没插进去 —— 运行时会 NameError。 另外回调必须 **return** 结论,DiscoveryResponder 才会把它随 ACK 回传给手表。 """ from __future__ import annotations import sys from pathlib import Path SERVER = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote\windows" r"\lanremote\server.py") HELPER = ''' def _selfcheck_report(self, from_ip: str) -> str: """生成一段给手表看的诊断结论(不弹任何窗口)。 用户要的是「电脑上什么都不出现」,所以结论直接回传给手表显示。 """ try: from .diagnostics import collect, verdict data = collect(self, self.store) _level, text = verdict(data) ips = data.get("ips") or [] port = self.port blocked = (data.get("firewall") or {}).get("blocked_count", 0) lines = [ f"电脑:{data.get('name') or '被控端'}", f"本机地址:{', '.join(ips[:2]) if ips else '读不到'}", f"监听端口:{port}", f"收到过的连接:{data.get('tcp_connections', 0)} 次", f"阻止规则:{blocked} 条", "", f"结论:{text}", ] return "\\n".join(lines) except Exception as exc: # noqa: BLE001 return f"电脑端诊断失败:{exc}" ''' def main() -> int: text = SERVER.read_text(encoding="utf-8") if "_selfcheck_report(self" in text: print(" 已经定义过了") else: anchor = " def _on_selfcheck_request(self, from_ip: str) -> None:" if anchor not in text: print(" [失败] 找不到 _on_selfcheck_request") return 1 text = text.replace(anchor, HELPER + anchor, 1) print(" server / _selfcheck_report 定义: OK") # 让回调把结论 return 出去,并且不再往界面上弹窗口 old = ''' def _on_selfcheck_request(self, from_ip: str) -> None: """手表说「我连不上」,就在电脑上把网络自检打开。 走 UDP 是有意的:连不上的典型原因就是 TCP 被防火墙拦了, 而 UDP 广播往往还通(那正是"能搜到但连不上"的原因)。 """''' new = ''' def _on_selfcheck_request(self, from_ip: str) -> str: """手表说「我连不上」→ 生成诊断结论,随 ACK 回给手表。 走 UDP 是有意的:连不上的典型原因就是 TCP 被防火墙拦了, 而 UDP 广播往往还通(那正是"能搜到但连不上"的原因)。 注意:**不往电脑上弹窗口**。结论直接回传给手表, 用户看的就是手表,电脑上保持干净。 """ return self._selfcheck_report(from_ip) def _open_selfcheck_window(self, from_ip: str) -> None: """有窗口的模式下才用得上(静默模式不会走到这里)。"""''' if old in text: text = text.replace(old, new, 1) print(" server / 回调改成 return 结论: OK") # 删掉原来那两段弹窗口的逻辑(现在不需要了) old_launch = ''' self._log(f"[自检] 收到来自 {from_ip} 的请求,正在生成诊断结论") note = self._selfcheck_report(from_ip) def launch() -> None: ''' new_launch = ''' self._log(f"[自检] 收到来自 {from_ip} 的请求,已生成诊断结论") def launch() -> None: ''' if old_launch in text: text = text.replace(old_launch, new_launch, 1) print(" server / 去掉重复的结论生成: OK") # _open_selfcheck_window 需要 from_ip 参数在 launch 里可见 old_sig = ''' def launch() -> None: import os import subprocess target = getattr(self, "ui", None)''' new_sig = ''' def launch() -> None: target = getattr(self, "ui", None)''' if old_sig in text: text = text.replace(old_sig, new_sig, 1) print(" server / 清理无用的 import: OK") SERVER.write_text(text, encoding="utf-8") import ast try: ast.parse(SERVER.read_text(encoding="utf-8")) print(" server.py: 语法 OK") except SyntaxError as exc: print(f" server.py: 语法错误 {exc}") return 1 # 打印改后的样子确认 print() lines = SERVER.read_text(encoding="utf-8").splitlines() for i, line in enumerate(lines): if "_on_selfcheck_request" in line and "def " in line: for j in range(i, min(len(lines), i + 30)): print(f" {j + 1}: {lines[j]}") break return 0 if __name__ == "__main__": sys.exit(main())