@andy.ian/dsh-homing 2.0.3 → 2.1.0
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/bin/restart-daemon.mjs +3 -0
- package/bin/restart.mjs +3 -0
- package/index.js +714 -21
- package/lib/devices.mjs +0 -1
- package/lib/files-page.mjs +0 -18
- package/lib/files.mjs +39 -27
- package/package.json +8 -9
- package/restart/client.mjs +184 -0
- package/restart/config.mjs +187 -0
- package/restart/engine.mjs +93 -0
- package/restart/face.mjs +62 -0
- package/restart/runner.mjs +184 -0
- package/restart/service.mjs +91 -0
- package/restart-daemon/README.md +66 -0
- package/restart-daemon/cli.mjs +321 -0
- package/restart-daemon/lib/detach.mjs +126 -0
- package/restart-daemon/lib/process-tree.mjs +69 -0
- package/restart-daemon/lib/restart-flow.mjs +111 -0
- package/restart-daemon/lib/update-state.mjs +72 -0
- package/restart-daemon/main.mjs +691 -0
- package/bin/supervisor.mjs +0 -488
- package/bin/watchdog.mjs +0 -144
- package/lib/vendor/node_modules/ssh2/examples/server-chat.js +0 -238
- package/lib/vendor/node_modules/ssh2/examples/sftp-server-download-only.js +0 -134
- package/lib/vendor/node_modules/ssh2/test/common.js +0 -316
- package/lib/vendor/node_modules/ssh2/test/fixtures/bad_rsa_private_key +0 -26
- package/lib/vendor/node_modules/ssh2/test/fixtures/https_cert.pem +0 -33
- package/lib/vendor/node_modules/ssh2/test/fixtures/https_key.pem +0 -52
- package/lib/vendor/node_modules/ssh2/test/fixtures/id_dsa +0 -12
- package/lib/vendor/node_modules/ssh2/test/fixtures/id_ecdsa +0 -5
- package/lib/vendor/node_modules/ssh2/test/fixtures/id_rsa +0 -15
- package/lib/vendor/node_modules/ssh2/test/fixtures/id_rsa.ppk +0 -26
- package/lib/vendor/node_modules/ssh2/test/fixtures/id_rsa_enc +0 -30
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa +0 -21
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa.pub +0 -1
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa.pub.result +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa.result +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc +0 -22
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc.pub +0 -1
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc.pub.result +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc.result +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc_gcm +0 -23
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc_gcm.pub +0 -1
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc_gcm.pub.result +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc_gcm.result +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa +0 -9
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa.pub +0 -1
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa.pub.result +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa.result +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc +0 -10
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc.pub +0 -1
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc.pub.result +0 -8
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc.result +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc_gcm +0 -10
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc_gcm.pub +0 -1
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc_gcm.pub.result +0 -8
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc_gcm.result +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ed25519 +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ed25519.pub +0 -1
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ed25519.pub.result +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ed25519.result +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa +0 -27
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa.pub +0 -1
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa.pub.result +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa.result +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc +0 -28
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc.pub +0 -1
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc.pub.result +0 -8
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc.result +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc_gcm +0 -29
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc_gcm.pub +0 -1
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc_gcm.pub.result +0 -8
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc_gcm.result +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa +0 -12
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa.pub +0 -1
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa.pub.result +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa.result +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa_enc +0 -15
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa_enc.pub +0 -1
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa_enc.pub.result +0 -8
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa_enc.result +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa +0 -5
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa.pub +0 -1
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa.pub.result +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa.result +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa_enc +0 -8
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa_enc.pub +0 -1
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa_enc.pub.result +0 -8
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa_enc.result +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa +0 -27
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa.pub +0 -1
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa.pub.result +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa.result +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc +0 -30
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc.pub +0 -1
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc.pub.result +0 -8
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc.result +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc_aes256 +0 -54
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc_aes256.pub +0 -1
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc_aes256.pub.result +0 -6
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc_aes256.result +0 -6
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_dsa_enc +0 -17
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_dsa_enc.result +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_rsa +0 -26
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_rsa.result +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_rsa_enc +0 -18
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_rsa_enc.result +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa.pub +0 -9
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa.pub.result +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa2.pub +0 -10
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa2.pub.result +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa3.pub +0 -11
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa3.pub.result +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa4.pub +0 -11
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa4.pub.result +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa5.pub +0 -8
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa5.pub.result +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa6.pub +0 -13
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa6.pub.result +0 -7
- package/lib/vendor/node_modules/ssh2/test/fixtures/openssh_new_rsa +0 -27
- package/lib/vendor/node_modules/ssh2/test/fixtures/ssh_host_dsa_key +0 -12
- package/lib/vendor/node_modules/ssh2/test/fixtures/ssh_host_ecdsa_key +0 -5
- package/lib/vendor/node_modules/ssh2/test/fixtures/ssh_host_rsa_key +0 -15
- package/lib/vendor/node_modules/ssh2/test/test-exec.js +0 -578
- package/lib/vendor/node_modules/ssh2/test/test-integration-openssh.js +0 -486
- package/lib/vendor/node_modules/ssh2/test/test-keygen.js +0 -60
- package/lib/vendor/node_modules/ssh2/test/test-misc-client-server.js +0 -1460
- package/lib/vendor/node_modules/ssh2/test/test-openssh.js +0 -261
- package/lib/vendor/node_modules/ssh2/test/test-protocol-crypto.js +0 -631
- package/lib/vendor/node_modules/ssh2/test/test-protocol-keyparser.js +0 -177
- package/lib/vendor/node_modules/ssh2/test/test-server-hostkeys.js +0 -138
- package/lib/vendor/node_modules/ssh2/test/test-sftp.js +0 -842
- package/lib/vendor/node_modules/ssh2/test/test-shell.js +0 -109
- package/lib/vendor/node_modules/ssh2/test/test-userauth-agent-openssh.js +0 -110
- package/lib/vendor/node_modules/ssh2/test/test-userauth-agent.js +0 -171
- package/lib/vendor/node_modules/ssh2/test/test-userauth.js +0 -611
- package/lib/vendor/node_modules/ssh2/test/test-worker-imports.js +0 -25
- package/lib/vendor/node_modules/ssh2/test/test.js +0 -20
package/bin/supervisor.mjs
DELETED
|
@@ -1,488 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* DSH 常驻守护进程(模型 B)
|
|
4
|
-
*
|
|
5
|
-
* 为什么要独立进程:插件跑在 DSH 里,不能重启 DSH(会把自己杀掉)。
|
|
6
|
-
* 相比「系统定时任务每 5 分钟轮询」,常驻进程的优势:
|
|
7
|
-
* - 自己拉起的子进程:child.on('exit') 瞬时感知
|
|
8
|
-
* - 外部拉起的进程:2 秒轻量轮询(进程列表 + 端口),开销 < 0.05% CPU
|
|
9
|
-
* - 纯 Node,一份代码跨平台;只有「怎么开机启动它」分平台
|
|
10
|
-
* - 无控制台窗口,不存在闪黑窗
|
|
11
|
-
*
|
|
12
|
-
* 已修的关键缺陷:
|
|
13
|
-
* ① 单实例:控制端口被占用即退出,避免两个守护互相抢着拉起
|
|
14
|
-
* ② 重启互斥:并发请求只执行一次,避免双实例
|
|
15
|
-
* ③ 主动退出(退出码 0)不自动拉起,否则用户关不掉程序
|
|
16
|
-
* ④ default 模式等待空闲超时 → 放弃本次重启,不再硬打断用户
|
|
17
|
-
* ⑤ 稳定运行 60 秒后把退避计数归零
|
|
18
|
-
* ⑥ 找不到 exe 时只记一次日志并停止自动拉起
|
|
19
|
-
* ⑦ 用代际号忽略旧子进程的迟到 exit 事件
|
|
20
|
-
* ⑫ 空闲判定同时看 activity.json 与 sessions/*.json
|
|
21
|
-
*
|
|
22
|
-
* 用法:
|
|
23
|
-
* node bin/supervisor.mjs 前台运行(调试)
|
|
24
|
-
* node bin/supervisor.mjs --detach 后台常驻(配合启动项)
|
|
25
|
-
* node bin/supervisor.mjs --status 查看状态后退出
|
|
26
|
-
* node bin/supervisor.mjs --restart [--immediate] 请求重启
|
|
27
|
-
*
|
|
28
|
-
* 控制接口:
|
|
29
|
-
* GET http://127.0.0.1:8790/status
|
|
30
|
-
* POST http://127.0.0.1:8790/restart?mode=immediate|default
|
|
31
|
-
*/
|
|
32
|
-
import fs from 'node:fs'
|
|
33
|
-
import net from 'node:net'
|
|
34
|
-
import os from 'node:os'
|
|
35
|
-
import path from 'node:path'
|
|
36
|
-
import http from 'node:http'
|
|
37
|
-
import { spawn, spawnSync } from 'node:child_process'
|
|
38
|
-
|
|
39
|
-
// ---------- 参数 ----------
|
|
40
|
-
function arg(name, fallback = '') {
|
|
41
|
-
const index = process.argv.indexOf('--' + name)
|
|
42
|
-
if (index < 0) return fallback
|
|
43
|
-
const next = process.argv[index + 1]
|
|
44
|
-
return next && !next.startsWith('--') ? next : 'true'
|
|
45
|
-
}
|
|
46
|
-
const has = (name) => process.argv.includes('--' + name)
|
|
47
|
-
|
|
48
|
-
const dshHome = process.env.DSH_HOME ?? path.join(os.homedir(), '.dsh')
|
|
49
|
-
const stateDir = arg('state', path.join(dshHome, 'watchdog'))
|
|
50
|
-
const controlPort = Number(arg('control', '8790'))
|
|
51
|
-
const idleSeconds = Number(arg('idle', '120'))
|
|
52
|
-
const bridgePort = Number(arg('bridge', '8787'))
|
|
53
|
-
const pollMs = Number(arg('poll', '2000'))
|
|
54
|
-
const logFile = path.join(stateDir, 'supervisor.log')
|
|
55
|
-
const stateFile = path.join(stateDir, 'supervisor.json')
|
|
56
|
-
const pidFile = path.join(stateDir, 'supervisor.pid')
|
|
57
|
-
|
|
58
|
-
fs.mkdirSync(stateDir, { recursive: true })
|
|
59
|
-
|
|
60
|
-
function log(message) {
|
|
61
|
-
const line = `${new Date().toISOString()} ${message}\n`
|
|
62
|
-
try { fs.appendFileSync(logFile, line) } catch { /* 日志写不了不影响主流程 */ }
|
|
63
|
-
if (!has('detach')) process.stdout.write(line)
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// ---------- 找 DSH 可执行文件(唯一真正分平台的部分) ----------
|
|
67
|
-
function findDshExe() {
|
|
68
|
-
const fromArg = arg('exe')
|
|
69
|
-
if (fromArg && fromArg !== 'true' && fs.existsSync(fromArg)) return fromArg
|
|
70
|
-
if (process.platform === 'win32') {
|
|
71
|
-
const candidates = [
|
|
72
|
-
path.join(process.env.LOCALAPPDATA ?? '', 'Programs', 'DeepSeek Harness', 'DeepSeek Harness.exe'),
|
|
73
|
-
path.join(process.env.ProgramFiles ?? '', 'DeepSeek Harness', 'DeepSeek Harness.exe'),
|
|
74
|
-
path.join(process.env['ProgramFiles(x86)'] ?? '', 'DeepSeek Harness', 'DeepSeek Harness.exe'),
|
|
75
|
-
path.join(process.env.ProgramData ?? '', 'DeepSeek Harness', 'DeepSeek Harness.exe'),
|
|
76
|
-
]
|
|
77
|
-
return candidates.find((item) => item && fs.existsSync(item)) ?? null
|
|
78
|
-
}
|
|
79
|
-
const candidates = process.platform === 'darwin'
|
|
80
|
-
? ['/Applications/DeepSeek Harness.app/Contents/MacOS/DeepSeek Harness']
|
|
81
|
-
: ['/usr/local/bin/deepseek-harness', '/usr/bin/deepseek-harness']
|
|
82
|
-
return candidates.find((item) => fs.existsSync(item)) ?? null
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const exePath = findDshExe()
|
|
86
|
-
const exeName = exePath ? path.basename(exePath) : 'DeepSeek Harness.exe'
|
|
87
|
-
const procName = path.basename(exeName, path.extname(exeName))
|
|
88
|
-
|
|
89
|
-
// ---------- 进程发现与探测 ----------
|
|
90
|
-
function listDshPids() {
|
|
91
|
-
try {
|
|
92
|
-
if (process.platform === 'win32') {
|
|
93
|
-
const out = spawnSync('tasklist', ['/FI', `IMAGENAME eq ${exeName}`, '/FO', 'CSV', '/NH'], {
|
|
94
|
-
encoding: 'utf8', windowsHide: true, timeout: 5000,
|
|
95
|
-
})
|
|
96
|
-
return String(out.stdout ?? '')
|
|
97
|
-
.split(/\r?\n/u)
|
|
98
|
-
.map((line) => line.match(/^"([^"]+)","(\d+)"/u))
|
|
99
|
-
.filter(Boolean)
|
|
100
|
-
.map((match) => Number(match[2]))
|
|
101
|
-
}
|
|
102
|
-
const out = spawnSync('ps', ['-eo', 'pid=,comm='], { encoding: 'utf8', timeout: 5000 })
|
|
103
|
-
return String(out.stdout ?? '')
|
|
104
|
-
.split(/\n/u)
|
|
105
|
-
.map((line) => line.trim().split(/\s+/u))
|
|
106
|
-
.filter((parts) => parts.length >= 2 && parts.slice(1).join(' ').includes(procName))
|
|
107
|
-
.map((parts) => Number(parts[0]))
|
|
108
|
-
} catch {
|
|
109
|
-
return []
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function portOpen(port) {
|
|
114
|
-
return new Promise((resolve) => {
|
|
115
|
-
const socket = net.connect({ host: '127.0.0.1', port, timeout: 800 })
|
|
116
|
-
socket.once('connect', () => { socket.destroy(); resolve(true) })
|
|
117
|
-
socket.once('timeout', () => { socket.destroy(); resolve(false) })
|
|
118
|
-
socket.once('error', () => resolve(false))
|
|
119
|
-
})
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
async function probe() {
|
|
123
|
-
const pids = listDshPids()
|
|
124
|
-
return { pids, bridge: await portOpen(bridgePort), running: pids.length > 0 }
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// ---------- 空闲判定:活动信标 + 会话文件 ----------
|
|
128
|
-
function idleAge() {
|
|
129
|
-
const files = [path.join(dshHome, 'integrations', 'dsh-homing', 'activity.json')]
|
|
130
|
-
try {
|
|
131
|
-
const sessions = path.join(dshHome, 'sessions')
|
|
132
|
-
for (const name of fs.readdirSync(sessions)) {
|
|
133
|
-
if (name.endsWith('.json')) files.push(path.join(sessions, name))
|
|
134
|
-
}
|
|
135
|
-
} catch { /* 没有 sessions 目录就只靠信标 */ }
|
|
136
|
-
let newest = 0
|
|
137
|
-
for (const file of files) {
|
|
138
|
-
try { newest = Math.max(newest, fs.statSync(file).mtimeMs) } catch { /* 跳过 */ }
|
|
139
|
-
}
|
|
140
|
-
return newest === 0 ? Infinity : (Date.now() - newest) / 1000
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// ---------- 运行状态 ----------
|
|
144
|
-
let child = null
|
|
145
|
-
let childGeneration = 0
|
|
146
|
-
let restarts = 0
|
|
147
|
-
let stoppingForRestart = false
|
|
148
|
-
let restarting = false
|
|
149
|
-
let launchFailed = false
|
|
150
|
-
const startedAt = new Date()
|
|
151
|
-
|
|
152
|
-
function persist(extra = {}) {
|
|
153
|
-
try {
|
|
154
|
-
fs.writeFileSync(stateFile, JSON.stringify({
|
|
155
|
-
pid: process.pid,
|
|
156
|
-
exe: exePath,
|
|
157
|
-
controlPort,
|
|
158
|
-
bridgePort,
|
|
159
|
-
idleSeconds,
|
|
160
|
-
restarts,
|
|
161
|
-
childPid: child?.pid ?? null,
|
|
162
|
-
platform: process.platform,
|
|
163
|
-
startedAt: startedAt.toISOString(),
|
|
164
|
-
updatedAt: new Date().toISOString(),
|
|
165
|
-
...extra,
|
|
166
|
-
}, null, 2))
|
|
167
|
-
} catch { /* 忽略 */ }
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
// ---------- 升级检测 ----------
|
|
171
|
-
// 规则:用户手动退出也要拉起;但 DSH 升级期间不能拉起(否则会拉起半装好的版本),
|
|
172
|
-
// 等升级完成后再拉起。三条信号联合判断「正在升级」:
|
|
173
|
-
// ① 更新器进程在跑 ② 可执行文件不存在 ③ 文件签名刚刚变过
|
|
174
|
-
const UPDATER_HINTS = ['setup', 'update', 'installer', 'squirrel', 'nsis']
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* 判断某个进程名是不是「DSH 的更新器」。
|
|
178
|
-
* 不能只看 installer/update 关键字——系统里 wslinstaller.exe 之类也会命中,
|
|
179
|
-
* 一旦误判就会永远认为"正在升级"、永远不拉起。
|
|
180
|
-
*/
|
|
181
|
-
function isDshUpdater(name) {
|
|
182
|
-
const lower = name.toLowerCase()
|
|
183
|
-
if (lower.includes('deepseek')) {
|
|
184
|
-
return UPDATER_HINTS.some((hint) => lower.includes(hint)) || lower.includes('unins')
|
|
185
|
-
}
|
|
186
|
-
return /^(update|squirrel|electron-updater)\.exe$/u.test(lower)
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
function exeSignature() {
|
|
190
|
-
try {
|
|
191
|
-
const stat = fs.statSync(exePath)
|
|
192
|
-
return `${Math.round(stat.mtimeMs)}:${stat.size}`
|
|
193
|
-
} catch {
|
|
194
|
-
return ''
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
function updaterRunning() {
|
|
199
|
-
try {
|
|
200
|
-
if (process.platform === 'win32') {
|
|
201
|
-
const out = spawnSync('tasklist', ['/FO', 'CSV', '/NH'], { encoding: 'utf8', windowsHide: true, timeout: 5000 })
|
|
202
|
-
return String(out.stdout ?? '')
|
|
203
|
-
.split(/\r?\n/u)
|
|
204
|
-
.map((line) => line.match(/^"([^"]+)"/u))
|
|
205
|
-
.filter(Boolean)
|
|
206
|
-
.map((match) => match[1])
|
|
207
|
-
.some((name) => isDshUpdater(name))
|
|
208
|
-
}
|
|
209
|
-
const out = spawnSync('ps', ['-eo', 'comm='], { encoding: 'utf8', timeout: 5000 })
|
|
210
|
-
return String(out.stdout ?? '')
|
|
211
|
-
.split(/\n/u)
|
|
212
|
-
.map((line) => line.trim())
|
|
213
|
-
.some((name) => isDshUpdater(name))
|
|
214
|
-
} catch {
|
|
215
|
-
return false
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
* 等升级结束。返回 true 表示可以安全拉起。
|
|
221
|
-
* 判据:文件存在 + 没有更新器在跑 + 文件签名 8 秒内没变。
|
|
222
|
-
*/
|
|
223
|
-
async function waitForUpgrade(maxMinutes = 30) {
|
|
224
|
-
const deadline = Date.now() + maxMinutes * 60_000
|
|
225
|
-
let signature = exeSignature()
|
|
226
|
-
let stableSince = Date.now()
|
|
227
|
-
let announced = false
|
|
228
|
-
while (Date.now() < deadline) {
|
|
229
|
-
const exists = signature !== ''
|
|
230
|
-
const updating = updaterRunning()
|
|
231
|
-
const current = exeSignature()
|
|
232
|
-
if (current !== signature) {
|
|
233
|
-
signature = current
|
|
234
|
-
stableSince = Date.now()
|
|
235
|
-
}
|
|
236
|
-
const stable = Date.now() - stableSince > 8000
|
|
237
|
-
if (!exists || updating || !stable) {
|
|
238
|
-
if (!announced) {
|
|
239
|
-
announced = true
|
|
240
|
-
log(`检测到升级中(存在=${exists} 更新器=${updating} 稳定=${stable}),等升级完成再拉起`)
|
|
241
|
-
}
|
|
242
|
-
await new Promise((resolve) => setTimeout(resolve, 2000))
|
|
243
|
-
continue
|
|
244
|
-
}
|
|
245
|
-
if (announced) log('升级已完成,开始拉起 DSH')
|
|
246
|
-
return true
|
|
247
|
-
}
|
|
248
|
-
log(`等待升级超时(${maxMinutes} 分钟),仍尝试拉起一次`)
|
|
249
|
-
return true
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
function launch() {
|
|
253
|
-
if (!exePath) {
|
|
254
|
-
if (!launchFailed) {
|
|
255
|
-
launchFailed = true
|
|
256
|
-
log('找不到 DSH 可执行文件,停止自动拉起(用 --exe 指定或检查安装位置)')
|
|
257
|
-
}
|
|
258
|
-
return null
|
|
259
|
-
}
|
|
260
|
-
launchFailed = false
|
|
261
|
-
log(`启动 DSH:${exePath}`)
|
|
262
|
-
const generation = ++childGeneration
|
|
263
|
-
const spawned = spawn(exePath, [], { stdio: 'ignore', windowsHide: false })
|
|
264
|
-
spawned.on('error', (error) => {
|
|
265
|
-
log(`启动失败:${error.code ?? error.message}`)
|
|
266
|
-
launchFailed = true
|
|
267
|
-
})
|
|
268
|
-
spawned.on('exit', (code, signal) => {
|
|
269
|
-
if (generation !== childGeneration) return // 旧子进程的迟到事件,忽略
|
|
270
|
-
log(`DSH 退出:code=${code} signal=${signal}${stoppingForRestart ? '(主动重启)' : ''}`)
|
|
271
|
-
child = null
|
|
272
|
-
if (stoppingForRestart) return
|
|
273
|
-
// 无论用户主动关闭还是崩溃都重新拉起;若检测到升级中,等升级完成再拉起
|
|
274
|
-
restarts += 1
|
|
275
|
-
const delay = Math.min(30_000, 1000 * restarts)
|
|
276
|
-
log(`${delay}ms 后重新拉起(第 ${restarts} 次)`)
|
|
277
|
-
setTimeout(async () => {
|
|
278
|
-
if (child) return
|
|
279
|
-
await waitForUpgrade()
|
|
280
|
-
if (pausedUntil > Date.now()) { log('暂停中,不拉起 DSH'); return }
|
|
281
|
-
if (!child) { child = launch(); persist() }
|
|
282
|
-
}, delay)
|
|
283
|
-
})
|
|
284
|
-
// 稳定运行 60 秒后把退避计数归零,避免一次崩溃永久 30 秒退避
|
|
285
|
-
const timer = setTimeout(() => {
|
|
286
|
-
if (generation === childGeneration && child) restarts = 0
|
|
287
|
-
}, 60_000)
|
|
288
|
-
timer.unref?.()
|
|
289
|
-
return spawned
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
async function killDsh() {
|
|
293
|
-
const pids = listDshPids()
|
|
294
|
-
if (pids.length === 0) return
|
|
295
|
-
log(`终止 DSH 进程:${pids.join(',')}`)
|
|
296
|
-
const parents = new Map()
|
|
297
|
-
try {
|
|
298
|
-
if (process.platform === 'win32') {
|
|
299
|
-
const script = `Get-CimInstance Win32_Process -Filter "Name='${exeName}'" | ForEach-Object { "$($_.ProcessId),$($_.ParentProcessId)" }`
|
|
300
|
-
const out = spawnSync('powershell', ['-NoProfile', '-Command', script], { encoding: 'utf8', windowsHide: true, timeout: 8000 })
|
|
301
|
-
for (const line of String(out.stdout ?? '').split(/\r?\n/u)) {
|
|
302
|
-
const [pid, ppid] = line.trim().split(',').map(Number)
|
|
303
|
-
if (pid) parents.set(pid, ppid)
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
} catch { /* 拿不到父子关系就全杀 */ }
|
|
307
|
-
// 先杀主进程(父进程不在集合内者),否则 Electron 会把 Host 退出当崩溃弹原生对话框
|
|
308
|
-
for (const pid of pids.filter((item) => !pids.includes(parents.get(item)))) {
|
|
309
|
-
try { process.kill(pid) } catch { /* 已退出 */ }
|
|
310
|
-
log(` 先终止主进程 ${pid}`)
|
|
311
|
-
}
|
|
312
|
-
const deadline = Date.now() + 10_000
|
|
313
|
-
while (Date.now() < deadline && listDshPids().length > 0) {
|
|
314
|
-
await new Promise((resolve) => setTimeout(resolve, 300))
|
|
315
|
-
}
|
|
316
|
-
for (const pid of listDshPids()) {
|
|
317
|
-
try {
|
|
318
|
-
if (process.platform === 'win32') spawnSync('taskkill', ['/F', '/PID', String(pid)], { windowsHide: true })
|
|
319
|
-
else process.kill(pid, 'SIGKILL')
|
|
320
|
-
} catch { /* 已退出 */ }
|
|
321
|
-
}
|
|
322
|
-
const portDeadline = Date.now() + 60_000
|
|
323
|
-
while (Date.now() < portDeadline && await portOpen(bridgePort)) {
|
|
324
|
-
await new Promise((resolve) => setTimeout(resolve, 500))
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
async function waitIdle(seconds) {
|
|
329
|
-
const deadline = Date.now() + seconds * 1000
|
|
330
|
-
while (Date.now() < deadline) {
|
|
331
|
-
if (idleAge() >= idleSeconds) return true
|
|
332
|
-
await new Promise((resolve) => setTimeout(resolve, 1000))
|
|
333
|
-
}
|
|
334
|
-
return false
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
async function restart(mode = 'default') {
|
|
338
|
-
if (restarting) { log('已有重启在进行,忽略本次请求'); return { skipped: true } }
|
|
339
|
-
restarting = true
|
|
340
|
-
try {
|
|
341
|
-
log(`收到重启请求(模式 ${mode})`)
|
|
342
|
-
if (mode !== 'immediate') {
|
|
343
|
-
const ok = await waitIdle(idleSeconds + 30)
|
|
344
|
-
if (!ok) {
|
|
345
|
-
log('用户仍在活跃,放弃本次重启(不再硬打断)')
|
|
346
|
-
return { ok: false, reason: 'busy' }
|
|
347
|
-
}
|
|
348
|
-
} else {
|
|
349
|
-
await new Promise((resolve) => setTimeout(resolve, 3000))
|
|
350
|
-
}
|
|
351
|
-
stoppingForRestart = true
|
|
352
|
-
await killDsh()
|
|
353
|
-
child = launch()
|
|
354
|
-
persist()
|
|
355
|
-
return { ok: true }
|
|
356
|
-
} finally {
|
|
357
|
-
stoppingForRestart = false
|
|
358
|
-
restarting = false
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
// ---------- 控制接口 ----------
|
|
363
|
-
// pausedUntil > 0 时暂停自动拉起(给用户关机/维护用)
|
|
364
|
-
let pausedUntil = 0
|
|
365
|
-
|
|
366
|
-
function cors(res) {
|
|
367
|
-
// 客户端插件运行在 DSH 页面里(http://127.0.0.1:<随机端口>),需要允许跨端口调用
|
|
368
|
-
res.setHeader('access-control-allow-origin', '*')
|
|
369
|
-
res.setHeader('access-control-allow-methods', 'GET, POST, OPTIONS')
|
|
370
|
-
res.setHeader('access-control-allow-headers', 'content-type')
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
function startControl() {
|
|
374
|
-
const server = http.createServer(async (req, res) => {
|
|
375
|
-
const url = new URL(req.url ?? '/', 'http://127.0.0.1')
|
|
376
|
-
cors(res)
|
|
377
|
-
if (req.method === 'OPTIONS') { res.writeHead(204); res.end(); return }
|
|
378
|
-
const paused = pausedUntil > Date.now()
|
|
379
|
-
if (url.pathname === '/status') {
|
|
380
|
-
const info = await probe()
|
|
381
|
-
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8' })
|
|
382
|
-
res.end(JSON.stringify({
|
|
383
|
-
supervisor: { pid: process.pid, restarts, restarting, exe: exePath },
|
|
384
|
-
...info,
|
|
385
|
-
idleSeconds: Math.round(idleAge()),
|
|
386
|
-
paused,
|
|
387
|
-
pausedSecondsLeft: paused ? Math.round((pausedUntil - Date.now()) / 1000) : 0,
|
|
388
|
-
}, null, 2))
|
|
389
|
-
return
|
|
390
|
-
}
|
|
391
|
-
if (url.pathname === '/pause') {
|
|
392
|
-
const minutes = Number(url.searchParams.get('minutes') ?? '30')
|
|
393
|
-
pausedUntil = Date.now() + Math.max(1, minutes) * 60_000
|
|
394
|
-
log(`暂停自动拉起 ${minutes} 分钟(手动重启仍可用)`)
|
|
395
|
-
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8' })
|
|
396
|
-
res.end(JSON.stringify({ paused: true, minutes }))
|
|
397
|
-
return
|
|
398
|
-
}
|
|
399
|
-
if (url.pathname === '/resume') {
|
|
400
|
-
pausedUntil = 0
|
|
401
|
-
log('恢复自动拉起')
|
|
402
|
-
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8' })
|
|
403
|
-
res.end(JSON.stringify({ paused: false }))
|
|
404
|
-
return
|
|
405
|
-
}
|
|
406
|
-
if (url.pathname === '/stop') {
|
|
407
|
-
log('收到停止指令:守护进程退出,不再拉起 DSH')
|
|
408
|
-
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8' })
|
|
409
|
-
res.end(JSON.stringify({ stopped: true }))
|
|
410
|
-
setTimeout(() => process.exit(0), 300)
|
|
411
|
-
return
|
|
412
|
-
}
|
|
413
|
-
if (url.pathname === '/restart') {
|
|
414
|
-
const mode = url.searchParams.get('mode') === 'immediate' ? 'immediate' : 'default'
|
|
415
|
-
res.writeHead(202, { 'content-type': 'application/json; charset=utf-8' })
|
|
416
|
-
res.end(JSON.stringify({ accepted: true, mode }))
|
|
417
|
-
void restart(mode)
|
|
418
|
-
return
|
|
419
|
-
}
|
|
420
|
-
res.writeHead(404, { 'content-type': 'text/plain; charset=utf-8' })
|
|
421
|
-
res.end('not found\n')
|
|
422
|
-
})
|
|
423
|
-
server.on('error', (error) => {
|
|
424
|
-
if (error.code === 'EADDRINUSE') {
|
|
425
|
-
log('控制端口已被占用,说明已有守护进程在运行;本实例退出(单实例保护)')
|
|
426
|
-
process.exit(0)
|
|
427
|
-
}
|
|
428
|
-
log(`控制端口错误:${error.code}`)
|
|
429
|
-
process.exit(1)
|
|
430
|
-
})
|
|
431
|
-
server.listen(controlPort, '127.0.0.1', () => log(`控制接口:http://127.0.0.1:${controlPort}/status`))
|
|
432
|
-
return server
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
// ---------- 主流程 ----------
|
|
436
|
-
if (has('status')) {
|
|
437
|
-
const info = await probe()
|
|
438
|
-
console.log(JSON.stringify({
|
|
439
|
-
exe: exePath,
|
|
440
|
-
...info,
|
|
441
|
-
idleSeconds: Math.round(idleAge()),
|
|
442
|
-
stateFile,
|
|
443
|
-
logFile,
|
|
444
|
-
}, null, 2))
|
|
445
|
-
process.exit(0)
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
if (has('restart')) {
|
|
449
|
-
const mode = has('immediate') ? 'immediate' : 'default'
|
|
450
|
-
const request = http.request({ host: '127.0.0.1', port: controlPort, path: `/restart?mode=${mode}`, method: 'POST' }, (res) => {
|
|
451
|
-
res.resume()
|
|
452
|
-
console.log(`守护进程已接受重启请求(${mode})→ 日志:${logFile}`)
|
|
453
|
-
process.exit(0)
|
|
454
|
-
})
|
|
455
|
-
request.on('error', () => {
|
|
456
|
-
console.error('守护进程没在运行(控制端口连不上)。先启动它,或直接重启 DSH。')
|
|
457
|
-
process.exit(1)
|
|
458
|
-
})
|
|
459
|
-
request.end()
|
|
460
|
-
} else {
|
|
461
|
-
fs.writeFileSync(pidFile, String(process.pid))
|
|
462
|
-
log(`守护进程启动:pid=${process.pid} 平台=${process.platform} exe=${exePath ?? '未找到'}`)
|
|
463
|
-
const cleanup = () => { try { fs.rmSync(pidFile, { force: true }) } catch { /* 忽略 */ } }
|
|
464
|
-
process.on('SIGINT', () => { log('收到 SIGINT,退出'); cleanup(); process.exit(0) })
|
|
465
|
-
process.on('SIGTERM', () => { log('收到 SIGTERM,退出'); cleanup(); process.exit(0) })
|
|
466
|
-
process.on('exit', cleanup)
|
|
467
|
-
|
|
468
|
-
startControl()
|
|
469
|
-
const info = await probe()
|
|
470
|
-
if (info.running) {
|
|
471
|
-
log(`DSH 已在运行(PID ${info.pids.join(',')}),接管监视(${pollMs / 1000} 秒轮询)`)
|
|
472
|
-
setInterval(async () => {
|
|
473
|
-
if (stoppingForRestart || restarting || launchFailed) return
|
|
474
|
-
const current = await probe()
|
|
475
|
-
if (!current.running) {
|
|
476
|
-
restarts += 1
|
|
477
|
-
log('检测到 DSH 已退出(外部拉起),准备拉起')
|
|
478
|
-
await waitForUpgrade()
|
|
479
|
-
if (pausedUntil > Date.now()) { log('暂停中,不拉起 DSH'); return }
|
|
480
|
-
if (!child) { child = launch(); persist() }
|
|
481
|
-
}
|
|
482
|
-
}, pollMs)
|
|
483
|
-
} else {
|
|
484
|
-
log('未发现运行中的 DSH,等待(可能是升级中)后拉起')
|
|
485
|
-
await waitForUpgrade()
|
|
486
|
-
if (pausedUntil > Date.now()) { log('暂停中,不拉起 DSH') } else { child = launch(); persist() }
|
|
487
|
-
}
|
|
488
|
-
}
|
package/bin/watchdog.mjs
DELETED
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* 守护进程的安装 / 状态 / 卸载命令(纯 Node,无 PowerShell、无计划任务)。
|
|
4
|
-
*
|
|
5
|
-
* 守护逻辑在 bin/supervisor.mjs:常驻、监视 DSH、崩溃自动拉起、升级期间等待。
|
|
6
|
-
* 本命令只负责入口管理:
|
|
7
|
-
* install 写入「登录时启动守护进程」(Win 启动文件夹 / macOS LaunchAgent / Linux systemd user unit)
|
|
8
|
-
* status 向守护进程控制接口取状态
|
|
9
|
-
* remove 删除启动项
|
|
10
|
-
* restart 请求重启(默认等空闲,--immediate 立即)
|
|
11
|
-
* stop 让守护进程退出(不再拉起 DSH)
|
|
12
|
-
*/
|
|
13
|
-
import fs from 'node:fs'
|
|
14
|
-
import path from 'node:path'
|
|
15
|
-
import os from 'node:os'
|
|
16
|
-
import http from 'node:http'
|
|
17
|
-
import { fileURLToPath } from 'node:url'
|
|
18
|
-
|
|
19
|
-
const here = path.dirname(fileURLToPath(import.meta.url))
|
|
20
|
-
const pkgRoot = path.resolve(here, '..')
|
|
21
|
-
const supervisor = path.join(pkgRoot, 'bin', 'supervisor.mjs')
|
|
22
|
-
const controlPort = Number(process.env.DSH_HOMING_CONTROL ?? 8790)
|
|
23
|
-
|
|
24
|
-
const has = (name) => process.argv.includes('--' + name)
|
|
25
|
-
const [command] = process.argv.slice(2)
|
|
26
|
-
|
|
27
|
-
function call(pathname, method = 'POST') {
|
|
28
|
-
return new Promise((resolve, reject) => {
|
|
29
|
-
const req = http.request({ host: '127.0.0.1', port: controlPort, path: pathname, method, timeout: 8000 }, (res) => {
|
|
30
|
-
const chunks = []
|
|
31
|
-
res.on('data', (chunk) => chunks.push(chunk))
|
|
32
|
-
res.on('end', () => resolve({ status: res.statusCode, body: Buffer.concat(chunks).toString('utf8') }))
|
|
33
|
-
})
|
|
34
|
-
req.on('timeout', () => { req.destroy(); reject(new Error('timeout')) })
|
|
35
|
-
req.on('error', reject)
|
|
36
|
-
req.end()
|
|
37
|
-
})
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function startupPath() {
|
|
41
|
-
if (process.platform === 'win32') {
|
|
42
|
-
return path.join(process.env.APPDATA ?? '', 'Microsoft', 'Windows', 'Start Menu', 'Programs', 'Startup', 'dsh-homing-supervisor.vbs')
|
|
43
|
-
}
|
|
44
|
-
if (process.platform === 'darwin') {
|
|
45
|
-
return path.join(os.homedir(), 'Library', 'LaunchAgents', 'online.gitsrc.dsh-homing.plist')
|
|
46
|
-
}
|
|
47
|
-
return path.join(os.homedir(), '.config', 'systemd', 'user', 'dsh-homing-supervisor.service')
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function install() {
|
|
51
|
-
const file = startupPath()
|
|
52
|
-
fs.mkdirSync(path.dirname(file), { recursive: true })
|
|
53
|
-
if (process.platform === 'win32') {
|
|
54
|
-
// 用 wscript 启动,登录时才不会闪控制台窗口
|
|
55
|
-
const vbs = `' 由 dsh-homing 生成:登录时以隐藏窗口启动守护进程\r\nSet sh = CreateObject("WScript.Shell")\r\nsh.Run """${process.execPath}"" ""${supervisor}"" --detach", 0, False\r\n`
|
|
56
|
-
fs.writeFileSync(file, vbs, 'ascii')
|
|
57
|
-
} else if (process.platform === 'darwin') {
|
|
58
|
-
const xml = `<?xml version="1.0" encoding="UTF-8"?>
|
|
59
|
-
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
60
|
-
<plist version="1.0"><dict>
|
|
61
|
-
<key>Label</key><string>online.gitsrc.dsh-homing</string>
|
|
62
|
-
<key>ProgramArguments</key><array><string>${process.execPath}</string><string>${supervisor}</string><string>--detach</string></array>
|
|
63
|
-
<key>RunAtLoad</key><true/>
|
|
64
|
-
</dict></plist>
|
|
65
|
-
`
|
|
66
|
-
fs.writeFileSync(file, xml, 'utf8')
|
|
67
|
-
} else {
|
|
68
|
-
const text = `[Unit]
|
|
69
|
-
Description=DSH Homing supervisor
|
|
70
|
-
|
|
71
|
-
[Service]
|
|
72
|
-
ExecStart=${process.execPath} ${supervisor} --detach
|
|
73
|
-
|
|
74
|
-
[Install]
|
|
75
|
-
WantedBy=default.target
|
|
76
|
-
`
|
|
77
|
-
fs.writeFileSync(file, text, 'utf8')
|
|
78
|
-
}
|
|
79
|
-
console.log(` ✓ 已写入启动项:${file}`)
|
|
80
|
-
if (process.platform === 'linux') console.log(' 启用:systemctl --user enable --now dsh-homing-supervisor')
|
|
81
|
-
console.log('\n常用命令:')
|
|
82
|
-
console.log(' dsh-homing-watchdog status')
|
|
83
|
-
console.log(' dsh-homing-watchdog restart [--immediate]')
|
|
84
|
-
console.log(' dsh-homing-watchdog stop')
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
async function status() {
|
|
88
|
-
try {
|
|
89
|
-
const res = await call('/status', 'GET')
|
|
90
|
-
console.log(res.body.trim())
|
|
91
|
-
} catch {
|
|
92
|
-
console.error('守护进程没在运行(控制端口连不上)。先 dsh-homing-watchdog install,或手动运行 supervisor.mjs --detach。')
|
|
93
|
-
process.exit(1)
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
function remove() {
|
|
98
|
-
const file = startupPath()
|
|
99
|
-
if (fs.existsSync(file)) {
|
|
100
|
-
fs.rmSync(file, { force: true })
|
|
101
|
-
console.log(` ✓ 已删除启动项:${file}`)
|
|
102
|
-
} else {
|
|
103
|
-
console.log(' 启动项不存在')
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
async function restart(immediate) {
|
|
108
|
-
const mode = immediate ? 'immediate' : 'default'
|
|
109
|
-
try {
|
|
110
|
-
const res = await call(`/restart?mode=${mode}`)
|
|
111
|
-
console.log(res.status === 202 ? ` 已请求重启(模式 ${mode})` : ` 请求被拒绝(HTTP ${res.status})`)
|
|
112
|
-
} catch {
|
|
113
|
-
console.error('守护进程没在运行。先 install 或手动启动 supervisor.mjs --detach。')
|
|
114
|
-
process.exit(1)
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
async function stop() {
|
|
119
|
-
try {
|
|
120
|
-
await call('/stop')
|
|
121
|
-
console.log(' 守护进程已退出(不会自动拉起 DSH)')
|
|
122
|
-
} catch {
|
|
123
|
-
console.error('守护进程没在运行')
|
|
124
|
-
process.exit(1)
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
switch (command) {
|
|
129
|
-
case 'install': install(); break
|
|
130
|
-
case 'status': await status(); break
|
|
131
|
-
case 'remove': remove(); break
|
|
132
|
-
case 'restart': await restart(has('--immediate') || has('-i')); break
|
|
133
|
-
case 'stop': await stop(); break
|
|
134
|
-
default:
|
|
135
|
-
console.log(`用法: dsh-homing-watchdog <install|status|remove|restart|stop> [--immediate]
|
|
136
|
-
|
|
137
|
-
install 写入登录自启(Windows 启动项 / macOS LaunchAgent / Linux systemd user unit)
|
|
138
|
-
status 向守护进程查询状态
|
|
139
|
-
remove 删除登录自启
|
|
140
|
-
restart 请求重启 DSH(默认等空闲,--immediate 立即)
|
|
141
|
-
stop 让守护进程退出(不再拉起 DSH)
|
|
142
|
-
`)
|
|
143
|
-
process.exit(command === undefined ? 0 : 1)
|
|
144
|
-
}
|