dsh-mobile-plugin 0.5.134 → 0.5.136
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -5
- package/config/agent-presets/standard/skills/dsh-mobile-kb/references/zeronews.md +1 -1
- package/lib/cloudflared/runner.cjs +10 -5
- package/lib/cloudflared/wiring.cjs +10 -9
- package/lib/impl.js +1 -1
- package/lib/index.js +18 -33
- package/lib/mobile/channels.js +9 -108
- package/lib/mobile/device-profile.js +1 -2
- package/lib/mobile/permissions.js +1 -6
- package/lib/mobile/plugin-self.js +8 -0
- package/lib/panel/index.html +8 -8
- package/lib/relay/pairing-check.cjs +9 -1
- package/lib/routes/mobile-api.js +0 -10
- package/lib/shell/half-route.js +18 -6
- package/lib/shell/optional-lines.js +95 -0
- package/lib/shell/runtime.js +37 -1
- package/lib/shell/tailcat-gate.js +55 -0
- package/lib/shell/transport-host.js +13 -1
- package/lib/shell-compat.cjs +39 -0
- package/lib/update-install.cjs +23 -1
- package/lib/zeronews/account.cjs +45 -6
- package/lib/zeronews/provision.cjs +78 -3
- package/lib/zeronews/purge.cjs +3 -1
- package/lib/zeronews/runner.cjs +82 -12
- package/lib/zeronews/upgrade.cjs +260 -0
- package/lib/zeronews/watcher.cjs +2 -2
- package/lib/zeronews/wiring.cjs +183 -13
- package/package.json +14 -13
- package/lib/mobile/channel-proxy.js +0 -412
package/lib/zeronews/runner.cjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// 2026-11-28:官方 4.1.1 起可用 `tunnel <id> set-local` 原地把旧隧道改到当前端口,失配时自动改,不再只报错。
|
|
1
2
|
// 2026-11-27:复用本机已在跑的客户端前比对 client_id / cloud_server_addr,另一个身份占位就明确报错不 attach。
|
|
2
3
|
// 2026-11-27:外部把隧道重建到新端口时,先用 endpoints 的落点刷新本次比对值,第一次 connect 就认下新端口。
|
|
3
4
|
// 2026-11-26:体检发现旧隧道落点端口没人监听就判失效(只改本机状态,不删不建云端隧道),交接线层清地址让兜底顶上。
|
|
@@ -93,7 +94,11 @@ function localPortOf(item) {
|
|
|
93
94
|
return 0;
|
|
94
95
|
}
|
|
95
96
|
|
|
96
|
-
/**
|
|
97
|
+
/**
|
|
98
|
+
* 从 endpoints 响应里取第一条 active 的 HTTPS 隧道:{ url, localPort, tunnelId },没有就 null。
|
|
99
|
+
* tunnelId 用来在落点失配时调 `tunnel <id> set-local` 原地改端口:本地 API 平铺给 `tunnel_id`,
|
|
100
|
+
* 账号侧那份是 `id`,两个都认。
|
|
101
|
+
*/
|
|
97
102
|
function pickEndpointDetail(payload) {
|
|
98
103
|
const data = payload && typeof payload === 'object' && 'data' in payload ? payload.data : payload;
|
|
99
104
|
const items = Array.isArray(data) ? data
|
|
@@ -108,7 +113,11 @@ function pickEndpointDetail(payload) {
|
|
|
108
113
|
const domain = typeof item.domain === 'string' && item.domain ? item.domain
|
|
109
114
|
: typeof item.public_url === 'string' ? item.public_url : '';
|
|
110
115
|
const url = endpointUrl(domain.includes('://') ? domain : `https://${domain}`);
|
|
111
|
-
if (url)
|
|
116
|
+
if (url) {
|
|
117
|
+
const tunnelId = typeof item.tunnel_id === 'string' && item.tunnel_id ? item.tunnel_id
|
|
118
|
+
: typeof item.id === 'string' ? item.id : '';
|
|
119
|
+
return { url, localPort: localPortOf(item), tunnelId };
|
|
120
|
+
}
|
|
112
121
|
}
|
|
113
122
|
return null;
|
|
114
123
|
}
|
|
@@ -180,6 +189,7 @@ function terminalError(message, reason) {
|
|
|
180
189
|
* @param {(bin:string,args:string[],opts:object)=>Promise<{code:number,stdout:string,stderr:string}>} [options.runImpl] 一次性命令注入
|
|
181
190
|
* @param {(port:number)=>Promise<boolean>} [options.probePort] 隧道落点端口有没有人监听(体检用,测试注入)
|
|
182
191
|
* @param {() => {clientId:string,node:string}} [options.identityOf] 当前配置记录的客户端身份(接线层从落盘状态读)
|
|
192
|
+
* @param {(binary:string)=>void} [options.onBinary] 拿到可执行文件后的回调(换代收尾用)
|
|
183
193
|
* @param {string} [options.base] 本地 API 根
|
|
184
194
|
*/
|
|
185
195
|
function createZeroNewsRunner(options = {}) {
|
|
@@ -191,6 +201,7 @@ function createZeroNewsRunner(options = {}) {
|
|
|
191
201
|
const runImpl = typeof options.runImpl === 'function' ? options.runImpl : defaultRun;
|
|
192
202
|
const probePort = typeof options.probePort === 'function' ? options.probePort : defaultProbePort;
|
|
193
203
|
const identityOf = typeof options.identityOf === 'function' ? options.identityOf : () => ({});
|
|
204
|
+
const onBinary = typeof options.onBinary === 'function' ? options.onBinary : null;
|
|
194
205
|
const base = String(options.base || DEFAULT_BASE).replace(/\/+$/, '');
|
|
195
206
|
const sleep = typeof options.sleepImpl === 'function' ? options.sleepImpl : (ms) => new Promise((r) => setTimeout(r, ms));
|
|
196
207
|
|
|
@@ -302,10 +313,12 @@ function createZeroNewsRunner(options = {}) {
|
|
|
302
313
|
}
|
|
303
314
|
|
|
304
315
|
/** 轮询 status.login,等浏览器确认完成(或超时 / 出现明确失败原因)。 */
|
|
305
|
-
async function waitLogin(timeoutMs) {
|
|
316
|
+
async function waitLogin(timeoutMs, gen) {
|
|
306
317
|
const deadline = Date.now() + timeoutMs;
|
|
307
318
|
for (let i = 0; i < LOGIN_POLL_MAX && Date.now() < deadline; i += 1) {
|
|
319
|
+
if (gen !== generation) return false;
|
|
308
320
|
const s = await cliStatus();
|
|
321
|
+
if (gen !== generation) return false;
|
|
309
322
|
if (s && (s.login === true || s.login === 'true')) return true;
|
|
310
323
|
// 明确失败(配额 / 拒绝 / 过期)就别再空等,交给上面按原因报错。
|
|
311
324
|
if (s && s.loginError) return false;
|
|
@@ -315,10 +328,12 @@ function createZeroNewsRunner(options = {}) {
|
|
|
315
328
|
}
|
|
316
329
|
|
|
317
330
|
/** 轮询 endpoints 拿到隧道域名,最多等到 START_TIMEOUT_MS。 */
|
|
318
|
-
async function waitEndpoint(timeoutMs, port) {
|
|
331
|
+
async function waitEndpoint(timeoutMs, port, gen) {
|
|
319
332
|
const deadline = Date.now() + timeoutMs;
|
|
320
333
|
for (let i = 0; i < START_POLL_MAX && Date.now() < deadline; i += 1) {
|
|
334
|
+
if (gen !== generation) return '';
|
|
321
335
|
const payload = await api('/api/v1/endpoints');
|
|
336
|
+
if (gen !== generation) return '';
|
|
322
337
|
const found = pickEndpoint(payload, port);
|
|
323
338
|
if (found) return found;
|
|
324
339
|
await sleep(1500);
|
|
@@ -326,13 +341,38 @@ function createZeroNewsRunner(options = {}) {
|
|
|
326
341
|
return '';
|
|
327
342
|
}
|
|
328
343
|
|
|
344
|
+
/**
|
|
345
|
+
* 把一条已经建好的隧道改指到本机当前端口:官方 4.1.1 起的
|
|
346
|
+
* `tunnel <id> set-local <ip> <port> --json`,原地改、不删隧道、不占额度、公网域名不变。
|
|
347
|
+
* 老客户端(4.1.0 及以前)没有这个子命令,却照样以 0 退出、只打帮助文本 ——
|
|
348
|
+
* 所以判据只认 JSON 里的 `ok: true`,认不出就当「不支持」,绝不把帮助文本当成功。
|
|
349
|
+
*/
|
|
350
|
+
async function repointTunnel(tunnelId, port, gen) {
|
|
351
|
+
const result = await runImpl(binary, ['tunnel', tunnelId, 'set-local', '127.0.0.1', String(port), '--json'], { timeoutMs: 20_000 });
|
|
352
|
+
if (gen !== generation) return { ok: false, supported: false, detail: '' };
|
|
353
|
+
const text = String((result && result.stdout) || '').trim();
|
|
354
|
+
const start = text.indexOf('{');
|
|
355
|
+
const end = text.lastIndexOf('}');
|
|
356
|
+
if (start >= 0 && end > start) {
|
|
357
|
+
try {
|
|
358
|
+
const parsed = JSON.parse(text.slice(start, end + 1));
|
|
359
|
+
if (parsed && parsed.ok === true) return { ok: true, supported: true, detail: '' };
|
|
360
|
+
} catch { /* 落到下面按原文报 */ }
|
|
361
|
+
}
|
|
362
|
+
const raw = String((result && result.stderr) || '').trim() || text.split('\n')[0];
|
|
363
|
+
const code = result && typeof result.code === 'number' ? result.code : 0;
|
|
364
|
+
if (code !== 0) return { ok: false, supported: true, detail: raw || `改端口退出码 ${code}` };
|
|
365
|
+
return { ok: false, supported: false, detail: raw };
|
|
366
|
+
}
|
|
367
|
+
|
|
329
368
|
/**
|
|
330
369
|
* 确保有一条指向本机端口的 HTTPS 隧道:已有一条就复用(免费档只有一条额度)。
|
|
331
370
|
* 建隧道走 CLI `add https --port N --json`(官方稳定接口;本地 /api/v1/tunnels 的字段口径会变,
|
|
332
371
|
* 直接用裸 API 会因未知字段被拒)。`add` 的 JSON 里带 public_url 就直接用,没有就回读 endpoints。
|
|
333
372
|
*/
|
|
334
|
-
async function ensureTunnel(port) {
|
|
373
|
+
async function ensureTunnel(port, gen) {
|
|
335
374
|
const found = pickEndpointDetail(await api('/api/v1/endpoints'));
|
|
375
|
+
if (gen !== generation) return '';
|
|
336
376
|
const requested = Number.isInteger(port) && port > 0 ? port : 0;
|
|
337
377
|
if (found) {
|
|
338
378
|
// portOf() 优先返回上一轮记下的隧道端口;本次传入值与缓存相同就说明它来自缓存。
|
|
@@ -343,9 +383,21 @@ function createZeroNewsRunner(options = {}) {
|
|
|
343
383
|
if (fromCache && Number.isInteger(found.localPort) && found.localPort > 0) current = found.localPort;
|
|
344
384
|
tunnelLocalPort = Number.isInteger(found.localPort) && found.localPort > 0 ? found.localPort : 0;
|
|
345
385
|
// 只有字段明确在、且指向另一个端口才算失配:老接口的 endpoints 没有 local_port,
|
|
346
|
-
//
|
|
347
|
-
//
|
|
386
|
+
// 那种照旧复用。失配时先用官方 `set-local` 把这条隧道原地指到当前端口(不删、不建、
|
|
387
|
+
// 不占额度、域名不变);客户端太旧没有这个子命令、或账号侧拒绝,才如实报错让用户自己处理。
|
|
348
388
|
if (current > 0 && found.localPort > 0 && found.localPort !== current) {
|
|
389
|
+
const moved = found.tunnelId ? await repointTunnel(found.tunnelId, current, gen) : null;
|
|
390
|
+
if (gen !== generation) return '';
|
|
391
|
+
if (moved && moved.ok) {
|
|
392
|
+
tunnelLocalPort = current;
|
|
393
|
+
return found.url;
|
|
394
|
+
}
|
|
395
|
+
if (moved && moved.supported) {
|
|
396
|
+
throw terminalError(`旧隧道指向旧端口,自动改到当前端口失败(${moved.detail}):请在 ZeroNews 控制台把隧道目标改成 ${current} 后重试(旧端口 ${found.localPort})`, 'port-mismatch');
|
|
397
|
+
}
|
|
398
|
+
if (moved) {
|
|
399
|
+
throw terminalError(`旧隧道指向旧端口,本机客户端不支持自动改端口:请把 ZeroNews 客户端升级到 4.1.1 后重试,或在控制台把隧道目标改成 ${current}(旧端口 ${found.localPort})`, 'port-mismatch');
|
|
400
|
+
}
|
|
349
401
|
throw terminalError(`旧隧道指向旧端口,请删除后重试(旧端口 ${found.localPort},当前本机端口 ${current})`, 'port-mismatch');
|
|
350
402
|
}
|
|
351
403
|
return found.url;
|
|
@@ -357,6 +409,7 @@ function createZeroNewsRunner(options = {}) {
|
|
|
357
409
|
let addError = '';
|
|
358
410
|
try {
|
|
359
411
|
const result = await runImpl(binary, ['add', 'https', `--port=${port}`, '--json'], { timeoutMs: 20000 });
|
|
412
|
+
if (gen !== generation) return '';
|
|
360
413
|
const text = String((result && result.stdout) || '').trim();
|
|
361
414
|
if (result && result.code !== 0) {
|
|
362
415
|
addError = String(result.stderr || result.stdout || '').trim() || `建隧道退出码 ${result.code}`;
|
|
@@ -372,7 +425,9 @@ function createZeroNewsRunner(options = {}) {
|
|
|
372
425
|
}
|
|
373
426
|
// 新建成功:记住它落在哪个端口上,下次启动沿用,不再判成「旧隧道指向旧端口」。
|
|
374
427
|
if (added) { tunnelLocalPort = port; return added; }
|
|
375
|
-
|
|
428
|
+
if (gen !== generation) return '';
|
|
429
|
+
const waited = await waitEndpoint(START_TIMEOUT_MS, port, gen);
|
|
430
|
+
if (gen !== generation) return '';
|
|
376
431
|
if (waited) { tunnelLocalPort = port; return waited; }
|
|
377
432
|
// 免费档只有 1 条 1Mbps 隧道;额度被旧隧道占住、或账号侧拒绝时,原文要透出来,
|
|
378
433
|
// 别让用户只看到「连上了但拿不到地址」这种猜不出原因的提示。
|
|
@@ -391,11 +446,15 @@ function createZeroNewsRunner(options = {}) {
|
|
|
391
446
|
try {
|
|
392
447
|
phase = 'installing';
|
|
393
448
|
const got = await ensure();
|
|
449
|
+
if (gen !== generation) return status();
|
|
394
450
|
if (!got || !got.ok) throw new Error(got && got.code === 'unsupported-platform' ? '这台系统不支持自动安装 ZeroNews 客户端' : '下载 ZeroNews 客户端失败,请稍后重试');
|
|
395
451
|
binary = got.path;
|
|
452
|
+
// 换代的收尾(上次切换断在中间)在进程起来之前做完:宁可回到能跑的旧版,也别留一个空位。
|
|
453
|
+
if (onBinary) { try { onBinary(binary); } catch { /* 收尾失败不挡启动 */ } }
|
|
396
454
|
|
|
397
455
|
phase = 'login';
|
|
398
456
|
const st = await cliStatus();
|
|
457
|
+
if (gen !== generation) return status();
|
|
399
458
|
clientId = (st && st.clientId) || '';
|
|
400
459
|
node = (st && st.node) || '';
|
|
401
460
|
// 先看明确失败:配额满了 / 被拒 / 过期,都属于「别再等、直接告诉用户怎么办」。
|
|
@@ -410,6 +469,7 @@ function createZeroNewsRunner(options = {}) {
|
|
|
410
469
|
pendingToken = '';
|
|
411
470
|
if (token) {
|
|
412
471
|
const applied = await runImpl(binary, ['authtoken', token], { timeoutMs: 15000 });
|
|
472
|
+
if (gen !== generation) return status();
|
|
413
473
|
const detail = String((applied && (applied.stderr || applied.stdout)) || '').trim();
|
|
414
474
|
// 官方有一条明确的坑:本机已有 config.yml 时 token 会被忽略并保留旧配置(退出码可能仍是 0)。
|
|
415
475
|
// 不认这条就会「以为写成功了」,接着起客户端、建隧道,最后卡在拿不到地址,报错对不上真因。
|
|
@@ -429,10 +489,11 @@ function createZeroNewsRunner(options = {}) {
|
|
|
429
489
|
configured = true;
|
|
430
490
|
} else if (st && st.loginPending) {
|
|
431
491
|
// 已经发起登录、正在等确认:不重新拉起 login(会顶掉设备码),只轮询 status 让它兑换授权。
|
|
432
|
-
const loggedIn = await waitLogin(LOGIN_TIMEOUT_MS);
|
|
492
|
+
const loggedIn = await waitLogin(LOGIN_TIMEOUT_MS, gen);
|
|
433
493
|
if (gen !== generation) return status();
|
|
434
494
|
if (!loggedIn) {
|
|
435
495
|
const after = await cliStatus();
|
|
496
|
+
if (gen !== generation) return status();
|
|
436
497
|
if (after && after.loginError) throw terminalError(loginErrorHint(after.loginError));
|
|
437
498
|
if (after && after.loginPending) throw new Error('还在浏览器里等确认登录;确认完成后点「我已登录,重试」即可继续。');
|
|
438
499
|
throw new Error('还没有完成登录,请先点「开始接入」在浏览器里确认登录。');
|
|
@@ -480,11 +541,16 @@ function createZeroNewsRunner(options = {}) {
|
|
|
480
541
|
phase = 'connecting';
|
|
481
542
|
const healthDeadline = Date.now() + START_TIMEOUT_MS;
|
|
482
543
|
for (let i = 0; i < START_POLL_MAX && Date.now() < healthDeadline; i += 1) {
|
|
483
|
-
if (
|
|
544
|
+
if (gen !== generation) return status();
|
|
545
|
+
const healthy = await api('/healthz');
|
|
546
|
+
if (gen !== generation) return status();
|
|
547
|
+
if (healthy) break;
|
|
484
548
|
await sleep(1000);
|
|
485
549
|
}
|
|
486
|
-
url = await ensureTunnel(portOf());
|
|
487
550
|
if (gen !== generation) return status();
|
|
551
|
+
const resolvedUrl = await ensureTunnel(portOf(), gen);
|
|
552
|
+
if (gen !== generation) return status();
|
|
553
|
+
url = resolvedUrl;
|
|
488
554
|
if (!url) throw new Error('客户端已连上,但还没拿到隧道公网地址,请稍后刷新');
|
|
489
555
|
phase = 'connected';
|
|
490
556
|
// 隧道地址是从守护进程的本地 API 读到的:能读到就说明它活着,running 认这一格。
|
|
@@ -576,7 +642,11 @@ function createZeroNewsRunner(options = {}) {
|
|
|
576
642
|
return status();
|
|
577
643
|
}
|
|
578
644
|
|
|
579
|
-
|
|
645
|
+
/** 这次用的可执行文件:换代要拿着它下载新版、改名切换。单独给,不塞进 status——
|
|
646
|
+
* 那份会原样转发给手机,本机路径不进契约。 */
|
|
647
|
+
function binaryPath() { return binary; }
|
|
648
|
+
|
|
649
|
+
return { start, stop, status, checkAlive, pickEndpoint, endpointUrl, binaryPath };
|
|
580
650
|
}
|
|
581
651
|
|
|
582
652
|
module.exports = { DEFAULT_BASE, LOGIN_TIMEOUT_MS, START_TIMEOUT_MS, endpointUrl, pickEndpoint, createZeroNewsRunner };
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
// 2026-11-28:客户端换代:新版下到 <bin>.next,校验过版本才在空档里换上去,起不来就换回旧版。
|
|
2
|
+
// ZeroNews 客户端换代层:下载新版、验版本、就地换、可回滚。全新版绝不在老版还在跑的时候覆盖它。
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 第三方客户端自己不会升级:这台机器装的是哪一版,就一直卡在哪一版。厂商发了新版(4.1.1 起才有
|
|
7
|
+
* `tunnel set-local`、`show traffic`),旧版用户拿不到新能力,也不该让用户自己去下载安装。
|
|
8
|
+
*
|
|
9
|
+
* 做法与「蓝绿」的思路一致,只是换的那一层是**同一个文件名的可执行文件**:
|
|
10
|
+
* ① 预热:新版下到 `<bin>.next`,旁边正在跑的那份原样不动,线路一秒都不受影响;
|
|
11
|
+
* ② 验货:拿 `.next` 跑一次 `--version`,版本低于目标就当失败,直接删掉,不进入切换;
|
|
12
|
+
* ③ 切换:客户端停下来之后才动盘 —— 旧版改名 `<bin>.prev`,`.next` 改名 `<bin>`,两次 rename
|
|
13
|
+
* 在同一个文件系统里,不存在「换到一半」的中间态;真断在中间,下次启动 `recover` 会认出来;
|
|
14
|
+
* ④ 回滚:新起来没连上 / 没拿到隧道地址,就把 `.prev` 换回来、用旧版重新起,绝不让用户留在
|
|
15
|
+
* 「升完反而用不了」的状态。
|
|
16
|
+
*
|
|
17
|
+
* 所有进程调用、文件系统都可注入,守卫用假体走完整条分支。
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
const fs = require('node:fs');
|
|
21
|
+
const path = require('node:path');
|
|
22
|
+
const { execFile } = require('node:child_process');
|
|
23
|
+
|
|
24
|
+
const { VERSION, downloadClientTo } = require('./provision.cjs');
|
|
25
|
+
|
|
26
|
+
/** 新版落在这个后缀上预热,旧版仍原样在 `<bin>` 上跑。 */
|
|
27
|
+
const NEXT_SUFFIX = '.next';
|
|
28
|
+
/** 切换时旧版挪到这里,回滚用。 */
|
|
29
|
+
const PREV_SUFFIX = '.prev';
|
|
30
|
+
|
|
31
|
+
/** 从 `zeronews version zeronews 4.1.1` 里取出版本号;取不到就给空串。 */
|
|
32
|
+
function parseVersion(text) {
|
|
33
|
+
const match = /(\d+)\.(\d+)\.(\d+)/.exec(String(text || ''));
|
|
34
|
+
return match ? `${match[1]}.${match[2]}.${match[3]}` : '';
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** 只比三段数字,够用且不引依赖;读不出的版本一律算 0,别因此判成「已经是新版」。 */
|
|
38
|
+
function compareVersions(a, b) {
|
|
39
|
+
const pick = (value) => String(value || '').split('.').map((part) => Number.parseInt(part, 10) || 0);
|
|
40
|
+
const left = pick(a);
|
|
41
|
+
const right = pick(b);
|
|
42
|
+
for (let i = 0; i < 3; i += 1) {
|
|
43
|
+
const x = left[i] || 0;
|
|
44
|
+
const y = right[i] || 0;
|
|
45
|
+
if (x !== y) return x < y ? -1 : 1;
|
|
46
|
+
}
|
|
47
|
+
return 0;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** 新版 / 旧版的路径:与可执行文件同目录,rename 才落不到别的文件系统上。 */
|
|
51
|
+
function pathsOf(binary) {
|
|
52
|
+
return { next: `${binary}${NEXT_SUFFIX}`, prev: `${binary}${PREV_SUFFIX}` };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function isFile(target, fsImpl = fs) {
|
|
56
|
+
try { return fsImpl.statSync(target).isFile(); } catch { return false; }
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** 等一小会儿(改名重试用,测试可注入)。 */
|
|
60
|
+
function defaultWait(ms) { return new Promise((resolve) => setTimeout(resolve, ms)); }
|
|
61
|
+
|
|
62
|
+
function defaultRun(binary, args, opts = {}) {
|
|
63
|
+
return new Promise((resolve) => {
|
|
64
|
+
// cwd 跟客户端自己的运行目录走:`start` / `shutdown` 都按它找配置与运行状态。
|
|
65
|
+
execFile(binary, args, { timeout: opts.timeoutMs || 15_000, cwd: path.dirname(binary), maxBuffer: 1 << 20 }, (err, stdout, stderr) => {
|
|
66
|
+
const code = err ? (typeof err.code === 'number' ? err.code : 1) : 0;
|
|
67
|
+
resolve({ code, stdout: String(stdout || ''), stderr: String(stderr || '') });
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* @param {object} options
|
|
74
|
+
* @param {string} [options.desiredVersion] 目标版本,默认就是 provision 里钉的那一版。
|
|
75
|
+
* @param {(bin:string,args:string[],opts:object)=>Promise<{code:number,stdout:string,stderr:string}>} [options.run] 跑一次命令(测试注入)
|
|
76
|
+
* @param {(dest:string,options:object)=>Promise<{ok:boolean,code?:string}>} [options.download] 下载(测试注入)
|
|
77
|
+
* @param {object} [options.fsImpl] 文件系统注入
|
|
78
|
+
* @param {(message:string)=>void} [options.log]
|
|
79
|
+
*/
|
|
80
|
+
function createClientUpdater(options = {}) {
|
|
81
|
+
const desiredVersion = options.desiredVersion || VERSION;
|
|
82
|
+
const run = typeof options.run === 'function' ? options.run : defaultRun;
|
|
83
|
+
const download = typeof options.download === 'function' ? options.download : downloadClientTo;
|
|
84
|
+
const fsImpl = options.fsImpl || fs;
|
|
85
|
+
const log = typeof options.log === 'function' ? options.log : () => {};
|
|
86
|
+
const wait = typeof options.wait === 'function' ? options.wait : defaultWait;
|
|
87
|
+
|
|
88
|
+
/** 这个可执行文件自己是哪一版(读 `--version`;跑不起来就是空串)。 */
|
|
89
|
+
async function versionOf(binary) {
|
|
90
|
+
if (!binary || !isFile(binary, fsImpl)) return '';
|
|
91
|
+
try {
|
|
92
|
+
const result = await run(binary, ['--version'], { timeoutMs: 10_000 });
|
|
93
|
+
return parseVersion(`${(result && result.stdout) || ''}\n${(result && result.stderr) || ''}`);
|
|
94
|
+
} catch { return ''; }
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** 已经预热好的新版版本号;没有 `.next` 就是空串。 */
|
|
98
|
+
async function stagedVersion(binary) {
|
|
99
|
+
const { next } = pathsOf(binary);
|
|
100
|
+
if (!isFile(next, fsImpl)) return '';
|
|
101
|
+
return versionOf(next);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* 预热:下到 `<target>.next` 再验货。版本不高于基准的、验不出版本的,当场删掉,
|
|
106
|
+
* 让下一次启动仍用手里这份能跑的,而不是一个来路不明的文件。
|
|
107
|
+
*
|
|
108
|
+
* @param {string} target 最终要顶上去的可执行文件路径(`.next` 落在它旁边)
|
|
109
|
+
* @param {string} [baseline] 拿来比版本的当前版本,默认就是 target 现在的版本
|
|
110
|
+
*/
|
|
111
|
+
async function stageAt(target, baseline) {
|
|
112
|
+
if (!target) return { ok: false, version: '', error: '没有可换代的客户端路径', code: '' };
|
|
113
|
+
const next = `${target}${NEXT_SUFFIX}`;
|
|
114
|
+
const current = baseline !== undefined ? baseline : await versionOf(target);
|
|
115
|
+
let got;
|
|
116
|
+
try {
|
|
117
|
+
got = await download(next, {});
|
|
118
|
+
} catch (error) {
|
|
119
|
+
return { ok: false, version: '', error: error && error.message ? error.message : String(error), code: '' };
|
|
120
|
+
}
|
|
121
|
+
if (!got || got.ok !== true) {
|
|
122
|
+
return { ok: false, version: '', error: (got && got.code) || 'download-failed', code: (got && got.code) || '' };
|
|
123
|
+
}
|
|
124
|
+
const version = await versionOf(next);
|
|
125
|
+
if (!version) {
|
|
126
|
+
fsImpl.rmSync(next, { force: true });
|
|
127
|
+
return { ok: false, version: '', error: '新版运行不起来', code: '' };
|
|
128
|
+
}
|
|
129
|
+
if (compareVersions(version, current) <= 0) {
|
|
130
|
+
// 官方下载地址在 macOS / Windows 上不带版本号,拿回来的可能仍是同一版:不值当为它切一次。
|
|
131
|
+
fsImpl.rmSync(next, { force: true });
|
|
132
|
+
return { ok: false, version, error: 'not-newer', code: '' };
|
|
133
|
+
}
|
|
134
|
+
log(`ZeroNews 客户端新版 ${version} 已下载,等空档切换`);
|
|
135
|
+
return { ok: true, version, error: '', code: '' };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** 预热到 `<bin>.next`:旁边正在跑的那份一个字节都不动。 */
|
|
139
|
+
function stage(binary) {
|
|
140
|
+
if (!binary) return Promise.resolve({ ok: false, version: '', error: '没有可换代的客户端路径', code: '' });
|
|
141
|
+
// 本机这份都找不到就别去下载:拿回来也没法比对,只会在盘上留一个来路不明的文件。
|
|
142
|
+
if (!isFile(binary, fsImpl)) return Promise.resolve({ ok: false, version: '', error: 'no-binary', code: '' });
|
|
143
|
+
return stageAt(binary);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** 预热到插件自己的目录(那块一定可写):只读安装位置只能走这条路换版本。 */
|
|
147
|
+
function stageInto(target, baseline) {
|
|
148
|
+
return stageAt(target, baseline);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** 把 `.next` 顶到正式位置(插件目录里没人占用,直接改名即可)。 */
|
|
152
|
+
function commitStaged(target) {
|
|
153
|
+
const next = `${target}${NEXT_SUFFIX}`;
|
|
154
|
+
if (!isFile(next, fsImpl)) return { ok: false, error: 'no-staged' };
|
|
155
|
+
try {
|
|
156
|
+
if (isFile(target, fsImpl)) fsImpl.rmSync(target, { force: true });
|
|
157
|
+
fsImpl.renameSync(next, target);
|
|
158
|
+
return { ok: true, error: '' };
|
|
159
|
+
} catch (error) {
|
|
160
|
+
return { ok: false, error: error && error.message ? error.message : String(error) };
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* 换上去:`bin` → `bin.prev`,`bin.next` → `bin`。调用方必须已经停掉客户端(Windows 上
|
|
166
|
+
* 占用中的可执行文件改名会失败)。旧 `.prev` 先清掉,只留一代能回滚的。
|
|
167
|
+
*
|
|
168
|
+
* Windows 上客户端刚退出时文件句柄可能还没释放(杀毒软件也会短占),改名会报 EBUSY / EPERM:
|
|
169
|
+
* 这种情况重试几秒再放弃,别一次失败就让所有人退回旧版本。
|
|
170
|
+
*/
|
|
171
|
+
async function apply(binary) {
|
|
172
|
+
const { next, prev } = pathsOf(binary);
|
|
173
|
+
if (!isFile(binary, fsImpl) || !isFile(next, fsImpl)) return { ok: false, error: 'no-staged' };
|
|
174
|
+
try {
|
|
175
|
+
fsImpl.rmSync(prev, { force: true });
|
|
176
|
+
} catch (error) {
|
|
177
|
+
return { ok: false, error: error && error.message ? error.message : String(error) };
|
|
178
|
+
}
|
|
179
|
+
const moved = await retryRename(binary, prev);
|
|
180
|
+
if (!moved.ok) return moved;
|
|
181
|
+
const placed = await retryRename(next, binary);
|
|
182
|
+
if (!placed.ok) {
|
|
183
|
+
// 第二步没成:先把旧版挪回来,别留下一个没有可执行文件的位置。
|
|
184
|
+
await retryRename(prev, binary);
|
|
185
|
+
return { ok: false, error: placed.error };
|
|
186
|
+
}
|
|
187
|
+
return { ok: true, error: '' };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** 改名重试:句柄刚释放那一下最容易失败,等一小会儿再试,别让一次抖动定成败。 */
|
|
191
|
+
async function retryRename(from, to) {
|
|
192
|
+
const attempts = 12;
|
|
193
|
+
let last = '';
|
|
194
|
+
for (let i = 0; i < attempts; i += 1) {
|
|
195
|
+
try {
|
|
196
|
+
fsImpl.renameSync(from, to);
|
|
197
|
+
return { ok: true, error: '' };
|
|
198
|
+
} catch (error) {
|
|
199
|
+
last = error && error.message ? error.message : String(error);
|
|
200
|
+
const code = error && typeof error.code === 'string' ? error.code : '';
|
|
201
|
+
const retryable = code === 'EBUSY' || code === 'EPERM' || code === 'EACCES';
|
|
202
|
+
if (!retryable || i === attempts - 1) break;
|
|
203
|
+
await wait(200);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return { ok: false, error: last };
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** 换回来:`.prev` 顶回原位,`.next` 丢掉。 */
|
|
210
|
+
function rollback(binary) {
|
|
211
|
+
const { next, prev } = pathsOf(binary);
|
|
212
|
+
if (!isFile(prev, fsImpl)) return { ok: false, error: 'no-previous' };
|
|
213
|
+
try {
|
|
214
|
+
if (isFile(binary, fsImpl)) fsImpl.rmSync(binary, { force: true });
|
|
215
|
+
fsImpl.renameSync(prev, binary);
|
|
216
|
+
fsImpl.rmSync(next, { force: true });
|
|
217
|
+
return { ok: true, error: '' };
|
|
218
|
+
} catch (error) {
|
|
219
|
+
return { ok: false, error: error && error.message ? error.message : String(error) };
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** 切换成功、新版确实连上之后才删旧版:留着它除了占几十兆没有别的用处。 */
|
|
224
|
+
function cleanup(binary) {
|
|
225
|
+
try { fsImpl.rmSync(pathsOf(binary).prev, { force: true }); } catch { /* 删不掉不影响线路 */ }
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* 上次换代断在中间(旧版已经改名、新版还没顶上)时把旧版挪回原位。
|
|
230
|
+
* 每次启动前跑一次,宁可回到能跑的旧版,也不留一个空位。
|
|
231
|
+
* @returns {'restored'|''}
|
|
232
|
+
*/
|
|
233
|
+
function recover(binary) {
|
|
234
|
+
if (!binary) return '';
|
|
235
|
+
const { prev } = pathsOf(binary);
|
|
236
|
+
if (isFile(binary, fsImpl) || !isFile(prev, fsImpl)) return '';
|
|
237
|
+
try {
|
|
238
|
+
fsImpl.renameSync(prev, binary);
|
|
239
|
+
return 'restored';
|
|
240
|
+
} catch { return ''; }
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** 让正在跑的客户端自己退出:官方 `shutdown` 是优雅停止,比直接杀进程干净。 */
|
|
244
|
+
async function shutdown(binary) {
|
|
245
|
+
if (!binary) return '';
|
|
246
|
+
try {
|
|
247
|
+
const result = await run(binary, ['shutdown'], { timeoutMs: 10_000 });
|
|
248
|
+
return String((result && (result.stderr || result.stdout)) || '').trim();
|
|
249
|
+
} catch { return ''; }
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
return {
|
|
253
|
+
desiredVersion, versionOf, stagedVersion, stage, stageInto, commitStaged,
|
|
254
|
+
apply, rollback, cleanup, recover, shutdown, pathsOf,
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
module.exports = {
|
|
259
|
+
NEXT_SUFFIX, PREV_SUFFIX, parseVersion, compareVersions, pathsOf, createClientUpdater,
|
|
260
|
+
};
|
package/lib/zeronews/watcher.cjs
CHANGED
|
@@ -32,10 +32,10 @@ function createZeroNewsWatcher(options = {}) {
|
|
|
32
32
|
let inFlight = null;
|
|
33
33
|
|
|
34
34
|
async function refresh() {
|
|
35
|
-
if (stopped || inFlight) return;
|
|
35
|
+
if (stopped || inFlight || getState().zeroNewsEnabled === false) return;
|
|
36
36
|
inFlight = (async () => {
|
|
37
37
|
const result = await readEndpoints();
|
|
38
|
-
if (stopped) return;
|
|
38
|
+
if (stopped || getState().zeroNewsEnabled === false) return;
|
|
39
39
|
// 只有「本机 API 正常回应」才按清单更新;连不上/读失败时保留现有线路,别抖。
|
|
40
40
|
if (!result || result.ok !== true) return;
|
|
41
41
|
const url = Array.isArray(result.urls) && result.urls.length ? result.urls[0] : '';
|