dsh-plugin-mesh 0.5.2 → 0.5.3
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/client/client.js +72 -1
- package/lib/_selftest-capabilities.mjs +43 -0
- package/lib/_selftest-diagnostics.mjs +92 -0
- package/lib/_selftest-osinfo.mjs +379 -0
- package/lib/_selftest-workbench.mjs +53 -0
- package/lib/capabilities.js +181 -25
- package/lib/diagnostics.js +109 -0
- package/lib/discovery.js +17 -0
- package/lib/index.js +64 -7
- package/lib/osinfo.js +352 -0
- package/llms.txt +2 -0
- package/package.json +4 -2
package/client/client.js
CHANGED
|
@@ -136,6 +136,42 @@ window.__ModuleLoader__.load({
|
|
|
136
136
|
return String(os).split(/\s+/)[0];
|
|
137
137
|
}
|
|
138
138
|
|
|
139
|
+
/** 结构化系统身份:新对端有 osInfo,老对端只有 os 字符串,回退解析。 */
|
|
140
|
+
function osInfoLabel(peer) {
|
|
141
|
+
const info = peer && peer.osInfo;
|
|
142
|
+
if (!info) return osLabel(peer && peer.os);
|
|
143
|
+
const label = [info.name, info.version].filter(Boolean).join(" ");
|
|
144
|
+
return label || osLabel(peer && peer.os);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* 一台机器上的「额外系统」(目前是 WSL 发行版)。
|
|
149
|
+
* 宿主机不在这里返回 —— 它已经由 osInfoLabel 表示了。
|
|
150
|
+
*/
|
|
151
|
+
function extraSystems(peer) {
|
|
152
|
+
const list = peer && Array.isArray(peer.systems) ? peer.systems : [];
|
|
153
|
+
return list
|
|
154
|
+
.filter((s) => s && s.kind && s.kind !== "host")
|
|
155
|
+
.map((s, i) => ({
|
|
156
|
+
key: `${s.kind}-${s.name || i}`,
|
|
157
|
+
kind: s.kind,
|
|
158
|
+
text: [s.kind === "wsl" ? "WSL" : s.kind, s.name, s.version].filter(Boolean).join(" "),
|
|
159
|
+
}));
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** WSL 发行版里的运行时,压成一行;没有就返回空串。 */
|
|
163
|
+
function wslSummary(caps) {
|
|
164
|
+
const list = caps && Array.isArray(caps.wsl) ? caps.wsl : [];
|
|
165
|
+
return list
|
|
166
|
+
.map((w) => {
|
|
167
|
+
const rts = Object.entries(w.runtimes || {})
|
|
168
|
+
.map(([n, v]) => `${n} ${v}`)
|
|
169
|
+
.join("、");
|
|
170
|
+
return `${w.name}${rts ? `:${rts}` : ""}`;
|
|
171
|
+
})
|
|
172
|
+
.join(" · ");
|
|
173
|
+
}
|
|
174
|
+
|
|
139
175
|
function shortAddress(host, port) {
|
|
140
176
|
if (!host) return "地址未知";
|
|
141
177
|
return port ? `${host}:${port}` : String(host);
|
|
@@ -533,6 +569,9 @@ window.__ModuleLoader__.load({
|
|
|
533
569
|
const ts = (state && state.tsnet) || {};
|
|
534
570
|
return [
|
|
535
571
|
`Mesh ${self.version || ""}`.trim(),
|
|
572
|
+
self.diskVersion && self.diskVersion !== self.version
|
|
573
|
+
? `disk: ${self.diskVersion} (已升级,进程未重启)`
|
|
574
|
+
: "",
|
|
536
575
|
`machine: ${self.machine || ""}`,
|
|
537
576
|
`address: ${shortAddress(self.host, self.port)}`,
|
|
538
577
|
`hostname: ${self.hostname || ""}`,
|
|
@@ -2574,6 +2613,15 @@ window.__ModuleLoader__.load({
|
|
|
2574
2613
|
: null,
|
|
2575
2614
|
!runtimes.length ? h("span", { className: "mesh-chip is-muted" }, "未探到运行时") : null,
|
|
2576
2615
|
),
|
|
2616
|
+
// WSL 发行版里的运行时单独一行:Windows 上的 Python 和 WSL 里的 Python
|
|
2617
|
+
// 是两个环境,混进同一排 chip 会让人以为"装过了",从而把任务派到没有它的地方。
|
|
2618
|
+
wslSummary(caps)
|
|
2619
|
+
? h(
|
|
2620
|
+
"div",
|
|
2621
|
+
{ className: "mesh-chips", style: { marginTop: 6 } },
|
|
2622
|
+
h("span", { className: "mesh-chip is-muted" }, `WSL · ${wslSummary(caps)}`),
|
|
2623
|
+
)
|
|
2624
|
+
: null,
|
|
2577
2625
|
runtimes.length > 6
|
|
2578
2626
|
? h(
|
|
2579
2627
|
"button",
|
|
@@ -2904,10 +2952,22 @@ window.__ModuleLoader__.load({
|
|
|
2904
2952
|
"div",
|
|
2905
2953
|
{ className: "mesh-meta" },
|
|
2906
2954
|
h(Status, { kind: online ? "ok" : "idle" }, isSelf ? "运行中" : online ? "在线" : "离线"),
|
|
2955
|
+
h("span", null, osInfoLabel(peer)),
|
|
2907
2956
|
h("span", { className: "mesh-address" }, address),
|
|
2908
2957
|
isSelf ? null : h("span", null, `最后心跳 ${ago(peer.lastSeen, now)}`),
|
|
2909
2958
|
peer.capabilitiesStale ? h(Badge, { kind: "warn" }, "能力可能过期") : null,
|
|
2910
2959
|
),
|
|
2960
|
+
// 同一台机器上的其它系统(WSL)。分开显示:Windows 上的 Python 和
|
|
2961
|
+
// WSL 里的 Python 是两个环境,混成一行会让 Placement 判断错。
|
|
2962
|
+
extraSystems(peer).length
|
|
2963
|
+
? h(
|
|
2964
|
+
"div",
|
|
2965
|
+
{ className: "mesh-meta" },
|
|
2966
|
+
extraSystems(peer).map((sys) =>
|
|
2967
|
+
h("span", { key: sys.key, className: "mesh-chip" }, h("span", { className: "mesh-chip-label" }, sys.text)),
|
|
2968
|
+
),
|
|
2969
|
+
)
|
|
2970
|
+
: null,
|
|
2911
2971
|
),
|
|
2912
2972
|
h(
|
|
2913
2973
|
"div",
|
|
@@ -4392,9 +4452,19 @@ window.__ModuleLoader__.load({
|
|
|
4392
4452
|
h("dt", null, "端口"), h("dd", null, String(self.port || "—")),
|
|
4393
4453
|
h("dt", null, "主机名"), h("dd", null, self.hostname || "—"),
|
|
4394
4454
|
h("dt", null, "MAC"), h("dd", null, self.mac || "—"),
|
|
4395
|
-
h("dt", null, "
|
|
4455
|
+
h("dt", null, "版本(进程)"), h("dd", null, self.version || "—"),
|
|
4456
|
+
// 进程版本 ≠ 磁盘版本 = 升级了没重启。光看一个版本号永远发现不了,
|
|
4457
|
+
// 于是用户会拿旧行为当新版本的 bug 报上来(真踩过)。
|
|
4458
|
+
h("dt", null, "版本(磁盘)"), h("dd", null, self.diskVersion || "—"),
|
|
4396
4459
|
h("dt", null, "对端"), h("dd", null, `${peers.length} 台,在线 ${peers.filter((p) => p && p.online).length} 台`),
|
|
4397
4460
|
),
|
|
4461
|
+
self.versionDrift
|
|
4462
|
+
? h(
|
|
4463
|
+
"div",
|
|
4464
|
+
{ className: "mesh-notice", role: "status" },
|
|
4465
|
+
`磁盘上是 ${self.diskVersion},当前进程还在跑 ${self.version}:重启 DSH 后新版本才生效。`,
|
|
4466
|
+
)
|
|
4467
|
+
: null,
|
|
4398
4468
|
h(
|
|
4399
4469
|
"div",
|
|
4400
4470
|
{ className: "mesh-actions-row" },
|
|
@@ -6567,6 +6637,7 @@ window.__ModuleLoader__.load({
|
|
|
6567
6637
|
TaskRow,
|
|
6568
6638
|
TaskDetail,
|
|
6569
6639
|
SettingsView,
|
|
6640
|
+
Diagnostics,
|
|
6570
6641
|
Composer,
|
|
6571
6642
|
TopBar,
|
|
6572
6643
|
TransferLayer,
|
|
@@ -37,6 +37,7 @@ import {
|
|
|
37
37
|
__hasUncertainProbe,
|
|
38
38
|
__probeFailCount,
|
|
39
39
|
__isUncertain,
|
|
40
|
+
__gpuProbeSpecs,
|
|
40
41
|
} from "./capabilities.js";
|
|
41
42
|
import { MeshDiscovery, APP_TAG } from "./discovery.js";
|
|
42
43
|
|
|
@@ -107,6 +108,30 @@ test("parseWindowsGpu 取第一行(多显卡时)", () => {
|
|
|
107
108
|
assert.equal(g.name, "Intel(R) UHD Graphics");
|
|
108
109
|
});
|
|
109
110
|
|
|
111
|
+
test("parseWindowsGpu 取第一条**真卡**(虚拟适配器排在前面时)", () => {
|
|
112
|
+
// 现场复现(真机):MuMu / GameViewer / Parsec 这类虚拟显示驱动常常排在
|
|
113
|
+
// 真卡前面,原实现"取第一行"于是把 3060 的机器报成 MuMuVMM。
|
|
114
|
+
const g = parseWindowsGpu("MuMuVMM Display Adapter\r\nNVIDIA GeForce RTX 3060");
|
|
115
|
+
assert.equal(g.name, "NVIDIA GeForce RTX 3060");
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test("parseWindowsGpu 全是虚拟适配器时退回第一行,而不是说「没有 GPU」", () => {
|
|
119
|
+
// 「只有虚拟显卡」是一台真机器的真实状态;报 null 等于"我们不知道",
|
|
120
|
+
// 会让 Placement 把一台能用的机器当成没探过。退回第一行至少是真话。
|
|
121
|
+
const g = parseWindowsGpu("Parsec Virtual Display Adapter\r\nBasic Display Adapter");
|
|
122
|
+
assert.equal(g.present, true);
|
|
123
|
+
assert.equal(g.name, "Parsec Virtual Display Adapter");
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test("GPU 探测候选链:Windows 先问 nvidia-smi,再退回 powershell", () => {
|
|
127
|
+
const win = __gpuProbeSpecs("win32");
|
|
128
|
+
assert.equal(win.length, 2, "Windows 应该有两个候选");
|
|
129
|
+
assert.equal(win[0].cmd, "nvidia-smi", "先问最可信的 nvidia-smi(只报真 N 卡)");
|
|
130
|
+
assert.equal(win[1].cmd, "powershell");
|
|
131
|
+
assert.equal(__gpuProbeSpecs("darwin").length, 1);
|
|
132
|
+
assert.equal(__gpuProbeSpecs("plan9").length, 0, "不认识的平台不该凭空造命令");
|
|
133
|
+
});
|
|
134
|
+
|
|
110
135
|
test("传入非字符串(对象)→ 返回 null「未知」,不是「确定没有」也不是乱码", () => {
|
|
111
136
|
// ⚠️ 这就是真实踩到的回归:runProbe 的返回值从 string 改成 {output,error}
|
|
112
137
|
// 之后 collectGpu 漏改,把**对象**交给了解析函数。旧实现里 String(对象) 变成
|
|
@@ -652,6 +677,24 @@ test("探测没探准 → 标出来,并说明不能据此断定「没有」",
|
|
|
652
677
|
assert.ok(s.includes("不能据此断定"), s);
|
|
653
678
|
});
|
|
654
679
|
|
|
680
|
+
test("sanitizeCapabilities:保留并清洗 WSL 运行时", () => {
|
|
681
|
+
const caps = sanitizeCapabilities({
|
|
682
|
+
runtimes: { node: "20.19" },
|
|
683
|
+
wsl: [
|
|
684
|
+
{ name: "Ubuntu", runtimes: { node: "22.1", python: "3.12" }, error: "" },
|
|
685
|
+
{ name: "", runtimes: { node: "1.0" } },
|
|
686
|
+
{ name: "Debian", runtimes: { git: "2.43" }, error: "x".repeat(100) },
|
|
687
|
+
],
|
|
688
|
+
});
|
|
689
|
+
assert.equal(caps.wsl.length, 2, "空名字的项应被丢掉");
|
|
690
|
+
assert.equal(caps.wsl[0].name, "Ubuntu");
|
|
691
|
+
assert.equal(caps.wsl[0].runtimes.node, "22.1");
|
|
692
|
+
assert.equal(caps.wsl[1].runtimes.git, "2.43");
|
|
693
|
+
assert.ok(caps.wsl[1].error.length <= 80);
|
|
694
|
+
});
|
|
695
|
+
|
|
696
|
+
console.log("\n── 指纹认领的边界:同机多实例不该互相顶掉 ──");
|
|
697
|
+
|
|
655
698
|
console.log("\n── 指纹认领的边界:同机多实例不该互相顶掉 ──");
|
|
656
699
|
|
|
657
700
|
test("同指纹 + 同名(重装场景)→ 认领,旧记录被顶掉", () => {
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 诊断块自测(`lib/diagnostics.js`)。
|
|
3
|
+
*
|
|
4
|
+
* 这组断言盯着一个反复踩的坑:**"进程版本 ≠ 磁盘版本"肉眼看不出来**。
|
|
5
|
+
* 磁盘已经升到 0.5.3、宿主进程还在跑 0.5.2 时,界面必须能自己说清楚,
|
|
6
|
+
* 否则用户会把旧行为当成新版本的 bug 报上来。
|
|
7
|
+
*
|
|
8
|
+
* node lib/_selftest-diagnostics.mjs
|
|
9
|
+
*/
|
|
10
|
+
import { strict as assert } from "node:assert";
|
|
11
|
+
import { fileURLToPath } from "node:url";
|
|
12
|
+
import { dirname, join } from "node:path";
|
|
13
|
+
import { buildDiagnostics, entryInfo, readPackageVersion } from "./diagnostics.js";
|
|
14
|
+
|
|
15
|
+
const HERE = dirname(fileURLToPath(import.meta.url));
|
|
16
|
+
const ENTRY = new URL("index.js", new URL(`file://${HERE}/`)).href;
|
|
17
|
+
|
|
18
|
+
let passed = 0;
|
|
19
|
+
let failed = 0;
|
|
20
|
+
function test(name, fn) {
|
|
21
|
+
try {
|
|
22
|
+
fn();
|
|
23
|
+
passed += 1;
|
|
24
|
+
console.log(` ✓ ${name}`);
|
|
25
|
+
} catch (err) {
|
|
26
|
+
failed += 1;
|
|
27
|
+
console.log(` ✗ ${name}`);
|
|
28
|
+
console.log(` ${err?.message ?? err}`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
console.log("\n── 版本漂移:进程 vs 磁盘 ──");
|
|
33
|
+
|
|
34
|
+
test("版本一致时 versionMatches=true,且不报磁盘更新", () => {
|
|
35
|
+
const d = buildDiagnostics({ version: "0.5.3", packageVersion: "0.5.3", entryMtimeMs: 0, startedAt: Date.now() });
|
|
36
|
+
assert.equal(d.versionMatches, true);
|
|
37
|
+
assert.equal(d.diskNewerThanProcess, false);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("升级了没重启:versionMatches=false,能看出磁盘比进程新", () => {
|
|
41
|
+
const startedAt = Date.now() - 60_000;
|
|
42
|
+
const d = buildDiagnostics({ version: "0.5.2", packageVersion: "0.5.3", entryMtimeMs: Date.now(), startedAt });
|
|
43
|
+
assert.equal(d.versionMatches, false);
|
|
44
|
+
assert.equal(d.diskNewerThanProcess, true, "入口文件比进程启动还新 → 就是没重启");
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test("入口 mtime 拿不到时不能诬告「磁盘更新」", () => {
|
|
48
|
+
const d = buildDiagnostics({ version: "0.5.3", packageVersion: "0.5.3", entryMtimeMs: 0, startedAt: Date.now() });
|
|
49
|
+
assert.equal(d.diskNewerThanProcess, false, "mtime 未知 ≠ 磁盘更新");
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test("空版本号不算匹配(宁可说不知道)", () => {
|
|
53
|
+
const d = buildDiagnostics({ version: "", packageVersion: "0.5.3" });
|
|
54
|
+
assert.equal(d.versionMatches, false);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
console.log("\n── 不泄露密钥 ──");
|
|
58
|
+
|
|
59
|
+
test("config 只挑安全字段,tsnetAuthKey / secret 一律不出现", () => {
|
|
60
|
+
const d = buildDiagnostics({
|
|
61
|
+
version: "0.5.3",
|
|
62
|
+
packageVersion: "0.5.3",
|
|
63
|
+
config: { port: 45917, requirePairing: true, tsnetAuthKey: "tskey-auth-SECRET", secret: "s2", apiToken: "tok" },
|
|
64
|
+
});
|
|
65
|
+
assert.equal(d.config.port, 45917);
|
|
66
|
+
assert.equal(d.config.requirePairing, true);
|
|
67
|
+
const text = JSON.stringify(d);
|
|
68
|
+
assert.ok(!text.includes("SECRET"), "tsnet 授权码泄进了诊断块");
|
|
69
|
+
assert.ok(!text.includes("\"tok\""), "apiToken 泄进了诊断块");
|
|
70
|
+
assert.ok(!("secret" in d.config));
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
console.log("\n── 真实磁盘读取 ──");
|
|
74
|
+
|
|
75
|
+
test("readPackageVersion 读得到本仓库版本号", () => {
|
|
76
|
+
const v = readPackageVersion(ENTRY);
|
|
77
|
+
assert.match(v, /^\d+\.\d+\.\d+/, `读到的版本号是 ${JSON.stringify(v)}`);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test("entryInfo 给出真实路径与 mtime", () => {
|
|
81
|
+
const info = entryInfo(ENTRY);
|
|
82
|
+
assert.ok(info.path.endsWith(join("lib", "index.js")), info.path);
|
|
83
|
+
assert.ok(info.mtimeMs > 0);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test("入口 URL 是垃圾时安静返回空,不抛", () => {
|
|
87
|
+
assert.equal(readPackageVersion("not-a-url"), "");
|
|
88
|
+
assert.deepEqual(entryInfo("not-a-url"), { path: "", mtimeMs: 0 });
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
console.log(`\n结果:${passed} 通过,${failed} 失败`);
|
|
92
|
+
if (failed > 0) process.exit(1);
|
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* osInfo 的自测:纯 fixture,不碰真实系统。
|
|
3
|
+
*
|
|
4
|
+
* 为什么不用真实采集:macOS/Android 的分支在开发机上跑不到;这些平台差异
|
|
5
|
+
* 正是最容易出 bug 的地方(sw_vers 输出、os-release 引号、getprop 换行)。
|
|
6
|
+
*/
|
|
7
|
+
import assert from "node:assert/strict";
|
|
8
|
+
import { detectOsInfo, sanitizeOsInfo, detectSystems, sanitizeSystems } from "./osinfo.js";
|
|
9
|
+
import { MeshDiscovery } from "./discovery.js";
|
|
10
|
+
|
|
11
|
+
let passed = 0;
|
|
12
|
+
let failed = 0;
|
|
13
|
+
function check(label, fn) {
|
|
14
|
+
try {
|
|
15
|
+
fn();
|
|
16
|
+
passed += 1;
|
|
17
|
+
console.log(` ✓ ${label}`);
|
|
18
|
+
} catch (err) {
|
|
19
|
+
failed += 1;
|
|
20
|
+
console.log(` ✗ ${label}`);
|
|
21
|
+
console.log(` ${err?.message ?? err}`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
console.log("\n── 平台分支:四个系统都要给出可读名字/版本 ──");
|
|
26
|
+
|
|
27
|
+
check("win32:os.version() 是产品名,release 是版本", () => {
|
|
28
|
+
const info = detectOsInfo({
|
|
29
|
+
platform: "win32",
|
|
30
|
+
release: "10.0.26200",
|
|
31
|
+
arch: "x64",
|
|
32
|
+
osVersion: () => "Windows 11 Home China",
|
|
33
|
+
});
|
|
34
|
+
assert.equal(info.platform, "win32");
|
|
35
|
+
assert.equal(info.name, "Windows 11 Home China");
|
|
36
|
+
assert.equal(info.version, "10.0.26200");
|
|
37
|
+
assert.equal(info.kernel, "10.0.26200");
|
|
38
|
+
assert.equal(info.arch, "x64");
|
|
39
|
+
assert.equal(info.distro, "");
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
check("win32:os.version() 拿不到时退回 Windows,不抛", () => {
|
|
43
|
+
const info = detectOsInfo({ platform: "win32", release: "10.0.19045", arch: "x64", osVersion: () => "" });
|
|
44
|
+
assert.equal(info.name, "Windows");
|
|
45
|
+
assert.equal(info.version, "10.0.19045");
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
check("darwin:sw_vers 的 productName/productVersion", () => {
|
|
49
|
+
const info = detectOsInfo({
|
|
50
|
+
platform: "darwin",
|
|
51
|
+
release: "24.1.0",
|
|
52
|
+
arch: "arm64",
|
|
53
|
+
run: (_cmd, args) => (args[0] === "-productName" ? "macOS" : "15.1"),
|
|
54
|
+
});
|
|
55
|
+
assert.equal(info.platform, "darwin");
|
|
56
|
+
assert.equal(info.name, "macOS");
|
|
57
|
+
assert.equal(info.version, "15.1");
|
|
58
|
+
assert.equal(info.kernel, "24.1.0");
|
|
59
|
+
assert.equal(info.arch, "arm64");
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
check("darwin:sw_vers 不可用时仍有 macOS 名字", () => {
|
|
63
|
+
const info = detectOsInfo({
|
|
64
|
+
platform: "darwin",
|
|
65
|
+
release: "24.1.0",
|
|
66
|
+
arch: "arm64",
|
|
67
|
+
run: () => {
|
|
68
|
+
throw new Error("ENOENT");
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
assert.equal(info.name, "macOS");
|
|
72
|
+
assert.equal(info.version, "");
|
|
73
|
+
assert.equal(info.kernel, "24.1.0");
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
check("linux:解析 /etc/os-release 的 NAME/VERSION_ID/ID", () => {
|
|
77
|
+
const fixture = [
|
|
78
|
+
'PRETTY_NAME="Ubuntu 26.04 LTS"',
|
|
79
|
+
'NAME="Ubuntu"',
|
|
80
|
+
'VERSION_ID="26.04"',
|
|
81
|
+
"ID=ubuntu",
|
|
82
|
+
"",
|
|
83
|
+
].join("\n");
|
|
84
|
+
const info = detectOsInfo({
|
|
85
|
+
platform: "linux",
|
|
86
|
+
release: "6.6.114.1-microsoft-standard-WSL2",
|
|
87
|
+
arch: "x64",
|
|
88
|
+
env: { XDG_CURRENT_DESKTOP: "ubuntu:GNOME" },
|
|
89
|
+
readFile: () => fixture,
|
|
90
|
+
});
|
|
91
|
+
assert.equal(info.platform, "linux");
|
|
92
|
+
assert.equal(info.name, "Ubuntu");
|
|
93
|
+
assert.equal(info.version, "26.04");
|
|
94
|
+
assert.equal(info.distro, "ubuntu");
|
|
95
|
+
assert.equal(info.desktop, "GNOME");
|
|
96
|
+
assert.equal(info.kernel, "6.6.114.1-microsoft-standard-WSL2");
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
check("linux:os-release 缺失时退回 Linux,desktop 可为空", () => {
|
|
100
|
+
const info = detectOsInfo({
|
|
101
|
+
platform: "linux",
|
|
102
|
+
release: "6.1.0",
|
|
103
|
+
arch: "x64",
|
|
104
|
+
env: {},
|
|
105
|
+
readFile: () => {
|
|
106
|
+
throw new Error("ENOENT");
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
assert.equal(info.name, "Linux");
|
|
110
|
+
assert.equal(info.version, "");
|
|
111
|
+
assert.equal(info.desktop, "");
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
check("android:getprop 的版本号,带换行也能取对", () => {
|
|
115
|
+
const info = detectOsInfo({
|
|
116
|
+
platform: "android",
|
|
117
|
+
release: "5.10.101-android13",
|
|
118
|
+
arch: "arm64",
|
|
119
|
+
run: () => "14\n",
|
|
120
|
+
});
|
|
121
|
+
assert.equal(info.platform, "android");
|
|
122
|
+
assert.equal(info.name, "Android");
|
|
123
|
+
assert.equal(info.version, "14");
|
|
124
|
+
assert.equal(info.arch, "arm64");
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
check("未知平台:给可读名字,platform 留空(UI 走回退徽章)", () => {
|
|
128
|
+
const info = detectOsInfo({ platform: "freebsd", release: "14.0", arch: "x64" });
|
|
129
|
+
assert.equal(info.platform, "");
|
|
130
|
+
assert.equal(info.name, "freebsd");
|
|
131
|
+
assert.equal(info.version, "14.0");
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
console.log("\n── sanitizeOsInfo:对端名片是网络输入 ──");
|
|
135
|
+
|
|
136
|
+
check("platform 白名单:未知值被清空", () => {
|
|
137
|
+
const info = sanitizeOsInfo({ platform: "plan9", name: "Plan 9" });
|
|
138
|
+
assert.equal(info.platform, "");
|
|
139
|
+
assert.equal(info.name, "Plan 9");
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
check("控制字符剥掉、空白折叠、字段截断", () => {
|
|
143
|
+
const info = sanitizeOsInfo({
|
|
144
|
+
platform: "linux",
|
|
145
|
+
name: ` Ubuntu\u0000\t${"x".repeat(80)} `,
|
|
146
|
+
version: "24.04\n",
|
|
147
|
+
kernel: "k".repeat(100),
|
|
148
|
+
desktop: "GNOME",
|
|
149
|
+
arch: "x64",
|
|
150
|
+
});
|
|
151
|
+
assert.ok(!info.name.includes("\u0000"));
|
|
152
|
+
assert.ok(info.name.length <= 48, `name 未截断: ${info.name.length}`);
|
|
153
|
+
assert.equal(info.version, "24.04");
|
|
154
|
+
assert.ok(info.kernel.length <= 64);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
check("空对象 / 垃圾输入 → null(调用方回退老 os 字段)", () => {
|
|
158
|
+
assert.equal(sanitizeOsInfo(null), null);
|
|
159
|
+
assert.equal(sanitizeOsInfo(undefined), null);
|
|
160
|
+
assert.equal(sanitizeOsInfo("win32"), null);
|
|
161
|
+
assert.equal(sanitizeOsInfo([]), null);
|
|
162
|
+
assert.equal(sanitizeOsInfo({}), null);
|
|
163
|
+
assert.equal(sanitizeOsInfo({ platform: "", name: "" }), null);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
check("老对端只报 os 字符串:没有 osInfo 时不伪造", () => {
|
|
167
|
+
assert.equal(sanitizeOsInfo(undefined), null);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
console.log("\n── systems:宿主机 + WSL 发行版(只读注册表,不启动发行版)──");
|
|
171
|
+
|
|
172
|
+
const WSL_REG = [
|
|
173
|
+
"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Lxss",
|
|
174
|
+
" DefaultVersion REG_DWORD 0x2",
|
|
175
|
+
"",
|
|
176
|
+
"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Lxss\\{1111}",
|
|
177
|
+
" State REG_DWORD 0x1",
|
|
178
|
+
" DistributionName REG_SZ Ubuntu",
|
|
179
|
+
" Flavor REG_SZ ubuntu",
|
|
180
|
+
" OsVersion REG_SZ 26.04",
|
|
181
|
+
"",
|
|
182
|
+
"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Lxss\\{2222}",
|
|
183
|
+
" State REG_DWORD 0x1",
|
|
184
|
+
" DistributionName REG_SZ docker-desktop",
|
|
185
|
+
].join("\n");
|
|
186
|
+
|
|
187
|
+
const winOpts = (run, readFile) => ({
|
|
188
|
+
platform: "win32",
|
|
189
|
+
release: "10.0.26200",
|
|
190
|
+
arch: "x64",
|
|
191
|
+
osVersion: () => "Windows 11 Home China",
|
|
192
|
+
run,
|
|
193
|
+
readFile,
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
check("win32:宿主机 + Ubuntu,docker-desktop 被过滤,且有版本时不该读 UNC", () => {
|
|
197
|
+
const list = detectSystems(
|
|
198
|
+
winOpts(
|
|
199
|
+
() => WSL_REG,
|
|
200
|
+
() => {
|
|
201
|
+
throw new Error("不该在读不到的情况下访问 UNC");
|
|
202
|
+
},
|
|
203
|
+
),
|
|
204
|
+
);
|
|
205
|
+
assert.equal(list.length, 2);
|
|
206
|
+
assert.equal(list[0].kind, "host");
|
|
207
|
+
assert.equal(list[0].name, "Windows 11 Home China");
|
|
208
|
+
assert.equal(list[1].kind, "wsl");
|
|
209
|
+
assert.equal(list[1].platform, "linux");
|
|
210
|
+
assert.equal(list[1].name, "Ubuntu");
|
|
211
|
+
assert.equal(list[1].version, "26.04");
|
|
212
|
+
assert.equal(list[1].distro, "ubuntu");
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
check("WSL 注册表缺 OsVersion 时,运行中的发行版读 /etc/os-release 补版本", () => {
|
|
216
|
+
const reg = [
|
|
217
|
+
"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Lxss\\{3333}",
|
|
218
|
+
" State REG_DWORD 0x1",
|
|
219
|
+
" DistributionName REG_SZ Debian",
|
|
220
|
+
].join("\n");
|
|
221
|
+
const list = detectSystems(
|
|
222
|
+
winOpts(
|
|
223
|
+
() => reg,
|
|
224
|
+
(path) => {
|
|
225
|
+
assert.match(path, /Debian/);
|
|
226
|
+
return 'NAME="Debian"\nVERSION_ID="12"\nID=debian\n';
|
|
227
|
+
},
|
|
228
|
+
),
|
|
229
|
+
);
|
|
230
|
+
assert.equal(list.length, 2);
|
|
231
|
+
assert.equal(list[1].name, "Debian");
|
|
232
|
+
assert.equal(list[1].version, "12");
|
|
233
|
+
assert.equal(list[1].distro, "debian");
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
check("停着的 WSL 发行版不读 UNC(不启动它),版本留空", () => {
|
|
237
|
+
const reg = [
|
|
238
|
+
"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Lxss\\{4444}",
|
|
239
|
+
" State REG_DWORD 0x0",
|
|
240
|
+
" DistributionName REG_SZ openSUSE",
|
|
241
|
+
].join("\n");
|
|
242
|
+
const list = detectSystems(
|
|
243
|
+
winOpts(
|
|
244
|
+
() => reg,
|
|
245
|
+
() => {
|
|
246
|
+
throw new Error("停着的发行版不该被访问");
|
|
247
|
+
},
|
|
248
|
+
),
|
|
249
|
+
);
|
|
250
|
+
assert.equal(list.length, 2);
|
|
251
|
+
assert.equal(list[1].name, "openSUSE");
|
|
252
|
+
assert.equal(list[1].version, "");
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
check("没有 WSL 注册表 → 只报宿主机", () => {
|
|
256
|
+
const list = detectSystems(
|
|
257
|
+
winOpts(
|
|
258
|
+
() => {
|
|
259
|
+
throw new Error("ERROR: The system was unable to find the specified registry key");
|
|
260
|
+
},
|
|
261
|
+
() => "",
|
|
262
|
+
),
|
|
263
|
+
);
|
|
264
|
+
assert.equal(list.length, 1);
|
|
265
|
+
assert.equal(list[0].kind, "host");
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
check("非 win32:不探测 WSL,只报宿主机", () => {
|
|
269
|
+
const list = detectSystems({
|
|
270
|
+
platform: "linux",
|
|
271
|
+
release: "6.1.0",
|
|
272
|
+
arch: "x64",
|
|
273
|
+
env: {},
|
|
274
|
+
readFile: () => 'NAME="Debian"\nVERSION_ID="12"\nID=debian\n',
|
|
275
|
+
run: () => {
|
|
276
|
+
throw new Error("非 Windows 不该跑 reg");
|
|
277
|
+
},
|
|
278
|
+
});
|
|
279
|
+
assert.equal(list.length, 1);
|
|
280
|
+
assert.equal(list[0].kind, "host");
|
|
281
|
+
assert.equal(list[0].name, "Debian");
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
check("sanitizeSystems:去重、kind 白名单、最多 4 项、宿主机排第一", () => {
|
|
285
|
+
const list = sanitizeSystems([
|
|
286
|
+
{ kind: "wsl", platform: "linux", name: "Ubuntu", version: "24.04" },
|
|
287
|
+
{ kind: "host", platform: "win32", name: "Win", version: "11" },
|
|
288
|
+
{ kind: "wsl", platform: "linux", name: "Ubuntu", version: "24.04" },
|
|
289
|
+
{ kind: "vm", platform: "linux", name: "Debian", version: "12" },
|
|
290
|
+
{ kind: "container", platform: "linux", name: "Alpine", version: "3.20" },
|
|
291
|
+
{ kind: "nope", platform: "linux", name: "Other", version: "1" },
|
|
292
|
+
]);
|
|
293
|
+
assert.equal(list.length, 4);
|
|
294
|
+
assert.equal(list[0].kind, "host");
|
|
295
|
+
assert.equal(list[0].name, "Win");
|
|
296
|
+
assert.ok(!list.some((s) => s.name === "Other"), "第 5 个及以后的系统应被截断");
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
console.log("\n── 名片收发:selfRecord 带 osInfo,对端 osInfo 会被清洗 ──");
|
|
300
|
+
|
|
301
|
+
check("selfRecord 带本机 osInfo", () => {
|
|
302
|
+
const d = new MeshDiscovery({ id: "self-os", port: 0, meta: { machine: "me" } });
|
|
303
|
+
const info = d.selfRecord().osInfo;
|
|
304
|
+
assert.ok(info, "selfRecord 应带 osInfo");
|
|
305
|
+
assert.ok(info.name, "至少要有可读的系统名");
|
|
306
|
+
assert.equal(info.platform, process.platform === "win32" ? "win32" : info.platform);
|
|
307
|
+
const list = d.selfRecord().systems;
|
|
308
|
+
assert.ok(Array.isArray(list) && list.length >= 1, "selfRecord 应带 systems[]");
|
|
309
|
+
assert.equal(list[0].kind, "host");
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
check("对端 osInfo 被保存;旧对端没有该字段时为 null", () => {
|
|
313
|
+
const d = new MeshDiscovery({ id: "self-os", port: 0, meta: { machine: "me" } });
|
|
314
|
+
d.note(
|
|
315
|
+
{
|
|
316
|
+
app: "dsh-mesh",
|
|
317
|
+
id: "peer-mac",
|
|
318
|
+
machine: "mac",
|
|
319
|
+
os: "darwin 24.1.0",
|
|
320
|
+
osInfo: {
|
|
321
|
+
platform: "darwin",
|
|
322
|
+
name: "macOS",
|
|
323
|
+
version: "15.1",
|
|
324
|
+
distro: "",
|
|
325
|
+
kernel: "24.1.0",
|
|
326
|
+
desktop: "",
|
|
327
|
+
arch: "arm64",
|
|
328
|
+
},
|
|
329
|
+
},
|
|
330
|
+
"10.0.0.9",
|
|
331
|
+
);
|
|
332
|
+
assert.equal(d.peers()[0].osInfo.platform, "darwin");
|
|
333
|
+
assert.equal(d.peers()[0].osInfo.version, "15.1");
|
|
334
|
+
|
|
335
|
+
d.note({ app: "dsh-mesh", id: "peer-old", machine: "old", os: "linux 6.1.0" }, "10.0.0.10");
|
|
336
|
+
const old = d.peers().find((p) => p.machine === "old");
|
|
337
|
+
assert.equal(old.osInfo, null, "旧对端不该被伪造出一个 osInfo");
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
check("对端 systems[] 被清洗并保存;宿主机排第一", () => {
|
|
341
|
+
const d = new MeshDiscovery({ id: "self-sys", port: 0, meta: { machine: "me" } });
|
|
342
|
+
d.note(
|
|
343
|
+
{
|
|
344
|
+
app: "dsh-mesh",
|
|
345
|
+
id: "peer-sys",
|
|
346
|
+
machine: "multi",
|
|
347
|
+
systems: [
|
|
348
|
+
{ kind: "wsl", platform: "linux", name: "Ubuntu", version: "26.04", distro: "ubuntu" },
|
|
349
|
+
{ kind: "host", platform: "win32", name: "Windows 11", version: "10.0.26200", arch: "x64" },
|
|
350
|
+
{ kind: "wsl", platform: "linux", name: "Ubuntu", version: "26.04", distro: "ubuntu" },
|
|
351
|
+
],
|
|
352
|
+
},
|
|
353
|
+
"10.0.0.12",
|
|
354
|
+
);
|
|
355
|
+
const list = d.peers()[0].systems;
|
|
356
|
+
assert.equal(list.length, 2, "重复项应被去掉");
|
|
357
|
+
assert.equal(list[0].kind, "host");
|
|
358
|
+
assert.equal(list[1].kind, "wsl");
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
check("对端送垃圾 osInfo:platform 白名单 + 文本截断", () => {
|
|
362
|
+
const d = new MeshDiscovery({ id: "self-os", port: 0, meta: { machine: "me" } });
|
|
363
|
+
d.note(
|
|
364
|
+
{
|
|
365
|
+
app: "dsh-mesh",
|
|
366
|
+
id: "peer-evil",
|
|
367
|
+
machine: "evil",
|
|
368
|
+
osInfo: { platform: "evil", name: "<script>".repeat(20), version: "x".repeat(100) },
|
|
369
|
+
},
|
|
370
|
+
"10.0.0.11",
|
|
371
|
+
);
|
|
372
|
+
const info = d.peers()[0].osInfo;
|
|
373
|
+
assert.equal(info.platform, "");
|
|
374
|
+
assert.ok(info.name.length <= 48);
|
|
375
|
+
assert.ok(info.version.length <= 48);
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
console.log(`\n结果:${passed} 通过,${failed} 失败`);
|
|
379
|
+
process.exit(failed === 0 ? 0 : 1);
|