@leiyi-works/dsh-auto-restart 0.2.3 → 0.2.4
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 +8 -1
- package/client.js +3 -1
- package/index.js +66 -1
- package/lib/install-watchdog.ps1 +12 -1
- package/lib/watchdog.ps1 +12 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -28,11 +28,18 @@
|
|
|
28
28
|
|
|
29
29
|
首次启动时插件会检测:没装就自动弹一个**中文控制台窗口**,说明用途并等你按回车安装。安装做三件事:把脚本拷到 `~/.dsh/dsh-auto-restart/`、在启动文件夹写一个隐藏引线、立刻启动看门狗。之后每次登录 Windows 它自动起来,所有自动重启都不再经过 WMI。
|
|
30
30
|
|
|
31
|
-
-
|
|
31
|
+
- 面板里只显示看门狗状态(运行中/未安装/已安装无心跳/脚本已更新待重装),**没有安装/卸载按钮**;
|
|
32
32
|
- **插件会定期体检**(默认每 60 秒):看门狗没在回答(进程没了/心跳过期)就**自动重新打开安装窗口**(两次提醒最少隔 5 分钟);这期间自动重启会退回 WMI 兜底;
|
|
33
|
+
- **脚本更新检测**:心跳里带脚本哈希;插件升级后如果后台还跑着旧脚本,会再弹一次安装窗口把看门狗重启到新脚本(安装器会先停掉旧进程);
|
|
33
34
|
- 卸载:在 `~/.dsh/dsh-auto-restart/` 里执行 `powershell -File install-watchdog.ps1 -Uninstall`;
|
|
34
35
|
- 看门狗日志:`~/.dsh/dsh-auto-restart/watchdog.log`。
|
|
35
36
|
|
|
37
|
+
### 多机同步注意
|
|
38
|
+
|
|
39
|
+
- 看门狗本体和安装脚本在 `~/.dsh/dsh-auto-restart/`,会跟着云端同步走;**启动文件夹的引线不在 `~/.dsh` 里,不会被同步**——所以每台机器第一次都会弹安装窗口,各自装一次即可(路径全部运行时由 `%USERPROFILE%` / `%APPDATA%` 计算,不同用户名不影响);
|
|
40
|
+
- 其它机器同步过来的旧重启请求(`watchdog-request.json` / `restart-request.json`)会在启动时按 exe 路径校验后清理,不会被本机看门狗误执行;
|
|
41
|
+
- `disabled`(急停文件)和 `config.json` 会同步:一处急停等于全机队急停、一处改配置全机队生效;如果不想要这个行为,把这两个文件加进云端同步的排除项。
|
|
42
|
+
|
|
36
43
|
> 少数安全软件(如 360)对「PowerShell 操作 WMI」有行为规则:装了看门狗后重启不走 WMI,就不会再因此弹窗;安装看门狗那一次仍可能被提示一次,允许即可。
|
|
37
44
|
|
|
38
45
|
会话记录在磁盘上,重启后回到同一个会话。**没有每小时次数上限**,只有 60 秒冷却防止连环重启。
|
package/client.js
CHANGED
|
@@ -271,7 +271,9 @@ window.__ModuleLoader__.load({
|
|
|
271
271
|
h('span', { style: styles.val }, status.watchdog === undefined
|
|
272
272
|
? '—'
|
|
273
273
|
: status.watchdog.alive === true
|
|
274
|
-
?
|
|
274
|
+
? status.watchdog.scriptStale === true
|
|
275
|
+
? '运行中(脚本已更新,等待重装)'
|
|
276
|
+
: `运行中(pid=${status.watchdog.heartbeat?.pid ?? '?'})`
|
|
275
277
|
: status.watchdog.installed === true
|
|
276
278
|
? '已安装但当前没有心跳(重启会用 WMI 兜底)'
|
|
277
279
|
: '未安装(DSH 启动时会弹安装窗口;未安装时重启走 WMI,可能触发安全软件提示)'),
|
package/index.js
CHANGED
|
@@ -190,6 +190,15 @@ function ensureWatchdogFiles() {
|
|
|
190
190
|
}
|
|
191
191
|
}
|
|
192
192
|
|
|
193
|
+
/** SHA1 of the watchdog script shipped with this plugin version. */
|
|
194
|
+
function watchdogScriptHash() {
|
|
195
|
+
try {
|
|
196
|
+
return createHash('sha1').update(readFileSync(join(PKG_DIR, 'lib', 'watchdog.ps1'))).digest('hex')
|
|
197
|
+
} catch {
|
|
198
|
+
return ''
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
193
202
|
/** The watchdog's heartbeat, when it is fresh enough to be trusted. */
|
|
194
203
|
function readWatchdogHeartbeat() {
|
|
195
204
|
const parsed = readJsonFile(watchdogHeartbeatPath())
|
|
@@ -198,7 +207,21 @@ function readWatchdogHeartbeat() {
|
|
|
198
207
|
if (!Number.isFinite(at)) return null
|
|
199
208
|
const ageMs = Date.now() - at
|
|
200
209
|
if (ageMs < 0 || ageMs > 20000) return null
|
|
201
|
-
return {
|
|
210
|
+
return {
|
|
211
|
+
pid: Number(parsed.pid) || 0,
|
|
212
|
+
at: new Date(at).toISOString(),
|
|
213
|
+
ageMs,
|
|
214
|
+
script: typeof parsed.script === 'string' ? parsed.script : '',
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* The running watchdog answers but was started from older code. The installer
|
|
220
|
+
* stops it and starts the new script, so this is worth re-prompting for.
|
|
221
|
+
*/
|
|
222
|
+
function watchdogScriptStale(heartbeat) {
|
|
223
|
+
const current = watchdogScriptHash()
|
|
224
|
+
return heartbeat !== null && current !== '' && heartbeat.script !== '' && heartbeat.script !== current
|
|
202
225
|
}
|
|
203
226
|
|
|
204
227
|
function watchdogAlive() {
|
|
@@ -255,6 +278,35 @@ function dispatchToWatchdog(state, cfg, reason) {
|
|
|
255
278
|
return { ok: true, dryRun: false, via: 'watchdog', delayMs: request.delayMs }
|
|
256
279
|
}
|
|
257
280
|
|
|
281
|
+
/**
|
|
282
|
+
* Removes restart requests left by another machine's sync: a request names the
|
|
283
|
+
* exe it was created for, so a request whose exe does not exist here (or differs
|
|
284
|
+
* from this machine's app) must never be replayed by the local watchdog.
|
|
285
|
+
*/
|
|
286
|
+
function cleanForeignRequests() {
|
|
287
|
+
const exe = resolveExe(readConfig({}))
|
|
288
|
+
for (const file of [watchdogRequestPath(), requestPath()]) {
|
|
289
|
+
const parsed = readJsonFile(file)
|
|
290
|
+
if (parsed === null || typeof parsed !== 'object') continue
|
|
291
|
+
const wanted = typeof parsed.exe === 'string' ? parsed.exe : ''
|
|
292
|
+
if (wanted === '') {
|
|
293
|
+
try { rmSync(file, { force: true }) } catch {}
|
|
294
|
+
continue
|
|
295
|
+
}
|
|
296
|
+
let same = false
|
|
297
|
+
try {
|
|
298
|
+
same = resolve(wanted) === resolve(exe)
|
|
299
|
+
} catch {
|
|
300
|
+
same = false
|
|
301
|
+
}
|
|
302
|
+
if (same) continue
|
|
303
|
+
try {
|
|
304
|
+
rmSync(file, { force: true })
|
|
305
|
+
logLine('warn', `已清理其它机器同步过来的重启请求:${wanted}`)
|
|
306
|
+
} catch {}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
258
310
|
/**
|
|
259
311
|
* Periodic watchdog health check: when the resident watchdog is not answering,
|
|
260
312
|
* re-open the console installer (rate-limited), because a restart without it
|
|
@@ -267,6 +319,14 @@ function watchdogHealthCheck(state, cfg, now = Date.now()) {
|
|
|
267
319
|
const heartbeat = readWatchdogHeartbeat()
|
|
268
320
|
if (heartbeat !== null) {
|
|
269
321
|
state.watchdogMissingLogged = false
|
|
322
|
+
if (!watchdogScriptStale(heartbeat)) return
|
|
323
|
+
// Running code is older than the plugin on disk: the installer stops the old
|
|
324
|
+
// process and starts the new script.
|
|
325
|
+
if (now - state.lastWatchdogOfferAt < cfg.watchdogReinstallCooldownMs) return
|
|
326
|
+
state.lastWatchdogOfferAt = now
|
|
327
|
+
state.watchdogStaleLogged = true
|
|
328
|
+
logLine('warn', `后台看门狗运行的脚本已过期(${heartbeat.script.slice(0, 8)} ≠ ${watchdogScriptHash().slice(0, 8)}),重新打开安装窗口以更新`)
|
|
329
|
+
offerWatchdogInstall()
|
|
270
330
|
return
|
|
271
331
|
}
|
|
272
332
|
if (now - state.lastWatchdogOfferAt < cfg.watchdogReinstallCooldownMs) return
|
|
@@ -279,6 +339,7 @@ function watchdogHealthCheck(state, cfg, now = Date.now()) {
|
|
|
279
339
|
}
|
|
280
340
|
offerWatchdogInstall()
|
|
281
341
|
}
|
|
342
|
+
|
|
282
343
|
function offerWatchdogInstall() {
|
|
283
344
|
const installer = watchdogInstallerPath()
|
|
284
345
|
if (!existsSync(installer)) {
|
|
@@ -874,6 +935,7 @@ function installRoutes(ctx, baseConfig) {
|
|
|
874
935
|
watchdog: {
|
|
875
936
|
alive: watchdogAlive(),
|
|
876
937
|
heartbeat: readWatchdogHeartbeat(),
|
|
938
|
+
scriptStale: watchdogScriptStale(readWatchdogHeartbeat()),
|
|
877
939
|
installed: watchdogInstalled(),
|
|
878
940
|
startupEntry: startupEntryPath(),
|
|
879
941
|
logPath: watchdogLogPath(),
|
|
@@ -992,6 +1054,7 @@ export function apply(ctx, config) {
|
|
|
992
1054
|
// Resident watchdog: keep its files current, report its state, and let the
|
|
993
1055
|
// periodic health check re-open the installer when it stops answering.
|
|
994
1056
|
ensureWatchdogFiles()
|
|
1057
|
+
cleanForeignRequests()
|
|
995
1058
|
const heartbeat = readWatchdogHeartbeat()
|
|
996
1059
|
if (heartbeat !== null) {
|
|
997
1060
|
logLine('info', `后台看门狗运行中:pid=${heartbeat.pid} 心跳=${heartbeat.at}`)
|
|
@@ -1066,4 +1129,6 @@ export const _internals = {
|
|
|
1066
1129
|
watchdogAlive,
|
|
1067
1130
|
readWatchdogHeartbeat,
|
|
1068
1131
|
watchdogInstalled,
|
|
1132
|
+
watchdogScriptHash,
|
|
1133
|
+
watchdogScriptStale,
|
|
1069
1134
|
}
|
package/lib/install-watchdog.ps1
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
param(
|
|
2
2
|
[switch]$Uninstall
|
|
3
3
|
)
|
|
4
4
|
|
|
@@ -13,6 +13,7 @@ $DataDir = Join-Path $env:USERPROFILE '.dsh\dsh-auto-restart'
|
|
|
13
13
|
$StartupDir = Join-Path $env:APPDATA 'Microsoft\Windows\Start Menu\Programs\Startup'
|
|
14
14
|
$StartupFile = Join-Path $StartupDir 'dsh-auto-restart-watchdog.vbs'
|
|
15
15
|
$WatchdogVbs = Join-Path $DataDir 'watchdog.vbs'
|
|
16
|
+
$WatchdogPs1 = Join-Path $DataDir 'watchdog.ps1'
|
|
16
17
|
$Heartbeat = Join-Path $DataDir 'watchdog-heartbeat.json'
|
|
17
18
|
$StopFile = Join-Path $DataDir 'watchdog.stop'
|
|
18
19
|
|
|
@@ -84,6 +85,16 @@ try {
|
|
|
84
85
|
) -join "`r`n"
|
|
85
86
|
Write-Utf8NoBom $StartupFile $vbs
|
|
86
87
|
if (Test-Path -LiteralPath $StopFile) { Remove-Item -LiteralPath $StopFile -Force -ErrorAction SilentlyContinue }
|
|
88
|
+
# Stop a watchdog that is still running old code, so the new script answers.
|
|
89
|
+
try {
|
|
90
|
+
$existing = @(Get-CimInstance Win32_Process -Filter "Name='powershell.exe'" -ErrorAction SilentlyContinue |
|
|
91
|
+
Where-Object { $_.CommandLine -ne $null -and $_.CommandLine.Contains($WatchdogPs1) })
|
|
92
|
+
foreach ($proc in $existing) {
|
|
93
|
+
Write-Host (' 停止旧看门狗进程 pid=' + $proc.ProcessId) -ForegroundColor DarkGray
|
|
94
|
+
Stop-Process -Id $proc.ProcessId -Force -ErrorAction SilentlyContinue
|
|
95
|
+
}
|
|
96
|
+
} catch {}
|
|
97
|
+
if (Test-Path -LiteralPath $Heartbeat) { Remove-Item -LiteralPath $Heartbeat -Force -ErrorAction SilentlyContinue }
|
|
87
98
|
Start-Process -FilePath 'wscript.exe' -ArgumentList ('//B "' + $WatchdogVbs + '"') | Out-Null
|
|
88
99
|
$installed = Test-Heartbeat 15
|
|
89
100
|
if (-not $installed) { throw '看门狗已启动但 15 秒内没有心跳,请把窗口内容发给助手。' }
|
package/lib/watchdog.ps1
CHANGED
|
@@ -149,6 +149,17 @@ function Write-Result([string]$Phase, [string]$Detail) {
|
|
|
149
149
|
}
|
|
150
150
|
|
|
151
151
|
Write-Log ('watchdog started pid=' + $PID)
|
|
152
|
+
# The heartbeat carries the hash of the script that is actually running, so the
|
|
153
|
+
# plugin can tell "the code on disk changed but this process still runs the old
|
|
154
|
+
# one" and ask for a reinstall (which restarts the watchdog).
|
|
155
|
+
$ScriptHash = ''
|
|
156
|
+
try {
|
|
157
|
+
$own = $MyInvocation.MyCommand.Path
|
|
158
|
+
if (-not [string]::IsNullOrWhiteSpace($own)) {
|
|
159
|
+
$ScriptHash = (Get-FileHash -LiteralPath $own -Algorithm SHA1).Hash.ToLowerInvariant()
|
|
160
|
+
}
|
|
161
|
+
} catch {}
|
|
162
|
+
Write-Log ('script hash=' + $ScriptHash)
|
|
152
163
|
$handledId = ''
|
|
153
164
|
$lastHeartbeat = [datetime]::MinValue
|
|
154
165
|
while ($true) {
|
|
@@ -156,7 +167,7 @@ while ($true) {
|
|
|
156
167
|
$now = Get-Date
|
|
157
168
|
if (($now - $lastHeartbeat).TotalSeconds -ge 5) {
|
|
158
169
|
try {
|
|
159
|
-
$beat = '{"pid":' + $PID + ',"at":"' + $now.ToString('o') + '"}'
|
|
170
|
+
$beat = '{"pid":' + $PID + ',"at":"' + $now.ToString('o') + '","script":"' + $ScriptHash + '"}'
|
|
160
171
|
[System.IO.File]::WriteAllText($HeartbeatPath, $beat, (New-Object System.Text.UTF8Encoding($false)))
|
|
161
172
|
} catch {}
|
|
162
173
|
$lastHeartbeat = $now
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leiyi-works/dsh-auto-restart",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"title": "自动重启",
|
|
6
6
|
"description": "装/更插件后自动重启 DSH 并回到原会话:检测 profile 插件文件变化,一直等所有会话空闲且最近活动安静,由常驻后台看门狗(登录自启、不碰 WMI)或 WMI 守护先杀主进程再拉起(可最大化窗口),带冷却、试运行、日志和右侧原生侧边栏面板。",
|