@andy.ian/dsh-homing 2.1.0 → 2.1.1

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.
@@ -1,3 +1,3 @@
1
1
  #!/usr/bin/env node
2
- // 守护进程入口(真正实现在 ../restart-daemon/main.mjs)
3
- import '../restart-daemon/main.mjs'
2
+ // 守护进程入口(真正实现在 ../restart/daemon/main.mjs)
3
+ import '../restart/daemon/main.mjs'
package/bin/restart.mjs CHANGED
@@ -1,3 +1,3 @@
1
1
  #!/usr/bin/env node
2
- // 命令行入口(真正实现在 ../restart-daemon/cli.mjs)
3
- import '../restart-daemon/cli.mjs'
2
+ // 命令行入口(真正实现在 ../restart/daemon/cli.mjs)
3
+ import '../restart/daemon/cli.mjs'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@andy.ian/dsh-homing",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "type": "module",
5
5
  "description": "把本机 DSH 通过 SSH 反向隧道安全开放到手机浏览器:TLS 端到端(服务器只透传)、账号 + TOTP、局域网与公网双入口,随 DSH 启动。",
6
6
  "keywords": [
@@ -44,7 +44,6 @@
44
44
  "bin/nginx-ws-map.conf",
45
45
  "lib/*.mjs",
46
46
  "lib/*.js",
47
- "restart-daemon/**",
48
47
  "cordis.patch.yml",
49
48
  "locale/*.json",
50
49
  "icon.svg",
@@ -1,11 +1,20 @@
1
1
  # 守护进程(restart-daemon)
2
2
 
3
- DSH 崩溃/退出后负责把它重新拉起的后台进程。**不在 DSH 的进程树里**,因此 `taskkill /T` 杀不到它。
3
+ DSH 崩溃/退出后负责把它重新拉起的后台进程。
4
+
5
+ 进程关系是**单向**的,两个方向都要记住:
6
+
7
+ | 方向 | 事实 | 后果 |
8
+ |---|---|---|
9
+ | DSH → 守护进程 | 守护进程**不在** DSH 的进程树里 | `taskkill /T` 杀掉 DSH 时,守护进程不受影响(它还能把 DSH 拉回来) |
10
+ | 守护进程 → DSH | DSH **是**守护进程的子进程(由它启动) | 停止守护进程时**不能**用 `/T` 或"结束进程树",否则会把 DSH 一起杀掉 |
11
+
12
+ 停止守护进程的安全做法见下文《DSH 是它的子进程》。
4
13
 
5
14
  ## 目录结构
6
15
 
7
16
  ```
8
- restart-daemon/
17
+ restart/daemon/
9
18
  ├─ main.mjs 守护进程本体(探测、重启、升级等待、控制口)
10
19
  ├─ cli.mjs 命令行:install / start / status / stop / restart / remove
11
20
  └─ lib/
@@ -50,13 +59,40 @@ VBScript 已弃用,代码中不再生成 VBS。
50
59
  - 变更加操作必须满足:**请求来源 PID == 插件宿主 PID**(监听 8787 的进程),且该 PID 属于 DSH 进程集合
51
60
  - 外部进程调用 `/restart-now` 一律 403
52
61
 
62
+ ### DSH 是它的子进程
63
+
64
+ DSH 由守护进程启动,因此**是守护进程的子进程**。这带来一条硬性约束:
65
+
66
+ > **停止守护进程时,绝对不能用"杀进程树"的方式。**
67
+
68
+ 用 `taskkill /T /PID <守护进程PID>` 或在任务管理器里"结束进程树",
69
+ 会把正在运行的 DSH(以及它的渲染进程)一起杀掉。
70
+
71
+ | 停止方式 | 对 DSH 的影响 |
72
+ |---|---|
73
+ | `node bin/restart.mjs stop` | ✅ 安全:先 HTTP `/stop` 优雅退出,失败则按 PID 单杀 |
74
+ | `Stop-Process -Id <守护进程PID>` | ✅ 安全:只结束单个进程 |
75
+ | `process.kill(<守护进程PID>)` | ✅ 安全:同上 |
76
+ | `taskkill /T /PID <守护进程PID>` | ❌ 会连带杀掉 DSH |
77
+ | 任务管理器 → 结束进程树 | ❌ 会连带杀掉 DSH |
78
+
79
+ `cli.mjs` 的 `stopDaemonSafely()` 实现了安全停止:
80
+
81
+ 1. 请求 `HTTP /stop`,让守护进程自己退出;
82
+ 2. 失败(未授权/不可达)时读取 `~/.dsh/restart-daemon/restart-daemon.pid`,
83
+ 用 `/health` 核对 PID 一致后才 `process.kill(pid)`;
84
+ 3. PID 不一致直接拒绝,避免 pid 文件过期导致杀错进程。
85
+
86
+ 回归测试 `test/unit.daemon-stop.test.mjs` 会校验:
87
+ CLI 可执行代码不含 `taskkill /T`,且 `main.mjs` 里杀 DSH 的全部 `taskkill` 调用均为逐 PID。
88
+
53
89
  ## 常用命令
54
90
 
55
91
  ```bash
56
92
  node bin/restart.mjs install # 安装开机自启
57
93
  node bin/restart.mjs start # 启动(CIM 优先)
58
94
  node bin/restart.mjs status # 状态(含自启项校验)
59
- node bin/restart.mjs stop # 停止
95
+ node bin/restart.mjs stop # 停止(安全:不杀进程树)
60
96
  ```
61
97
 
62
98
  ## 测试
@@ -284,21 +284,66 @@ async function start() {
284
284
  async function restart() {
285
285
  try {
286
286
  const res = await call('/restart-now', 'POST', {})
287
- console.log(res.status === 202 ? ' 已请求立即重启(策略层不参与,慎用)' : ` 请求被拒绝(HTTP ${res.status})`)
287
+ if (res.status === 202) console.log(' 已请求立即重启 DSH(不经过策略层,慎用)')
288
+ else console.log(' 请求被拒绝(HTTP ' + res.status + '):重启 DSH 需要插件宿主发起,请用页面或策略接口')
288
289
  } catch {
289
290
  console.error('守护进程没在运行。先 install 或手动启动 bin/restart-daemon.mjs --detach。')
290
291
  process.exit(1)
291
292
  }
292
293
  }
293
294
 
294
- async function stop() {
295
+ /**
296
+ * 安全停止守护进程。
297
+ *
298
+ * 铁律:绝不能用"杀进程树"(taskkill /T)——
299
+ * 因为 DSH 是由守护进程启动的,是它的**子进程**;
300
+ * 杀树会把正在运行的 DSH 一起带走。
301
+ *
302
+ * 策略:
303
+ * 1. 先请求 HTTP /stop,让守护进程自己优雅退出(最安全)
304
+ * 2. 失败(未授权/不可达)时,读 pid 文件,确认该 PID 就是守护进程,再单杀该 PID
305
+ */
306
+ async function stopDaemonSafely() {
307
+ // ① 优雅退出
295
308
  try {
296
- await call('/stop')
297
- console.log(' 守护进程已退出(不会自动拉起 DSH)')
298
- } catch {
299
- console.error('守护进程没在运行')
309
+ const res = await call('/stop')
310
+ if (res.status === 200) return { ok: true, how: 'http-stop' }
311
+ } catch { /* 继续走 PID 路径 */ }
312
+
313
+ // ② 按 PID 单杀(不碰子进程)
314
+ const pidFile = path.join(process.env.DSH_HOME?.trim() || path.join(os.homedir(), '.dsh'), 'restart-daemon', 'restart-daemon.pid')
315
+ let pid = 0
316
+ try { pid = Number(fs.readFileSync(pidFile, 'utf8').trim()) } catch { /* 没有 pid 文件 */ }
317
+
318
+ // 用 /health 核对 PID,防止 pid 文件过期导致杀错进程
319
+ try {
320
+ const health = await call('/health', 'GET')
321
+ if (health.status === 200) {
322
+ const body = JSON.parse(health.body ?? '{}')
323
+ if (body.pid && body.pid !== pid) {
324
+ return { ok: false, reason: 'pid-mismatch', healthPid: body.pid, pidFilePid: pid }
325
+ }
326
+ pid = body.pid
327
+ }
328
+ } catch { /* 端口不通,继续用 pid 文件 */ }
329
+
330
+ if (!Number.isFinite(pid) || pid <= 0) return { ok: false, reason: 'no-pid' }
331
+
332
+ try {
333
+ process.kill(pid) // 只杀这一个进程,子进程(DSH)不受影响
334
+ return { ok: true, how: 'pid-kill', pid }
335
+ } catch (error) {
336
+ return { ok: false, reason: error.code ?? error.message, pid }
337
+ }
338
+ }
339
+
340
+ async function stop() {
341
+ const result = await stopDaemonSafely()
342
+ if (!result.ok) {
343
+ console.error(' 停止失败:' + (result.reason ?? '未知原因'))
300
344
  process.exit(1)
301
345
  }
346
+ console.log(' 守护进程已停止(方式:' + result.how + ';DSH 子进程不受影响)')
302
347
  }
303
348
 
304
349
  switch (command) {
File without changes