#!/bin/bash # 采用最优配置 quic + 10,并清理 MSS 规则。 # # v4 实测(MTU 1400,各 60 次): # quic+6 建齐 10s 0 失败 100% 中位 1399 P90 2611 P99 13705 # quic+8 建齐 16s 1 失败 100% 中位 1280 P90 2279 P99 3856 # quic+10 建齐 13s 0 失败 100% 中位 1256 P90 2068 P99 2991 <- 最好 # # 注意:v4 脚本自己的排序逻辑有多行输出没处理干净,误选了 quic+8, # 这里按数据手工改成 quic+10。 # # MSS 说明:接口 MTU 已经是 1400,内核自己算出来的 MSS 就是 1360; # iptables 的 --set-mss 只能把 MSS 往小改,所以那条 1400 的规则 # 现在实际是空转。统一改成 1360,避免以后 MTU 变回去时留下不一致。 set -u SVC=/etc/systemd/system/lanremote-tunnel.service UA='LanRemote/1.8.2' echo "════════ 1. 采用 quic + 10 ════════" sed -i "s/--protocol [a-z0-9]*/--protocol quic/" "$SVC" sed -i "s/--ha-connections [0-9]*/--ha-connections 10/" "$SVC" grep -oE '\-\-protocol [a-z0-9]+|--ha-connections [0-9]+|--edge-ip-version [0-9]+|--retries [0-9]+' "$SVC" | sed 's/^/ /' echo echo "════════ 2. 清理 MSS 规则(只留一条 1360)════════" iptables -t mangle -S OUTPUT 2>/dev/null | grep TCPMSS | sed 's/^/ 清理前: /' while iptables -t mangle -D OUTPUT -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss 1400 2>/dev/null; do :; done while iptables -t mangle -D OUTPUT -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss 1360 2>/dev/null; do :; done iptables -t mangle -A OUTPUT -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss 1360 iptables -t mangle -S OUTPUT 2>/dev/null | grep TCPMSS | sed 's/^/ 清理后: /' echo echo "════════ 3. MTU 和持久化 ════════" echo " wlan0 MTU 现在: $(cat /sys/class/net/wlan0/mtu)" grep -c 'mtu 1400' /etc/systemd/system/lanremote-nettune.service | sed 's/^/ net-tune 里的 MTU 设置行数: /' echo " net-tune 服务: $(systemctl is-enabled lanremote-nettune) / $(systemctl is-active lanremote-nettune)" echo echo "════════ 4. 重启隧道并等连接建齐 ════════" systemctl daemon-reload systemctl restart lanremote-tunnel for i in $(seq 1 40); do sleep 4 n=$(curl -s --max-time 4 http://127.0.0.1:20241/metrics 2>/dev/null \ | grep '^cloudflared_tunnel_ha_connections' | awk '{print $2}') [ "${n:-0}" -ge 10 ] && break done echo " 建齐 $n/10 条(等了约 $((i*4)) 秒)" curl -s --max-time 5 http://127.0.0.1:20241/metrics 2>/dev/null \ | grep 'server_locations.*} 1' | sed 's/^/ /' echo echo "════════ 5. 等公网可用 ════════" for i in $(seq 1 20); do c=$(curl -s -o /dev/null -w '%{http_code}' --max-time 12 -H "User-Agent: $UA" https://relay.xxmxxs.top/v1/health) printf " 第 %d 次: %s\n" "$i" "$c" [ "$c" = "200" ] && break sleep 3 done echo echo "════════ 6. 快速验收(30 次)════════" ok=0; times=() for i in $(seq 1 30); do out=$(curl -s -o /dev/null -w '%{http_code} %{time_total}' --max-time 25 -H "User-Agent: $UA" https://relay.xxmxxs.top/v1/health) c=$(echo "$out" | awk '{print $1}'); ms=$(echo "$out" | awk '{printf "%d", $2*1000}') [ "$c" = "200" ] && { ok=$((ok+1)); times+=("$ms"); } done stats=$(printf '%s\n' "${times[@]:-0}" | sort -n | awk '{a[NR]=$1} END {printf "中位 %d P90 %d P99 %d 最大 %d", a[int(NR/2)+1], a[int(NR*0.9)+1], a[int(NR*0.99)+1], a[NR]}') echo " 可用率 $ok/30 = $((ok*100/30))% $stats ms" echo echo " 最终: $(grep -oE '\-\-protocol [a-z0-9]+|--ha-connections [0-9]+' "$SVC" | tr '\n' ' ') MTU=$(cat /sys/class/net/wlan0/mtu)"