3 Commits
Author SHA1 Message Date
kicer 9566cb8151 feat(local): 状态页停在「已启动」等用户点击,不再 2 秒自动跳转
启动完成后一律用 click 模式,页面留在状态页,启动摘要和日志尾巴都能看完再进应用。
「首次使用」那段提示改成 ui_done 的可选第三参数,只有首次运行才写进页面。

顺带去掉「(关掉这个页面不影响服务)」这句废话,以及给隧道加一行 ui_step,
和 MCP 子进程的提示方式保持一致。
2026-11-15 12:41:14 +08:00
kicer 0c9a92aabd feat(local): 启动器顺带拉起隧道 client,配了 tunnel/client.yml 才起
client 作为第 5 个子进程挂在启动器下,停止时由 do_cleanup 的 kill_tree 一并收掉,
所以不需要单独的 start/stop 脚本。它自带指数退避重连、进程不退出,也不需要有守护循环。

stop.sh 补一步兜底:启动器被 kill -9 时 EXIT trap 不跑,client 会漏成孤儿,
按它自己写的 tunnel.pid 收掉(正常路径读不到这个文件,do_cleanup 已删)。
2026-11-15 12:27:39 +08:00
kicer dab9d8f000 fix(tunnel): client 日志改成只在状态变化时输出,依赖锁死并改回官方源
client.js 原来每次重连都打「连接/连接错误/断开」,服务器不可达时会一直刷屏。
改成状态机:启动一条、连上一条、断开一条,重试全程静默,和 MCP 服务的风格一致。

client.example.yml 改成 IP 为主的示例(多数用户只有 IP,没有域名和证书)。
ws 从 ^8.18.0 锁到 8.23.0,与项目其余 package.json 的精确版本风格统一;
package-lock 的 resolved 改回 registry.npmjs.org,让 npm 的 --replace-registry-host
能按用户配置的镜像自动替换。
2026-11-15 12:22:59 +08:00
7 changed files with 72 additions and 40 deletions
+23 -15
View File
@@ -257,12 +257,12 @@ CSS
elif [ -s "$UI_DIR/redirect" ] || [ -s "$UI_DIR/ready" ]; then
url=$(cat "$UI_DIR/redirect" 2>/dev/null)
[ -n "$url" ] || url=$(cat "$UI_DIR/ready")
# 后续启动:会自己跳;首次安装:只停在这页等点击。两种情况都留一个按钮兜底。
# auto 模式(写了 redirect)才会自己跳;click 模式就停在这页等点击。
if [ -s "$UI_DIR/redirect" ]; then
printf '<p class="note">2 秒后自动打开应用;没反应就点下面的按钮。</p>\n'
fi
printf '<p class="note"><a class="open" href="%s">打开应用</a> <span class="addr">%s</span></p>\n' "$url" "$url"
printf '<p class="tip">停止服务:运行 <code>./local/stop.sh</code>(关掉这个页面不影响服务)。</p>\n'
printf '<p class="tip">停止服务:运行 <code>./local/stop.sh</code>。</p>\n'
else
printf '<p class="tip">本页每秒自动刷新,进度会自己出现。</p>\n'
fi
@@ -319,15 +319,14 @@ ui_stop_ticker() {
ui_idle() { ui_stop_ticker; rm -f "$UI_DIR/busy"; ui_render; }
ui_done() {
# $1 应用地址 $2 auto|click(首次安装用 click:给链接让用户自己点,不自动跳)
# $1 应用地址 $2 auto|click(click = 停在状态页等用户点,不自动跳) $3 note(HTML,可选)
ui_stop_ticker
rm -f "$UI_DIR/busy" "$UI_DIR/error" "$UI_DIR/redirect" "$UI_DIR/ready" "$UI_DIR/note"
printf '%s\n' "${1:-}" > "$UI_DIR/ready"
if [ "${2:-auto}" = 'auto' ]; then
printf '%s\n' "${1:-}" > "$UI_DIR/redirect"
else
# 首次安装用户就停在这页,那下一步怎么配模型也得写在页上,不能只在终端里。
printf '<p class="hint">首次使用:进去后点左下角「用户区」→ 设置 → 模型池,添加平台和 API Key,再点「重启」(服务会自动拉起来)。</p>\n' > "$UI_DIR/note"
elif [ -n "${3:-}" ]; then
printf '%s\n' "$3" > "$UI_DIR/note"
fi
ui_title '已启动'
}
@@ -795,7 +794,7 @@ kill_tree_hard() {
do_cleanup() {
# 心跳子进程也得收掉,不然退出后它会继续每秒重绘页面。
ui_stop_ticker
rm -f "$RUNTIME/ui/busy" "$RUNTIME/supervisor.pid" "$RUNTIME/preview.port"
rm -f "$RUNTIME/ui/busy" "$RUNTIME/supervisor.pid" "$RUNTIME/preview.port" "$RUNTIME/tunnel.pid"
for p in $CORE_PID $MCP_PIDS; do kill_tree "$p"; done
CORE_PID=''
MCP_PIDS=''
@@ -1104,15 +1103,24 @@ while :; do
if [ -n "${PREVIEW_PORT:-}" ] && ! port_open "$PREVIEW_PORT"; then
warn "预览网关没起来(端口 ${PREVIEW_PORT})——日志:local/.runtime/preview.log"
fi
if [ "$FIRST_RUN" = 1 ]; then
# 首次安装:停在状态页,给一个按钮让用户自己点(先看完「首次使用」提示再进去)。
ui_done "http://${HOST_ADDR}:${PORT}" click
# 状态页没弹出来时(Linux / AGENT_LOCAL_UI=0)总得给个入口,否则用户什么都看不到。
[ "$UI_OPEN" = 1 ] || open_browser
else
ui_done "http://${HOST_ADDR}:${PORT}" auto
open_browser
# 隧道 client(可选):配了 tunnel/client.yml 才起,当第 5 个子进程,
# 停止时由 do_cleanup 的 kill_tree 一并收掉。它自带指数退避重连、进程不退出,
# 所以不需要守护循环;连不上时静默重试,真要查状态就看 local/.runtime/tunnel.log。
if [ -f "$ROOT/tunnel/client.yml" ]; then
( cd "$ROOT/tunnel" && exec node client.js >>"$RUNTIME/tunnel.log" 2>&1 ) &
MCP_PIDS="$MCP_PIDS $!"
printf '%s\n' "$!" > "$RUNTIME/tunnel.pid"
ui_step '隧道:已启动' ok
fi
# 一律停在状态页:启动摘要(语音/技能/隧道)和日志尾巴都留在屏幕上,
# 用户看完自己点「打开应用」,不会 2 秒就跳走。
if [ "$FIRST_RUN" = 1 ]; then
ui_done "http://${HOST_ADDR}:${PORT}" click '<p class="hint">首次使用:进去后点左下角「用户区」→ 设置 → 模型池,添加平台和 API Key,再点「重启」(服务会自动拉起来)。</p>'
else
ui_done "http://${HOST_ADDR}:${PORT}" click
fi
# 状态页没弹出来时(Linux / AGENT_LOCAL_UI=0)总得给个入口,否则用户什么都看不到。
[ "$UI_OPEN" = 1 ] || open_browser
printf '\n'
say "已启动:http://$HOST_ADDR:$PORT"
sleep 1
+11
View File
@@ -98,6 +98,17 @@ for p in "$PORT" $MCP_PORTS "$pv_port"; do
done
done
# 5) 隧道 client:它不监听端口,正常随启动器的 do_cleanup 一起收掉;
# 启动器被 kill -9 时 EXIT trap 不跑,会漏成孤儿,按它自己写的 pid 文件兜底。
if [ -f "$RUNTIME/tunnel.pid" ]; then
tpid=$(cat "$RUNTIME/tunnel.pid" 2>/dev/null || printf '')
case "$tpid" in ''|*[!0-9]*) tpid='';; esac
if [ -n "$tpid" ] && kill -0 "$tpid" 2>/dev/null; then
stop_pid "$tpid" '隧道 client'
fi
rm -f "$RUNTIME/tunnel.pid"
fi
if [ "$stopped" = 1 ]; then
say '已停止'
else
+1 -1
View File
@@ -20,7 +20,7 @@
## 快速验证(本机,不碰 nginx)
```bash
cd tunnel && npm install
cd tunnel && npm install # 国内慢就加 --registry=https://registry.npmmirror.com
cp client.example.yml client.yml # server 改成 ws://127.0.0.1:19000/tunnel,token 先留空
node server.js & # 首次启动生成 server.yml,日志打印 TOKEN
# 把 TOKEN 填进 client.yml 的 token,再:
+13 -9
View File
@@ -1,9 +1,13 @@
# tunnel 本机端配置 —— 复制为 client.yml 后改(client.yml 不进版本库)
# 服务器隧道地址:对外走 TLS 用 wss,本机自测用 ws://127.0.0.1:19000/tunnel
server: "wss://agent.home.example.com/tunnel"
# 服务器隧道地址,两种形态按需选:
# 只有 IP(最省事,但要求服务器把 19000 映射到 0.0.0.0)
# ws://1.2.3.4:19000/tunnel ← 明文,token 在公网裸奔,仅限内网/临时调试
# 有域名 + 证书(推荐,前面套 nginx 反代)
# wss://tunnel.example.com/tunnel
server: "ws://1.2.3.4:19000/tunnel"
# 和服务器 server.yml 里的 token 一致
# 和服务器 server.yml 里自动生成的 token 一致
token: ""
# 握手 UA:伪装成浏览器,降低被 DPI 盯上的概率。不写就用内置默认值
@@ -15,14 +19,14 @@ maxBodyMb: 64
# 要暴露的本地服务:加服务 = 加一项,两边代码都不用动
# 路由优先级:固定端口指定的 name → host 精确 → *.域名 通配 → "*" 兜底
services:
- name: agent
host: agent.home.example.com
# 直接按 IP 访问时,请求的 Host 头就是那个 IP,用 "*" 兜底最省事(单服务形态)
- host: "*"
port: 8090
# 没有域名时用 "*" 兜底:所有请求都给这一项(单服务形态)
# - host: "*"
# 有域名时按 Host 精确匹配,一个域名对一个服务
# - name: agent
# host: agent.example.com
# port: 8090
# - name: nas
# host: nas.home.example.com
# host: nas.example.com
# port: 5000
+20 -11
View File
@@ -147,11 +147,15 @@ async function handleRequest(id, st) {
}
// ---------- 连接 / 重连 ----------
// 日志只在状态变化时说:启动一条、连上一条、断开一条,重试过程完全静默
// (和 MCP 服务一个风格:启动时报一次自己在哪,之后没人连也不刷屏)。
let linked = false; // 当前是否连着
let everLinked = false; // 是否连上过(区分首连和重连)
function connect() {
if (stopping) return;
const sep = CFG.server.includes('?') ? '&' : '?';
const url = CFG.server + sep + 'token=' + encodeURIComponent(CFG.token);
log('连接', CFG.server);
ws = new WebSocket(url, {
headers: { 'User-Agent': CFG.userAgent },
handshakeTimeout: 20000,
@@ -160,29 +164,34 @@ function connect() {
ws.on('open', () => {
backoff = 1000;
log('已连接,暴露 ' + CFG.services.map(s => s.host + '→127.0.0.1:' + s.port).join(', '));
linked = true;
log(everLinked ? '已重新连接' : '已连接 ' + CFG.server);
everLinked = true;
});
ws.on('message', onMessage);
ws.on('close', (code, reason) => {
if (stopping) return;
log('断开 code=' + code + ' ' + (reason || '') + ',' + Math.round(backoff / 1000) + 's 后重连');
if (linked) { // 只在「连着 → 断开」这个跳变上说一次
linked = false;
log('断开' + (reason ? ':' + reason : '') + ',重连中');
}
setTimeout(connect, backoff);
backoff = Math.min(backoff * 2, 30000); // 指数退避,封顶 30s
});
ws.on('error', (e) => log('连接错误', e.message));
ws.on('error', () => {}); // close 紧接着会来,不重复报
}
process.on('SIGINT', () => { stopping = true; log('退出'); try { ws && ws.close(1000, 'client shutdown'); } catch (_) {} process.exit(0); });
process.on('SIGTERM', () => { stopping = true; process.exit(0); });
// 启动自检:本地端口不通时早点说,别等服务器转发过来才报错
// 启动:一条说清楚往哪连、暴露了什么(之后只在状态变化时再说)
log('隧道 → ' + CFG.server + '(' + CFG.services.map(s => s.host + '→127.0.0.1:' + s.port).join(', ') + ')');
// 本地端口不通时早点说(只报问题,正常不吭声)
for (const s of CFG.services) {
const req = http.request({ host: '127.0.0.1', port: s.port, path: '/', method: 'HEAD', timeout: 3000 }, (res) => {
log('自检 ' + s.host + ' → 127.0.0.1:' + s.port + ' 可达(HTTP ' + res.statusCode + ')');
res.resume();
});
req.on('error', (e) => log('自检警告:127.0.0.1:' + s.port + ' 不可达(' + e.code + '),服务没起来?'));
req.on('timeout', () => { req.destroy(); log('自检警告:127.0.0.1:' + s.port + ' 超时'); });
const req = http.request({ host: '127.0.0.1', port: s.port, path: '/', method: 'HEAD', timeout: 3000 }, (res) => res.resume());
req.on('error', (e) => log('警告:127.0.0.1:' + s.port + ' 不可达(' + e.code + '),本地服务没起来?'));
req.on('timeout', () => { req.destroy(); log('警告:127.0.0.1:' + s.port + ' 超时'); });
req.end();
}
+3 -3
View File
@@ -8,13 +8,13 @@
"name": "agent-tunnel",
"version": "0.1.0",
"dependencies": {
"ws": "^8.18.0",
"ws": "8.23.0",
"yaml": "2.6.1"
}
},
"node_modules/ws": {
"version": "8.23.0",
"resolved": "https://mirrors.cloud.tencent.com/npm/ws/-/ws-8.23.0.tgz",
"resolved": "https://registry.npmjs.org/ws/-/ws-8.23.0.tgz",
"integrity": "sha512-wCkmX8QMOrKjgO8HW2WyDbTSGeqkocxgDzS9iR79rEAexty6/2qzilx3Ag8Tqf9ykop3irLKle0MtVn5qjVnoQ==",
"license": "MIT",
"engines": {
@@ -35,7 +35,7 @@
},
"node_modules/yaml": {
"version": "2.6.1",
"resolved": "https://mirrors.cloud.tencent.com/npm/yaml/-/yaml-2.6.1.tgz",
"resolved": "https://registry.npmjs.org/yaml/-/yaml-2.6.1.tgz",
"integrity": "sha512-7r0XPzioN/Q9kXBro/XPnA6kznR73DHq+GXh5ON7ZozRO6aMjbmiBuKste2wslTFkC5d1dw0GooOCepZXJ2SAg==",
"bin": {
"yaml": "bin.mjs"
+1 -1
View File
@@ -9,7 +9,7 @@
"client": "node client.js"
},
"dependencies": {
"ws": "^8.18.0",
"ws": "8.23.0",
"yaml": "2.6.1"
}
}