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
能按用户配置的镜像自动替换。
This commit is contained in:
2026-11-15 12:22:59 +08:00
parent d455c2d203
commit dab9d8f000
5 changed files with 38 additions and 25 deletions
+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"
}
}