dsh-drydock 1.0.2 → 1.1.0-rc.2
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.en.md +3 -3
- package/README.md +112 -7
- package/bin/drydock-doctor.mjs +239 -0
- package/bin/dsh-boot.cmd +4 -0
- package/bin/dsh-boot.mjs +215 -11
- package/bin/open-boot.mjs +35 -4
- package/bin/rescue-daemon.mjs +34 -5
- package/lib/bundles-official.mjs +350 -0
- package/lib/capabilities.mjs +215 -0
- package/lib/client.js +2 -2
- package/lib/diagnostics.mjs +1731 -0
- package/lib/enginectl.mjs +336 -13
- package/lib/envprobe.mjs +942 -0
- package/lib/index.js +54 -1
- package/lib/remote.js +1 -1
- package/lib/rescue-profile.mjs +1330 -0
- package/package.json +10 -3
package/bin/dsh-boot.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
/**
|
|
3
|
-
* dsh-boot.mjs — Steam 式启动序列(P1-1)。
|
|
3
|
+
* dsh-boot.mjs — Steam 式启动序列(P1-1)+ 1.1.0 桌面版救援链(S5)。
|
|
4
4
|
*
|
|
5
5
|
* 流程:verify(启动前自检)→ 有问题自动 fix(隔离坏 bundle,可逆)→ 拉起 `dsh web` → 健康等待。
|
|
6
6
|
* 相当于在引擎启动前设一道"安检",坏插件被自动拦下,避免主进程崩溃。
|
|
@@ -13,24 +13,43 @@
|
|
|
13
13
|
* - 显式使用稳定 cwd(默认用户主目录,--cwd 可覆盖),避免把包目录当工作目录。
|
|
14
14
|
* - 新增 `--help`。
|
|
15
15
|
*
|
|
16
|
+
* v1.1.0(桌面版移植 M5,契约 F5/F6,红线第 1/2 条)新增两条**官方通道**能力:
|
|
17
|
+
* - `--rescue-profile`:不走"启动本 profile",改为走官方 `dsh rescue --from-default-profile web`
|
|
18
|
+
* 建/重建救援 profile 并启动它(rescue 是官方 help 里的示例名 —— 官方自己推荐"从出厂 web 模板造救援 profile")。
|
|
19
|
+
* **桌面 profile 下一律拒绝**(code: desktop-delegated):桌面场景绝不 spawn Host。
|
|
20
|
+
* - `--heal-recovery`:官方恢复(禁用第三方 bundle + 把 cordis.patch.yml 改名
|
|
21
|
+
* `cordis.patch.yml.bak-<timestamp>` + 下次启动建空 patch)之后,从侧车 `plugin-manager.json`
|
|
22
|
+
* 与 `.rescue-bak-*` 备份**重建归属行**。默认 dry-run 只打印计划;`--apply` 才真写,
|
|
23
|
+
* 并把 `--confirm <profileName>` 当二次确认;desktop 还要 `--allow-desktop`。
|
|
24
|
+
* 持锁(`profiles/desktop/lock` 存在)时**一律拒绝写盘**并给只读预览。
|
|
25
|
+
*
|
|
16
26
|
* 用法:
|
|
17
27
|
* node bin/dsh-boot.mjs [--profile <dir>] [--dsh <cmd>] [--repair-only] [--wait-ms <n>] [--pause] [--cwd <dir>]
|
|
18
|
-
* --
|
|
19
|
-
*
|
|
20
|
-
* --help 显示用法
|
|
28
|
+
* node bin/dsh-boot.mjs --rescue-profile [--dsh <cmd>] [--wait-ms <n>] [--cwd <dir>]
|
|
29
|
+
* node bin/dsh-boot.mjs --heal-recovery [--profile <dir>] [--apply --confirm <profileName> [--allow-desktop]]
|
|
21
30
|
*
|
|
22
31
|
* 退出码:
|
|
23
|
-
* 0 = 引擎已就绪(或 repair-only 下 profile
|
|
24
|
-
* 1 = 启动后仍未就绪(或 fix
|
|
25
|
-
* 2 = verify 发现问题且 fix 后仍有问题(repair-only
|
|
32
|
+
* 0 = 引擎已就绪(或 repair-only 下 profile 健康 / dry-run 计划已打印)
|
|
33
|
+
* 1 = 启动后仍未就绪(或 fix 无法解决 / 救援与自愈执行失败)
|
|
34
|
+
* 2 = verify 发现问题且 fix 后仍有问题(repair-only);或参数冲突(--rescue-profile 与 --heal-recovery 互斥)
|
|
35
|
+
* 3 = 被安全门拒绝(desktop 持锁 / 未确认 / 未允许 desktop / 桌面场景拒绝 spawn Host)
|
|
26
36
|
*/
|
|
27
37
|
import { realpathSync } from "node:fs";
|
|
28
|
-
import { join, resolve } from "node:path";
|
|
38
|
+
import { basename, join, resolve } from "node:path";
|
|
29
39
|
import { homedir } from "node:os";
|
|
30
40
|
import { fileURLToPath } from "node:url";
|
|
31
41
|
import { verifyProfile, fixProfile, isolateFailedEntries } from "../lib/preflight.mjs";
|
|
32
|
-
import { ENGINE_PORT, chdirStable, engineHealth, portOwner, probe, startEngineWithQuarantine } from "../lib/enginectl.mjs";
|
|
42
|
+
import { ENGINE_PORT, chdirStable, engineHealth, portOwner, probe, startEngineWithQuarantine, waitForEngine } from "../lib/enginectl.mjs";
|
|
33
43
|
import { entryUrlOf, launcherPortFromEnv } from "../lib/ports.mjs";
|
|
44
|
+
import {
|
|
45
|
+
DEFAULT_TEMPLATE,
|
|
46
|
+
RESCUE_PROFILE_NAME,
|
|
47
|
+
buildRescueProfile,
|
|
48
|
+
detectOfficialRecoveryTraces,
|
|
49
|
+
planSelfHeal,
|
|
50
|
+
resolveDshHome,
|
|
51
|
+
selfHealAfterRecovery
|
|
52
|
+
} from "../lib/rescue-profile.mjs";
|
|
34
53
|
|
|
35
54
|
const SELF = fileURLToPath(import.meta.url);
|
|
36
55
|
const PROFILE_DEFAULT = join(homedir(), ".dsh", "profiles", "web");
|
|
@@ -39,6 +58,8 @@ const HELP_TEXT = `DSH 启动序列(自检 → 修复 → 启动 → 健康等
|
|
|
39
58
|
|
|
40
59
|
用法:
|
|
41
60
|
node bin/dsh-boot.mjs [选项]
|
|
61
|
+
node bin/dsh-boot.mjs --rescue-profile [选项]
|
|
62
|
+
node bin/dsh-boot.mjs --heal-recovery [--apply --confirm <profileName> [--allow-desktop]]
|
|
42
63
|
|
|
43
64
|
选项:
|
|
44
65
|
--profile <dir> profile 目录(默认 ~/.dsh/profiles/web)
|
|
@@ -47,13 +68,42 @@ const HELP_TEXT = `DSH 启动序列(自检 → 修复 → 启动 → 健康等
|
|
|
47
68
|
--wait-ms <n> 启动后等待引擎就绪的最长时间(默认 45000)
|
|
48
69
|
--pause 结束时等按键再关闭窗口(供 open-boot 弹出的可见启动窗口使用)
|
|
49
70
|
--cwd <dir> 引擎工作目录(默认用户主目录,避免锁住包目录)
|
|
71
|
+
|
|
72
|
+
救援 profile 与恢复后自愈(1.1.0 桌面版支持):
|
|
73
|
+
--rescue-profile 不走"启动本 profile":走**官方通道**建/重建救援 profile 并启动它
|
|
74
|
+
(执行 dsh rescue --from-default-profile web —— 官方 help 里的示例用法)。
|
|
75
|
+
完全不走 npm 的 dsh plugin --profile <x>,因此不需要绕 rejectElectronProfile,
|
|
76
|
+
也绝不碰 desktop profile。桌面 profile 下一律拒绝(code: desktop-delegated)。
|
|
77
|
+
--heal-recovery 检测桌面官方"恢复"留下的痕迹(空 patch / cordis.patch.yml.bak-<时间戳> /
|
|
78
|
+
package.json.bundles 被摘),并打印**自愈计划**(默认 dry-run,只读不写)。
|
|
79
|
+
--apply 与 --heal-recovery 合用才真正写盘:写前自动备份(.rescue-bak-<时间戳>)、
|
|
80
|
+
只动带归属标记的行、写完复检(失败自动回滚)。
|
|
81
|
+
--confirm <name> 与 --apply 合用的**二次确认**:必须等于目标 profile 名(如 web / desktop)。
|
|
82
|
+
--allow-desktop 显式允许对 desktop profile 写盘(只在 --apply 时需要;不加则拒绝对 desktop
|
|
83
|
+
写盘并给中文原因,退出码 3)。dry-run 只读预览不需要它。
|
|
50
84
|
--help 显示本帮助
|
|
51
85
|
|
|
52
|
-
退出码:0=就绪 / 1
|
|
86
|
+
退出码:0=就绪 / 1=未就绪或执行失败 / 2=repair-only 修复未完成或参数冲突 /
|
|
87
|
+
3=被安全门拒绝(desktop 持锁 / 未确认 / 未允许 desktop / 桌面场景拒绝 spawn Host)
|
|
53
88
|
`;
|
|
54
89
|
|
|
55
90
|
function parseArgs(argv) {
|
|
56
|
-
const args = {
|
|
91
|
+
const args = {
|
|
92
|
+
profile: PROFILE_DEFAULT,
|
|
93
|
+
dsh: "dsh",
|
|
94
|
+
repairOnly: false,
|
|
95
|
+
waitMs: 45000,
|
|
96
|
+
pause: false,
|
|
97
|
+
help: false,
|
|
98
|
+
cwd: null,
|
|
99
|
+
// 1.1.0 桌面版救援链
|
|
100
|
+
rescueProfile: false,
|
|
101
|
+
healRecovery: false,
|
|
102
|
+
apply: false,
|
|
103
|
+
confirm: null,
|
|
104
|
+
allowDesktop: false,
|
|
105
|
+
unknown: []
|
|
106
|
+
};
|
|
57
107
|
for (let i = 0; i < argv.length; i++) {
|
|
58
108
|
if (argv[i] === "--profile" && argv[i + 1]) { args.profile = resolve(argv[++i]); }
|
|
59
109
|
else if (argv[i] === "--dsh" && argv[i + 1]) { args.dsh = argv[++i]; }
|
|
@@ -62,6 +112,15 @@ function parseArgs(argv) {
|
|
|
62
112
|
else if (argv[i] === "--wait-ms" && argv[i + 1]) { args.waitMs = Number(argv[++i]) || 45000; }
|
|
63
113
|
else if (argv[i] === "--repair-only") { args.repairOnly = true; }
|
|
64
114
|
else if (argv[i] === "--pause") { args.pause = true; }
|
|
115
|
+
// --confirm 取不到值时不吞掉下一个开关(与 --profile 同一判定风格)
|
|
116
|
+
else if (argv[i] === "--confirm") {
|
|
117
|
+
if (argv[i + 1] && !argv[i + 1].startsWith("-")) args.confirm = argv[++i];
|
|
118
|
+
else args.confirm = "";
|
|
119
|
+
}
|
|
120
|
+
else if (argv[i] === "--rescue-profile") { args.rescueProfile = true; }
|
|
121
|
+
else if (argv[i] === "--heal-recovery") { args.healRecovery = true; }
|
|
122
|
+
else if (argv[i] === "--apply") { args.apply = true; }
|
|
123
|
+
else if (argv[i] === "--allow-desktop") { args.allowDesktop = true; }
|
|
65
124
|
else if (argv[i] === "--help" || argv[i] === "-h") { args.help = true; }
|
|
66
125
|
else if (argv[i].startsWith("-")) { args.unknown.push(argv[i]); }
|
|
67
126
|
}
|
|
@@ -73,9 +132,18 @@ async function main() {
|
|
|
73
132
|
const log = (line) => console.log(`[dsh-boot] ${line}`);
|
|
74
133
|
if (args.help) { console.log(HELP_TEXT); return 0; }
|
|
75
134
|
if (args.unknown.length > 0) log(`⚠ 无法识别的参数(已忽略):${args.unknown.join(" ")}(用 --help 查看用法)`);
|
|
135
|
+
if (args.apply && !args.healRecovery) log("⚠ --apply 只与 --heal-recovery 合用;当前模式下已忽略。");
|
|
76
136
|
// 稳定 cwd(审计③ L2):引擎的 cwd 不能落在本包 node_modules / profile 目录里
|
|
77
137
|
chdirStable(args.cwd);
|
|
78
138
|
|
|
139
|
+
if (args.rescueProfile && args.healRecovery) {
|
|
140
|
+
log("✗ 参数冲突:--rescue-profile 与 --heal-recovery 互斥(前者会启动一个 Host,后者只读/写 patch 行)。");
|
|
141
|
+
log(" 请只选一个:--rescue-profile(引擎坏了、要另起救援 profile)或 --heal-recovery(官方恢复之后补回归属行)。");
|
|
142
|
+
return 2;
|
|
143
|
+
}
|
|
144
|
+
if (args.rescueProfile) return await runRescueProfile(args, log);
|
|
145
|
+
if (args.healRecovery) return await runHealRecovery(args, log);
|
|
146
|
+
|
|
79
147
|
if (!process.argv.includes("--profile")) {
|
|
80
148
|
log(`profile 默认: ${args.profile}(可用 --profile 指定)`);
|
|
81
149
|
}
|
|
@@ -144,6 +212,142 @@ async function main() {
|
|
|
144
212
|
return 1;
|
|
145
213
|
}
|
|
146
214
|
|
|
215
|
+
/**
|
|
216
|
+
* `--rescue-profile`:走官方通道建/重建救援 profile 并启动它。
|
|
217
|
+
* 红线 2:桌面 profile 下一律拒绝(绝不 spawn 桌面 Host),退出码 3(安全门)。
|
|
218
|
+
*/
|
|
219
|
+
async function runRescueProfile(args, log) {
|
|
220
|
+
const profileName = basename(args.profile);
|
|
221
|
+
if (profileName.toLowerCase() === "desktop") {
|
|
222
|
+
log("✗ 拒绝执行 --rescue-profile:桌面场景绝不 spawn / kill Host(契约红线 2)。");
|
|
223
|
+
log(" code: desktop-delegated");
|
|
224
|
+
log(" 原因:--rescue-profile 走官方 `dsh rescue --from-default-profile <模板>`,该命令会**启动**救援 profile 的 Host;"
|
|
225
|
+
+ "桌面版独占 profiles/desktop 与进程级单实例锁,引擎生命周期归 Electron(能力表 engineLifecycle: delegated)。");
|
|
226
|
+
log(" 请改用:① 从系统托盘完全退出 DeepSeek Harness 后重开;"
|
|
227
|
+
+ "② 或 --heal-recovery 修复 cordis.patch.yml 归属行(只需文件读写,不需要启停 Host)。");
|
|
228
|
+
log(` 也可以在**完全退出桌面版之后**,自行在终端执行官方命令:${args.dsh} rescue --from-default-profile ${DEFAULT_TEMPLATE}`);
|
|
229
|
+
return 3;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// 引擎已在跑 → 不重建:官方 --from-default-profile 是「重建」(会删掉现有 rescue 目录与备份),
|
|
233
|
+
// 且新 Host 会和现有引擎抢同一个端口。
|
|
234
|
+
const health = await engineHealth(ENGINE_PORT);
|
|
235
|
+
if (health.ok) {
|
|
236
|
+
log(`✓ 引擎已在 ${ENGINE_PORT} 运行(HTTP 握手正常),无需救援重建;未改动任何 profile。`);
|
|
237
|
+
log(` 说明:官方 --from-default-profile 是「重建」——它会删除现有 ${RESCUE_PROFILE_NAME} 目录(含 cordis.patch.yml 与 .rescue-bak-* 备份),`
|
|
238
|
+
+ `且新 Host 会和现有引擎抢 ${ENGINE_PORT}。真要重建请先停掉引擎。`);
|
|
239
|
+
return 0;
|
|
240
|
+
}
|
|
241
|
+
if (await probe(ENGINE_PORT)) {
|
|
242
|
+
const owner = portOwner(ENGINE_PORT);
|
|
243
|
+
log(`✗ 端口 ${ENGINE_PORT} 被${owner ? ` pid ${owner}` : "其他进程"}占用,但它不是 dsh 引擎(HTTP 握手失败)。`);
|
|
244
|
+
log(` 救援 profile 启动后会监听 ${ENGINE_PORT},现在启动必然失败;请先释放该端口再重试。未改动任何 profile。`);
|
|
245
|
+
return 1;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const homeInfo = resolveDshHome({ profileDir: args.profile, env: process.env });
|
|
249
|
+
log(`① 走官方通道重建救援 profile(DSH_HOME=${homeInfo.home};来源:${homeInfo.source})…`);
|
|
250
|
+
const build = await buildRescueProfile({
|
|
251
|
+
dshHome: homeInfo.home,
|
|
252
|
+
sourceTemplate: DEFAULT_TEMPLATE,
|
|
253
|
+
profileName: RESCUE_PROFILE_NAME,
|
|
254
|
+
dshCmd: args.dsh,
|
|
255
|
+
dryRun: false,
|
|
256
|
+
// 追加给 web 应用的参数(官方 CLI 的自身开关必须在前,其余原样透传给应用)
|
|
257
|
+
extraArgs: ["--port", String(ENGINE_PORT), "--no-open"],
|
|
258
|
+
cwd: args.cwd,
|
|
259
|
+
logDir: join(homeInfo.home, "logs")
|
|
260
|
+
});
|
|
261
|
+
for (const step of build.steps) log(step);
|
|
262
|
+
if (build.warnings.length > 0) for (const warning of build.warnings) log(`⚠ ${warning}`);
|
|
263
|
+
log(`理由:${build.reason}`);
|
|
264
|
+
|
|
265
|
+
if (!build.ok) {
|
|
266
|
+
log(`✗ 救援 profile 创建/启动失败(code: ${build.code})。`);
|
|
267
|
+
if (build.logPath) log(` 日志:${build.logPath}`);
|
|
268
|
+
if (build.command) log(` 可手动重跑官方命令:${build.command}`);
|
|
269
|
+
return build.code === "desktop-delegated" ? 3 : 1;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
log(`② 等待救援引擎就绪(端口 ${ENGINE_PORT},最长 ${Math.round(args.waitMs / 1000)}s)…`);
|
|
273
|
+
const ready = await waitForEngine(ENGINE_PORT, args.waitMs, 300, engineHealth);
|
|
274
|
+
if (ready) {
|
|
275
|
+
log(`✓ 救援引擎已就绪:http://127.0.0.1:${ENGINE_PORT}/`);
|
|
276
|
+
log(` 这个 profile 完全来自出厂模板(${build.profileDir}),没继承你 web profile 的第三方插件 —— 引擎坏掉时用它进得去。`);
|
|
277
|
+
return 0;
|
|
278
|
+
}
|
|
279
|
+
log("✗ 救援引擎在超时内未通过 HTTP 健康检查(可能是命令立刻退出或域名/端口冲突)。");
|
|
280
|
+
if (build.logPath) log(` 日志:${build.logPath}`);
|
|
281
|
+
log(` 可手动重跑官方命令:${build.command}`);
|
|
282
|
+
return 1;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* `--heal-recovery`:官方恢复之后从侧车 + 备份重建归属行。
|
|
287
|
+
* 默认 dry-run(只打印计划);`--apply` 才写,且需要 `--confirm <profileName>`(+ desktop 的 `--allow-desktop`)。
|
|
288
|
+
*/
|
|
289
|
+
async function runHealRecovery(args, log) {
|
|
290
|
+
const profileDir = args.profile;
|
|
291
|
+
const profileName = basename(profileDir);
|
|
292
|
+
const isDesktop = profileName.toLowerCase() === "desktop";
|
|
293
|
+
log(`profile:${profileDir}${isDesktop ? "(desktop:Electron 独占,drydock 只做 cordis.patch.yml 级可逆写)" : ""}`);
|
|
294
|
+
|
|
295
|
+
log("① 检测官方恢复痕迹(只读)…");
|
|
296
|
+
const traces = detectOfficialRecoveryTraces({ profileDir });
|
|
297
|
+
for (const item of traces.evidence) log(` · [${item.code}] ${item.detail}`);
|
|
298
|
+
log(` 结论:${traces.detected ? "检测到官方恢复痕迹" : "未检测到官方恢复痕迹"}(置信度 ${traces.confidence})——${traces.reason}`);
|
|
299
|
+
|
|
300
|
+
log("② 生成自愈计划(只读,不写盘)…");
|
|
301
|
+
const plan = planSelfHeal({ profileDir });
|
|
302
|
+
for (const line of plan.message.split("\n")) log(` ${line}`);
|
|
303
|
+
if (plan.bomStripped.length > 0) log(` ⚠ BOM:已识别并剥离 ${plan.bomStripped.length} 处(写盘一律无 BOM)`);
|
|
304
|
+
|
|
305
|
+
if (plan.willRestore.length + plan.willAdd.length === 0) {
|
|
306
|
+
log("✓ 无需恢复:侧车与备份里的归属行都已在 patch 中(或没有可用备份)。未改动任何文件。");
|
|
307
|
+
return 0;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
if (!args.apply) {
|
|
311
|
+
log("ℹ 默认 dry-run:以上只是计划,**未写盘**。");
|
|
312
|
+
log(` 要真正执行:--heal-recovery --apply --confirm ${profileName}${isDesktop ? " --allow-desktop" : ""}`);
|
|
313
|
+
if (isDesktop && plan.desktopLocked) log(" ⚠ 该 desktop profile 现在被 Electron 持锁:真执行会被拒绝(code: desktop-locked)——请先从系统托盘完全退出。");
|
|
314
|
+
return 0;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// ---- 安全门(与 lib/rescue-profile.mjs 同序;这里先给出更直白的中文原因)
|
|
318
|
+
if (isDesktop && !args.allowDesktop) {
|
|
319
|
+
log("✗ 拒绝写盘:没有 --allow-desktop。");
|
|
320
|
+
log(" 原因:desktop profile 由 Electron 独占(F8);写盘必须先由人显式允许(这是刻意的双确认之一)。");
|
|
321
|
+
log(` 本次未改动任何文件。确认后重跑:--heal-recovery --apply --confirm ${profileName} --allow-desktop`);
|
|
322
|
+
return 3;
|
|
323
|
+
}
|
|
324
|
+
if (typeof args.confirm !== "string" || args.confirm === "" || args.confirm !== profileName) {
|
|
325
|
+
log(`✗ 拒绝写盘:二次确认不匹配(需要 --confirm ${profileName}${typeof args.confirm === "string" && args.confirm !== "" ? `,收到 ${args.confirm}` : ",当前未提供"})。`);
|
|
326
|
+
log(" 原因:自愈会改写 cordis.patch.yml,必须由人确认**目标 profile 名**(双确认的另一半)。");
|
|
327
|
+
log(" 本次未改动任何文件。");
|
|
328
|
+
return 3;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
log("③ 执行自愈(写前备份 → 只动带归属标记的行 → 复检)…");
|
|
332
|
+
log(` 目标 profile:${profileName}${isDesktop ? "(desktop)" : ""}`);
|
|
333
|
+
for (const row of plan.willRestore) log(` · 将改动:${row.name ?? row.id}(disabled: ${row.disabled},${row.action})`);
|
|
334
|
+
for (const row of plan.willAdd) log(` · 将改动:${row.name ?? row.id}(disabled: ${row.disabled},来源 ${row.from})`);
|
|
335
|
+
|
|
336
|
+
const heal = selfHealAfterRecovery({ profileDir, dryRun: false, confirm: args.confirm, allowDesktop: args.allowDesktop === true });
|
|
337
|
+
for (const line of heal.message.split("\n")) log(` ${line}`);
|
|
338
|
+
if (!heal.ok) {
|
|
339
|
+
const zeroChange = heal.code === "desktop-locked" || heal.code === "confirm-required" || heal.code === "desktop-not-allowed" || heal.code === "backup-failed" || heal.code === "read-failed";
|
|
340
|
+
log(`✗ 自愈未完成(code: ${heal.code})——${zeroChange ? "整个目录一个字节都没变" : "已回滚到写前状态(见上面说明)"}。`);
|
|
341
|
+
}
|
|
342
|
+
if (heal.ok) {
|
|
343
|
+
log(`✓ 自愈完成(code: ${heal.code})${heal.backupPath ? `;写前备份:${heal.backupPath}` : ""}`);
|
|
344
|
+
log(" 注意:改的是 patch 行,**不是**已装包。下次引擎启动时这些归属行会重新生效;第三方插件更新/重装后仍建议跑一次 verify。");
|
|
345
|
+
return 0;
|
|
346
|
+
}
|
|
347
|
+
if (heal.code === "desktop-locked" || heal.code === "confirm-required" || heal.code === "desktop-not-allowed") return 3;
|
|
348
|
+
return 1;
|
|
349
|
+
}
|
|
350
|
+
|
|
147
351
|
/**
|
|
148
352
|
* 只在"直接运行本文件"时执行入口(被 import 时不执行,便于测试)。
|
|
149
353
|
* 语义与 bin/open-boot.mjs:776-792 完全对齐(同一判定,不另写一套)。
|
package/bin/open-boot.mjs
CHANGED
|
@@ -67,10 +67,10 @@ import { removePatchEntriesFor } from "../lib/patch-clean.mjs";
|
|
|
67
67
|
import { DRYDOCK_FAVICON_LINK, drydockLogoMarkSvg, handleApi as handleRescueApi, rescuePageHtml } from "./rescue-daemon.mjs";
|
|
68
68
|
import { entryUrlOf, launcherPortFromEnv, lockPortOf, rescuePortFromEnv } from "../lib/ports.mjs";
|
|
69
69
|
import {
|
|
70
|
-
ENGINE_PORT, LAUNCHER_APP, bootWindowPlan, chdirStable, createSingleFlight, detectAvailableTerminals, engineHealth,
|
|
70
|
+
ENGINE_PORT, LAUNCHER_APP, bootWindowPlan, chdirStable, corsPreflightResponse, corsResponseHeaders, createSingleFlight, detectAvailableTerminals, engineHealth,
|
|
71
71
|
guardWriteRequest, isAlive, killTreeForce, launchViaCandidates, launcherHealth, looksLikeProfileDir, newApiToken, portOwner,
|
|
72
72
|
portOwnerProbe, probe, processImage, processImageProbe, readPidInfo, resolveEngineCwd, startEngineWithQuarantine, stopEngine,
|
|
73
|
-
validateCliValue, validateIntegerValue, validatePortValue, waitForEngine
|
|
73
|
+
validateCliValue, validateIntegerValue, validatePortValue, waitForEngine, writeOriginPolicyFromEnv
|
|
74
74
|
} from "../lib/enginectl.mjs";
|
|
75
75
|
|
|
76
76
|
const PROFILE_DEFAULT = join(homedir(), ".dsh", "profiles", "web");
|
|
@@ -349,6 +349,9 @@ const HELP_TEXT = `浏览器启动器 / 3081 常驻入口(dsh-drydock 引擎
|
|
|
349
349
|
DSH_RESCUE_PORT rescue-daemon 备份入口端口(默认 3082)
|
|
350
350
|
DSH_PM_SCRIPTS_DIR 生成的启动脚本目录(默认 <DSH_HOME>/scripts;测试隔离用)
|
|
351
351
|
DSH_PM_NO_SCRIPTS=1 跳过 dsh-launcher.cmd 生成(只读/受限环境)
|
|
352
|
+
DSH_DRYDOCK_ALLOW_DESKTOP_ORIGIN=1 允许桌面版页面来源 dsh-app://app 的写请求(默认关;网页版不要开)
|
|
353
|
+
DSH_DRYDOCK_DESKTOP_TOKEN=<48hex> 桌面版令牌(宿主用 mintDesktopToken() 生成,同时交给本服务与桌面页面)
|
|
354
|
+
DSH_DRYDOCK_ALLOW_NULL_ORIGIN=1 桌面页面确实发 Origin: null 时再开(需同时开上面第一项;只认桌面令牌)
|
|
352
355
|
守护单实例锁端口 = 启动器端口 + 1000(默认 3081 → 4081;--supervise 用它保证单实例)。
|
|
353
356
|
|
|
354
357
|
--uninstall 语义(冻结契约,供管理器卸载钩子调用):
|
|
@@ -369,6 +372,11 @@ const HELP_TEXT = `浏览器启动器 / 3081 常驻入口(dsh-drydock 引擎
|
|
|
369
372
|
|
|
370
373
|
写接口同源白名单(L11):http://127.0.0.1:<port> / http://localhost:<port> / http://[::1]:<port>
|
|
371
374
|
(同机同端口的等价主机名;其他 Origin 一律 403。端口取本服务监听端口。)
|
|
375
|
+
桌面版(1.1.0):显式设 DSH_DRYDOCK_ALLOW_DESKTOP_ORIGIN=1 时额外允许 dsh-app://app(Electron 页面来源,
|
|
376
|
+
大小写不敏感、允许末尾 "/"),令牌照旧校验;其他 dsh-app 页面与任何 http(s) 第三方源仍然 403。
|
|
377
|
+
CORS 预检(1.1.0 M6):桌面页带自定义头 X-DSH-PM-Token 跨源 fetch 时浏览器先发 OPTIONS —— 启动器直接
|
|
378
|
+
应答 204 + Access-Control-Allow-Origin/Methods/Headers/Max-Age(只回显被允许的那个 Origin,绝不 *);
|
|
379
|
+
来源不被允许的 OPTIONS 一律 403 且不回任何 Allow 头。预检排在令牌校验之前(规范要求,不带令牌)。
|
|
372
380
|
|
|
373
381
|
输入校验(表驱动解析:t14-F1 / t18-F1 / t21-F1 三次实测的"静默回落默认目标"家族):
|
|
374
382
|
取值开关(--profile / --port / --dsh / --cwd / --interval / --wait-ms / --heartbeat-min)的取值:
|
|
@@ -2847,18 +2855,26 @@ function startServer(args) {
|
|
|
2847
2855
|
const launcherCmdPath = ensureLauncherCmd(args);
|
|
2848
2856
|
// L11 ②:一次性令牌(每次启动随机生成)——只注入页面 meta 与请求头,**不写日志、不落 profile 文件**
|
|
2849
2857
|
const token = newApiToken();
|
|
2858
|
+
// v1.1.0 M2(契约 F1/F2):桌面版渲染进程的页面来源是自定义协议 `dsh-app://app`(Electron 从打包
|
|
2859
|
+
// 静态资源加载,不是 HTTP 源),桌面页发来的写请求 Origin 会被同源白名单判成"跨站"→403。默认全关,
|
|
2860
|
+
// 只有显式设置 DSH_DRYDOCK_ALLOW_DESKTOP_ORIGIN=1 才多认这一个固定来源;令牌照旧校验(桌面拿不到页面
|
|
2861
|
+
// meta 里的令牌 → 宿主进程经 DSH_DRYDOCK_DESKTOP_TOKEN 把桌面令牌同时交给本服务与桌面页面)。
|
|
2862
|
+
// 不设环境变量 = 与 1.0.x 完全一致(web 零回归)。只改调用点,判定逻辑在 lib/enginectl.mjs。
|
|
2863
|
+
const writeOriginPolicy = writeOriginPolicyFromEnv();
|
|
2850
2864
|
// L11 ③:boot 与救援 /api/start 共用同一个单飞闸门
|
|
2851
2865
|
const flight = createSingleFlight("open-boot:boot");
|
|
2852
2866
|
// rc.2 P2-1:插件更新/卸载事务共用第二个单飞闸门(并发第二个请求 → 409,不再并行跑两条 pnpm)
|
|
2853
2867
|
const pluginsFlight = createSingleFlight("open-boot:plugins");
|
|
2854
2868
|
const rescueCtx = { profile: args.profile, dsh: args.dsh, cwd: args.cwd, port: args.port, flight };
|
|
2855
2869
|
const sendJson = (res, code, body) => {
|
|
2856
|
-
|
|
2870
|
+
// v1.1.0 M6(S6):正式响应也要带 Access-Control-Allow-Origin + Vary(预检过了、正式响应没带
|
|
2871
|
+
// 浏览器照样拦);只回显被允许的 Origin(由 handler 入口按同一套白名单算好),来源不允许时为空
|
|
2872
|
+
res.writeHead(code, { "Content-Type": "application/json; charset=utf-8", ...(res.dshCorsHeaders || {}) });
|
|
2857
2873
|
res.end(JSON.stringify(body));
|
|
2858
2874
|
};
|
|
2859
2875
|
/** L11 ①②:写接口的 Origin + 令牌检查。返回 true 表示已拒绝并应答。 */
|
|
2860
2876
|
const rejectWrite = (req, res) => {
|
|
2861
|
-
const failure = guardWriteRequest(req, { port: args.port, token });
|
|
2877
|
+
const failure = guardWriteRequest(req, { port: args.port, token, ...writeOriginPolicy });
|
|
2862
2878
|
if (!failure) return false;
|
|
2863
2879
|
sendJson(res, failure.status, { ok: false, code: failure.code, error: failure.message });
|
|
2864
2880
|
return true;
|
|
@@ -2871,6 +2887,21 @@ function startServer(args) {
|
|
|
2871
2887
|
const server = createServer(async (req, res) => {
|
|
2872
2888
|
const url = new URL(req.url, "http://127.0.0.1");
|
|
2873
2889
|
const pathname = url.pathname;
|
|
2890
|
+
// v1.1.0 M6(S6,P0 收尾):CORS 预检分支 —— 必须排在路由与 `guardWriteRequest` **之前**
|
|
2891
|
+
// (预检请求按 CORS 规范不带令牌;排在令牌校验之后会永远 401,正式请求根本发不出去)。
|
|
2892
|
+
// 真包证据:桌面把 dsh-app 注册为 corsEnabled:true,桌面页面(来源 dsh-app://app)带自定义头
|
|
2893
|
+
// X-DSH-PM-Token 跨源 fetch 本端口时浏览器必定先发 OPTIONS。这里来源被允许 → 204 + Allow-*
|
|
2894
|
+
// (Allow-Origin 回显被允许的那个 Origin,绝不 *);来源不被允许 → 403 且不回任何 Allow 头。
|
|
2895
|
+
// 只新增这个分支,不改变 guardWriteRequest 的任何既有判定(POST/GET 与令牌逻辑原样)。
|
|
2896
|
+
const allowDesktopApp = writeOriginPolicy.allowDesktopApp;
|
|
2897
|
+
const corsHeaders = corsResponseHeaders(req, { port: args.port, allowDesktopApp });
|
|
2898
|
+
if (corsHeaders) res.dshCorsHeaders = corsHeaders; // sendJson 复用它给正式响应补 Allow-Origin + Vary
|
|
2899
|
+
const pre = corsPreflightResponse(req, { port: args.port, allowDesktopApp });
|
|
2900
|
+
if (pre) {
|
|
2901
|
+
res.writeHead(pre.status, pre.message ? { "Content-Type": "application/json; charset=utf-8", ...pre.headers } : { ...pre.headers });
|
|
2902
|
+
res.end(pre.message ? JSON.stringify({ ok: false, code: pre.status, error: pre.message }) : "");
|
|
2903
|
+
return;
|
|
2904
|
+
}
|
|
2874
2905
|
if (pathname === "/") {
|
|
2875
2906
|
sendHtml(res, bootPageHtml({ token, enginePort: ENGINE_PORT }));
|
|
2876
2907
|
return;
|
package/bin/rescue-daemon.mjs
CHANGED
|
@@ -40,8 +40,9 @@ import { fileURLToPath } from "node:url";
|
|
|
40
40
|
import { verifyProfile, fixProfile, isolateFailedEntries } from "../lib/preflight.mjs";
|
|
41
41
|
import { entryUrlOf, launcherPortFromEnv, rescuePortFromEnv } from "../lib/ports.mjs";
|
|
42
42
|
import {
|
|
43
|
-
ENGINE_PORT, RESCUE_APP, chdirStable, createSingleFlight, engineHealth, guardWriteRequest, isAlive,
|
|
44
|
-
launcherHealth, newApiToken, portOwner, probe, readPidInfo, startEngineWithQuarantine, stopEngine, validatePortValue
|
|
43
|
+
ENGINE_PORT, RESCUE_APP, chdirStable, corsPreflightResponse, corsResponseHeaders, createSingleFlight, engineHealth, guardWriteRequest, isAlive,
|
|
44
|
+
launcherHealth, newApiToken, portOwner, probe, readPidInfo, startEngineWithQuarantine, stopEngine, validatePortValue,
|
|
45
|
+
writeOriginPolicyFromEnv
|
|
45
46
|
} from "../lib/enginectl.mjs";
|
|
46
47
|
|
|
47
48
|
const SELF = fileURLToPath(import.meta.url);
|
|
@@ -79,6 +80,11 @@ const HELP_TEXT = `DSH 独立救援守护(默认 3082 = 备用救援入口)
|
|
|
79
80
|
--cwd <dir> 引擎/守护工作目录(默认用户主目录)
|
|
80
81
|
--help 显示本帮助
|
|
81
82
|
|
|
83
|
+
环境变量:
|
|
84
|
+
DSH_DRYDOCK_ALLOW_DESKTOP_ORIGIN=1 允许桌面版页面来源 dsh-app://app 的写请求(默认关;网页版不要开)
|
|
85
|
+
DSH_DRYDOCK_DESKTOP_TOKEN=<48hex> 桌面版令牌(宿主用 mintDesktopToken() 生成,同时交给本服务与桌面页面)
|
|
86
|
+
DSH_DRYDOCK_ALLOW_NULL_ORIGIN=1 桌面页面确实发 Origin: null 时再开(需同时开上面第一项;只认桌面令牌)
|
|
87
|
+
|
|
82
88
|
端口分工(v0.9.1 起):
|
|
83
89
|
3080 = dsh 引擎;3081 = open-boot(唯一网页入口,已含 /rescue 页面 + /rescue/api/* 完整救援能力);
|
|
84
90
|
3082 = 本守护(独立备份入口)。浏览器主页/桌面快捷方式请指向 3081。
|
|
@@ -87,6 +93,10 @@ const HELP_TEXT = `DSH 独立救援守护(默认 3082 = 备用救援入口)
|
|
|
87
93
|
页面按钮:启动引擎并打开主界面 / 运行检查 / 修复引擎配置 / 状态 / 停止引擎(带校验,可强制)
|
|
88
94
|
|
|
89
95
|
写接口防护:POST /api/fix|start|stop 需要同源 Origin + 一次性令牌(页面会自动带上);
|
|
96
|
+
桌面版(1.1.0)页面来源是 dsh-app://app,需显式 DSH_DRYDOCK_ALLOW_DESKTOP_ORIGIN=1 才放行(令牌照旧校验)。
|
|
97
|
+
CORS 预检(1.1.0 M6):桌面页带 X-DSH-PM-Token 跨源 fetch 时浏览器先发 OPTIONS —— 本守护直接应答
|
|
98
|
+
204 + Access-Control-Allow-Origin/Methods/Headers/Max-Age(只回显被允许的那个 Origin,绝不 *);
|
|
99
|
+
来源不被允许的 OPTIONS → 403 且不回任何 Allow 头;预检排在令牌校验之前(规范要求,预检不带令牌)。
|
|
90
100
|
读取接口 GET /api/status|verify 保持开放,供本地工具探测。
|
|
91
101
|
`;
|
|
92
102
|
|
|
@@ -134,7 +144,9 @@ function readBody(req, limit = 8192) {
|
|
|
134
144
|
*/
|
|
135
145
|
async function handleApi(pathname, method, req, res, url, ctx = args) {
|
|
136
146
|
const json = (code, body) => {
|
|
137
|
-
|
|
147
|
+
// v1.1.0 M6(S6):正式响应补 CORS 头(预检过了、正式响应没带 Access-Control-Allow-Origin 浏览器照样拦)。
|
|
148
|
+
// 头由调用方(本守护/挂载它的 open-boot)在 handler 入口按同一套 Origin 白名单算好,只回显被允许的 Origin,绝不 *
|
|
149
|
+
res.writeHead(code, { "Content-Type": "application/json; charset=utf-8", ...(res.dshCorsHeaders || {}) });
|
|
138
150
|
res.end(JSON.stringify(body));
|
|
139
151
|
};
|
|
140
152
|
if (!ctx || typeof ctx !== "object" || !ctx.profile) {
|
|
@@ -372,6 +384,10 @@ function main() {
|
|
|
372
384
|
|
|
373
385
|
// L11:一次性令牌(每次启动随机生成)只注入页面 meta 与请求头,**不写日志、不落 profile 文件**
|
|
374
386
|
const token = newApiToken();
|
|
387
|
+
// v1.1.0 M2(契约 F1/F2):桌面版渲染进程的页面来源是自定义协议 `dsh-app://app`,桌面页发来的写请求
|
|
388
|
+
// Origin 会被同源白名单判成"跨站"→403。默认全关,只有 DSH_DRYDOCK_ALLOW_DESKTOP_ORIGIN=1 才多认这一个
|
|
389
|
+
// 固定来源;令牌照旧校验(桌面令牌由宿主经 DSH_DRYDOCK_DESKTOP_TOKEN 同时交给服务与桌面页面)。
|
|
390
|
+
const writeOriginPolicy = writeOriginPolicyFromEnv();
|
|
375
391
|
const flight = createSingleFlight("rescue-write");
|
|
376
392
|
const ctx = { profile: args.profile, dsh: args.dsh, cwd: args.cwd, port: args.port, flight };
|
|
377
393
|
const entryUrl = entryUrlOf(launcherPortFromEnv()) || "http://127.0.0.1:3081/";
|
|
@@ -388,6 +404,19 @@ function main() {
|
|
|
388
404
|
const server = createServer(async (req, res) => {
|
|
389
405
|
const url = new URL(req.url, "http://127.0.0.1");
|
|
390
406
|
const pathname = url.pathname;
|
|
407
|
+
// v1.1.0 M6(S6,P0 收尾):CORS 预检分支 —— 必须排在路由与 `guardWriteRequest` **之前**
|
|
408
|
+
// (预检请求按 CORS 规范不带令牌;桌面版页面来源 dsh-app://app 带 X-DSH-PM-Token 跨源 fetch 本端口时
|
|
409
|
+
// 浏览器必定先发 OPTIONS)。来源被允许 → 204 + Allow-*(回显被允许的 Origin,绝不 *);
|
|
410
|
+
// 来源不被允许 → 403 且不回任何 Allow 头。既有 guardWriteRequest 判定一行未改。
|
|
411
|
+
const allowDesktopApp = writeOriginPolicy.allowDesktopApp;
|
|
412
|
+
const corsHeaders = corsResponseHeaders(req, { port: args.port, allowDesktopApp });
|
|
413
|
+
if (corsHeaders) res.dshCorsHeaders = corsHeaders; // handleApi 的 json() 复用它给正式响应补 Allow-Origin + Vary
|
|
414
|
+
const pre = corsPreflightResponse(req, { port: args.port, allowDesktopApp });
|
|
415
|
+
if (pre) {
|
|
416
|
+
res.writeHead(pre.status, pre.message ? { "Content-Type": "application/json; charset=utf-8", ...pre.headers } : { ...pre.headers });
|
|
417
|
+
res.end(pre.message ? JSON.stringify({ ok: false, code: pre.status, error: pre.message }) : "");
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
391
420
|
if (pathname === "/" || pathname === "/rescue") {
|
|
392
421
|
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8", "Cache-Control": "no-store" });
|
|
393
422
|
res.end(pageHtml);
|
|
@@ -395,9 +424,9 @@ function main() {
|
|
|
395
424
|
}
|
|
396
425
|
// 写接口防护①②(L11):Origin + 一次性令牌;读取接口(GET /api/status|verify)保持可被本地工具探测
|
|
397
426
|
if (req.method === "POST") {
|
|
398
|
-
const failure = guardWriteRequest(req, { port: args.port, token });
|
|
427
|
+
const failure = guardWriteRequest(req, { port: args.port, token, ...writeOriginPolicy });
|
|
399
428
|
if (failure) {
|
|
400
|
-
res.writeHead(failure.status, { "Content-Type": "application/json; charset=utf-8" });
|
|
429
|
+
res.writeHead(failure.status, { "Content-Type": "application/json; charset=utf-8", ...(res.dshCorsHeaders || {}) });
|
|
401
430
|
res.end(JSON.stringify({ ok: false, code: failure.code, error: failure.message }));
|
|
402
431
|
return;
|
|
403
432
|
}
|