# -*- coding: utf-8 -*- """生成「一键修复防火墙.bat」。 必须满足三个硬性条件,否则要么 cmd 解析错乱,要么问题反复: 1. 换行必须 CRLF(裸 LF 会让 cmd 逐段误读, 整个脚本被当成一堆无效命令 —— 真机上踩过) 2. 编码用 GBK + chcp 936(中文 Windows 的 cmd 默认代码页) 3. **必须补一条程序级放行规则**,只删阻止规则治标不治本: Windows 在程序监听端口时若找不到该程序的规则就会弹窗, 没人点「允许」就自动生成新的阻止规则 —— 于是"删了又回来"。 生成后自动自检字节层面。 """ from __future__ import annotations import sys from pathlib import Path LINES = [ "@echo off", "chcp 936 >nul", "title 局域网遥控 - 修复防火墙", '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", "", "rem 找到被控端 exe 的实际位置(脚本可能和 exe 同目录,也可能在子目录里)", 'set "EXE=%~dp0ykxmz.exe"', 'if not exist "%EXE%" set "EXE=%~dp0..\\ykxmz.exe"', 'if not exist "%EXE%" set "EXE=%~dp0ykxmz\\ykxmz.exe"', "", "echo ============================================================", "echo 局域网遥控 - 修复防火墙", "echo ============================================================", "echo.", "echo 为什么要修:", "echo Windows 防火墙的「阻止」规则优先级高于「放行」规则。", "echo 程序监听的端口如果没有该程序的放行规则,系统会弹窗询问;", "echo 没人点「允许」时,系统就自动生成阻止规则。", "echo 于是表现成:手表能搜到电脑,但连不上(连接超时)。", "echo.", "echo 本脚本做四件事:", "echo 1. 删掉已有的阻止规则", "echo 2. 删掉旧的端口放行规则", "echo 3. 重建端口放行规则", "echo 4. 添加【程序级】放行规则 —— 这一步才是治本,", "echo 有了它系统以后不会再自动生成阻止规则。", "echo.", "echo 将放行的程序: %EXE%", "echo.", "echo ------------------------------------------------------------", "echo [1/4] 删除阻止规则 ...", "echo ------------------------------------------------------------", '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 ------------------------------------------------------------", "echo [2/4] 删除旧的放行规则 ...", "echo ------------------------------------------------------------", '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 ------------------------------------------------------------", "echo [3/4] 重建端口放行 8765(TCP) / 8766(UDP) ...", "echo ------------------------------------------------------------", 'netsh advfirewall firewall add rule name="LAN Remote TCP" ' "dir=in action=allow protocol=TCP localport=8765 profile=any", 'netsh advfirewall firewall add rule name="LAN Remote UDP" ' "dir=in action=allow protocol=UDP localport=8766 profile=any", "echo.", "echo ------------------------------------------------------------", "echo [4/4] 添加程序级放行(治本)...", "echo ------------------------------------------------------------", '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 DONE", "", ":NOEXE", "echo [注意] 没找到 ykxmz.exe,跳过程序级放行。", "echo 端口放行已经加好,但系统可能仍会生成阻止规则。", "", ":DONE", "echo.", "echo ============================================================", "echo 完成!现在可以用手表连接了。", "echo ============================================================", "echo.", "echo 如果 Ctrl+C 关掉本窗口前想确认结果,可以执行:", "echo netsh advfirewall firewall show rule name=\"LAN Remote App TCP\"", "echo.", "pause", ] def main() -> int: target = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote\tools\一键修复防火墙.bat") target.parent.mkdir(parents=True, exist_ok=True) 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_lf = data.count(b"\n") - crlf has_bom = data[:3] == b"\xef\xbb\xbf" print(f" 路径: {target}") print(f" 大小: {len(data)} 字节") print(f" CRLF: {crlf} 裸LF: {lone_lf} BOM: {has_bom}") ok = lone_lf == 0 and not has_bom and crlf >= len(LINES) - 1 print(f" 换行自检: {'通过' if ok else '失败'}") if not ok: return 1 text = data.decode("gbk") for probe in ("需要管理员权限", "删除阻止规则", "程序级放行", "完成!"): if probe not in text: print(f" 编码自检失败:找不到「{probe}」") return 1 print(" 编码自检: 通过") for probe in ('set "EXE=', 'program="%EXE%"', "LAN Remote App TCP"): if probe not in text: print(f" 内容自检失败:缺少 {probe}") return 1 print(" 程序级放行规则自检: 通过") return 0 if __name__ == "__main__": sys.exit(main())