# -*- coding: utf-8 -*- """生成指向**正确 exe** 的修复脚本,并自检。 真机踩到的坑:程序级放行规则被写成了 python.exe(源码模式的值), 于是打包后的 ykxmz.exe 依然没有程序级放行, 系统继续自动生成阻止规则 —— "删了又回来"。 这里显式传入 exe 路径,并在生成后核对脚本里确实是这个路径。 """ from __future__ import annotations import sys from pathlib import Path EXE = r"C:\Users\shu\Desktop\deepseekagent\LanRemote\dist_exe\ykxmz\ykxmz.exe" PORTS = (8765, 8766) LINES = [ "@echo off", "chcp 936 >nul", "title 局域网遥控 - 修复防火墙(指定 exe)", 'cd /d "%~dp0"', "", "net session >nul 2>nul", "if not errorlevel 1 goto ELEVATED", "echo.", "echo 需要管理员权限,正在请求提权,请在弹窗里点「是」...", 'powershell -NoProfile -Command "Start-Process -FilePath \'%~f0\' -Verb RunAs"', "exit /b", "", ":ELEVATED", "", f'set "EXE={EXE}"', "", "echo ============================================================", "echo 局域网遥控 - 修复防火墙", "echo ============================================================", "echo.", "echo [1/4] 删除所有阻止规则(按程序名)...", 'netsh advfirewall firewall delete rule name="lanremote-agent.exe" dir=in', 'netsh advfirewall firewall delete rule name="python.exe" dir=in', 'netsh advfirewall firewall delete rule name="pythonw.exe" dir=in', "echo.", "echo [2/4] 删除旧的放行规则 ...", 'netsh advfirewall firewall delete rule name="LAN Remote TCP"', 'netsh advfirewall firewall delete rule name="LAN Remote UDP"', 'netsh advfirewall firewall delete rule name="LAN Remote App TCP"', 'netsh advfirewall firewall delete rule name="LAN Remote App UDP"', "echo.", "echo [3/4] 端口放行 ...", f'netsh advfirewall firewall add rule name="LAN Remote TCP" ' f"dir=in action=allow protocol=TCP localport={PORTS[0]} profile=any", f'netsh advfirewall firewall add rule name="LAN Remote UDP" ' f"dir=in action=allow protocol=UDP localport={PORTS[1]} profile=any", "echo.", "echo [4/4] 程序级放行(指向被控端 exe 本身)...", "echo 程序: %EXE%", 'if not exist "%EXE%" goto NOEXE', 'netsh advfirewall firewall add rule name="LAN Remote App TCP" ' 'dir=in action=allow program="%EXE%" enable=yes protocol=TCP profile=any', 'netsh advfirewall firewall add rule name="LAN Remote App UDP" ' 'dir=in action=allow program="%EXE%" enable=yes protocol=UDP profile=any', "goto SHOW", "", ":NOEXE", "echo [错误] 找不到文件: %EXE%", "echo 被控端可能被移动过,请把它放回该路径后重跑本脚本。", "goto END", "", ":SHOW", "echo.", "echo ============================================================", "echo 完成!核对下面 Program 一栏是否为本机被控端 exe:", "echo ============================================================", 'netsh advfirewall firewall show rule name="LAN Remote App TCP"', "", ":END", "echo.", "pause", ] def main() -> int: target = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote\_fix_exe.bat") with open(target, "w", encoding="gbk", newline="\r\n") as handle: handle.write("\n".join(LINES) + "\n") data = target.read_bytes() crlf = data.count(b"\r\n") lone = data.count(b"\n") - crlf text = data.decode("gbk") print(f" 路径: {target}") print(f" 大小: {len(data)} 字节 CRLF={crlf} 裸LF={lone}") problems = [] if lone: problems.append("存在裸 LF") if 'program="%EXE%"' not in text: problems.append("缺少 program=%EXE%") if EXE not in text: problems.append("EXE 路径未写入") if not Path(EXE).is_file(): problems.append(f"目标 exe 不存在: {EXE}") if problems: for p in problems: print(f" [失败] {p}") return 1 print(" 自检: 通过(路径正确、CRLF 正确、目标文件存在)") return 0 if __name__ == "__main__": sys.exit(main())