# 天下苦 ATrust 久矣：把校园网压缩成一个 14 MB 的本地节点

深信服 aTrust 客户端在 macOS 上常驻 10 个进程、吞掉 **429 MB** 内存；菜单栏点了「退出」，后台 root 守护进程照样霸占 **233 MB**；系统里塞了 5 个开机守护配置、3 个提权助手、一个带卸载监控的终端管控模块，甚至偷偷放了两份 304 MB 的程序本体——最要命的是强制开启 TUN 抢路由表，和 Clash 这类工具水火不容。

本文用开源项目 [zju-connect](https://github.com/Mythologyli/zju-connect) 配合 [FlClash](https://github.com/chen08209/FlClash)，把整个校园网压缩成**一个仅占 12～14 MB 内存的本地 SOCKS5 节点**：校内网、图书馆数据库、外网全自动分流，能做到开机自启、60 秒穿隧保活、崩溃后自动复活。

---

## 一、为什么要跟 aTrust 说再见

### 1. 四条罪状

**罪状一：内存黑洞与进程全家桶**

实测运行时会拉起 **4 个 Electron 渲染/主进程（196 MB）+ 4 个 root 级守护进程（233 MB），合计 10 个进程、429 MB**。

**罪状二：顽固常驻与系统权限污染**

最流氓的是：菜单栏点「退出客户端」，**内存一分都拿不回来**。退出图形界面后，系统里那几个开机自启的 root 守护进程（配置成登录即启动）依然活得好好的，白白占着 233 MB。

深挖系统目录还能发现：

| 类别 | 内容 |
| --- | --- |
| 程序本体 | 304 MB 主程序，另外还藏了一份一模一样的 304 MB 副本 |
| 开机自启 | 5 个系统级守护配置 |
| 特权组件 | 3 个提权助手（helper） |
| 终端管控 | 一个带卸载监控脚本的终端管控模块（UEM） |
| 登录项 / 后台项 | 4 条厂商后台任务 + 1 条客户端 |

**罪状三：莫名其妙掉线且无法自愈**

离开电脑吃个饭，或者合盖休眠再唤醒，它就给你弹「会话已过期」或干脆假死。一方面是学校网关有 15～30 分钟空闲超时，另一方面它自己的进程管理也一塌糊涂——清理时甚至抓到一个**连续挂了 18 天没退出的僵尸进程，独自占着 158 MB**。

**罪状四：霸道独占路由表**

aTrust 一连上就创建虚拟网卡接管全局路由。你想用 FlClash 查海外文献时，两个 TUN 直接抢路由表，轻则网页转圈，重则全网断流。

### 2. 最终的形态

<figure class="svg-figure"><a class="svg-figure-link" href="/img/svg/whut-architecture-v2.svg" target="_blank" rel="noopener" title="点击查看原尺寸" aria-label="whut-2api 架构图（点击查看原尺寸）"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1220 660" role="img" aria-labelledby="arch-whut-title"> <title id="arch-whut-title">WHUT 校内网接入架构 — zju-connect + FlClash</title> <style> text { font-family: 'PingFang SC', 'Helvetica Neue', Arial, sans-serif; } .arch-flow { stroke-dasharray: 12,6; animation: arch-flow 1s linear infinite; } @keyframes arch-flow { to { stroke-dashoffset: -36; } } .arch-pulse { transform-origin: 535px 235px; animation: arch-pulse 2.4s ease-in-out infinite; } @keyframes arch-pulse { 0%,100% { opacity:.85; } 50% { opacity:1; } } .arch-spin { transform-origin: 535px 235px; animation: arch-spin 14s linear infinite; } @keyframes arch-spin { from { transform: rotate(0deg);} to { transform: rotate(360deg);} } .arch-glow { filter: drop-shadow(0 0 7px rgba(14,165,233,.45)); animation: arch-glow 2.4s ease-in-out infinite; } @keyframes arch-glow { 0%,100% { filter: drop-shadow(0 0 6px rgba(14,165,233,.35)); } 50% { filter: drop-shadow(0 0 13px rgba(14,165,233,.75)); } } .arch-fade { animation: arch-fade 1.2s ease-out; } @keyframes arch-fade { from { opacity:0; } to { opacity:1; } } .arch-t { font-size: 14px; font-weight: 700; } .arch-s { font-size: 11px; } .arch-xs { font-size: 10px; } .arch-hd { font-size: 12px; font-weight: 700; fill: #64748b; letter-spacing: .5px; } </style> <defs> <marker id="arch-ah" markerWidth="9" markerHeight="7" refX="8" refY="3.5" orient="auto"> <polygon points="0 0, 9 3.5, 0 7" fill="#94a3b8"/> </marker> <marker id="arch-ah-green" markerWidth="9" markerHeight="7" refX="8" refY="3.5" orient="auto"> <polygon points="0 0, 9 3.5, 0 7" fill="#10b981"/> </marker> </defs> <!-- ===== 背景 ===== --> <rect width="1220" height="660" rx="10" fill="#f8fafc" stroke="#e2e8f0" stroke-width="2"/> <!-- ===== 标题 ===== --> <text x="610" y="34" text-anchor="middle" font-size="21" font-weight="700" fill="#1e293b"> WHUT 校内网接入 · zju-connect + FlClash </text> <text x="610" y="55" text-anchor="middle" font-size="12" fill="#64748b"> 一台 Mac，装上就能在校外任何网络访问校内资源 · 1 个后台服务 · 14 MB · 不会掉线 </text> <!-- ===== 列标题 ===== --> <g class="arch-hd" text-anchor="middle"> <text x="114" y="88">你的应用</text> <text x="320" y="88">两个入口</text> <text x="535" y="88">分流内核</text> <text x="760" y="88">出口</text> <text x="1063" y="88">目的地</text> </g> <!-- ===== 第 1 列：应用 ===== --> <g> <rect x="24" y="100" width="180" height="264" rx="10" fill="#6366f1" fill-opacity="0.06" stroke="#6366f1" stroke-opacity="0.35" stroke-width="1.5"/> <rect x="40" y="116" width="148" height="46" rx="7" fill="#fff" stroke="#c7d2fe" stroke-width="1.5"/> <text x="114" y="137" text-anchor="middle" font-size="13" font-weight="700" fill="#4338ca">浏览器</text> <text x="114" y="153" text-anchor="middle" class="arch-xs" fill="#6366f1">Chrome / Safari</text> <rect x="40" y="172" width="148" height="46" rx="7" fill="#fff" stroke="#c7d2fe" stroke-width="1.5"/> <text x="114" y="193" text-anchor="middle" font-size="13" font-weight="700" fill="#4338ca">终端</text> <text x="114" y="209" text-anchor="middle" class="arch-xs" fill="#6366f1">ssh / curl / scp</text> <rect x="40" y="228" width="148" height="46" rx="7" fill="#fff" stroke="#c7d2fe" stroke-width="1.5"/> <text x="114" y="249" text-anchor="middle" font-size="13" font-weight="700" fill="#4338ca">VSCode 远程</text> <text x="114" y="265" text-anchor="middle" class="arch-xs" fill="#6366f1">Remote-SSH</text> <rect x="40" y="284" width="148" height="46" rx="7" fill="#fff" stroke="#c7d2fe" stroke-width="1.5"/> <text x="114" y="305" text-anchor="middle" font-size="13" font-weight="700" fill="#4338ca">Docker 等</text> <text x="114" y="321" text-anchor="middle" class="arch-xs" fill="#6366f1">不认代理的程序</text> <text x="114" y="352" text-anchor="middle" class="arch-xs" fill="#818cf8">无需任何额外配置</text> </g> <!-- ===== 第 2 列：入口 ===== --> <g> <rect x="230" y="108" width="180" height="98" rx="10" fill="#f59e0b" fill-opacity="0.09" stroke="#f59e0b" stroke-width="2"/> <text x="320" y="132" text-anchor="middle" font-size="14" font-weight="700" fill="#b45309">TUN 虚拟网卡</text> <text x="320" y="151" text-anchor="middle" class="arch-s" fill="#d97706">utun4 · 198.18.0.1</text> <text x="320" y="170" text-anchor="middle" class="arch-xs" fill="#b45309">网络层 · 系统强制改道</text> <text x="320" y="188" text-anchor="middle" class="arch-xs" fill="#b45309">所有程序，一个都跑不掉</text> <rect x="230" y="222" width="180" height="98" rx="10" fill="#f59e0b" fill-opacity="0.05" stroke="#f59e0b" stroke-opacity="0.55" stroke-width="2" stroke-dasharray="5,4"/> <text x="320" y="246" text-anchor="middle" font-size="14" font-weight="700" fill="#b45309">系统代理</text> <text x="320" y="265" text-anchor="middle" class="arch-s" fill="#d97706">127.0.0.1:7890</text> <text x="320" y="284" text-anchor="middle" class="arch-xs" fill="#b45309">应用层 · 程序自愿遵守</text> <text x="320" y="302" text-anchor="middle" class="arch-xs" fill="#b45309">只是"建议"，可关掉</text> <text x="320" y="344" text-anchor="middle" class="arch-xs" fill="#94a3b8">两者可只开其一</text> <text x="320" y="358" text-anchor="middle" class="arch-xs" fill="#94a3b8">最终分流结果相同</text> </g> <!-- ===== 第 3 列：FlClash 内核 ===== --> <g class="arch-pulse"> <circle cx="535" cy="235" r="104" fill="none" stroke="#0ea5e9" stroke-opacity="0.35" stroke-width="1.5" stroke-dasharray="4,7" class="arch-spin"/> <rect x="440" y="120" width="190" height="230" rx="16" fill="#0ea5e9" fill-opacity="0.1" stroke="#0ea5e9" stroke-width="3" class="arch-glow"/> <text x="535" y="158" text-anchor="middle" font-size="18" font-weight="700" fill="#0369a1">FlClash</text> <text x="535" y="180" text-anchor="middle" class="arch-s" fill="#0284c7">规则分流内核</text> <line x1="460" y1="194" x2="610" y2="194" stroke="#7dd3fc" stroke-width="1"/> <text x="460" y="214" class="arch-s" fill="#0369a1">校内资源</text> <text x="620" y="214" text-anchor="end" class="arch-xs" fill="#0ea5e9">104 条规则</text> <text x="460" y="236" class="arch-s" fill="#0369a1">国际流量</text> <text x="620" y="236" text-anchor="end" class="arch-xs" fill="#0ea5e9">订阅自带</text> <text x="460" y="258" class="arch-s" fill="#0369a1">国内网站</text> <text x="620" y="258" text-anchor="end" class="arch-xs" fill="#0ea5e9">+ 自定义</text> <text x="460" y="280" class="arch-s" fill="#0369a1">其余</text> <text x="620" y="280" text-anchor="end" class="arch-xs" fill="#0ea5e9">兜底</text> <line x1="460" y1="296" x2="610" y2="296" stroke="#7dd3fc" stroke-width="1"/> <text x="535" y="316" text-anchor="middle" class="arch-xs" fill="#0284c7">规则从上往下，命中即走</text> <text x="535" y="334" text-anchor="middle" class="arch-xs" fill="#0284c7">改配置不用动订阅</text> </g> <!-- ===== 第 4 列：出口 ===== --> <g> <!-- whut --> <rect x="660" y="112" width="200" height="86" rx="10" fill="#10b981" fill-opacity="0.12" stroke="#10b981" stroke-width="2.5"/> <text x="760" y="136" text-anchor="middle" font-size="15" font-weight="700" fill="#047857">whut 节点</text> <text x="760" y="155" text-anchor="middle" class="arch-s" fill="#059669">Socks5 · 127.0.0.1:1090</text> <text x="760" y="174" text-anchor="middle" class="arch-xs" fill="#047857">zju-connect 隧道客户端</text> <text x="760" y="190" text-anchor="middle" class="arch-xs" fill="#047857">launchd 服务 · 常驻 14 MB</text> <!-- 机场 --> <rect x="660" y="212" width="200" height="86" rx="10" fill="#8b5cf6" fill-opacity="0.1" stroke="#8b5cf6" stroke-width="2"/> <text x="760" y="236" text-anchor="middle" font-size="15" font-weight="700" fill="#6d28d9">机场节点</text> <text x="760" y="255" text-anchor="middle" class="arch-s" fill="#7c3aed">你的订阅 · 47 个节点</text> <text x="760" y="274" text-anchor="middle" class="arch-xs" fill="#6d28d9">香港 / 日本 / 美国 …</text> <text x="760" y="290" text-anchor="middle" class="arch-xs" fill="#6d28d9">切换国家在这里改</text> <!-- DIRECT --> <rect x="660" y="312" width="200" height="66" rx="10" fill="#94a3b8" fill-opacity="0.1" stroke="#94a3b8" stroke-width="2"/> <text x="760" y="338" text-anchor="middle" font-size="15" font-weight="700" fill="#475569">DIRECT</text> <text x="760" y="357" text-anchor="middle" class="arch-s" fill="#64748b">不走任何代理</text> <text x="760" y="372" text-anchor="middle" class="arch-xs" fill="#64748b">用你自己的网络</text> </g> <!-- ===== 第 5 列：目的地 ===== --> <g> <rect x="930" y="112" width="266" height="86" rx="10" fill="#10b981" fill-opacity="0.07" stroke="#10b981" stroke-width="1.8"/> <text x="1063" y="136" text-anchor="middle" font-size="15" font-weight="700" fill="#047857">校内资源</text> <text x="1063" y="155" text-anchor="middle" class="arch-s" fill="#059669">图书馆数据库（96 个域名）</text> <text x="1063" y="172" text-anchor="middle" class="arch-xs" fill="#047857">CNKI / IEEE / Elsevier / WoS / Springer …</text> <text x="1063" y="188" text-anchor="middle" class="arch-xs" fill="#047857">校内网站 · 内网服务器 · 教务系统</text> <rect x="930" y="212" width="266" height="86" rx="10" fill="#8b5cf6" fill-opacity="0.06" stroke="#8b5cf6" stroke-width="1.5"/> <text x="1063" y="236" text-anchor="middle" font-size="15" font-weight="700" fill="#6d28d9">国际互联网</text> <text x="1063" y="255" text-anchor="middle" class="arch-s" fill="#7c3aed">Google / GitHub / YouTube</text> <text x="1063" y="274" text-anchor="middle" class="arch-xs" fill="#6d28d9">出口 IP 会变成节点所在的海外地址</text> <text x="1063" y="290" text-anchor="middle" class="arch-xs" fill="#6d28d9">实测出口：103.151.172.152</text> <rect x="930" y="312" width="266" height="66" rx="10" fill="#94a3b8" fill-opacity="0.07" stroke="#94a3b8" stroke-width="1.5"/> <text x="1063" y="338" text-anchor="middle" font-size="15" font-weight="700" fill="#475569">国内网站</text> <text x="1063" y="357" text-anchor="middle" class="arch-s" fill="#64748b">百度 / B 站 / 淘宝 …</text> <text x="1063" y="372" text-anchor="middle" class="arch-xs" fill="#64748b">直连最快，省节点流量</text> </g> <!-- ===== 连线：应用 → 入口 ===== --> <path d="M204 150 Q217 150 230 157" fill="none" stroke="#94a3b8" stroke-width="2" marker-end="url(#arch-ah)" class="arch-flow"/> <path d="M204 250 Q217 250 230 271" fill="none" stroke="#94a3b8" stroke-width="2" marker-end="url(#arch-ah)" class="arch-flow"/> <!-- ===== 连线：入口 → 内核 ===== --> <path d="M410 157 Q425 157 440 175" fill="none" stroke="#94a3b8" stroke-width="2" marker-end="url(#arch-ah)" class="arch-flow"/> <path d="M410 271 Q425 271 440 295" fill="none" stroke="#94a3b8" stroke-width="2" marker-end="url(#arch-ah)" class="arch-flow"/> <!-- ===== 连线：内核 → 出口 ===== --> <path d="M630 165 Q645 155 660 155" fill="none" stroke="#10b981" stroke-width="2.5" marker-end="url(#arch-ah-green)" class="arch-flow"/> <path d="M630 250 Q645 250 660 250" fill="none" stroke="#8b5cf6" stroke-width="2" marker-end="url(#arch-ah)" class="arch-flow"/> <path d="M630 330 Q645 340 660 345" fill="none" stroke="#94a3b8" stroke-width="2" marker-end="url(#arch-ah)" class="arch-flow"/> <!-- ===== 连线：出口 → 目的地 ===== --> <path d="M860 155 Q895 155 930 155" fill="none" stroke="#10b981" stroke-width="2.5" marker-end="url(#arch-ah-green)" class="arch-flow"/> <text x="895" y="146" text-anchor="middle" class="arch-xs" fill="#059669">aTrust</text> <text x="895" y="170" text-anchor="middle" class="arch-xs" fill="#059669">隧道</text> <path d="M860 255 Q895 255 930 255" fill="none" stroke="#8b5cf6" stroke-width="2" marker-end="url(#arch-ah)" class="arch-flow"/> <text x="895" y="246" text-anchor="middle" class="arch-xs" fill="#7c3aed">加密</text> <text x="895" y="270" text-anchor="middle" class="arch-xs" fill="#7c3aed">代理</text> <path d="M860 345 Q895 345 930 345" fill="none" stroke="#94a3b8" stroke-width="2" marker-end="url(#arch-ah)" class="arch-flow"/> <text x="895" y="336" text-anchor="middle" class="arch-xs" fill="#64748b">直连</text> <text x="895" y="360" text-anchor="middle" class="arch-xs" fill="#64748b">出门</text> <!-- ===== 底部：三个卖点 ===== --> <g> <rect x="24" y="400" width="380" height="92" rx="10" fill="#10b981" fill-opacity="0.07" stroke="#10b981" stroke-opacity="0.5" stroke-width="1.5"/> <text x="44" y="428" font-size="14" font-weight="700" fill="#047857">① 在哪都能连</text> <text x="44" y="450" class="arch-s" fill="#059669">家 / 公司 / 咖啡厅 / 手机热点</text> <text x="44" y="468" class="arch-xs" fill="#047857">只要能访问外网，就能连回校内</text> <text x="44" y="484" class="arch-xs" fill="#047857">不用记 IP、不用改路由、不用装证书</text> <rect x="420" y="400" width="380" height="92" rx="10" fill="#0ea5e9" fill-opacity="0.07" stroke="#0ea5e9" stroke-opacity="0.5" stroke-width="1.5"/> <text x="440" y="428" font-size="14" font-weight="700" fill="#0369a1">② 不会随时掉线</text> <text x="440" y="450" class="arch-s" fill="#0284c7">每 60 秒发一次穿隧心跳</text> <text x="440" y="468" class="arch-xs" fill="#0369a1">同时刷新学校网关会话、家里路由器 NAT、运营商连接跟踪</text> <text x="440" y="484" class="arch-xs" fill="#0369a1">意外退出 30 秒内自动重启，用缓存凭据重新登录</text> <rect x="816" y="400" width="380" height="92" rx="10" fill="#6366f1" fill-opacity="0.07" stroke="#6366f1" stroke-opacity="0.5" stroke-width="1.5"/> <text x="836" y="428" font-size="14" font-weight="700" fill="#4338ca">③ 只占 14 MB</text> <text x="836" y="450" class="arch-s" fill="#6366f1">1 个 launchd 后台服务，没有界面</text> <text x="836" y="468" class="arch-xs" fill="#4338ca">无 GUI、无 Electron、无内核扩展、无系统证书</text> <text x="836" y="484" class="arch-xs" fill="#4338ca">崩溃/被杀/重启电脑，都会自己回来</text> </g> <!-- ===== 底部：内存对比 ===== --> <rect x="24" y="508" width="1172" height="132" rx="10" fill="#fff" stroke="#e2e8f0" stroke-width="1.5"/> <text x="44" y="534" font-size="13" font-weight="700" fill="#334155">后台开销对比（实测）</text> <!-- 现在 --> <rect x="44" y="548" width="546" height="74" rx="8" fill="#10b981" fill-opacity="0.08" stroke="#10b981" stroke-width="2"/> <text x="62" y="572" font-size="13" font-weight="700" fill="#047857">zju-connect + FlClash（现在）</text> <text x="62" y="594" class="arch-s" fill="#059669">后台服务：1 个 · 常驻 14 MB（峰值 24 MB）· CPU 0.0%</text> <text x="62" y="612" class="arch-xs" fill="#047857">FlClash 是你要用的代理软件，本来就要开；zju-connect 是它下面的一个节点</text> <text x="570" y="580" text-anchor="end" font-size="24" font-weight="700" fill="#059669">14 MB</text> <text x="570" y="602" text-anchor="end" class="arch-xs" fill="#059669">1 进程</text> <!-- 过去 --> <rect x="606" y="548" width="570" height="74" rx="8" fill="#f1f5f9" stroke="#cbd5e1" stroke-width="1.5" stroke-dasharray="6,4"/> <text x="624" y="572" font-size="13" font-weight="700" fill="#64748b">aTrust 官方客户端（已彻底移除）</text> <text x="624" y="594" class="arch-s" fill="#94a3b8">后台进程：10 个 · 常驻 429 MB · 空闲 CPU 9%</text> <text x="624" y="612" class="arch-xs" fill="#94a3b8">抢路由表 · 与其它代理冲突 · 长时间无操作后掉线 · 需手动重连</text> <text x="1158" y="580" text-anchor="end" font-size="24" font-weight="700" fill="#94a3b8">429 MB</text> <text x="1158" y="602" text-anchor="end" class="arch-xs" fill="#94a3b8">10 进程</text> <line x1="1030" y1="573" x2="1162" y2="573" stroke="#cbd5e1" stroke-width="2"/> </svg></a></figure>

一句话概括：

```
你的程序（浏览器 / 终端 / VSCode / Docker）
        │
        ▼
   FlClash（分流内核，规则决定去向）
   ├── 校内资源 ──► whut 节点 = zju-connect ──► aTrust 隧道 ──► 校内
   ├── 国际流量 ──► 你的订阅节点
   └── 其它     ──► DIRECT
```

关键在于：**zju-connect 在 FlClash 眼里就是一个普通的 Socks5 节点**，只不过这个节点背后通向学校内网。

---

## 二、从零搭建

> 下文以武汉理工大学（WHUT）为例。其他学校需要替换的只有三处：aTrust 服务器域名、探测出来的登录域、以及你自己学校的域名与网段。

### Step 0：下载并确认可行性

**先下载。** 到项目的 GitHub Releases 页面，按自己的系统选对应的压缩包，解压后就是一个单文件的可执行程序，不需要安装。

**再做探测。** 这一步是决定方案能否落地的生死线：zju-connect 最初为浙大开发，虽然现在支持通用 aTrust 协议，但各高校在深信服网关上配置的认证分支差别极大——有人是账号密码，有人是网页跳转授权，有人还要短信验证。

```toml
# 以 whut 为例
./zju-connect -protocol atrust -server vpn.whut.edu.cn -port 443 -auth-info

```

程序内置了一个探测开关 `-auth-info`：只要告诉它服务器地址和协议类型（`atrust`），它就会把服务端支持的认证方式打印出来。武汉理工大学返回的是：

```json
[
  {"loginDomain":"radius.com","authType":"auth/psw","authName":"VPN登录认证"},
  {"loginDomain":"local","authType":"auth/psw","authName":"厂商运维登录"},
  {"authType":"auth/qrcode","authName":"Scan with aTrust"}
]
```

> **判读标准**：数组里只要有 `"authType":"auth/psw"`（账号密码），并记下对应的 `"loginDomain"`（这里是 `radius.com`），方案就能落地。
> 如果只有 `auth/cas`、`auth/httpsOauth2` 这类必须浏览器交互的方式，那么它无法驱动这套流程——这是最大的前提。

实测 WHUT 这条通道**不需要跳 CAS、不需要短信、不需要图形验证码**，凭校园卡号 + 密码就能一步静默登录。

### Step 1：写配置文件，跑通第一次登录

新建一个目录放程序和配置，然后把下面的内容保存成 `config.toml`（只保留了实际要动的项）：

```toml
# 协议与服务器：必须是 atrust
# easyconnect 只建 L4 的 TCP 代理，没有 UDP 转发和完整 L3 隧道
protocol = "atrust"
server_address = "vpn.whut.edu.cn"
server_port = 443

# 账号密码：就是学校统一身份认证的账号
username = "你的校园卡号"
password = "你的密码"

# 认证方式：填 Step 0 探测出来的两个值
auth_type = "auth/psw"
login_domain = "radius.com"

# 登录态缓存：首次登录后生成，之后重启可免密秒连
client_data_file = "client_data.json"

# 非浙大用户必开：否则会写入浙大专属的 DNS/路由预设
disable_zju_config = true

# 本地端口：刻意避开 FlClash 的 7890
socks_bind = ":1090"
http_bind = ":1091"

# 保活：必须填一个能命中校内路由的地址（原因见第三节）
disable_keep_alive = false
keep_alive_url = "https://www.whut.edu.cn/favicon.ico"

# 防回环：开启 FlClash 的 TUN 后，必须把底层连接绑到物理网卡
bind_interface = "en0"
auto_detect_interface = false

# 不启用 TUN，只做本地代理，分流交给 FlClash
tun_mode = false
add_route = false
dns_hijack = false
fake_ip = false
dial_direct_proxy = ""
```

配置里有账号密码，顺手把权限收紧：

```bash
chmod 600 config.toml
```

然后在终端**前台**跑一次（把程序名换成你解压出来的文件名）：

```bash
./zju-connect -config config.toml
```

看到这几行就说明通了，同时目录里会多出 `client_data.json`：

```text
Best node: 202.114.49.227:441, latency: 34 ms
Received IP: ...
VPN client started
SOCKS5 server listening on :1090
HTTP server listening on :1091
```

此时按 `Ctrl+C` 停掉——接下来交给 FlClash 和后台服务。

> `client_data.json` 是登录态缓存。之后每次启动如果它还有效，日志里会是 `Already logged in`，不需要密码；失效了会自动重新走一次密码登录。删掉它就会重新登录一次。

### Step 2：让 FlClash 用上这个节点

#### 2.1 覆写脚本：把校园网配置"焊"进去

FlClash 不认识「学校 VPN」这种东西，但它认识 Socks5 节点。所以只要把 `127.0.0.1:1090` 注册成一个节点，再把这个节点配进一条分流规则就完事了。

麻烦的地方在于：**这些配置没法直接写进订阅文件**——你哪天刷新订阅或者换了服务商，新文件会整个覆盖旧文件，辛苦加的节点和规则瞬间灰飞烟灭。

正解是 FlClash（≥ 0.8.85）自带的 **JavaScript 覆写脚本**：脚本独立保存，每次加载/刷新订阅时，FlClash 把原始配置传给脚本的 `main(config)`，脚本改完返回。订阅文件本身永不被修改，原订阅那几千条规则一条不丢。

<details>
<summary><b>点开查看完整覆写脚本（可整段复制）</b></summary>

```javascript
// FlClash 覆写脚本：注入 whut 节点、校园网策略组、防回环规则与学术库分流规则

// 必须强制直连的站点（有些站排斥代理 IP），会生成最高优先级的 DIRECT 规则
const DIRECT_EXTRA = [
  // 'example.com',
];

// 公司/家里内网网段：若与学校下发的 10/8、172.16/12 冲突，写这里优先直连
const LOCAL_EXEMPT = [
  // '10.8.0.0/16',
];

const NODE       = { name: 'whut', server: '127.0.0.1', port: 1090, type: 'socks5', udp: true };
const GROUP_NAME = '🏫 校园网';

// 校内主域名
const CAMPUS_SUFFIX = ['whut.edu.cn', 'wutnews.net', 'wutp.com.cn'];

// 图书馆数据库根域名（从学校 aTrust 服务端下发的资源表整理而来）
const ACADEMIC = [
  'acm.org', 'acs.org', 'addup.com.cn', 'aip.org', 'airitilibrary.cn',
  'ams.org', 'aps.org', 'ascelibrary.org', 'asmedl.org', 'bookan.com.cn',
  'calis.edu.cn', 'cas.org', 'cei.cn', 'cei.gov.cn', 'chaoxing.com',
  'cipread.com', 'clarivate.cn', 'clarivate.com', 'cnki.net', 'cnscisoft.com',
  'cqvip.com', 'csdl.ac.cn', 'csmar.com', 'cxstar.cn', 'drcnet.com.cn',
  'duxiu.com', 'ebooklibrary.org', 'ebscohost.com', 'els-cdn.com', 'elsevier.com',
  'emerald.com', 'emeraldinsight.com', 'enetedu.com', 'eng24.com', 'engineeringvillage.com',
  'epschinadata.com', 'epsnet.com.cn', 'fenqubiao.com', 'flylatex.cn', 'gddata.net',
  'gongtuedu.cn', 'gtarsc.com', 'guodao.cn', 'hbdlib.cn', 'hnlat.com',
  'hwshu.com', 'ieee.org', 'innography.com', 'inoteexpress.com', 'iop.org',
  'isiknowledge.com', 'knovel.com', 'koolearn.com', 'lawyee.net', 'lawyee.org',
  'libvideo.com', 'lilun.cn', 'nature.com', 'navalengineers.org', 'osapublishing.org',
  'pbgrd.com', 'pqdtcn.com', 'proquest.com', 'qdexam.com', 'rarelit.net',
  'reflib.org', 'rsc.org', 'sae.org', 'science.org', 'sciencedirect.com',
  'sciencedirectassets.com', 'sciencereading.cn', 'scinote.com', 'serialssolutions.com', 'shujugo.cn',
  'specialsci.cn', 'springer.com', 'springerlink.com', 'sslibrary.com', 'tandfonline.com',
  'thomsonreuters.com', 'umajor.org', 'umi.com', 'wanfangdata.com.cn', 'webofknowledge.com',
  'webofscience.com', 'wiley.com', 'wokinfo.com', 'worldlibrary.org', 'wutp.com.cn',
  'xinhuaonline.com', 'zhihuiya.com', 'zhizhen.com',
];

// 校内网段（同样来自服务端资源表）
const CAMPUS_CIDR = [
  '10.0.0.0/8',
  '172.16.0.0/12',
  '192.168.245.0/20',
  '202.114.48.0/20',
  '202.114.80.0/20',
  '202.114.160.0/20',
  '218.197.96.0/19',
  '218.197.128.0/20',
  '59.69.64.0/18',
];

function main(config) {
  config.proxies = config.proxies || [];
  config['proxy-groups'] = config['proxy-groups'] || [];
  config.rules = config.rules || [];

  // 1) 注册 / 更新 whut 节点
  const i = config.proxies.findIndex(p => p && p.name === NODE.name);
  if (i >= 0) config.proxies[i] = NODE;
  else config.proxies.push(NODE);

  // 2) 建「🏫 校园网」组：只给 whut 和 DIRECT
  //    不要放「选择节点」这类组进去，否则手滑点中会让校内流量绕道海外节点
  const group = { name: GROUP_NAME, type: 'select', proxies: [NODE.name, 'DIRECT'] };
  const gi = config['proxy-groups'].findIndex(g => g && g.name === GROUP_NAME);
  if (gi >= 0) config['proxy-groups'][gi] = group;
  else config['proxy-groups'].unshift(group);

  // 3) 清掉原订阅里可能冲突的旧规则（比如排在前面的 xxx.edu.cn,DIRECT）
  const kept = config.rules.filter(r => {
    if (typeof r !== 'string') return true;
    if (r.includes(GROUP_NAME) || r.includes('zju-connect')) return false;
    if (CAMPUS_SUFFIX.some(d => r.includes(d)) && r.endsWith(',DIRECT')) return false;
    return true;
  });

  // 4) 组装高优先级规则（顺序即优先级）
  const injected = [
    // 防回环：zju-connect 自身与 VPN 网关必须直连
    'PROCESS-NAME,zju-connect,DIRECT',
    'DOMAIN,vpn.whut.edu.cn,DIRECT',

    // 自定义强制直连
    ...DIRECT_EXTRA.map(d => `DOMAIN-SUFFIX,${d},DIRECT`),

    // 自定义内网豁免（排在校园网 10/8 之前才有效）
    ...LOCAL_EXEMPT.map(c => `IP-CIDR,${c},DIRECT,no-resolve`),

    // 校内主域
    ...CAMPUS_SUFFIX.map(d => `DOMAIN-SUFFIX,${d},${GROUP_NAME}`),

    // 学术数据库
    ...ACADEMIC.map(d => `DOMAIN-SUFFIX,${d},${GROUP_NAME}`),

    // 校内网段
    ...CAMPUS_CIDR.map(c => `IP-CIDR,${c},${GROUP_NAME},no-resolve`),
  ];

  // 注入到最前面，原订阅的数千条规则完整保留在后面
  config.rules = [...injected, ...kept];
  return config;
}
```

</details>

> **⚠️ 上面这份脚本是按武汉理工大学（WHUT）的数据写的，其他学校不能直接复制。**
> 校名、校内域名、图书馆数据库、校内网段都不一样，照抄只会得到一份"哪些都匹配不上"的配置。
>
> 正确做法：**把这份脚本 + 下面这段提示词一起发给你的 AI agent**，让它按你学校的数据改写。逻辑不用动，只换数据。

```text
下面这份是 FlClash 覆写脚本，原本是为武汉理工大学（WHUT）写的。
请帮我改写成我这所学校能用的版本，要求如下：

1. 只替换数据，不动逻辑。保留 main(config) 的结构、规则的注入顺序与优先级、
   DIRECT_EXTRA / LOCAL_EXEMPT 两个空数组的位置和注释。

2. 需要替换的数据共四项，全部来自我学校 aTrust 服务端下发的资源表：
   - CAMPUS_SUFFIX：我校校内主域名（如 xxx.edu.cn 及其它自有域名）
   - ACADEMIC：图书馆数据库的根域名列表
   - CAMPUS_CIDR：校内 IP 段
   - 防回环规则里的 VPN 域名：DOMAIN,<我校 VPN 域名>,DIRECT
   节点名 whut 可以改成我校缩写；策略组名「🏫 校园网」保持不变。

3. 我还没抓到资源表。请先告诉我获取方法：用 zju-connect 的 -debug-dump
   参数启动一次，服务端下发的 CIDR 与域名清单会打印在日志里。
   请给我具体命令，并说明该看日志里的哪几行。

4. 拿到清单后按下面的规则整理，并在交付时说明每条数据的处理依据：
   - 剔除广告 / 统计 / CDN 类域名（如 doubleclick、googleadservices、
     akamaihd、cnzz 等）
   - 把子域名归并到可注册根域（www.sciencedirect.com → sciencedirect.com）
   - CIDR 原样保留，不要自行合并或改写
   - 每一条最终数据都要能追溯到我给你的原始清单，不要凭空添加

5. 改完给我三样东西：
   - 完整脚本
   - 替换了哪些数据、各多少条
   - 验证方法：注入后规则总条数、策略组成员、首条与末条规则分别应该是什么

6. 如果我学校网关只支持 auth/cas 或 auth/httpsOauth2 这类浏览器交互方式，
   请直接告诉我这套方案走不通，不要硬改。认证方式可以用 -auth-info 探测（见 Step 0）。
```

**在 FlClash 里启用**：

1. **配置** → 当前订阅右侧 **⋮** → **更多** → **覆写**
2. 覆写模式选 **脚本** → **前往配置脚本** → 右上角 **添加**
3. 粘贴脚本保存 → 返回勾选启用 → **刷新一次订阅**
4. 去 **设置 → 常规** 打开 **启动时自动运行**，省掉每次开机手点「启动 + TUN」

启用后打开 **代理** 面板，会多出 `whut` 这个节点和一个 `🏫 校园网` 分组，组里只有 `whut` 和 `DIRECT` 两个选项。

#### 2.2 这段配置到底在做什么

Clash 系配置只有三个核心字段：

```yaml
proxies:        # 有哪些「出口」
proxy-groups:   # 怎么「选」出口
rules:          # 什么流量「该用」哪个出口
```

**`proxies`：出口清单。** 每条是一台服务器的连接方式，`type` 决定协议（`socks5` / `ss` / `vmess` / `trojan` / `vless` / `http` 等）。我们那个节点长这样——注意它没有任何加密参数，因为目标就在本机：

```yaml
- { name: "whut", type: socks5, server: 127.0.0.1, port: 1090, udp: true }
```

**这就是整套方案的本质**：把学校 VPN 伪装成一个 `socks5` 节点。对 FlClash 来说它平平无奇，对学校来说它就是一条正常的 aTrust 隧道。

**`proxy-groups`：策略组**，也就是面板上点的那一栏一栏：

| type | 行为 | 典型用途 |
| --- | --- | --- |
| `select` | 手动选，不自动切 | 校园网、地区选择 |
| `url-test` | 自动选延迟最低的 | 节点优选 |
| `fallback` | 按顺序用，前一个挂了换下一个 | 主备 |
| `load-balance` | 轮询/哈希分散 | 多线程下载 |

组可以嵌套组（所以容易误点）；`DIRECT` 和 `REJECT` 是内置出口，不用定义——前者直连，后者掐断（广告拦截常用）。

**`rules`：分流规则**，语法只有一种形状 `<TYPE>,<匹配内容>,<出口>[,no-resolve]`：

```yaml
- PROCESS-NAME,zju-connect,DIRECT              # 按进程名
- DOMAIN-SUFFIX,cnki.net,🏫 校园网               # 域名后缀
- IP-CIDR,202.114.48.0/20,🏫 校园网,no-resolve    # IP 段
- MATCH,DIRECT                                 # 兜底，必须放最后
```

三个必须记住的点：

1. **顺序即优先级，第一条命中就停。** 所以例外要写在通用规则前面——如果你的订阅里有一条 `xxx.edu.cn,DIRECT` 排在前面，校内网站就全被截胡直连了。
2. **`no-resolve` 是性能开关。** 不加它，Clash 为比对 IP 规则必须先解析域名，每个请求多一次 DNS；加上表示「只拿已经解析好的 IP 比对」。
3. **一次访问的决策链是固定的**：查 `rules` → 命中某条 → 看它指定的组 → 看组当前选了谁 → 查 `proxies` 里这个出口 → 发出去。所以 `🏫 校园网` 组的选择**只影响命中校园网规则的那部分流量**，管不到你刷 YouTube。

### Step 3：让它常驻后台（可选）

前面的命令都是前台运行的，关掉终端就没了。要让它开机自启、崩溃自动恢复，思路只有一条：**把它交给系统的服务管理器托管**。

各系统的做法（细节各不相同，按自己的环境来）：

| 系统 | 用什么 |
| --- | --- |
| macOS | 用户级 LaunchAgent（`launchctl`）或 LaunchDaemon（需要 root） |
| Linux | systemd 服务单元 |
| Windows | 任务计划程序的「开机时运行」，或 NSSM 之类的工具把它注册成服务 |

不管哪种方式，要满足这四个条件：

1. **程序以前台方式运行**，由服务管理器负责拉起和重启（不要 `nohup` 一把就跑）；
2. **工作目录设成程序所在目录**，否则找不到 `config.toml` 和 `client_data.json`；
3. **标准输出重定向到日志文件**，出问题才有据可查；
4. **重启策略设成「无条件」**——这一点最容易踩坑，下面单独说。

## 三、几个必须弄懂的点

### 1. TUN 模式 vs 系统代理模式

一个经典误解：

> 「开 TUN 就是建个虚拟网卡，让所有进程把流量发往 7890 端口，对吧？」

**大错特错——TUN 的流量压根不碰 7890 端口。**

**系统代理模式 = 门口贴告示。** FlClash 在 `127.0.0.1:7890` 开个柜台，再往系统偏好里写一行字（`scutil --proxy` 能看到 `HTTPEnable: 1`、`HTTPPort: 7890`），相当于贴了张「请把包裹送到 3 号驿站」。这张告示**没有任何强制力**：

- 识字的（Chrome、Safari、微信）主动读系统设置，把请求送到 7890；
- 不识字的（`curl` 不设环境变量、`ssh`、Docker 容器、游戏客户端）压根不看告示，直接按目标地址往外闯；
- 告示底下还有一行小字：macOS 系统代理的例外列表里写着 `10.*`、`172.16-31.*`、`192.168.*` 不用送驿站——所以哪怕程序认代理，访问 `10.x.x.x` 的校内服务器也必定直连失败。

**TUN 模式 = 在唯一出入口设卡改道。** FlClash 创建虚拟网卡 `utun4`（IP `198.18.0.1`）并直接改路由表：

```text
1        → 198.18.0.1  utun4   ┐
2/7      → 198.18.0.1  utun4   │
4/6      → 198.18.0.1  utun4   │  这 8 条加起来
8/5      → 198.18.0.1  utun4   │  正好覆盖整个 IPv4
16/4     → 198.18.0.1  utun4   │  且前缀比默认路由 0/0 更长
32/3     → 198.18.0.1  utun4   │
64/2     → 198.18.0.1  utun4   │
128.0/1  → 198.18.0.1  utun4   ┘
```

**它不给程序任何选择权**：不管 `ssh 10.x.x.x` 还是 Docker，只要往外发 IP 包，内核一查路由表就把原始包塞进 `utun4`，由 FlClash 在网络层截获。

<figure class="svg-figure"><a class="svg-figure-link" href="/img/svg/two-entries-one-brain.svg" target="_blank" rel="noopener" title="点击查看原尺寸" aria-label="两条入口一个大脑（点击查看原尺寸）"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1220 500" role="img" aria-labelledby="brain-t"> <title id="brain-t">两条入口，同一个核心大脑</title> <style> text { font-family: 'PingFang SC', 'Helvetica Neue', Arial, sans-serif; } .brain-mono { font-family: 'SF Mono', Menlo, Consolas, monospace; } .brain-flow { stroke-dasharray: 10,6; animation: brain-flow 1.1s linear infinite; } @keyframes brain-flow { to { stroke-dashoffset: -32; } } </style> <defs> <marker id="brain-ah-indigo" markerWidth="10" markerHeight="8" refX="9" refY="4" orient="auto"> <polygon points="0 0, 10 4, 0 8" fill="#6366f1"/> </marker> <marker id="brain-ah-amber" markerWidth="10" markerHeight="8" refX="9" refY="4" orient="auto"> <polygon points="0 0, 10 4, 0 8" fill="#f59e0b"/> </marker> </defs> <rect width="1220" height="500" rx="10" fill="#f8fafc" stroke="#e2e8f0" stroke-width="2"/> <!-- ===== 路径 A：应用层 ===== --> <rect x="40" y="76" width="466" height="170" rx="10" fill="#6366f1" fill-opacity="0.08" stroke="#6366f1" stroke-width="2"/> <text x="68" y="110" font-size="17" font-weight="700" fill="#4338ca">路径 A：应用层（L7）</text> <text x="68" y="140" font-size="13" fill="#475569">Chrome 等主动送包裹</text> <text x="68" y="180" font-size="14.5" font-weight="700" fill="#3730a3" class="brain-mono">127.0.0.1:7890</text> <text x="196" y="180" font-size="13" fill="#475569">端口</text> <text x="68" y="212" font-size="12.5" fill="#6366f1">（HTTP / SOCKS5 协议）</text> <path d="M512 161 L548 161" fill="none" stroke="#6366f1" stroke-width="2.5" class="brain-flow" marker-end="url(#brain-ah-indigo)"/> <!-- ===== 路径 B：网络层 ===== --> <rect x="40" y="300" width="466" height="170" rx="10" fill="#f59e0b" fill-opacity="0.09" stroke="#f59e0b" stroke-width="2"/> <text x="68" y="334" font-size="17" font-weight="700" fill="#b45309">路径 B：网络层（L3）</text> <text x="68" y="364" font-size="13" fill="#475569">路由表强制改道截流</text> <text x="68" y="404" font-size="14.5" font-weight="700" fill="#92400e" class="brain-mono">utun4</text> <text x="132" y="404" font-size="13" fill="#475569">虚拟网卡</text> <text x="68" y="436" font-size="12.5" fill="#b45309">（原始 IP 数据包）</text> <path d="M512 385 L548 385" fill="none" stroke="#f59e0b" stroke-width="2.5" class="brain-flow" marker-end="url(#brain-ah-amber)"/> <!-- ===== 右侧：同一个核心大脑 ===== --> <rect x="552" y="56" width="620" height="414" rx="12" fill="#ffffff" stroke="#0ea5e9" stroke-width="2.5"/> <text x="578" y="104" font-size="17" font-weight="700" fill="#0369a1">FlClash（Mihomo）同一个核心大脑</text> <line x1="578" y1="122" x2="1146" y2="122" stroke="#bae6fd" stroke-width="1.5"/> <!-- 树主干 --> <path d="M578 132 L578 406" fill="none" stroke="#7dd3fc" stroke-width="2.5" stroke-linecap="round"/> <!-- 行 1：同一套规则引擎 --> <path d="M578 178 H602" fill="none" stroke="#0ea5e9" stroke-width="2.5" stroke-linecap="round"/> <rect x="602" y="150" width="544" height="56" rx="8" fill="#0ea5e9" fill-opacity="0.09" stroke="#0ea5e9" stroke-opacity="0.55" stroke-width="1.5"/> <text x="626" y="185" font-size="14.5" font-weight="700" fill="#0369a1">同一套规则引擎（Rules）</text> <!-- 行 2：校园网 --> <path d="M578 254 H602" fill="none" stroke="#10b981" stroke-width="2.5" stroke-linecap="round"/> <rect x="602" y="226" width="544" height="56" rx="8" fill="#10b981" fill-opacity="0.09" stroke="#10b981" stroke-width="2"/> <text x="626" y="261" font-size="14.5" font-weight="700" fill="#047857">🏫 校园网 → <tspan class="brain-mono" fill="#065f46">whut</tspan>（<tspan class="brain-mono" fill="#065f46">127.0.0.1:1090</tspan>）</text> <!-- 行 3：选择节点 --> <path d="M578 330 H602" fill="none" stroke="#8b5cf6" stroke-width="2.5" stroke-linecap="round"/> <rect x="602" y="302" width="544" height="56" rx="8" fill="#8b5cf6" fill-opacity="0.09" stroke="#8b5cf6" stroke-width="2"/> <rect x="626" y="318" width="24" height="24" rx="5" fill="#8b5cf6" fill-opacity="0.85"/> <text x="662" y="337" font-size="14.5" font-weight="700" fill="#6d28d9">选择节点 → 香港机场</text> <!-- 行 4：DIRECT --> <path d="M578 406 H602" fill="none" stroke="#94a3b8" stroke-width="2.5" stroke-linecap="round"/> <rect x="602" y="378" width="544" height="56" rx="8" fill="#94a3b8" fill-opacity="0.1" stroke="#94a3b8" stroke-width="2"/> <text x="626" y="413" font-size="14.5" font-weight="700" fill="#475569"><tspan class="brain-mono" fill="#334155">DIRECT</tspan> → 物理网卡 <tspan class="brain-mono" fill="#334155">en0</tspan></text> </svg></a></figure>

实测验证：清空所有代理环境变量后 `curl https://www.cnki.net`，同时观察谁连了 7890——连 7890 的只有 Chrome 和向日葵，**`curl` 自始至终没碰过 7890**，但它在 0.19 秒内拿到了 HTTP 200，`zju-connect` 日志里同步出现 `115.31.65.10:443 -> VPN`。把系统代理关掉只留 TUN，浏览器、终端、SSH 全部照常工作。

一句话：**代理模式是「请程序配合」，TUN 是「不给程序选择权」。**

### 2. TUN 下的回环死锁：内存为什么瞬间飙到 479 MB

刚开 TUN 时踩过一个惊心动魄的坑：**突然彻底连不上校园网**，日志几分钟内暴涨到 2.9 MB（三万多行），Go 堆峰值从 14 MB 冲到 **479 MB**。

原因很典型：登录成功后选出的隧道网关，**恰好落在学校下发的某个校内网段里**（实测是 `202.114.48.0/20` 这个段）。于是形成了教科书级别的路由环路：

```text
[1] 访问校内网 ──► 进入 utun4（FlClash）
                        │ 命中校内规则
                        ▼
[2] 转发给 whut 节点（127.0.0.1:1090 = zju-connect）
                        │ zju-connect 需要先连学校网关
                        ▼
[3] 发起 TCP → 网关地址:441
                        │ 内核查路由：全流量已被 utun4 接管
                        ▼
[4] 这个建连包又被塞回 utun4 → 命中校内规则
                        ▼
[5] 又送去 127.0.0.1:1090 ──► 回到 [2]，每秒成千上万次 💥
```

**双保险解法**：

1. 在 `config.toml` 里钉死物理网卡（最彻底，让它绕开默认路由表）：
   ```toml
   bind_interface = "en0"
   auto_detect_interface = false
   ```
2. 在 FlClash 规则最顶部按进程名兜底：
   ```yaml
   - PROCESS-NAME,zju-connect,DIRECT
   - DOMAIN,vpn.<你的学校域名>,DIRECT
   ```

加上之后环路立刻消失，压测下内存峰值稳定在 14～24 MB（`phys_footprint_peak`）。

> 教训：**「VPN 客户端自己连服务器的流量」在 TUN 模式下是一个结构性问题**。任何「代理套代理」的方案，都要先想清楚那条底层连接从哪出去。
> 如果哪天在日志里看到这个网关地址，可以自己核对一下：`route -n get <网关IP> | grep interface`，输出 `utun4` 就说明还在环路里，输出 `en0` 就说明免疫成功。

### 3. 为什么它不会掉线

一条 VPN 要跨越三道「健忘关卡」，任何一道忘了你就断线：

| 关卡 | 长时间没数据会怎样 | 默认超时 |
| --- | --- | --- |
| ① 家用/公司路由器 NAT 表 | 回收端口映射，回来的包找不到你，直接丢 | 5～30 分钟 |
| ② 运营商 NAT / 连接跟踪 | 掐断空闲 TCP 会话状态 | 5～30 分钟 |
| ③ 学校 VPN 网关会话表 | 判定空闲超时，踢掉登录态 | 通常 15～30 分钟 |

zju-connect 有个每 60 秒一次的 KeepAlive，但**默认地址是 `http://www.baidu.com/favicon.ico`**——这个请求走本地直连，压根没进隧道，属于自欺欺人。改成一个走校内路由的地址才有意义：

```text
11:08:22  www.whut.edu.cn -> 202.114.56.2
11:08:22  202.114.56.2:443 -> VPN
11:08:22  KeepAlive: OK, status code 404
11:09:22  KeepAlive: OK, status code 404
11:10:22  KeepAlive: OK, status code 404
```

秒级误差为 0，而且真的穿过隧道（返回 404 无所谓，TLS 握手和 HTTP 往返已经完成），**一次请求同时刷新了三张表**。

光有心跳还不够。**v1.3.1 稳定版没有「会话失效后自愈」的能力**：网关回 `invalid SID` 时它只记一行日志，然后每分钟继续拿这张废卡去刷，进程活着、端口在听，没人重启它。实测合盖带走 1 小时 19 分，唤醒后连续 27 次 `code 10000004: invalid SID`，隧道彻底失效。

上游在 2026 年 9 月补上了两个提交（`support sid refresh`、`exit when the session is invalid`），但只进了 main 分支、没发稳定版。想彻底稳，用 nightly 构建或从 main 自行编译：失效时进程会主动退出，交给服务管理器重启并重新登录——实测从 kill 到 `VPN client started` 只要 **1.9 秒**。

> 顺带一个当前 nightly 的 bug：如果日志里每 30 分钟出现一行 `authConfig refresh failed: context canceled`，是 `Session.request()` 用已取消的 setup context 覆盖了请求自带的 ctx，一行补丁即可修好（改用 `s.client.Do(req)`）。

### 4. 公司内网和学校内网撞了同一个 IP，会连到哪一台？

这是整套方案里最容易踩、也最容易误解的一处。分两种情况：**网段撞了**和**同一个 IP 撞了**。后者更隐蔽，先讲它。

#### （1）同一个 IP：`ssh school` 会连到公司的机器

假设两边各有一台服务器，内网地址都是 `192.168.1.150`：

```sshconfig
# ~/.ssh/config
Host school
  HostName 192.168.1.150      # 学校那台
Host office
  HostName 192.168.1.150      # 公司那台
```

现在你人在公司，连着公司 Wi-Fi（本机地址 `192.168.1.93/24`），TUN 开着，`🏫 校园网` 也选中了 `whut`。这时候敲 `ssh school` 会发生什么？

**答案：连到公司那台。而且 `ssh office` 和 `ssh school` 会连到同一台机器。**

原因在**别名只是本地标签**：

```
ssh school
   │  ssh 查 ~/.ssh/config，把名字替换成参数
   ▼
192.168.1.150:22          ← 从这一步起，"school" 这个词就消失了
   │  内核查路由（最长前缀匹配）
   ▼
192.168.1.0/24 → en0      ← 你在公司连着公司 Wi-Fi，这条路由是系统自带的
   │                      （前缀 /24 比 TUN 的 /1 具体得多，优先命中）
   ▼
公司局域网 → 公司那台 192.168.1.150
```

注意最后一步：**FlClash 从头到尾没有参与**。包在路由层就被 `en0` 拿走了，压根没进 `utun4`，所以 `whut` 节点、`🏫 校园网` 分组、那一百多条规则，一条都没机会生效。

| 你敲的命令 | 实际连到 |
| --- | --- |
| `ssh school` | 公司的 `192.168.1.150` |
| `ssh office` | 公司的 `192.168.1.150` |

所以这种情况下，**学校那台机器在你的网络里等于"不存在"**——不是连错，而是它的地址被本地网段提前截胡了。

根本原因是一句 IP 常识：

> **同一个 IP 在同一时刻只能指向一台机器。** 你连着公司 Wi-Fi 时，`192.168.1.150` 在你的网络世界里就代表公司那台。

这也带来一个**安全上的隐患**：你以为自己在学校机器上操作，实际动的是公司的机器。所以登录后养成一个习惯——`hostname` 或 `cat /etc/hostname` 确认一下。

#### （2）网段层面：更常见的"半冲突"

上一节的冲突是两台机器**地址完全相同**，结果是连错机器。这一种更常见、也更难察觉：两台机器地址不同，但公司那片地址**正好落在学校划走的范围内**，于是被当成校内流量送进了隧道。症状不是连错，而是**超时**。

下面用一个完整例子走一遍。

**场景**（假设这家公司用 `10.8.1.0/24` 网段；上一节举的是 `192.168.1.x`，这里换一段，因为学校划走的正是 `10/8` 这种大段）：

| 位置 | 设备 | 地址 |
| --- | --- | --- |
| 公司 | 你的电脑 | `10.8.1.93/24` |
| 公司 | 公司网关 | `10.8.1.1` |
| 公司 | 同事的机器（和你同网段） | `10.8.1.50` |
| 公司 | 财务系统（另一栋楼，靠公司路由器中转） | `10.20.5.7:443` |
| 学校 | 实验室服务器 | `10.201.5.30:22` |

学校下发的资源表里包含这三个大段：

| 网段 | 覆盖范围 |
| --- | --- |
| `10.0.0.0/8` | 10.0.0.0 ~ 10.255.255.255（最容易撞） |
| `172.16.0.0/12` | 172.16.x ~ 172.31.x |
| `192.168.245.0/20` | 192.168.240 ~ 192.168.255 |

**第一步：只连公司 Wi-Fi，不开 TUN。**

路由表里只有两条相关的：

```text
default       → 10.8.1.1   en0
10.8.1.0/24   → en0                直连（on-link）
```

| 你访问 | 走哪 | 结果 |
| --- | --- | --- |
| `10.8.1.50`（同事机器） | `10.8.1.0/24` 直连 | ✅ 通 |
| `10.20.5.7`（公司财务） | `default` → 公司网关 → 公司内部转发 | ✅ 通 |
| `10.201.5.30`（学校实验室） | `default` → 公司网关 → 公网 | ❌ 不通（私网地址，公网上没人认） |

**第二步：开 TUN，`🏫 校园网` 选 `whut`。**

FlClash 往路由表里加了 8 条（这里只列相关的）：

```text
default         → 10.8.1.1    en0
10.8.1.0/24     → en0                      ← 原有的，还在
10.0.0.0/8      → 198.18.0.1  utun4        ← 新加的，就这一条改变了一切
172.16.0.0/12   → 198.18.0.1  utun4
```

同样三条访问，结果换了个样：

| 你访问 | 走哪 | 结果 | 说明 |
| --- | --- | --- | --- |
| `10.8.1.50`（同事机器） | `10.8.1.0/24`（前缀 24）→ `en0` | ✅ 通 | 直连路由更具体，TUN 抢不走 |
| `10.20.5.7`（公司财务） | `10.0.0.0/8`（前缀 8）→ `utun4` | ❌ **超时** | **本来能通，现在断了** |
| `10.201.5.30`（学校实验室） | `10.0.0.0/8` → `utun4` | ✅ 通 | **本来不通，现在通了** |

看清楚这笔交易：**你为了连上学校的 `10.201.5.30`，把公司的 `10.20.5.7` 弄断了。**

为什么 `/8` 会赢过默认路由：

```text
查路由表，找能匹配 10.20.5.7 的条目：
  ① 10.0.0.0/8        → 198.18.0.1（utun4）   前缀长度 8
  ② default 0.0.0.0/0 → 10.8.1.1  （en0）     前缀长度 0
→ 最长前缀匹配选 ①（8 > 0）
→ 交给 utun4 → FlClash 判成校内流量 → whut → 学校隧道
→ 学校内网里没有 10.20.5.7 这台机器 → 丢弃
→ 你这边一直等 → Connection timed out
```

为什么同网段的 `10.8.1.50` 反而没事：

```text
候选：10.8.1.0/24 (en0, 24)  vs  10.0.0.0/8 (utun4, 8)  vs  default (0)
→ /24 最长 → 走 en0 → 甚至不用经过网关，直接 ARP 找到这台机器
```

**这里有个关键点：规则只认"地址在不在这个网段里"，它不知道那台机器属于谁。** 所以同一条 `10.0.0.0/8`，对两类地址产生了完全相反的效果——学校的通了，公司的断了。

也正因为如此，症状是**超时**而不是"拒绝"：包真的被送进了隧道，学校那边找不到这个地址，**既不回"拒绝"也不回"不可达"，只静默丢弃**。所以 `ssh` 会卡在 `Connecting...` 十几秒才报 `Connection timed out`，`ping` 一直转圈。看起来像"网络卡了"，实际是"包跑错地方了"。

<figure class="svg-figure"><a class="svg-figure-link" href="/img/svg/route-decision.svg" target="_blank" rel="noopener" title="点击查看原尺寸" aria-label="路由决策（点击查看原尺寸）"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1220 820" role="img" aria-labelledby="route-t"> <title id="route-t">内网 IP 访问的路由决策流程</title> <style> text { font-family: 'PingFang SC', 'Helvetica Neue', Arial, sans-serif; } .route-mono { font-family: 'SF Mono', Menlo, Consolas, monospace; } .route-flow { stroke-dasharray: 10,6; animation: route-flow 1.1s linear infinite; } @keyframes route-flow { to { stroke-dashoffset: -32; } } </style> <defs> <marker id="route-arrow" markerWidth="10" markerHeight="8" refX="9" refY="4" orient="auto"> <polygon points="0 0, 10 4, 0 8" fill="#94a3b8"/> </marker> </defs> <rect width="1220" height="820" rx="10" fill="#f8fafc" stroke="#e2e8f0" stroke-width="2"/> <!-- ===== 起点 ===== --> <rect x="350" y="22" width="520" height="48" rx="24" fill="#6366f1" fill-opacity="0.1" stroke="#6366f1" stroke-width="2"/> <text x="610" y="44" text-anchor="middle" font-size="13.5" font-weight="700" fill="#4338ca">访问一个内网 IP</text> <text x="610" y="61" text-anchor="middle" font-size="11" fill="#6366f1">例如 192.168.1.50 或 10.8.0.5</text> <path d="M610 70 L610 96" fill="none" stroke="#94a3b8" stroke-width="2" class="route-flow" marker-end="url(#route-arrow)"/> <!-- ===== 第 1 步 ===== --> <rect x="100" y="100" width="1020" height="336" rx="12" fill="#ffffff" stroke="#e2e8f0" stroke-width="2"/> <text x="128" y="134" font-size="16" font-weight="700" fill="#1e293b">第 1 步：查询 macOS 本机路由表</text> <text x="1092" y="134" text-anchor="end" font-size="12.5" font-weight="700" fill="#b45309">（越具体的子网优先级越高！）</text> <line x1="128" y1="146" x2="1092" y2="146" stroke="#e2e8f0" stroke-width="1.5"/> <path d="M128 146 L128 344" fill="none" stroke="#94a3b8" stroke-width="2.5" stroke-linecap="round"/> <!-- 情况 A --> <path d="M128 188 H166" fill="none" stroke="#10b981" stroke-width="2.5" stroke-linecap="round"/> <rect x="166" y="158" width="930" height="136" rx="10" fill="#10b981" fill-opacity="0.09" stroke="#10b981" stroke-width="2.5"/> <text x="190" y="185" font-size="15" font-weight="700" fill="#047857">情况 A：目标是 192.168.1.50</text> <text x="190" y="210" font-size="12.5" fill="#334155">因为本机 Wi-Fi（<tspan class="route-mono" fill="#0f766e">en0</tspan>）IP 是 <tspan class="route-mono" fill="#0f766e">192.168.1.93/24</tspan>，</text> <text x="190" y="232" font-size="12.5" fill="#334155">系统自带 <tspan class="route-mono" fill="#0f766e">192.168.1.0/24 → en0</tspan> 的直连子网路由（前缀 <tspan class="route-mono" fill="#0f766e">/24</tspan>）</text> <text x="190" y="254" font-size="12.5" fill="#334155">比 TUN 的 <tspan class="route-mono" fill="#0f766e">128.0/1</tspan>（前缀 <tspan class="route-mono" fill="#0f766e">/1</tspan>）具体得多！</text> <text x="190" y="282" font-size="13.5" font-weight="700" fill="#047857">👉 结果：直接从物理网卡 en0 走公司/家里局域网，TUN 抢不走！</text> <!-- 情况 B --> <path d="M128 344 H166" fill="none" stroke="#f59e0b" stroke-width="2.5" stroke-linecap="round"/> <rect x="166" y="316" width="930" height="96" rx="10" fill="#f59e0b" fill-opacity="0.09" stroke="#f59e0b" stroke-width="2"/> <text x="190" y="343" font-size="15" font-weight="700" fill="#b45309">情况 B：目标是跨网段的公司服务器（如 10.8.0.5）</text> <text x="190" y="369" font-size="12.5" fill="#334155">本机没有 <tspan class="route-mono" fill="#b45309">10.8.0.0/16</tspan> 的直连路由，</text> <text x="190" y="393" font-size="12.5" fill="#334155">于是掉入 TUN 的 <tspan class="route-mono" fill="#b45309">8/5</tspan> 路由 → 进 <tspan class="route-mono" fill="#b45309">utun4</tspan>（FlClash）！</text> <!-- 过渡箭头 --> <path d="M610 436 L610 478" fill="none" stroke="#94a3b8" stroke-width="2" class="route-flow" marker-end="url(#route-arrow)"/> <text x="630" y="463" font-size="12" font-weight="700" fill="#64748b">（仅情况 B 进入 FlClash）</text> <!-- ===== 第 2 步 ===== --> <rect x="100" y="482" width="1020" height="300" rx="12" fill="#ffffff" stroke="#0ea5e9" stroke-opacity="0.45" stroke-width="2"/> <text x="128" y="516" font-size="16" font-weight="700" fill="#1e293b">第 2 步：FlClash 从上至下匹配规则</text> <line x1="128" y1="528" x2="1092" y2="528" stroke="#e2e8f0" stroke-width="1.5"/> <path d="M128 528 L128 676" fill="none" stroke="#94a3b8" stroke-width="2.5" stroke-linecap="round"/> <!-- 未配置豁免 --> <path d="M128 568 H166" fill="none" stroke="#f43f5e" stroke-width="2.5" stroke-linecap="round"/> <rect x="166" y="540" width="930" height="94" rx="10" fill="#f43f5e" fill-opacity="0.08" stroke="#f43f5e" stroke-width="2"/> <text x="190" y="571" font-size="14" font-weight="700" fill="#be123c">若未配置豁免：命中学校下发的 <tspan class="route-mono" fill="#9f1239">IP-CIDR,10.0.0.0/8,🏫 校园网</tspan></text> <text x="190" y="603" font-size="13.5" font-weight="700" fill="#be123c">👉 惨剧：把公司内网请求发进了学校隧道，直接超时失联 ❌</text> <!-- 配置了豁免 --> <path d="M128 676 H166" fill="none" stroke="#10b981" stroke-width="2.5" stroke-linecap="round"/> <rect x="166" y="648" width="930" height="112" rx="10" fill="#10b981" fill-opacity="0.09" stroke="#10b981" stroke-width="2.5"/> <text x="190" y="676" font-size="15" font-weight="700" fill="#047857">若在 <tspan class="route-mono" fill="#065f46">LOCAL_EXEMPT</tspan> 配置了豁免：</text> <text x="190" y="704" font-size="13" fill="#334155">优先命中排在前面的 <tspan class="route-mono" fill="#0f766e">IP-CIDR,10.8.0.0/16,DIRECT</tspan></text> <text x="190" y="734" font-size="13.5" font-weight="700" fill="#047857">👉 结果：FlClash 放行直连，正常访问公司服务器 ✅</text> </svg></a></figure>

**解决方案**：把属于公司的网段明确告诉 FlClash「这些别往隧道送」。在覆写脚本顶部填 `LOCAL_EXEMPT`：

```javascript
const LOCAL_EXEMPT = [
  '10.8.0.0/16',     // 公司办公网（含你和同事的机器）
  '10.20.0.0/16',    // 公司园区网（含财务系统）
];
```

它会生成两条 `DIRECT` 规则，并**插在校园网规则之前**（顺序即优先级）：

```yaml
- IP-CIDR,10.8.0.0/16,DIRECT,no-resolve        # 先命中
- IP-CIDR,10.20.0.0/16,DIRECT,no-resolve       # 先命中
- IP-CIDR,10.0.0.0/8,🏫 校园网,no-resolve        # 排在后面
```

修好之后 `10.20.5.7` 的路径变成：

```text
10.20.5.7 → 没有具体路由 → 进 utun4
          → FlClash 查规则：命中 IP-CIDR,10.20.0.0/16,DIRECT
          → 从物理网卡 en0 发出 → 公司网关 → 财务系统
```

（直连出口会绑定物理网卡，不会又绕回 TUN —— 否则就是第三节第 2 小节讲的回环死锁。）

最终三条都通：

| 你访问 | 结果 |
| --- | --- |
| `10.8.1.50`（公司同事） | ✅ |
| `10.20.5.7`（公司财务） | ✅ |
| `10.201.5.30`（学校实验室） | ✅ |

#### （3）真正连到学校那台同名机器，怎么办

`LOCAL_EXEMPT` 只解决"别把公司流量送错"，解决不了"两台同 IP 的机器我都要用"。

因为 IP 语义上已经只能指一台，所以任何解法都必须引入一个**额外标识**——端口号、别名、或者域名。三个方向，按"需不需要学校配合"排：

| 方向 | 做法 | 学校要配合吗 |
| --- | --- | --- |
| 加端口号 | 在 `config.toml` 里配 `port_forwarding`，把学校那台的 22 端口映射到本机一个专用端口（如 `127.0.0.1:2222`），`ssh school` 指向它 | 不要 |
| 显式指定出口 | 给这个主机单独写 `ProxyCommand`，让 zju-connect 在学校那边替你发起连接 | 不要 |
| 换一个地址 | 用学校那台的域名（如果有），或给它装个 Tailscale、或经校内跳板机中转 | 通常要 |

前两种都是纯本机改动，**配置一次、之后照常 `ssh school`**，区别只是"改配置文件"还是"改 SSH 配置"。第三种体验最好，但依赖学校那边的条件。

另外提一句：如果公司自己也配了 VPN 客户端（Cisco AnyConnect、深信服、Tailscale 之类），它们也开 TUN 的话会和 FlClash 抢路由表。**不要同时开两个 TUN**——需要连公司 VPN 时，把 FlClash 的 TUN 关掉。

---

### 5. 出问题时按这个顺序查

| 现象 | 原因 | 处理 |
| --- | --- | --- |
| 面板上 whut 显示 timeout | 进程没跑，端口无人监听 | 重启服务 |
| 进程在跑、端口在听，但隧道不通 | 登录态过期（日志出现 `invalid SID`） | 重启服务让它重新登录 |
| 服务一切正常，但网页打不开 | 策略组选错，或规则顺序不对 | 把 `🏫 校园网` 切回 `whut`，检查有没有排在前面的 `DIRECT` |

三条排查命令（macOS，其他系统换成对应的服务管理命令）：

```bash
launchctl list | grep zju-connect      # 中间有数字 = 在跑，横杠 = 没跑
tail -20 你的日志文件                   # 看它说了什么
lsof -nP -iTCP:1090 -sTCP:LISTEN       # 端口在不在
```

**关于内存数字**：活动监视器和 `footprint` 报的是 `phys_footprint`（实测 12～14 MB，压测 20～24 MB），而 `ps` / `htop` 的 RSS 会显示 25～29 MB——因为它把共享的 `__TEXT` 代码段（约 5.9 MB）也算进去了，那不是真实内存压力。**看内存压力用活动监视器，别看 RSS。**

## 四、把 aTrust 从系统里清走

新方案稳定运行几天后，可以动手清理。这一步**不建议照抄任何网上的命令**——不同系统的残留位置、不同版本的组件名称都不一样，照抄轻则漏删，重则误删别的东西。

更稳妥的做法是：**把下面这段提示词发给你的 AI agent，让它先只读盘点、再分两步（先停用后删除）执行。**

```text
请帮我彻底清理深信服 aTrust / EasyConnect 客户端及其所有残留，并遵循以下要求：

1. 先只读盘点，不要直接删除任何东西。找出并列出：
   - 正在运行的相关进程
   - 开机自启项 / 后台服务 / 后台任务
   - 安装目录、配置目录、缓存目录、日志目录
   - 特权组件（提权 helper）、内核扩展或系统扩展
   - 安装过的系统证书（尤其根证书）
   - 安装包注册记录（pkgutil / 注册表卸载项）
   - 登录项与后台项白名单
   输出一张清单给我确认，并标注每一项是「停用即可」还是「必须删除」。

2. 按我的操作系统给对应做法，不要混用其它系统的方式：
   - macOS：launchctl 的 bootout/disable 与 LaunchDaemons/LaunchAgents、
     PrivilegedHelperTools、pkgutil --forget、系统扩展与 kext、钥匙串证书、
     sfltool dumpbtm 查看的登录项
   - Windows：服务（sc query / sc config）、计划任务、注册表 Run 键与卸载项、
     ProgramData 与 Program Files 目录、网络过滤驱动（NDIS/WFP）、证书存储
   - Linux：systemd 单元、/opt 与 /etc 下的目录、内核模块、
     NetworkManager 插件、系统证书库

3. 分两步执行：
   第一步只「停用 + 禁用自启」（完全可逆，告诉我怎么恢复）；
   等我确认一段时间内网络和日常使用都正常，再做第二步删除。

4. 每一步执行前先把将执行的命令告诉我，执行后给出验证结果
   （进程数、自启项数、剩余文件、注册记录是否清零）。

5. 如果我机器上没有内核扩展、系统扩展或根证书，请明确告诉我
   「删除后不会留下后门或代理劫持」，而不是含糊带过。

6. 不要动其它 VPN 或代理工具，也不要改动系统网络设置以外的任何东西。
```

---

## 结语

至此，那个臃肿、霸道、占着 429 MB 还不肯退出的客户端被彻底请出了系统，取而代之的是一套 **13 MB、全自动**的方案：

- **在哪都能连**：只要能上网就能连回校内，不用记 IP、不改路由、不装证书；
- **不会悄悄掉线**：60 秒穿隧心跳 + 会话失效自动重登；
- **不吃内存**：一个没有界面的后台服务，12～14 MB；
- **配置不会丢**：节点、策略组、规则全在覆写脚本里，换服务商零维护。

整个过程中最有价值的其实不是「跑通了」，而是搞清楚了三个问题：**流量到底走了哪条路、为什么会绕成环、失败了由谁负责。** 这三个问题想明白，类似的方案都能自己搭起来。

Enjoy!


---

> 作者: [Songxuan](https://github.com/Song-xuan-xuan)  
> URL: https://blog.songtf.cn/posts/zju-connect/  

