# -*- coding: utf-8 -*- """防止「源码模式跑修复,把打包版的程序级放行规则冲掉」。 真机事故链: 打包版安装的规则本来是对的(指向 LanRemote-Agent.exe)。 之后有人从源码模式(sys.executable = python.exe)跑了一次修复, 于是 App 规则被改写成 python.exe, 打包版 EXE 从此没有程序级放行 → Windows 又给它建阻止规则 → 又连不上。 修法:修复脚本的目标程序改用 target_executable(): * 打包运行时 -> 就是自己(正确) * 源码运行时 -> 如果机器上已有指向某个 EXE 的规则且文件还在, 就沿用那个目标,不动打包版 """ from __future__ import annotations import ast import sys from pathlib import Path ROOT = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote") FIREWALL = ROOT / "windows" / "lanremote" / "firewall.py" OLD = '''def app_rule_exists() -> bool:''' NEW = '''def target_executable() -> str: """修复脚本应该放行哪个程序。 正常情况下就是当前进程自己(打包后 sys.executable 是 EXE)。 但源码模式下它是 python.exe —— 如果机器上已经有一条指向打包版 EXE 的程序级放行规则,就沿用那个目标,避免源码模式跑一次修复 把打包版的规则冲掉(真机上就这么踩过一次)。 """ current = executable_path() if getattr(sys, "frozen", False): return current existing = app_rule_program() base = os.path.basename(existing or "").lower() if (existing and base.endswith(".exe") and "python" not in base and os.path.isfile(existing)): return existing return current def app_rule_exists() -> bool:''' OLD_BUILD = ''' exe = executable_path()''' NEW_BUILD = ''' exe = target_executable()''' def main() -> int: text = FIREWALL.read_text(encoding="utf-8") ok = True for old, new, tag in ((OLD, NEW, "target_executable"), (OLD_BUILD, NEW_BUILD, "build_script 目标")): if old not in text: print(f" [失败] {tag} 锚点未匹配") ok = False else: text = text.replace(old, new, 1) if ok: FIREWALL.write_text(text, encoding="utf-8") print(" firewall.py: 已加 target_executable(),修复不再冲掉打包版规则") try: ast.parse(FIREWALL.read_text(encoding="utf-8")) print(" 语法: OK") except SyntaxError as exc: print(f" 语法错误: {exc}") ok = False return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())