dsh-plugin-mesh 0.5.5 → 0.5.7
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 +49 -0
- package/client/client.js +983 -20
- package/lib/_selftest-update.mjs +267 -0
- package/lib/_selftest-workbench.mjs +141 -0
- package/lib/desktop-auth.js +88 -0
- package/lib/desktop-web.js +146 -0
- package/lib/index.js +445 -2
- package/lib/ssh-host.js +380 -0
- package/lib/update.js +232 -0
- package/lib/web-proxy.js +333 -0
- package/lib/ws-mini.js +156 -0
- package/llms.txt +23 -0
- package/package.json +7 -3
- package/scripts/check-tdz.mjs +1 -1
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 「检查更新」的自测:版本比较 + registry 查询失败时的语义。
|
|
3
|
+
*
|
|
4
|
+
* 这一块的**核心断言不是"能查出新版本",而是"查不到时必须说查不到"**。
|
|
5
|
+
* 本项目最贵的教训(AGENTS.md 硬约束 1):探测失败被当成否定事实 ——
|
|
6
|
+
* 这里如果写成「没问到 → 当作已是最新」,用户会永远升不了级,而且看不出来。
|
|
7
|
+
*
|
|
8
|
+
* 全部无网络:fetch 是注入的假实现。
|
|
9
|
+
*
|
|
10
|
+
* node lib/_selftest-update.mjs
|
|
11
|
+
*/
|
|
12
|
+
import { strict as assert } from "node:assert";
|
|
13
|
+
import {
|
|
14
|
+
buildUpdateReport,
|
|
15
|
+
buildUpgradeCommand,
|
|
16
|
+
compareVersions,
|
|
17
|
+
fetchLatestVersion,
|
|
18
|
+
parseVersion,
|
|
19
|
+
pluginDirFromUrl,
|
|
20
|
+
updateKind,
|
|
21
|
+
DEFAULT_REGISTRIES,
|
|
22
|
+
} from "./update.js";
|
|
23
|
+
|
|
24
|
+
let passed = 0;
|
|
25
|
+
let failed = 0;
|
|
26
|
+
function test(name, fn) {
|
|
27
|
+
try {
|
|
28
|
+
fn();
|
|
29
|
+
passed += 1;
|
|
30
|
+
console.log(` ✓ ${name}`);
|
|
31
|
+
} catch (err) {
|
|
32
|
+
failed += 1;
|
|
33
|
+
console.log(` ✗ ${name}`);
|
|
34
|
+
console.log(` ${err?.message ?? err}`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function testAsync(name, fn) {
|
|
39
|
+
try {
|
|
40
|
+
await fn();
|
|
41
|
+
passed += 1;
|
|
42
|
+
console.log(` ✓ ${name}`);
|
|
43
|
+
} catch (err) {
|
|
44
|
+
failed += 1;
|
|
45
|
+
console.log(` ✗ ${name}`);
|
|
46
|
+
console.log(` ${err?.message ?? err}`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const jsonResponse = (body, status = 200) => ({
|
|
51
|
+
ok: status >= 200 && status < 300,
|
|
52
|
+
status,
|
|
53
|
+
json: async () => body,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
console.log("\n── 版本解析与比较 ──");
|
|
57
|
+
|
|
58
|
+
test("解析 x.y.z / v 前缀 / 预发布 / build 元数据", () => {
|
|
59
|
+
assert.deepEqual(parseVersion("0.5.6"), { major: 0, minor: 5, patch: 6, pre: [] });
|
|
60
|
+
assert.deepEqual(parseVersion("v1.2.3"), { major: 1, minor: 2, patch: 3, pre: [] });
|
|
61
|
+
assert.deepEqual(parseVersion(" 1.2.3-beta.2 "), { major: 1, minor: 2, patch: 3, pre: ["beta", "2"] });
|
|
62
|
+
assert.deepEqual(parseVersion("1.2.3+build.9"), { major: 1, minor: 2, patch: 3, pre: [] });
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test("不是语义化版本 → null(不能假装能比)", () => {
|
|
66
|
+
for (const bad of ["", "latest", "1.2", "1.2.3.4", "x.y.z", null, undefined, "1.2.x"]) {
|
|
67
|
+
assert.equal(parseVersion(bad), null, JSON.stringify(bad));
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test("主/次/修订逐段比较", () => {
|
|
72
|
+
assert.equal(compareVersions("0.5.6", "0.5.7"), -1);
|
|
73
|
+
assert.equal(compareVersions("0.6.0", "0.5.9"), 1);
|
|
74
|
+
assert.equal(compareVersions("1.0.0", "1.0.0"), 0);
|
|
75
|
+
assert.equal(compareVersions("v1.0.0", "1.0.0"), 0, "v 前缀不影响比较");
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("预发布小于同号正式版;预发布之间按 semver 规则", () => {
|
|
79
|
+
assert.equal(compareVersions("1.0.0-beta", "1.0.0"), -1);
|
|
80
|
+
assert.equal(compareVersions("1.0.0", "1.0.0-rc.1"), 1);
|
|
81
|
+
assert.equal(compareVersions("1.0.0-alpha", "1.0.0-beta"), -1);
|
|
82
|
+
assert.equal(compareVersions("1.0.0-beta.2", "1.0.0-beta.10"), -1, "数字段按数值比");
|
|
83
|
+
assert.equal(compareVersions("1.0.0-beta", "1.0.0-beta.1"), -1, "少的段更小");
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test("任一侧无法解析 → null,不是 0", () => {
|
|
87
|
+
assert.equal(compareVersions("0.5.6", "latest"), null);
|
|
88
|
+
assert.equal(compareVersions("", "0.5.6"), null);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test("更新量级:major / minor / patch / prerelease / none", () => {
|
|
92
|
+
assert.equal(updateKind("0.5.6", "1.0.0"), "major");
|
|
93
|
+
assert.equal(updateKind("0.5.6", "0.6.0"), "minor");
|
|
94
|
+
assert.equal(updateKind("0.5.6", "0.5.7"), "patch");
|
|
95
|
+
assert.equal(updateKind("0.6.0-beta.1", "0.6.0"), "prerelease");
|
|
96
|
+
assert.equal(updateKind("0.5.6", "0.5.6-rc.1"), "none", "预发布比正式版旧");
|
|
97
|
+
assert.equal(updateKind("0.5.6", "0.5.6"), "none");
|
|
98
|
+
assert.equal(updateKind("0.5.7", "0.5.6"), "none", "更旧不叫更新");
|
|
99
|
+
assert.equal(updateKind("0.5.6", "latest"), "unknown");
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
console.log("\n── 升级命令 / 安装目录 ──");
|
|
103
|
+
|
|
104
|
+
test("插件安装目录从 import.meta.url 推出(lib/ 的上一级,带结尾斜杠)", () => {
|
|
105
|
+
const dir = pluginDirFromUrl("file:///Users/peng/.dsh/profiles/desktop/node_modules/dsh-plugin-mesh/lib/index.js");
|
|
106
|
+
assert.equal(dir, "/Users/peng/.dsh/profiles/desktop/node_modules/dsh-plugin-mesh/");
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test("升级命令用的是 release 真实资产名与 tag(与 scripts/release-plugin.sh 一致)", () => {
|
|
110
|
+
const cmd = buildUpgradeCommand({ version: "0.6.0", pluginDir: "/tmp/plugin/" });
|
|
111
|
+
assert.ok(cmd.includes("plugin-v0.6.0"), cmd);
|
|
112
|
+
assert.ok(cmd.includes("dsh-plugin-mesh-0.6.0.tar.gz"), cmd);
|
|
113
|
+
assert.ok(cmd.includes('tar xz -C "/tmp/plugin/"'), cmd);
|
|
114
|
+
assert.ok(cmd.startsWith("curl -L https://ghfast.top/https://github.com/peng456/dsh-plugin-mesh/releases/download/"), cmd);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
test("没有版本就不编一条假命令", () => {
|
|
118
|
+
assert.equal(buildUpgradeCommand({ version: "", pluginDir: "/x/" }), "");
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
console.log("\n── 报告组装:失败绝不能说成「已是最新」──");
|
|
122
|
+
|
|
123
|
+
const okLatest = { ok: true, version: "0.6.0", registry: DEFAULT_REGISTRIES[0] };
|
|
124
|
+
|
|
125
|
+
test("有新版本 → hasUpdate / kind / 链接 / 可复制命令都在", () => {
|
|
126
|
+
const r = buildUpdateReport({
|
|
127
|
+
current: "0.5.6",
|
|
128
|
+
diskVersion: "0.5.6",
|
|
129
|
+
latest: okLatest,
|
|
130
|
+
pluginDir: "/p/",
|
|
131
|
+
now: Date.parse("2026-10-29T00:00:00Z"),
|
|
132
|
+
});
|
|
133
|
+
assert.equal(r.ok, true);
|
|
134
|
+
assert.equal(r.current, "0.5.6");
|
|
135
|
+
assert.equal(r.latest, "0.6.0");
|
|
136
|
+
assert.equal(r.comparable, true);
|
|
137
|
+
assert.equal(r.hasUpdate, true);
|
|
138
|
+
assert.equal(r.kind, "minor");
|
|
139
|
+
assert.equal(r.versionDrift, false);
|
|
140
|
+
assert.equal(r.checkedAt, "2026-10-29T00:00:00.000Z");
|
|
141
|
+
assert.ok(r.releaseUrl.endsWith("/releases/tag/plugin-v0.6.0"), r.releaseUrl);
|
|
142
|
+
assert.ok(r.npmUrl.endsWith("/v/0.6.0"), r.npmUrl);
|
|
143
|
+
assert.ok(r.upgradeCommand.includes("0.6.0"));
|
|
144
|
+
assert.ok(r.upgradeCommand.includes('"/p/"'));
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
test("同版本 → 已是最新(hasUpdate=false, kind=none)", () => {
|
|
148
|
+
const r = buildUpdateReport({ current: "0.6.0", latest: okLatest, pluginDir: "/p/" });
|
|
149
|
+
assert.equal(r.ok, true);
|
|
150
|
+
assert.equal(r.hasUpdate, false);
|
|
151
|
+
assert.equal(r.kind, "none");
|
|
152
|
+
assert.equal(r.comparable, true);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
test("版本号无法比较 → comparable=false,且 hasUpdate 不能是 true", () => {
|
|
156
|
+
const r = buildUpdateReport({
|
|
157
|
+
current: "0.5.6",
|
|
158
|
+
latest: { ok: true, version: "2026.1", registry: "x" },
|
|
159
|
+
});
|
|
160
|
+
assert.equal(r.ok, true);
|
|
161
|
+
assert.equal(r.comparable, false);
|
|
162
|
+
assert.equal(r.hasUpdate, false);
|
|
163
|
+
assert.equal(r.kind, "unknown");
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
test("registry 查询失败 → ok:false 且带原因;不能有 latest,也不能说没有更新", () => {
|
|
167
|
+
const r = buildUpdateReport({
|
|
168
|
+
current: "0.5.6",
|
|
169
|
+
latest: { ok: false, error: "检查更新失败:registry.npmjs.org 超时(5000ms)", failures: ["a 超时", "b HTTP 502"] },
|
|
170
|
+
});
|
|
171
|
+
assert.equal(r.ok, false);
|
|
172
|
+
assert.ok(r.error.includes("超时"), r.error);
|
|
173
|
+
assert.deepEqual(r.failures, ["a 超时", "b HTTP 502"]);
|
|
174
|
+
assert.equal("latest" in r, false, "没问到最新版本就不能回报 latest");
|
|
175
|
+
assert.equal("hasUpdate" in r, false, "没问到就不能声称有/没有更新");
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
test("磁盘版本与进程版本不一致 → versionDrift=true(升级了没重启)", () => {
|
|
179
|
+
const r = buildUpdateReport({ current: "0.5.6", diskVersion: "0.6.0", latest: okLatest });
|
|
180
|
+
assert.equal(r.versionDrift, true);
|
|
181
|
+
const same = buildUpdateReport({ current: "0.6.0", diskVersion: "0.6.0", latest: okLatest });
|
|
182
|
+
assert.equal(same.versionDrift, false);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
console.log("\n── registry 查询:逐源尝试,失败原因全留 ──");
|
|
186
|
+
|
|
187
|
+
await testAsync("官方源成功 → 直接用,不再问镜像", async () => {
|
|
188
|
+
const calls = [];
|
|
189
|
+
const r = await fetchLatestVersion({
|
|
190
|
+
registries: ["https://a.test/latest", "https://b.test/latest"],
|
|
191
|
+
fetchImpl: async (url) => {
|
|
192
|
+
calls.push(url);
|
|
193
|
+
return jsonResponse({ version: "0.6.0" });
|
|
194
|
+
},
|
|
195
|
+
});
|
|
196
|
+
assert.equal(r.ok, true);
|
|
197
|
+
assert.equal(r.version, "0.6.0");
|
|
198
|
+
assert.equal(r.registry, "https://a.test/latest");
|
|
199
|
+
assert.deepEqual(calls, ["https://a.test/latest"]);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
await testAsync("官方源 HTTP 500 → 回退镜像成功", async () => {
|
|
203
|
+
const r = await fetchLatestVersion({
|
|
204
|
+
registries: ["https://a.test/latest", "https://b.test/latest"],
|
|
205
|
+
fetchImpl: async (url) =>
|
|
206
|
+
url.startsWith("https://a.test") ? jsonResponse({ error: "boom" }, 500) : jsonResponse({ version: "0.7.0" }),
|
|
207
|
+
});
|
|
208
|
+
assert.equal(r.ok, true);
|
|
209
|
+
assert.equal(r.version, "0.7.0");
|
|
210
|
+
assert.equal(r.registry, "https://b.test/latest");
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
await testAsync("两个源都挂 → ok:false,且每个原因都保留(能看出是谁挂了)", async () => {
|
|
214
|
+
const r = await fetchLatestVersion({
|
|
215
|
+
registries: ["https://a.test/latest", "https://b.test/latest"],
|
|
216
|
+
fetchImpl: async (url) => {
|
|
217
|
+
if (url.startsWith("https://a.test")) throw new Error("ENOTFOUND a.test");
|
|
218
|
+
return jsonResponse({}, 502);
|
|
219
|
+
},
|
|
220
|
+
});
|
|
221
|
+
assert.equal(r.ok, false);
|
|
222
|
+
assert.equal(r.failures.length, 2, JSON.stringify(r.failures));
|
|
223
|
+
assert.ok(r.failures[0].includes("ENOTFOUND"), r.failures[0]);
|
|
224
|
+
assert.ok(r.failures[1].includes("HTTP 502"), r.failures[1]);
|
|
225
|
+
assert.ok(r.error.includes("检查更新失败"), r.error);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
await testAsync("超时要写成「超时」,不能和 HTTP 错误混为一谈", async () => {
|
|
229
|
+
const timeout = Object.assign(new Error("The operation was aborted due to timeout"), { name: "TimeoutError" });
|
|
230
|
+
const r = await fetchLatestVersion({
|
|
231
|
+
registries: ["https://a.test/latest"],
|
|
232
|
+
timeoutMs: 1234,
|
|
233
|
+
fetchImpl: async () => {
|
|
234
|
+
throw timeout;
|
|
235
|
+
},
|
|
236
|
+
});
|
|
237
|
+
assert.equal(r.ok, false);
|
|
238
|
+
assert.ok(r.failures[0].includes("超时(1234ms)"), r.failures[0]);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
await testAsync("响应里没有 version → 该源不算成功,继续下一个", async () => {
|
|
242
|
+
const r = await fetchLatestVersion({
|
|
243
|
+
registries: ["https://a.test/latest", "https://b.test/latest"],
|
|
244
|
+
fetchImpl: async (url) =>
|
|
245
|
+
url.startsWith("https://a.test") ? jsonResponse({ name: "dsh-plugin-mesh" }) : jsonResponse({ version: "0.6.0" }),
|
|
246
|
+
});
|
|
247
|
+
assert.equal(r.ok, true);
|
|
248
|
+
assert.equal(r.version, "0.6.0");
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
await testAsync("registry 返回无法比较的版本 → 该源不算成功,最终报失败", async () => {
|
|
252
|
+
const r = await fetchLatestVersion({
|
|
253
|
+
registries: ["https://a.test/latest"],
|
|
254
|
+
fetchImpl: async () => jsonResponse({ version: "2026.1" }),
|
|
255
|
+
});
|
|
256
|
+
assert.equal(r.ok, false);
|
|
257
|
+
assert.ok(r.failures[0].includes("2026.1"), r.failures[0]);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
await testAsync("没有 fetch 的运行环境 → 说清楚无法检查,而不是静默", async () => {
|
|
261
|
+
const r = await fetchLatestVersion({ fetchImpl: null, registries: [] });
|
|
262
|
+
assert.equal(r.ok, false);
|
|
263
|
+
assert.ok(r.error.includes("fetch"), r.error);
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
console.log(`\n${failed === 0 ? "全部通过" : "有失败"}:${passed} 通过 / ${failed} 失败`);
|
|
267
|
+
process.exit(failed === 0 ? 0 : 1);
|
|
@@ -143,6 +143,9 @@ const {
|
|
|
143
143
|
SwitchBody,
|
|
144
144
|
SwitchRow,
|
|
145
145
|
HomeSwitchBar,
|
|
146
|
+
UpdateLink,
|
|
147
|
+
UpdateLinkView,
|
|
148
|
+
FeedbackLink,
|
|
146
149
|
} = mod.__test;
|
|
147
150
|
|
|
148
151
|
const noop = () => {};
|
|
@@ -1066,5 +1069,143 @@ test("一键关闭里的互联网行也标出受阻(避免只说「未连接
|
|
|
1066
1069
|
assert.ok(txt.includes("启动受阻"), txt);
|
|
1067
1070
|
});
|
|
1068
1071
|
|
|
1072
|
+
console.log("\n── 主页头部:问题反馈 / 检查更新(轻入口,不占卡片) ──");
|
|
1073
|
+
|
|
1074
|
+
const updateReport = {
|
|
1075
|
+
ok: true,
|
|
1076
|
+
current: "0.5.6",
|
|
1077
|
+
diskVersion: "0.5.6",
|
|
1078
|
+
versionDrift: false,
|
|
1079
|
+
latest: "0.6.0",
|
|
1080
|
+
comparable: true,
|
|
1081
|
+
hasUpdate: true,
|
|
1082
|
+
kind: "minor",
|
|
1083
|
+
checkedAt: new Date(now).toISOString(),
|
|
1084
|
+
releaseUrl: "https://github.com/peng456/dsh-plugin-mesh/releases/tag/plugin-v0.6.0",
|
|
1085
|
+
pluginDir: "/p/node_modules/dsh-plugin-mesh/",
|
|
1086
|
+
upgradeCommand: 'curl -L https://example.test/v0.6.0.tar.gz | tar xz -C "/p/node_modules/dsh-plugin-mesh/"',
|
|
1087
|
+
};
|
|
1088
|
+
|
|
1089
|
+
test("主页头部把两个入口收成一行小字,不再挂卡片", () => {
|
|
1090
|
+
const el = HomeBoard({
|
|
1091
|
+
state,
|
|
1092
|
+
self: state.self,
|
|
1093
|
+
peers: [peer],
|
|
1094
|
+
tasks: [],
|
|
1095
|
+
now,
|
|
1096
|
+
lastOk: now,
|
|
1097
|
+
focus: false,
|
|
1098
|
+
layout: [],
|
|
1099
|
+
sizes: {},
|
|
1100
|
+
customCards: [],
|
|
1101
|
+
density: "comfortable",
|
|
1102
|
+
onLayoutChange: noop,
|
|
1103
|
+
onFocusChange: noop,
|
|
1104
|
+
onSizesChange: noop,
|
|
1105
|
+
onCustomCardsChange: noop,
|
|
1106
|
+
onDensityChange: noop,
|
|
1107
|
+
onReset: noop,
|
|
1108
|
+
onDevices: noop,
|
|
1109
|
+
onTasks: noop,
|
|
1110
|
+
onConnect: noop,
|
|
1111
|
+
onScan: noop,
|
|
1112
|
+
scanning: false,
|
|
1113
|
+
hostDown: false,
|
|
1114
|
+
onDispatched: noop,
|
|
1115
|
+
push: noop,
|
|
1116
|
+
reload: async () => {},
|
|
1117
|
+
firstRun: null,
|
|
1118
|
+
});
|
|
1119
|
+
const txt = textOf(el).join(" ");
|
|
1120
|
+
assert.ok(txt.includes("问题反馈"), txt.slice(0, 300));
|
|
1121
|
+
assert.ok(txt.includes("检查更新"), txt);
|
|
1122
|
+
const cardIds = [];
|
|
1123
|
+
walk(el, (n) => {
|
|
1124
|
+
if (n.type === "article" && n.props && n.props["data-mesh-card"]) cardIds.push(n.props["data-mesh-card"]);
|
|
1125
|
+
});
|
|
1126
|
+
assert.deepEqual(cardIds, [], "布局为空时不该再有 update / feedback 卡片");
|
|
1127
|
+
});
|
|
1128
|
+
|
|
1129
|
+
test("反馈入口是预填好的 GitHub Issue:带版本与诊断,不带 secret / 授权码", () => {
|
|
1130
|
+
const link = FeedbackLink({ state });
|
|
1131
|
+
assert.equal(link.type, "a");
|
|
1132
|
+
const href = String(link.props.href);
|
|
1133
|
+
assert.ok(href.startsWith("https://github.com/peng456/dsh-plugin-mesh/issues/new?"), href.slice(0, 120));
|
|
1134
|
+
const u = new URL(href);
|
|
1135
|
+
assert.ok(String(u.searchParams.get("title")).includes("0.3.33"), String(u.searchParams.get("title")));
|
|
1136
|
+
assert.ok(String(u.searchParams.get("body")).includes("诊断信息"));
|
|
1137
|
+
assert.ok(!href.includes(state.self.secret), "配对码/secret 不能进 URL");
|
|
1138
|
+
assert.ok(!decodeURIComponent(href).includes("tsnetAuthKey"), "授权码字段不能出现");
|
|
1139
|
+
assert.ok(String(link.props.title).includes("geekstartup@163.com"), "GitHub 不通时邮件仍是退路");
|
|
1140
|
+
});
|
|
1141
|
+
|
|
1142
|
+
test("检查更新:没点之前就是一个小字,点了先说「检查中…」", () => {
|
|
1143
|
+
const idle = textOf(UpdateLink({ push: noop })).join(" ");
|
|
1144
|
+
assert.ok(idle.includes("检查更新"), idle);
|
|
1145
|
+
assert.ok(!idle.includes("已是最新"), "没查过就不能有任何结论");
|
|
1146
|
+
const checking = textOf(UpdateLinkView({ phase: "checking", report: null, onCheck: noop, onCopy: noop })).join(" ");
|
|
1147
|
+
assert.ok(checking.includes("检查中"), checking);
|
|
1148
|
+
});
|
|
1149
|
+
|
|
1150
|
+
test("有更新:旁边多一个字「新版本 v0.6.0」,命令不铺在页面上", () => {
|
|
1151
|
+
const txt = textOf(UpdateLinkView({ phase: "done", report: updateReport, onCheck: noop, onCopy: noop })).join(" ");
|
|
1152
|
+
assert.ok(txt.includes("新版本 v0.6.0"), txt);
|
|
1153
|
+
assert.ok(!txt.includes("curl"), "升级命令点一下才复制,不占版面");
|
|
1154
|
+
});
|
|
1155
|
+
|
|
1156
|
+
test("检查失败 / 无法比较:各多一个字说明,绝不说「已是最新」", () => {
|
|
1157
|
+
const failed = textOf(
|
|
1158
|
+
UpdateLinkView({
|
|
1159
|
+
phase: "done",
|
|
1160
|
+
report: { ok: false, current: "0.5.6", error: "检查更新失败:registry.npmjs.org 超时(5000ms)" },
|
|
1161
|
+
onCheck: noop,
|
|
1162
|
+
onCopy: noop,
|
|
1163
|
+
}),
|
|
1164
|
+
).join(" ");
|
|
1165
|
+
assert.ok(failed.includes("检查失败"), failed);
|
|
1166
|
+
assert.ok(!failed.includes("已是最新"), "失败绝不能说成已是最新");
|
|
1167
|
+
assert.ok(!failed.includes("新版本"), "失败时不能暗示有更新");
|
|
1168
|
+
|
|
1169
|
+
const incomparable = textOf(
|
|
1170
|
+
UpdateLinkView({
|
|
1171
|
+
phase: "done",
|
|
1172
|
+
report: { ...updateReport, latest: "2026.1", comparable: false, hasUpdate: false },
|
|
1173
|
+
onCheck: noop,
|
|
1174
|
+
onCopy: noop,
|
|
1175
|
+
}),
|
|
1176
|
+
).join(" ");
|
|
1177
|
+
assert.ok(incomparable.includes("无法比较"), incomparable);
|
|
1178
|
+
assert.ok(!incomparable.includes("已是最新"));
|
|
1179
|
+
});
|
|
1180
|
+
|
|
1181
|
+
test("点「新版本 v0.6.0」复制升级命令;点「检查更新」重查(不是跳页)", () => {
|
|
1182
|
+
let copied = "";
|
|
1183
|
+
let checked = 0;
|
|
1184
|
+
const el = UpdateLinkView({
|
|
1185
|
+
phase: "done",
|
|
1186
|
+
report: updateReport,
|
|
1187
|
+
onCheck: () => {
|
|
1188
|
+
checked += 1;
|
|
1189
|
+
},
|
|
1190
|
+
onCopy: (text) => {
|
|
1191
|
+
copied = text;
|
|
1192
|
+
},
|
|
1193
|
+
});
|
|
1194
|
+
let updateBtn = null;
|
|
1195
|
+
let checkBtn = null;
|
|
1196
|
+
walk(el, (n) => {
|
|
1197
|
+
if (n.type !== "button") return;
|
|
1198
|
+
const label = textOf(n).join("");
|
|
1199
|
+
if (label.includes("新版本")) updateBtn = n;
|
|
1200
|
+
if (label === "检查更新") checkBtn = n;
|
|
1201
|
+
});
|
|
1202
|
+
assert.ok(updateBtn, "要有一个「新版本」按钮");
|
|
1203
|
+
assert.ok(checkBtn, "要有一个「检查更新」按钮");
|
|
1204
|
+
updateBtn.props.onClick();
|
|
1205
|
+
checkBtn.props.onClick();
|
|
1206
|
+
assert.equal(copied, updateReport.upgradeCommand);
|
|
1207
|
+
assert.equal(checked, 1);
|
|
1208
|
+
});
|
|
1209
|
+
|
|
1069
1210
|
console.log(`\n${failed === 0 ? "全部通过" : "有失败"}:${passed} 通过 / ${failed} 失败\n`);
|
|
1070
1211
|
process.exit(failed ? 1 : 0);
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 「把本机另一个 DSH 引擎的 Web 界面转发出去」——给手机在应用内开面板用。
|
|
3
|
+
*
|
|
4
|
+
* 背景:同一台机器上可能同时跑着两个引擎(桌面 App 一个、CLI/web profile 一个),
|
|
5
|
+
* 而用户要的是**桌面 App 那个**(有历史会话)。它只监听 127.0.0.1,且客户端
|
|
6
|
+
* 会话 cookie 是 SameSite=Strict、token 又是进程级随机 —— 从网络侧直接打不开。
|
|
7
|
+
*
|
|
8
|
+
* 这里的做法:在**我们自己这个引擎**里开一条前缀路由,把请求转发到
|
|
9
|
+
* 127.0.0.1:<desktopPort>,并自己签发一枚该 authority 的会话 cookie。
|
|
10
|
+
*
|
|
11
|
+
* 为什能签:cookie 只是 HMAC 签名,密钥持久化在 <DSH_HOME>/.credentials.yaml 的
|
|
12
|
+
* `client-connection/browser-session` 记录里(同一 DSH_HOME 下的两个引擎共用)。
|
|
13
|
+
* 签名算法:`v1.<b64url(json{version,authority,issuedAt,expiresAt})>.<b64url(hmac-sha256(secretBytes, body))>`,
|
|
14
|
+
* cookie 名是 `dsh-auth-<b64url(sha256(authority))>`,密钥是**解码后的字节**而不是那串 base64。
|
|
15
|
+
*
|
|
16
|
+
* 边界:这是"同一台机器、同一个用户"的自我委托,不是跨用户授权;拿到它等于拿到
|
|
17
|
+
* 该引擎的操作权,所以路由必须连同整条链路一起看成敏感面。
|
|
18
|
+
*/
|
|
19
|
+
import { createHmac, createHash } from "node:crypto";
|
|
20
|
+
import { readFileSync, statSync } from "node:fs";
|
|
21
|
+
|
|
22
|
+
/** 凭据文件里那条记录的键。 */
|
|
23
|
+
const AUTH_RECORD_KEY = "client-connection/browser-session";
|
|
24
|
+
/** cookie 名固定前缀(见 dsh-client-connection)。 */
|
|
25
|
+
const COOKIE_PREFIX = "dsh-auth-";
|
|
26
|
+
/** 签出来的 cookie 给多久:对齐上游默认的 30 天。 */
|
|
27
|
+
const COOKIE_MAX_AGE_MS = 30 * 24 * 3600 * 1000;
|
|
28
|
+
|
|
29
|
+
function b64u(buffer) {
|
|
30
|
+
return Buffer.from(buffer).toString("base64").replaceAll("+", "-").replaceAll("/", "_").replace(/=+$/u, "");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function fromB64u(text) {
|
|
34
|
+
const padded = String(text).replaceAll("-", "+").replaceAll("_", "/");
|
|
35
|
+
return Buffer.from(padded + "=".repeat((4 - (padded.length % 4)) % 4), "base64");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 从凭据 YAML 里抠出 browser-session 的 secret。
|
|
40
|
+
*
|
|
41
|
+
* 用一个只认这种形式的窄解析而不是引入 YAML 依赖:这里读的是本机自己的文件,
|
|
42
|
+
* 结构由 dsh 写死(records → client-connection/browser-session → payload.secret)。
|
|
43
|
+
* @returns {Buffer | undefined} 解码后的密钥字节
|
|
44
|
+
*/
|
|
45
|
+
function parseSecret(text) {
|
|
46
|
+
const at = text.indexOf(`${AUTH_RECORD_KEY}:`);
|
|
47
|
+
if (at === -1) return undefined;
|
|
48
|
+
const rest = text.slice(at);
|
|
49
|
+
const match = /secret:\s*(\S+)/.exec(rest);
|
|
50
|
+
if (!match) return undefined;
|
|
51
|
+
const bytes = fromB64u(match[1]);
|
|
52
|
+
return bytes.byteLength > 0 ? bytes : undefined;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 读(并缓存)浏览器会话密钥;凭据文件改动后自动重读。
|
|
57
|
+
* @param credentialsPath - <DSH_HOME>/.credentials.yaml 的绝对路径
|
|
58
|
+
* @returns {Buffer | undefined}
|
|
59
|
+
*/
|
|
60
|
+
export function createSecretReader(credentialsPath) {
|
|
61
|
+
let cache;
|
|
62
|
+
return () => {
|
|
63
|
+
try {
|
|
64
|
+
const st = statSync(credentialsPath);
|
|
65
|
+
if (cache && cache.mtimeMs === st.mtimeMs && cache.size === st.size) return cache.secret;
|
|
66
|
+
const secret = parseSecret(readFileSync(credentialsPath, "utf8"));
|
|
67
|
+
cache = { mtimeMs: st.mtimeMs, size: st.size, secret };
|
|
68
|
+
return secret;
|
|
69
|
+
} catch {
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 给某个 authority 签一枚会话 cookie。
|
|
77
|
+
* @param secret - 解码后的密钥字节
|
|
78
|
+
* @param authority - 目标 authority,必须与请求的 Host 完全一致(含端口)
|
|
79
|
+
* @returns {{ name: string, header: string }}
|
|
80
|
+
*/
|
|
81
|
+
export function signSessionCookie(secret, authority) {
|
|
82
|
+
const name = COOKIE_PREFIX + b64u(createHash("sha256").update(authority).digest());
|
|
83
|
+
const issuedAt = Date.now();
|
|
84
|
+
const payload = { version: 1, authority, issuedAt, expiresAt: issuedAt + COOKIE_MAX_AGE_MS };
|
|
85
|
+
const body = b64u(Buffer.from(JSON.stringify(payload), "utf8"));
|
|
86
|
+
const signature = b64u(createHmac("sha256", secret).update(body).digest());
|
|
87
|
+
return { name, header: `${name}=v1.${body}.${signature}` };
|
|
88
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 前缀反向代理:把 `/mesh/web/...` 转发到本机另一个 DSH 引擎的 Web 界面。
|
|
3
|
+
*
|
|
4
|
+
* 前缀只在**本机之间**用:对端(手机上的代理)把浏览器请求原样带过来,
|
|
5
|
+
* 这里去掉前缀再打给 loopback。浏览器一侧看到的永远是根的绝对路径,
|
|
6
|
+
* 所以对端前端里的 /api、/plugins 不需要任何改写。
|
|
7
|
+
*/
|
|
8
|
+
import { request as httpRequest } from "node:http";
|
|
9
|
+
import { connect as netConnect } from "node:net";
|
|
10
|
+
import { signSessionCookie } from "./desktop-auth.js";
|
|
11
|
+
|
|
12
|
+
/** 前缀本身。 */
|
|
13
|
+
export const DESKTOP_WEB_PREFIX = "/mesh/web";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 客户端的流式载波(WebSocket):`new WebSocket(remoteStreamUrl(...))` 开的就是它。
|
|
17
|
+
* 少了这条 upgrade 转发,前端会永远停在"自动重连中",工作区与会话列表也就不会出现
|
|
18
|
+
* ——POST 请求都能通,所以症状看起来像"只有一部分功能好了"。
|
|
19
|
+
*/
|
|
20
|
+
export const DESKTOP_WEB_STREAM_PATH = `${DESKTOP_WEB_PREFIX}/api/remote.mux`;
|
|
21
|
+
|
|
22
|
+
/** 去掉前缀;不是这个前缀下的请求返回 undefined。 */
|
|
23
|
+
function remainder(url) {
|
|
24
|
+
if (url === DESKTOP_WEB_PREFIX || url === `${DESKTOP_WEB_PREFIX}/`) return "/";
|
|
25
|
+
if (!url.startsWith(`${DESKTOP_WEB_PREFIX}/`)) return undefined;
|
|
26
|
+
return url.slice(DESKTOP_WEB_PREFIX.length);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 造一个转发 handler。
|
|
31
|
+
* @param options - port(目标引擎端口)、secretOf(读密钥的函数)、log、warn
|
|
32
|
+
* @returns {(req, res) => Promise<void>}
|
|
33
|
+
*/
|
|
34
|
+
export function createDesktopWebForwarder({ port, secretOf, log = () => {}, warn = () => {} }) {
|
|
35
|
+
const authority = `127.0.0.1:${port}`;
|
|
36
|
+
|
|
37
|
+
function forward(req, res, retried) {
|
|
38
|
+
const secret = secretOf();
|
|
39
|
+
if (!secret) {
|
|
40
|
+
res.writeHead(503, { "content-type": "text/plain; charset=utf-8" });
|
|
41
|
+
res.end("读不到本机的浏览器会话密钥(.credentials.yaml 里没有 browser-session 记录)。\n");
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const path = remainder(req.url ?? "/");
|
|
45
|
+
if (path === undefined) {
|
|
46
|
+
res.writeHead(404, { "content-type": "text/plain; charset=utf-8" });
|
|
47
|
+
res.end("not found\n");
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
// 探针:不转发,直接回答"这条链路在不在"。
|
|
51
|
+
if (path === "/__probe") {
|
|
52
|
+
const body = Buffer.from(JSON.stringify({ ok: true, target: authority }), "utf8");
|
|
53
|
+
res.writeHead(200, { "content-type": "application/json; charset=utf-8", "content-length": String(body.length) });
|
|
54
|
+
res.end(req.method === "HEAD" ? undefined : body);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const headers = { ...req.headers, host: authority, cookie: signSessionCookie(secret, authority).header };
|
|
59
|
+
// 浏览器是从"手机代理的 origin"发起的:Origin 与 Host 不一致会被对端的
|
|
60
|
+
// 同源栅栏拒成 403(而插件自己的路由不过栅栏,所以看起来"一部分能用")。
|
|
61
|
+
// 我们本来就是替浏览器发的,这里把来源改写成目标 authority。
|
|
62
|
+
headers.origin = `http://${authority}`;
|
|
63
|
+
if (headers.referer !== undefined) headers.referer = `http://${authority}/`;
|
|
64
|
+
if (String(headers["sec-fetch-site"] ?? "") === "cross-site") headers["sec-fetch-site"] = "same-origin";
|
|
65
|
+
const up = httpRequest({ host: "127.0.0.1", port, method: req.method, path, headers }, (reply) => {
|
|
66
|
+
// 401 = 密钥换过或算错了:重读一次密钥再试一遍(幂等方法)。
|
|
67
|
+
if (reply.statusCode === 401 && !retried && (req.method === "GET" || req.method === "HEAD")) {
|
|
68
|
+
reply.resume();
|
|
69
|
+
forward(req, res, true);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const out = { ...reply.headers };
|
|
73
|
+
// 认证由这里维护,别把对端的 Set-Cookie 泄到上一层。
|
|
74
|
+
delete out["set-cookie"];
|
|
75
|
+
delete out["transfer-encoding"];
|
|
76
|
+
res.writeHead(reply.statusCode ?? 502, out);
|
|
77
|
+
reply.pipe(res);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
up.on("error", (error) => {
|
|
81
|
+
warn(`桌面引擎转发失败(${authority}):${error?.message ?? error}`);
|
|
82
|
+
if (res.headersSent) {
|
|
83
|
+
res.destroy();
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
res.writeHead(502, { "content-type": "text/plain; charset=utf-8" });
|
|
87
|
+
res.end(`连不上本机 ${authority}:${error?.message ?? error}\n`);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
if (req.readableEnded) up.end();
|
|
91
|
+
else req.pipe(up);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return async function handle(req, res) {
|
|
95
|
+
log(`${req.method ?? "GET"} ${req.url ?? "/"} → ${authority}`);
|
|
96
|
+
forward(req, res, false);
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* 造一个 upgrade 转发器:把流式端点的握手原样转给本机 loopback 引擎。
|
|
103
|
+
* @param options - port、secretOf、log、warn
|
|
104
|
+
* @returns {(req, socket, head) => void}
|
|
105
|
+
*/
|
|
106
|
+
export function createDesktopWebUpgrade({ port, secretOf, log = () => {}, warn = () => {} }) {
|
|
107
|
+
const authority = `127.0.0.1:${port}`;
|
|
108
|
+
return function handleUpgrade(req, socket, head) {
|
|
109
|
+
const secret = secretOf();
|
|
110
|
+
if (!secret) {
|
|
111
|
+
warn("读不到会话密钥,无法转发 WebSocket");
|
|
112
|
+
socket.destroy();
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
const url = req.url ?? "/";
|
|
116
|
+
const path = url.startsWith(DESKTOP_WEB_PREFIX) ? url.slice(DESKTOP_WEB_PREFIX.length) || "/" : url;
|
|
117
|
+
try {
|
|
118
|
+
const h = req.headers;
|
|
119
|
+
appendFileSync("/tmp/mesh-web.log", `UPGRADE-REQ ${path} ext=${h["sec-websocket-extensions"] ?? "-"} origin=${h.origin ?? "-"} ua=${String(h["user-agent"] ?? "-").slice(0, 30)}\n`);
|
|
120
|
+
} catch {}
|
|
121
|
+
log(`UPGRADE ${path} → ${authority}`);
|
|
122
|
+
const target = netConnect(port, "127.0.0.1", () => {
|
|
123
|
+
const lines = [`${req.method} ${path} HTTP/1.1`];
|
|
124
|
+
for (const [key, value] of Object.entries(req.headers)) {
|
|
125
|
+
if (value === undefined) continue;
|
|
126
|
+
const name = key.toLowerCase();
|
|
127
|
+
// 这几个由代理改写(见 HTTP 分支同样的理由:同源栅栏 + 认证)。
|
|
128
|
+
if (name === "host" || name === "cookie" || name === "origin" || name === "referer" || name === "sec-fetch-site") continue;
|
|
129
|
+
for (const item of Array.isArray(value) ? value : [value]) lines.push(`${key}: ${item}`);
|
|
130
|
+
}
|
|
131
|
+
lines.push(`host: ${authority}`);
|
|
132
|
+
lines.push(`cookie: ${signSessionCookie(secret, authority).header}`);
|
|
133
|
+
lines.push(`origin: http://${authority}`);
|
|
134
|
+
lines.push("sec-fetch-site: same-origin");
|
|
135
|
+
target.write(`${lines.join("\r\n")}\r\n\r\n`);
|
|
136
|
+
if (head && head.length) target.write(head);
|
|
137
|
+
target.pipe(socket);
|
|
138
|
+
socket.pipe(target);
|
|
139
|
+
});
|
|
140
|
+
target.on("error", (error) => {
|
|
141
|
+
warn(`WebSocket 转发失败:${error?.message ?? error}`);
|
|
142
|
+
socket.destroy();
|
|
143
|
+
});
|
|
144
|
+
socket.on("error", () => target.destroy());
|
|
145
|
+
};
|
|
146
|
+
}
|