@andy.ian/dsh-homing 2.1.4 → 2.1.5
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 +42 -0
- package/SECURITY.md +90 -0
- package/bin/backup.mjs +3 -2
- package/bin/bootstrap-server.mjs +2 -1
- package/bin/renew-cert.mjs +2 -1
- package/index.js +686 -87
- package/lib/compress.mjs +29 -5
- package/lib/exec-path.mjs +85 -0
- package/lib/files-page.mjs +27 -2
- package/lib/files.mjs +3 -2
- package/lib/git.mjs +5 -2
- package/lib/guard.mjs +43 -0
- package/lib/local-socket.mjs +50 -0
- package/lib/log-file.mjs +50 -0
- package/lib/node-runtime.mjs +44 -0
- package/lib/office.mjs +133 -0
- package/lib/orientation.mjs +158 -0
- package/lib/pty.mjs +6 -3
- package/lib/secret.mjs +46 -0
- package/lib/shells.mjs +2 -1
- package/lib/term-page.mjs +2 -0
- package/lib/vendor/des-vnc.mjs +266 -0
- package/lib/vendor/novnc/LICENSE.txt +62 -0
- package/lib/vendor/novnc/core/base64.js +104 -0
- package/lib/vendor/novnc/core/crypto/aes.js +178 -0
- package/lib/vendor/novnc/core/crypto/bigint.js +34 -0
- package/lib/vendor/novnc/core/crypto/crypto.js +90 -0
- package/lib/vendor/novnc/core/crypto/des.js +330 -0
- package/lib/vendor/novnc/core/crypto/dh.js +55 -0
- package/lib/vendor/novnc/core/crypto/md5.js +82 -0
- package/lib/vendor/novnc/core/crypto/rsa.js +132 -0
- package/lib/vendor/novnc/core/decoders/copyrect.js +27 -0
- package/lib/vendor/novnc/core/decoders/h264.js +321 -0
- package/lib/vendor/novnc/core/decoders/hextile.js +181 -0
- package/lib/vendor/novnc/core/decoders/jpeg.js +161 -0
- package/lib/vendor/novnc/core/decoders/raw.js +59 -0
- package/lib/vendor/novnc/core/decoders/rre.js +44 -0
- package/lib/vendor/novnc/core/decoders/tight.js +393 -0
- package/lib/vendor/novnc/core/decoders/tightpng.js +27 -0
- package/lib/vendor/novnc/core/decoders/zlib.js +51 -0
- package/lib/vendor/novnc/core/decoders/zrle.js +185 -0
- package/lib/vendor/novnc/core/deflator.js +84 -0
- package/lib/vendor/novnc/core/display.js +578 -0
- package/lib/vendor/novnc/core/encodings.js +54 -0
- package/lib/vendor/novnc/core/inflator.js +65 -0
- package/lib/vendor/novnc/core/input/domkeytable.js +311 -0
- package/lib/vendor/novnc/core/input/fixedkeys.js +129 -0
- package/lib/vendor/novnc/core/input/gesturehandler.js +567 -0
- package/lib/vendor/novnc/core/input/keyboard.js +294 -0
- package/lib/vendor/novnc/core/input/keysym.js +616 -0
- package/lib/vendor/novnc/core/input/keysymdef.js +688 -0
- package/lib/vendor/novnc/core/input/util.js +191 -0
- package/lib/vendor/novnc/core/input/vkeys.js +116 -0
- package/lib/vendor/novnc/core/input/xtscancodes.js +173 -0
- package/lib/vendor/novnc/core/ra2.js +312 -0
- package/lib/vendor/novnc/core/rfb.js +3420 -0
- package/lib/vendor/novnc/core/util/browser.js +233 -0
- package/lib/vendor/novnc/core/util/cursor.js +249 -0
- package/lib/vendor/novnc/core/util/element.js +32 -0
- package/lib/vendor/novnc/core/util/events.js +138 -0
- package/lib/vendor/novnc/core/util/eventtarget.js +35 -0
- package/lib/vendor/novnc/core/util/int.js +15 -0
- package/lib/vendor/novnc/core/util/logging.js +56 -0
- package/lib/vendor/novnc/core/util/strings.js +28 -0
- package/lib/vendor/novnc/core/websock.js +369 -0
- package/lib/vendor/novnc/vendor/pako/LICENSE +21 -0
- package/lib/vendor/novnc/vendor/pako/README.md +6 -0
- package/lib/vendor/novnc/vendor/pako/lib/utils/common.js +45 -0
- package/lib/vendor/novnc/vendor/pako/lib/zlib/adler32.js +27 -0
- package/lib/vendor/novnc/vendor/pako/lib/zlib/constants.js +47 -0
- package/lib/vendor/novnc/vendor/pako/lib/zlib/crc32.js +36 -0
- package/lib/vendor/novnc/vendor/pako/lib/zlib/deflate.js +1846 -0
- package/lib/vendor/novnc/vendor/pako/lib/zlib/gzheader.js +35 -0
- package/lib/vendor/novnc/vendor/pako/lib/zlib/inffast.js +324 -0
- package/lib/vendor/novnc/vendor/pako/lib/zlib/inflate.js +1527 -0
- package/lib/vendor/novnc/vendor/pako/lib/zlib/inftrees.js +322 -0
- package/lib/vendor/novnc/vendor/pako/lib/zlib/messages.js +11 -0
- package/lib/vendor/novnc/vendor/pako/lib/zlib/trees.js +1195 -0
- package/lib/vendor/novnc/vendor/pako/lib/zlib/zstream.js +24 -0
- package/lib/vendor/vnc.bundle.js +14592 -0
- package/lib/vendor/ws/LICENSE +20 -0
- package/lib/vendor/ws/index.js +22 -0
- package/lib/vendor/ws/lib/buffer-util.js +142 -0
- package/lib/vendor/ws/lib/constants.js +19 -0
- package/lib/vendor/ws/lib/event-target.js +292 -0
- package/lib/vendor/ws/lib/extension.js +203 -0
- package/lib/vendor/ws/lib/limiter.js +55 -0
- package/lib/vendor/ws/lib/permessage-deflate.js +530 -0
- package/lib/vendor/ws/lib/receiver.js +743 -0
- package/lib/vendor/ws/lib/sender.js +610 -0
- package/lib/vendor/ws/lib/stream.js +161 -0
- package/lib/vendor/ws/lib/subprotocol.js +62 -0
- package/lib/vendor/ws/lib/validation.js +152 -0
- package/lib/vendor/ws/lib/websocket-server.js +562 -0
- package/lib/vendor/ws/lib/websocket.js +1426 -0
- package/lib/vendor/ws/package.json +74 -0
- package/lib/vendor/ws/wrapper.mjs +21 -0
- package/lib/vnc-app.js +567 -0
- package/lib/vnc-auth-proxy.mjs +179 -0
- package/lib/vnc-boot.js +45 -0
- package/lib/vnc-page.mjs +77 -0
- package/lib/vnc-pointer.mjs +17 -0
- package/lib/vnc.mjs +75 -0
- package/lib/window-app.js +155 -0
- package/lib/window-page.mjs +111 -0
- package/lib/window-remote.mjs +226 -0
- package/lib/ws-bridge.mjs +130 -0
- package/package.json +4 -2
- package/permissions.json +91 -0
- package/restart/client.mjs +85 -19
- package/restart/daemon/cli.mjs +55 -13
- package/restart/daemon/lib/detach.mjs +3 -2
- package/restart/daemon/lib/process-tree.mjs +17 -3
- package/restart/daemon/lib/restart-flow.mjs +36 -0
- package/restart/daemon/main.mjs +299 -80
- package/restart/engine.mjs +24 -8
- package/restart/port.mjs +88 -0
- package/restart/runner.mjs +4 -3
- package/restart/service.mjs +8 -5
- package/restart/watchdog.mjs +134 -0
package/restart/daemon/cli.mjs
CHANGED
|
@@ -11,23 +11,29 @@
|
|
|
11
11
|
* stop 让守护进程退出(不再拉起 DSH)
|
|
12
12
|
*/
|
|
13
13
|
import fs from 'node:fs'
|
|
14
|
+
import { systemExe } from '../../lib/exec-path.mjs'
|
|
14
15
|
import path from 'node:path'
|
|
15
16
|
import os from 'node:os'
|
|
16
17
|
import http from 'node:http'
|
|
17
18
|
import { spawnSync } from 'node:child_process'
|
|
18
19
|
import { fileURLToPath } from 'node:url'
|
|
19
20
|
import { startHidden, startViaCim } from './lib/detach.mjs'
|
|
21
|
+
import { isValidRestartPort, resolveRestartPort } from '../port.mjs'
|
|
20
22
|
|
|
21
23
|
const here = path.dirname(fileURLToPath(import.meta.url))
|
|
22
24
|
const pkgRoot = path.resolve(here, '..')
|
|
23
25
|
const daemonEntry = path.join(pkgRoot, 'bin', 'restart-daemon.mjs')
|
|
24
|
-
|
|
26
|
+
|
|
25
27
|
|
|
26
28
|
const has = (name) => process.argv.includes('--' + name)
|
|
27
29
|
const argValue = (name, fallback) => {
|
|
28
30
|
const idx = process.argv.indexOf(name)
|
|
29
31
|
return idx >= 0 && process.argv[idx + 1] ? process.argv[idx + 1] : fallback
|
|
30
32
|
}
|
|
33
|
+
const cliPortArg = Number(argValue('--control', ''))
|
|
34
|
+
const controlPort = isValidRestartPort(cliPortArg) ? cliPortArg : resolveRestartPort()
|
|
35
|
+
const daemonArgs = ['--detach', '--control', String(controlPort)]
|
|
36
|
+
|
|
31
37
|
const [command] = process.argv.slice(2)
|
|
32
38
|
|
|
33
39
|
function call(pathname, method = 'POST', headers = {}) {
|
|
@@ -44,6 +50,8 @@ function call(pathname, method = 'POST', headers = {}) {
|
|
|
44
50
|
})
|
|
45
51
|
}
|
|
46
52
|
|
|
53
|
+
const SCHTASKS = systemExe('schtasks')
|
|
54
|
+
const REG_EXE = systemExe('reg')
|
|
47
55
|
const TASK_NAME = 'dsh-homing-restart-daemon'
|
|
48
56
|
|
|
49
57
|
function startupPath() {
|
|
@@ -76,7 +84,7 @@ function findNodeRuntime() {
|
|
|
76
84
|
function checkStartup() {
|
|
77
85
|
if (process.platform === 'win32') {
|
|
78
86
|
try {
|
|
79
|
-
const q = spawnSync(
|
|
87
|
+
const q = spawnSync(SCHTASKS, ['/query', '/tn', TASK_NAME, '/fo', 'LIST'], { encoding: 'utf8', windowsHide: true, timeout: 10000 })
|
|
80
88
|
if (q.status === 0) {
|
|
81
89
|
const action = /任务要运行的任务|Task To Run/i.exec(q.stdout ?? '')
|
|
82
90
|
const last = /上次运行结果|Last Result/i.exec(q.stdout ?? '')
|
|
@@ -93,7 +101,7 @@ function checkStartup() {
|
|
|
93
101
|
}
|
|
94
102
|
if (process.platform === 'win32') {
|
|
95
103
|
try {
|
|
96
|
-
const q = spawnSync(
|
|
104
|
+
const q = spawnSync(REG_EXE, ['query', 'HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run', '/v', 'dsh-homing-restart-daemon'], { encoding: 'utf8', windowsHide: true, timeout: 8000 })
|
|
97
105
|
if (q.status === 0) return { exists: true, ok: true, mode: 'registry', reason: '注册表 Run(HKCU)', file: 'HKCU\\...\\Run' }
|
|
98
106
|
} catch { /* 忽略 */ }
|
|
99
107
|
}
|
|
@@ -116,7 +124,7 @@ function writeElevatedLauncher(nodePath, daemonPath) {
|
|
|
116
124
|
'@echo off',
|
|
117
125
|
'rem Right-click -> Run as administrator: create the dsh-homing restart-daemon logon task',
|
|
118
126
|
'chcp 65001 >nul',
|
|
119
|
-
'"' + nodePath + '" "' + path.join(path.dirname(daemonPath), 'restart.mjs') + '" install --task',
|
|
127
|
+
'"' + nodePath + '" "' + path.join(path.dirname(daemonPath), 'restart.mjs') + '" install --task --control ' + controlPort,
|
|
120
128
|
'echo.',
|
|
121
129
|
'pause',
|
|
122
130
|
]
|
|
@@ -147,8 +155,8 @@ function install() {
|
|
|
147
155
|
}
|
|
148
156
|
if (process.platform === 'win32') {
|
|
149
157
|
// 微软推荐:登录触发的任务计划(不需要脚本文件,也没有窗口)
|
|
150
|
-
const action = '"' + nodePath + '" "' + preferredDaemon + '"
|
|
151
|
-
const created = spawnSync(
|
|
158
|
+
const action = '"' + nodePath + '" "' + preferredDaemon + '" ' + daemonArgs.join(' ')
|
|
159
|
+
const created = spawnSync(SCHTASKS, ['/create', '/f', '/tn', TASK_NAME, '/sc', 'onlogon', '/tr', action, '/rl', 'LIMITED'], {
|
|
152
160
|
encoding: 'utf8', windowsHide: true, timeout: 15000,
|
|
153
161
|
})
|
|
154
162
|
let mode = 'task'
|
|
@@ -161,10 +169,10 @@ function install() {
|
|
|
161
169
|
process.exit(1)
|
|
162
170
|
}
|
|
163
171
|
// 兜底:注册表 Run(PowerShell 隐藏启动,不依赖 VBS)
|
|
164
|
-
const ps = 'Start-Process -FilePath "' + nodePath + '" -ArgumentList @("' + preferredDaemon
|
|
172
|
+
const ps = 'Start-Process -FilePath "' + nodePath + '" -ArgumentList @("' + [preferredDaemon, ...daemonArgs].join('","') + '") -WindowStyle Hidden'
|
|
165
173
|
const encoded = Buffer.from(ps, 'utf16le').toString('base64')
|
|
166
174
|
const runValue = 'powershell -NoProfile -WindowStyle Hidden -EncodedCommand ' + encoded
|
|
167
|
-
const reg = spawnSync(
|
|
175
|
+
const reg = spawnSync(REG_EXE, ['add', 'HKCU\\' + 'Software\\Microsoft\\Windows\\CurrentVersion\\Run', '/v', 'dsh-homing-restart-daemon', '/t', 'REG_SZ', '/d', runValue, '/f'], {
|
|
168
176
|
encoding: 'utf8', windowsHide: true, timeout: 10000,
|
|
169
177
|
})
|
|
170
178
|
if (reg.status !== 0) {
|
|
@@ -183,7 +191,7 @@ function install() {
|
|
|
183
191
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
184
192
|
<plist version="1.0"><dict>
|
|
185
193
|
<key>Label</key><string>online.gitsrc.dsh-homing</string>
|
|
186
|
-
<key>ProgramArguments</key><array><string>${nodePath}</string><string>${preferredDaemon}</string
|
|
194
|
+
<key>ProgramArguments</key><array><string>${nodePath}</string><string>${preferredDaemon}</string>${daemonArgs.map((item) => '<string>' + item + '</string>').join('')}</array>
|
|
187
195
|
<key>RunAtLoad</key><true/>
|
|
188
196
|
</dict></plist>
|
|
189
197
|
`
|
|
@@ -194,7 +202,7 @@ function install() {
|
|
|
194
202
|
Description=DSH Homing restart daemon
|
|
195
203
|
|
|
196
204
|
[Service]
|
|
197
|
-
ExecStart=${nodePath} ${preferredDaemon}
|
|
205
|
+
ExecStart=${nodePath} ${preferredDaemon} ${daemonArgs.join(' ')}
|
|
198
206
|
|
|
199
207
|
[Install]
|
|
200
208
|
WantedBy=default.target
|
|
@@ -230,7 +238,7 @@ async function status() {
|
|
|
230
238
|
|
|
231
239
|
function remove() {
|
|
232
240
|
if (process.platform === 'win32') {
|
|
233
|
-
const del = spawnSync(
|
|
241
|
+
const del = spawnSync(SCHTASKS, ['/delete', '/f', '/tn', TASK_NAME], { encoding: 'utf8', windowsHide: true, timeout: 10000 })
|
|
234
242
|
if (del.status === 0) console.log(' 已删除任务计划:' + TASK_NAME)
|
|
235
243
|
}
|
|
236
244
|
const file = startupPath()
|
|
@@ -242,6 +250,39 @@ function remove() {
|
|
|
242
250
|
}
|
|
243
251
|
}
|
|
244
252
|
|
|
253
|
+
async function stopDaemonOnOtherPort() {
|
|
254
|
+
const dir = path.join(process.env.DSH_HOME?.trim() || path.join(os.homedir(), '.dsh'), 'restart-daemon')
|
|
255
|
+
let heartbeat = null
|
|
256
|
+
try { heartbeat = JSON.parse(fs.readFileSync(path.join(dir, 'heartbeat.json'), 'utf8')) } catch { return }
|
|
257
|
+
if (!heartbeat || !heartbeat.pid || heartbeat.pid === process.pid) return
|
|
258
|
+
const age = Date.now() - Date.parse(heartbeat.ts ?? 0)
|
|
259
|
+
if (!(age >= 0 && age < 60_000)) return
|
|
260
|
+
const oldPort = Number(heartbeat.port)
|
|
261
|
+
if (!Number.isFinite(oldPort) || oldPort === controlPort) return
|
|
262
|
+
const health = await new Promise((resolve) => {
|
|
263
|
+
const request = http.request({ host: '127.0.0.1', port: oldPort, path: '/health', method: 'GET', timeout: 1500, agent: false }, (res) => {
|
|
264
|
+
const chunks = []
|
|
265
|
+
res.on('data', (chunk) => chunks.push(chunk))
|
|
266
|
+
res.on('end', () => { try { resolve(JSON.parse(Buffer.concat(chunks).toString('utf8'))) } catch { resolve(null) } })
|
|
267
|
+
})
|
|
268
|
+
request.on('timeout', () => { request.destroy(); resolve(null) })
|
|
269
|
+
request.on('error', () => resolve(null))
|
|
270
|
+
request.end()
|
|
271
|
+
})
|
|
272
|
+
if (!health || health.service !== 'dsh-homing-restart-daemon') return
|
|
273
|
+
await new Promise((resolve) => {
|
|
274
|
+
const request = http.request({ host: '127.0.0.1', port: oldPort, path: '/stop', method: 'POST', timeout: 3000, agent: false }, (res) => {
|
|
275
|
+
res.resume()
|
|
276
|
+
res.on('end', resolve)
|
|
277
|
+
})
|
|
278
|
+
request.on('timeout', () => { request.destroy(); resolve() })
|
|
279
|
+
request.on('error', resolve)
|
|
280
|
+
request.end()
|
|
281
|
+
})
|
|
282
|
+
console.log(' 端口配置已变更:已请求旧端口 ' + oldPort + ' 上的守护进程退出')
|
|
283
|
+
await new Promise((resolve) => { setTimeout(resolve, 800) })
|
|
284
|
+
}
|
|
285
|
+
|
|
245
286
|
async function start() {
|
|
246
287
|
// 已在运行就不重复启动
|
|
247
288
|
try {
|
|
@@ -252,13 +293,14 @@ async function start() {
|
|
|
252
293
|
}
|
|
253
294
|
} catch { /* 没在跑,继续启动 */ }
|
|
254
295
|
|
|
296
|
+
await stopDaemonOnOtherPort()
|
|
255
297
|
const here = path.dirname(fileURLToPath(import.meta.url))
|
|
256
298
|
const script = path.join(here, 'main.mjs')
|
|
257
299
|
// 守护进程是控制台程序:必须无窗口启动,否则会弹黑框
|
|
258
|
-
let result = startViaCim({ command: process.execPath, args: [script] })
|
|
300
|
+
let result = startViaCim({ command: process.execPath, args: [script, '--control', String(controlPort)] })
|
|
259
301
|
if (!result.ok) {
|
|
260
302
|
console.log(' CIM 启动失败(' + (result.error ?? '未知') + '),回退到 spawn-hidden')
|
|
261
|
-
result = startHidden({ command: process.execPath, args: [script], stateDir: path.join(process.env.DSH_HOME?.trim() || path.join(os.homedir(), '.dsh'), 'restart-daemon') })
|
|
303
|
+
result = startHidden({ command: process.execPath, args: [script, '--control', String(controlPort)], stateDir: path.join(process.env.DSH_HOME?.trim() || path.join(os.homedir(), '.dsh'), 'restart-daemon') })
|
|
262
304
|
}
|
|
263
305
|
if (!result.ok) {
|
|
264
306
|
console.error(' 启动失败:' + (result.error ?? '未知原因'))
|
|
@@ -23,7 +23,8 @@
|
|
|
23
23
|
import fs from 'node:fs';
|
|
24
24
|
import os from 'node:os';
|
|
25
25
|
import path from 'node:path';
|
|
26
|
-
import { spawn, spawnSync } from 'node:child_process'
|
|
26
|
+
import { spawn, spawnSync } from 'node:child_process'
|
|
27
|
+
import { powershellExe } from '../../../lib/exec-path.mjs';
|
|
27
28
|
|
|
28
29
|
/**
|
|
29
30
|
* 沿父链查找是否命中谓词。
|
|
@@ -90,7 +91,7 @@ export function startViaCim({ command, args = [], cwd }) {
|
|
|
90
91
|
try {
|
|
91
92
|
const ps = buildCimStartScript({ command, args, cwd });
|
|
92
93
|
const encoded = Buffer.from(ps, 'utf16le').toString('base64');
|
|
93
|
-
const out = spawnSync(
|
|
94
|
+
const out = spawnSync(powershellExe(), ['-NoProfile', '-NonInteractive', '-EncodedCommand', encoded], {
|
|
94
95
|
encoding: 'utf8',
|
|
95
96
|
windowsHide: true,
|
|
96
97
|
timeout: 25_000,
|
|
@@ -57,13 +57,27 @@ export function isAncestorOrSelf(ancestor, pid, parentOf) {
|
|
|
57
57
|
* - 插件宿主必须属于 DSH 进程集合
|
|
58
58
|
*/
|
|
59
59
|
export function authorizeRestart({ srcPid, hostPid, pids }) {
|
|
60
|
-
if (!hostPid) return { ok: false, reason: 'plugin-host-unknown' };
|
|
61
60
|
if (!srcPid) return { ok: false, reason: 'source-unknown' };
|
|
62
|
-
if (srcPid !== hostPid) return { ok: false, reason: 'source-not-plugin-host' };
|
|
63
61
|
if (!Array.isArray(pids) || pids.length === 0) return { ok: false, reason: 'no-dsh-processes' };
|
|
64
|
-
|
|
62
|
+
// 来源必须确实是 DSH 进程(PID 由内核 TCP 表反查,客户端无法伪造)
|
|
63
|
+
if (!pids.includes(srcPid)) return { ok: false, reason: 'source-not-dsh-process' };
|
|
64
|
+
// 8787 监听者可选校验:如果拿不到监听者,仅凭"来源 ∈ DSH"放行;
|
|
65
|
+
// 拿到时必须也属于 DSH,防止端口被无关进程占用
|
|
66
|
+
if (hostPid && !pids.includes(hostPid)) return { ok: false, reason: 'host-not-in-dsh-processes' };
|
|
65
67
|
return { ok: true, rootPid: null, treeSize: pids.length };
|
|
66
68
|
}
|
|
67
69
|
|
|
68
70
|
|
|
69
71
|
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* 校验请求来源是否属于"我们管理的 DSH 进程树"。
|
|
75
|
+
* 仅在持有有效子进程句柄(rootPid 非空)时强制生效。
|
|
76
|
+
* @returns {{ enforced: boolean, ok: boolean, reason: string }}
|
|
77
|
+
*/
|
|
78
|
+
export function isInManagedTree({ srcPid, rootPid, parentOf }) {
|
|
79
|
+
if (!rootPid) return { enforced: false, ok: true, reason: 'no-managed-child' };
|
|
80
|
+
if (!srcPid) return { enforced: true, ok: false, reason: 'source-unknown' };
|
|
81
|
+
const ok = isAncestorOrSelf(rootPid, srcPid, parentOf);
|
|
82
|
+
return { enforced: true, ok, reason: ok ? 'in-managed-tree' : 'source-not-in-managed-tree' };
|
|
83
|
+
}
|
|
@@ -109,3 +109,39 @@ export function createRestartFlow({
|
|
|
109
109
|
get inFlight() { return inFlight; },
|
|
110
110
|
};
|
|
111
111
|
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* 监视循环的决策(纯函数)
|
|
115
|
+
* @returns {{ action: 'alive'|'skip'|'relaunch', reason?: string }}
|
|
116
|
+
*/
|
|
117
|
+
export function planMonitorTick({
|
|
118
|
+
running,
|
|
119
|
+
stopping = false,
|
|
120
|
+
restarting = false,
|
|
121
|
+
launchPending = false,
|
|
122
|
+
launchFailed = false,
|
|
123
|
+
paused = false,
|
|
124
|
+
launchedAt = 0,
|
|
125
|
+
now = Date.now(),
|
|
126
|
+
graceMs = 10_000,
|
|
127
|
+
}) {
|
|
128
|
+
if (running) return { action: 'alive' };
|
|
129
|
+
if (stopping || restarting) return { action: 'skip', reason: 'busy' };
|
|
130
|
+
if (launchPending) return { action: 'skip', reason: 'launch-pending' };
|
|
131
|
+
// 刚发起启动:给新进程 10 秒的启动窗口,避免 tasklist 尚未刷新时重复拉起
|
|
132
|
+
if (launchedAt && now - launchedAt < graceMs) return { action: 'skip', reason: 'launch-grace' };
|
|
133
|
+
if (launchFailed) return { action: 'skip', reason: 'launch-failed' };
|
|
134
|
+
if (paused) return { action: 'skip', reason: 'paused' };
|
|
135
|
+
return { action: 'relaunch' };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* 是否应该把「重启次数」归零。
|
|
140
|
+
* 条件:确实有计数,且 DSH 已连续稳定运行超过 stableMs。
|
|
141
|
+
*
|
|
142
|
+
* runningSince 必须表示「本轮连续运行的起点」;否则重启后会把很久以前的
|
|
143
|
+
* 起点拿来做减法,刚重启就立刻归零(历史 bug)。
|
|
144
|
+
*/
|
|
145
|
+
export function shouldResetRestarts({ restarts, runningSince, now, stableMs = 60_000 }) {
|
|
146
|
+
return Number(restarts) > 0 && Number(runningSince) > 0 && now - runningSince > stableMs;
|
|
147
|
+
}
|