# -*- coding: utf-8 -*- """把 MTU 调优加进 install.sh(新装也带上)。 这是这次找到的真正根因: 接口 MTU 写 1500,但 4G 路径扛不住 —— ping 实测包 1488 就丢、 1500 全丢。TLS 握手要发大证书包 -> 被丢 -> 新建连接约 30% 失败。 修复:tcp_mtu_probing=1 + MSS 夹 1360(不动接口 MTU,实测降它反而更差)。 效果:隧道可用率 48%~75% -> 90%~92%。 """ from __future__ import annotations import sys from pathlib import Path INSTALL = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote\relay" r"\install.sh") ANCHOR = 'echo "[4/5]' NEW = '''echo echo "[3c] 网络调优(修 MTU —— 这是可用率从 48% 提到 90% 的关键)" # 实测:接口 MTU 1500 时,ping 包 1488 就丢、1500 全丢。 # TLS 握手要发大证书包 -> 被丢 -> 新建连接约 30% 失败。 # 夹 MSS 到 1360 + 打开路径 MTU 探测之后,可用率 48%~75% -> 90%~92%。 # 注意:**不要改接口 MTU**,实测降到 1400 反而掉到 50%。 if [ -f "$SCRIPT_DIR/net-tune.sh" ]; then cp -f "$SCRIPT_DIR/net-tune.sh" "$RELAY_DIR/net-tune.sh" elif [ -f "$RELAY_DIR/net-tune.sh" ]; then : # 已经在位 fi if [ -f "$RELAY_DIR/net-tune.sh" ]; then chmod +x "$RELAY_DIR/net-tune.sh" # 内核参数持久化 cat > /etc/sysctl.d/99-lanremote-mtu.conf <<'MTUEOF' # LanRemote: 修路径 MTU 问题。 # 接口 MTU 1500 时大包会被丢(1488/1500 全丢),导致 TLS 握手约 30% 失败。 net.ipv4.tcp_mtu_probing = 1 MTUEOF sysctl -p /etc/sysctl.d/99-lanremote-mtu.conf >/dev/null 2>&1 # 开机自动夹 MSS cat > /etc/systemd/system/lanremote-nettune.service </dev/null 2>&1 systemctl start lanremote-nettune.service sleep 1 echo " tcp_mtu_probing = $(cat /proc/sys/net/ipv4/tcp_mtu_probing)" echo " MSS 规则 $(iptables -t mangle -L -n 2>/dev/null | grep -ci mss) 条" echo " 已注册 lanremote-nettune.service(开机自动应用)" else echo " 跳过:找不到 net-tune.sh" fi ''' + ANCHOR def main() -> int: text = INSTALL.read_text(encoding="utf-8") if "lanremote-nettune" in text: print(" 已经加过") return 0 if ANCHOR not in text: print(" [失败] 锚点没找到") for index, line in enumerate(text.splitlines(), 1): if line.startswith('echo "['): print(f" {index}: {line}") return 1 text = text.replace(ANCHOR, NEW, 1) INSTALL.write_text(text, encoding="utf-8", newline="\n") raw = INSTALL.read_bytes() if b"\r\n" in raw: INSTALL.write_bytes(raw.replace(b"\r\n", b"\n")) print(" 顺手修了 CRLF") print(" 已加 [3c] 网络调优段") return 0 if __name__ == "__main__": sys.exit(main())