dsh-plugin-mesh 0.5.13 → 0.5.15

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 CHANGED
@@ -3627,7 +3627,7 @@ window.__ModuleLoader__.load({
3627
3627
  },
3628
3628
  })
3629
3629
  : null,
3630
- // SSH 终端:与「访问 DSH Web」同一位置,连的是对端 22 端口
3630
+ // SSH 终端:与「访问 DSH Web」同一位置;端口默认 22,面板里可改
3631
3631
  !isSelf && onSsh
3632
3632
  ? h(IconButton, {
3633
3633
  icon: "terminal",
@@ -4459,6 +4459,8 @@ window.__ModuleLoader__.load({
4459
4459
  const [phase, setPhase] = react.useState("form");
4460
4460
  const [username, setUsername] = react.useState("");
4461
4461
  const [password, setPassword] = react.useState("");
4462
+ // SSH 端口:默认 22,可改(对端 sshd 不一定在 22 上,比如手机侧是 8022)
4463
+ const [port, setPort] = react.useState("22");
4462
4464
  const [hasKey, setHasKey] = react.useState(false);
4463
4465
  // 密钥属于哪个用户名:换了用户名就不能再拿它去认证(否则只会得到认证失败)
4464
4466
  const [keyUser, setKeyUser] = react.useState("");
@@ -4470,9 +4472,16 @@ window.__ModuleLoader__.load({
4470
4472
  const wsRef = react.useRef(null);
4471
4473
  const idRef = react.useRef("");
4472
4474
  const outRef = react.useRef(null);
4475
+ // 自动连接的标记:同一台设备只自动试一次,失败了就停在表单上等人操作
4476
+ const autoRef = react.useRef("");
4477
+ // 上一次连接用的是密钥还是密码:失败时据此决定要不要直接把密码框摊开
4478
+ const usedKeyRef = react.useRef(false);
4473
4479
 
4480
+ // 点开面板就先连:这台设备已经记住用户名、且公钥已确认部署时,不需要再看表单。
4481
+ // 连不上才退回表单——错误会一起显示,并给「改用密码连接」的入口。
4474
4482
  react.useEffect(() => {
4475
4483
  let alive = true;
4484
+ autoRef.current = "";
4476
4485
  api("/ssh/known", { machine })
4477
4486
  .then((r) => {
4478
4487
  if (!alive || !r || !r.ok) return;
@@ -4481,6 +4490,17 @@ window.__ModuleLoader__.load({
4481
4490
  setKeyUser(r.username);
4482
4491
  }
4483
4492
  if (r.hasKey) setHasKey(true);
4493
+ // 记住上次用过的端口,下次连同一台设备不用重填
4494
+ if (Number(r.port) > 0) setPort(String(r.port));
4495
+ if (r.username && r.hasKey && autoRef.current !== machine) {
4496
+ autoRef.current = machine;
4497
+ // 用 known 里的值显式传参:此刻 setUsername/setPort 还没落地
4498
+ connect({
4499
+ username: r.username,
4500
+ port: Number(r.port) > 0 ? Number(r.port) : 22,
4501
+ useKey: true,
4502
+ });
4503
+ }
4484
4504
  })
4485
4505
  .catch(() => {});
4486
4506
  return () => {
@@ -4505,7 +4525,15 @@ window.__ModuleLoader__.load({
4505
4525
 
4506
4526
  react.useEffect(() => () => closeSocket(), []);
4507
4527
 
4508
- const connect = () => {
4528
+ const connect = (next) => {
4529
+ // next 允许"自动连接"直接给定参数:那一刻 state 还没落地(setState 是异步的),
4530
+ // 靠 state 读到的还是空用户名/默认端口。
4531
+ const opts = next || {};
4532
+ const wantUser = String(opts.username ?? username).trim();
4533
+ const wantPort = Number(opts.port ?? port) > 0 ? Number(opts.port ?? port) : 22;
4534
+ const wantPassword = opts.password ?? password;
4535
+ const wantKey = opts.useKey ?? (keyApplies && !forcePassword);
4536
+ usedKeyRef.current = wantKey;
4509
4537
  setError("");
4510
4538
  setOutput("");
4511
4539
  setPhase("connecting");
@@ -4525,9 +4553,9 @@ window.__ModuleLoader__.load({
4525
4553
  type: "open",
4526
4554
  machine,
4527
4555
  host: peer.host || "",
4528
- port: 22,
4529
- username: username.trim(),
4530
- password: keyApplies && !forcePassword ? "" : password,
4556
+ port: wantPort,
4557
+ username: wantUser,
4558
+ password: wantKey ? "" : wantPassword,
4531
4559
  cols: 100,
4532
4560
  rows: 30,
4533
4561
  }),
@@ -4557,11 +4585,14 @@ window.__ModuleLoader__.load({
4557
4585
  if (msg.type === "error") {
4558
4586
  setError(String(msg.message || "未知错误"));
4559
4587
  setPhase("error");
4588
+ // 这次是拿密钥试的,既然失败就直接把密码框展开,不用再点一次「改用密码连接」
4589
+ if (usedKeyRef.current) setForcePassword(true);
4560
4590
  }
4561
4591
  };
4562
4592
  ws.onerror = () => {
4563
4593
  setError("连接通道错误(WebSocket)");
4564
4594
  setPhase("error");
4595
+ if (usedKeyRef.current) setForcePassword(true);
4565
4596
  };
4566
4597
  ws.onclose = () => {
4567
4598
  setPhase((prev) => (prev === "error" ? prev : "closed"));
@@ -4614,13 +4645,28 @@ window.__ModuleLoader__.load({
4614
4645
  h(
4615
4646
  "div",
4616
4647
  { className: "mesh-ssh-note" },
4617
- keyApplies
4648
+ keyApplies && !forcePassword
4618
4649
  ? "已部署本机公钥,直接连接即可(免密)。"
4619
- : hasKey
4620
- ? `公钥属于 ${keyUser};当前用户名不同,请输入密码。`
4621
- : "首次连接需要用户名和密码;成功后会自动把本机公钥写到对端,之后免密。",
4650
+ : forcePassword && hasKey
4651
+ ? "密钥登录没通过;输入密码登录后会重新部署本机公钥,下次恢复免密。"
4652
+ : hasKey
4653
+ ? `公钥属于 ${keyUser};当前用户名不同,请输入密码。`
4654
+ : "首次连接需要用户名和密码;成功后会自动把本机公钥写到对端,之后免密。",
4622
4655
  ),
4623
4656
  h("label", { className: "mesh-ssh-field" }, h("span", null, "用户名"), h("input", { value: username, onChange: (e) => setUsername(e.target.value), autoCapitalize: "none", spellCheck: false })),
4657
+ h(
4658
+ "label",
4659
+ { className: "mesh-ssh-field" },
4660
+ h("span", null, "端口"),
4661
+ h("input", {
4662
+ value: port,
4663
+ inputMode: "numeric",
4664
+ autoCapitalize: "none",
4665
+ spellCheck: false,
4666
+ placeholder: "22",
4667
+ onChange: (e) => setPort(e.target.value.replace(/[^0-9]/g, "")),
4668
+ }),
4669
+ ),
4624
4670
  keyApplies && !forcePassword
4625
4671
  ? null
4626
4672
  : h("label", { className: "mesh-ssh-field" }, h("span", null, "密码"), h("input", { type: "password", value: password, onChange: (e) => setPassword(e.target.value) })),
@@ -6858,6 +6904,10 @@ window.__ModuleLoader__.load({
6858
6904
  const failed = !!report && report.ok !== true;
6859
6905
  const incomparable = !!report && report.ok === true && report.comparable === false;
6860
6906
  const canInstall = hasUpdate && !pending && report.diskVersion !== report.latest;
6907
+ // 查完「没有新版本」以前什么都不渲染,看上去就像检查没生效;
6908
+ // 这里补一个结果:已是最新 vX(点完能看见查到的版本号)。
6909
+ const upToDate =
6910
+ !!report && report.ok === true && !hasUpdate && !incomparable && !pending && !!(report.latest || report.current);
6861
6911
  const label = checking ? "检查中…" : "检查更新";
6862
6912
  const title = failed
6863
6913
  ? `上次检查失败:${String(report.error || "未知原因")}(点一下重试)`
@@ -6889,6 +6939,16 @@ window.__ModuleLoader__.load({
6889
6939
  `已安装 v${pending.version}`,
6890
6940
  )
6891
6941
  : null,
6942
+ upToDate
6943
+ ? h(
6944
+ "span",
6945
+ {
6946
+ className: "mesh-meta-pill",
6947
+ title: `当前 v${report.current};registry 最新 v${report.latest || report.current}${report.cached ? "(本次为缓存结果)" : ""}`,
6948
+ },
6949
+ `已是最新 v${report.latest || report.current}`,
6950
+ )
6951
+ : null,
6892
6952
  pending && pending.restartRequired
6893
6953
  ? h(
6894
6954
  "button",
@@ -0,0 +1,106 @@
1
+ /**
2
+ * 「这次 SSH 连哪儿」的决策自测。
3
+ *
4
+ * 这段守着一个具体事故:宿主没有 100.x 路由,用 tailnet 地址去连 22 端口
5
+ * 必然卡到 `timed out waiting for handshake`。所以:
6
+ * · 有实测可连的候选 → 直连(局域网优先,不占 sidecar 端口)
7
+ * · 一个都连不上但对端有 tailnet 端点 → 走本机回环 + tailnet 字节流
8
+ * · 两条都不成立 → 报清楚原因,不编地址
9
+ *
10
+ * node lib/_selftest-ssh-target.mjs
11
+ */
12
+ import { strict as assert } from "node:assert";
13
+ import { planSshTarget, sshDirectCandidates } from "./ssh-target.js";
14
+
15
+ let passed = 0;
16
+ let failed = 0;
17
+ function test(name, fn) {
18
+ try {
19
+ fn();
20
+ passed += 1;
21
+ console.log(` ✓ ${name}`);
22
+ } catch (err) {
23
+ failed += 1;
24
+ console.log(` ✗ ${name}`);
25
+ console.log(` ${err?.message ?? err}`);
26
+ }
27
+ }
28
+
29
+ console.log("\n── 直连优先 ──");
30
+
31
+ test("探测证明能连 → 直连,端口保留", () => {
32
+ const plan = planSshTarget({ reachableHost: "192.168.1.8", port: 22, tsnetHost: "100.81.214.118" });
33
+ assert.equal(plan.mode, "direct");
34
+ assert.equal(plan.host, "192.168.1.8");
35
+ assert.equal(plan.port, 22);
36
+ });
37
+
38
+ test("自定义端口不被吃掉", () => {
39
+ const plan = planSshTarget({ reachableHost: "192.168.1.8", port: 8022, tsnetHost: "100.81.214.118" });
40
+ assert.equal(plan.port, 8022);
41
+ });
42
+
43
+ console.log("\n── 直连不通 → 走 tailnet 字节流 ──");
44
+
45
+ test("有 tailnet 端点 → stream 到 <tailnet>:<port>", () => {
46
+ const plan = planSshTarget({ port: 22, tsnetHost: "100.81.214.118" });
47
+ assert.equal(plan.mode, "stream");
48
+ assert.equal(plan.address, "100.81.214.118:22");
49
+ });
50
+
51
+ test("stream 模式也带自定义端口", () => {
52
+ const plan = planSshTarget({ port: 8022, tsnetHost: "100.81.214.118" });
53
+ assert.equal(plan.address, "100.81.214.118:8022");
54
+ });
55
+
56
+ console.log("\n── 两条都不成立:报原因,不编地址 ──");
57
+
58
+ test("没有 tailnet 端点 → none,且带上候选说明", () => {
59
+ const plan = planSshTarget({
60
+ endpoints: [{ host: "192.168.1.8", transport: "lan" }],
61
+ port: 22,
62
+ requestedHost: "192.168.1.8",
63
+ });
64
+ assert.equal(plan.mode, "none");
65
+ assert.match(plan.note, /192\.168\.1\.8|局域网可达/);
66
+ assert.doesNotMatch(plan.note, /^直连|^经本机/);
67
+ });
68
+
69
+ test("端口非法时退回 22(不产生 :0)", () => {
70
+ const plan = planSshTarget({ port: 0, tsnetHost: "100.64.0.9" });
71
+ assert.equal(plan.address, "100.64.0.9:22");
72
+ });
73
+
74
+ console.log("\n── 候选顺序:局域网优先,tailnet 不进直连候选 ──");
75
+
76
+ test("私有地址排在前面,重复项去掉", () => {
77
+ const list = sshDirectCandidates(
78
+ [
79
+ { host: "100.81.214.118", transport: "tsnet" },
80
+ { host: "192.168.1.8", transport: "lan" },
81
+ { host: "192.168.1.8", transport: "ws" },
82
+ { host: "120.244.161.183", transport: "lan" },
83
+ ],
84
+ "192.168.1.8",
85
+ );
86
+ assert.deepEqual(list, ["192.168.1.8", "120.244.161.183"]);
87
+ });
88
+
89
+ test("客户端给的私有地址即使不在端点表里也参与直连", () => {
90
+ const list = sshDirectCandidates([{ host: "100.81.214.118", transport: "tsnet" }], "10.0.0.9");
91
+ assert.deepEqual(list, ["10.0.0.9"]);
92
+ });
93
+
94
+ test("tailnet 地址绝不进直连候选(宿主没有路由)", () => {
95
+ const list = sshDirectCandidates(
96
+ [
97
+ { host: "100.81.214.118", transport: "tsnet" },
98
+ { host: "100.74.219.116", transport: "tsnet" },
99
+ ],
100
+ "",
101
+ );
102
+ assert.deepEqual(list, []);
103
+ });
104
+
105
+ console.log(`\n${failed === 0 ? "全部通过" : "有失败"}:${passed} 通过 / ${failed} 失败\n`);
106
+ process.exit(failed === 0 ? 0 : 1);
@@ -0,0 +1,218 @@
1
+ /**
2
+ * 「观看端怎么到对端 GUI」的决策自测。
3
+ *
4
+ * 这段是**故障复现的护栏**:真实现场是手机在移动数据下把自己报成运营商
5
+ * CGNAT 的 `10.73.218.12:3080`,观看端照单全收 → 反向代理永远 502
6
+ * `socket hang up`。所以这里盯着四件事:
7
+ * · 自报地址真拨通时,行为与以前一字不差(局域网不能被加戏)
8
+ * · 拨不通但对端有 tailnet 通道 → 必须改成"本机回环端口 + tailnet 字节流"
9
+ * · 隧道前缀只认相对前缀;老版本对端给的绝对地址只取路径、不取主机
10
+ * · 两条都不可用时,报错要说清"试过什么、怎么失败",不许说成对端已关闭
11
+ *
12
+ * node lib/_selftest-web-target.mjs
13
+ */
14
+ import { strict as assert } from "node:assert";
15
+ import { needsReachabilityProbe, planWebTarget, tunnelPrefixOf } from "./web-target.js";
16
+
17
+ let passed = 0;
18
+ let failed = 0;
19
+ function test(name, fn) {
20
+ try {
21
+ fn();
22
+ passed += 1;
23
+ console.log(` ✓ ${name}`);
24
+ } catch (err) {
25
+ failed += 1;
26
+ console.log(` ✗ ${name}`);
27
+ console.log(` ${err?.message ?? err}`);
28
+ }
29
+ }
30
+
31
+ console.log("\n── 直连:能拨通就一动不动 ──");
32
+
33
+ test("没探测(undefined)按可达处理,不加任何延迟", () => {
34
+ const plan = planWebTarget({ reportedBase: "http://192.168.1.8:3080/", reachable: undefined });
35
+ assert.equal(plan.mode, "direct");
36
+ assert.equal(plan.remoteBase, "http://192.168.1.8:3080/");
37
+ });
38
+
39
+ test("探测通过 → 直连,地址原样保留", () => {
40
+ const plan = planWebTarget({ reportedBase: "http://192.168.1.8:3080/", reachable: true });
41
+ assert.equal(plan.mode, "direct");
42
+ assert.equal(plan.remoteBase, "http://192.168.1.8:3080/");
43
+ });
44
+
45
+ console.log("\n── 现实故障:对端自报运营商地址 + GUI 绑 0.0.0.0(无隧道)──");
46
+
47
+ const carrier = "http://10.73.218.12:3080/";
48
+
49
+ test("拨不通但有 tailnet 地址 → 走本机回环 + tailnet 字节流", () => {
50
+ const plan = planWebTarget({
51
+ reportedBase: carrier,
52
+ reportedPort: 3080,
53
+ reachable: false,
54
+ tunnel: null,
55
+ tsnetHost: "100.81.214.118",
56
+ meshPort: 45917,
57
+ probeNote: "1500ms 内没有响应(This operation was aborted)",
58
+ });
59
+ assert.equal(plan.mode, "stream");
60
+ assert.equal(plan.address, "100.81.214.118:3080");
61
+ assert.equal(plan.prefix, "");
62
+ assert.match(plan.note, /100\.81\.214\.118:3080/);
63
+ });
64
+
65
+ test("端口取对端报的 port,不硬编码 3080", () => {
66
+ const plan = planWebTarget({
67
+ reportedBase: "http://10.0.0.9:8080/",
68
+ reportedPort: 8080,
69
+ reachable: false,
70
+ tsnetHost: "100.64.0.9",
71
+ meshPort: 45917,
72
+ });
73
+ assert.equal(plan.address, "100.64.0.9:8080");
74
+ });
75
+
76
+ test("没有 tsnet 地址 → 不许假装能连,报错要带上失败原因", () => {
77
+ const plan = planWebTarget({
78
+ reportedBase: carrier,
79
+ reportedPort: 3080,
80
+ reachable: false,
81
+ tsnetHost: "",
82
+ probeNote: "1500ms 内没有响应(aborted)",
83
+ });
84
+ assert.equal(plan.mode, "none");
85
+ assert.equal(plan.remoteBase, carrier);
86
+ assert.match(plan.note, /1500ms 内没有响应/);
87
+ assert.doesNotMatch(plan.note, /关闭|不存在|offline/);
88
+ });
89
+
90
+ console.log("\n── 隧道:对端 GUI 只听回环时走 mesh 口 ──");
91
+
92
+ test("有相对前缀 + 票据 → 目标是 mesh 口,不是 GUI 口", () => {
93
+ const plan = planWebTarget({
94
+ reportedBase: "http://127.0.0.1:3080/",
95
+ reportedPort: 3080,
96
+ reachable: false,
97
+ tunnel: { prefix: "/mesh/peer-web", ticket: "abc.def", header: "x-dsh-peer-web" },
98
+ tsnetHost: "100.100.1.2",
99
+ meshPort: 45917,
100
+ });
101
+ assert.equal(plan.mode, "stream");
102
+ assert.equal(plan.address, "100.100.1.2:45917");
103
+ assert.equal(plan.prefix, "/mesh/peer-web");
104
+ assert.equal(plan.ticket, "abc.def");
105
+ });
106
+
107
+ test("前缀缺尾斜杠/带尾斜杠都能收敛成同一种写法", () => {
108
+ assert.equal(tunnelPrefixOf({ prefix: "mesh/peer-web/" }), "/mesh/peer-web");
109
+ assert.equal(tunnelPrefixOf({ prefix: "/mesh/peer-web///" }), "/mesh/peer-web");
110
+ });
111
+
112
+ test("老版本对端的绝对地址只取路径,主机一律丢掉(那台主机同样可能不可达)", () => {
113
+ assert.equal(tunnelPrefixOf({ url: "http://10.12.225.124:45917/mesh/peer-web/" }), "/mesh/peer-web");
114
+ assert.equal(tunnelPrefixOf({ url: "http://10.12.225.124:45917/mesh/peer-web" }), "/mesh/peer-web");
115
+ });
116
+
117
+ test("前缀优先于 url(新版本两个都给时按前缀走)", () => {
118
+ assert.equal(
119
+ tunnelPrefixOf({ prefix: "/mesh/peer-web", url: "http://10.1.2.3:45917/mesh/other/" }),
120
+ "/mesh/peer-web",
121
+ );
122
+ });
123
+
124
+ test("给了前缀但没有票据 → 不冒充隧道,退回直连 GUI", () => {
125
+ const plan = planWebTarget({
126
+ reportedBase: "http://10.9.9.9:3080/",
127
+ reportedPort: 3080,
128
+ reachable: false,
129
+ tunnel: { prefix: "/mesh/peer-web" },
130
+ tsnetHost: "100.64.1.1",
131
+ meshPort: 45917,
132
+ });
133
+ assert.equal(plan.mode, "stream");
134
+ assert.equal(plan.address, "100.64.1.1:3080");
135
+ assert.equal(plan.prefix, "");
136
+ });
137
+
138
+ test("有票据但没有 mesh 端口 → 同样不冒充隧道", () => {
139
+ const plan = planWebTarget({
140
+ reportedBase: "http://10.9.9.9:3080/",
141
+ reportedPort: 3080,
142
+ reachable: false,
143
+ tunnel: { prefix: "/mesh/peer-web", ticket: "abc.def" },
144
+ tsnetHost: "100.64.1.1",
145
+ meshPort: 0,
146
+ });
147
+ assert.equal(plan.address, "100.64.1.1:3080");
148
+ });
149
+
150
+ console.log("\n── 回环自报:绝不能连到本机自己 ──");
151
+
152
+ test("对端报 127.0.0.1 + 给了隧道 → 必须走隧道,不能直连", () => {
153
+ const plan = planWebTarget({
154
+ reportedBase: "http://127.0.0.1:3080/",
155
+ reportedPort: 3080,
156
+ // 故意给 true:就算探测环节误判"可达",回环自报也必须被否掉
157
+ reachable: true,
158
+ tunnel: { prefix: "/mesh/peer-web", ticket: "abc.def" },
159
+ tsnetHost: "100.100.1.2",
160
+ meshPort: 45917,
161
+ });
162
+ assert.equal(plan.mode, "stream");
163
+ assert.equal(plan.address, "100.100.1.2:45917");
164
+ assert.equal(plan.prefix, "/mesh/peer-web");
165
+ });
166
+
167
+ test("对端报 127.0.0.1 但没给隧道 → 如实说不可用,不要假装直连", () => {
168
+ const plan = planWebTarget({
169
+ reportedBase: "http://127.0.0.1:3080/",
170
+ reportedPort: 3080,
171
+ reachable: true,
172
+ tunnel: null,
173
+ tsnetHost: "100.100.1.2",
174
+ meshPort: 45917,
175
+ guiBoundEverywhere: false,
176
+ });
177
+ assert.equal(plan.mode, "none");
178
+ assert.match(plan.note, /只听回环/);
179
+ assert.doesNotMatch(plan.note, /关闭|offline/);
180
+ });
181
+
182
+ test("对端 GUI 只绑回环、又没隧道 → 不许拨它的 3080(那里没有监听)", () => {
183
+ const plan = planWebTarget({
184
+ reportedBase: "http://10.9.9.9:3080/",
185
+ reportedPort: 3080,
186
+ reachable: false,
187
+ tunnel: null,
188
+ tsnetHost: "100.64.1.1",
189
+ meshPort: 45917,
190
+ guiBoundEverywhere: false,
191
+ });
192
+ assert.equal(plan.mode, "none");
193
+ });
194
+
195
+ console.log("\n── 什么时候该探测(别给局域网加延迟)──");
196
+
197
+ test("同网段地址不探测", () => {
198
+ assert.equal(needsReachabilityProbe("192.168.1.8", ["192.168.1.5"]), false);
199
+ });
200
+
201
+ test("回环不探测", () => {
202
+ assert.equal(needsReachabilityProbe("127.0.0.1", ["192.168.1.5"]), false);
203
+ });
204
+
205
+ test("运营商 CGNAT 的 10.x 要探测(这正是骗人的那一类)", () => {
206
+ assert.equal(needsReachabilityProbe("10.73.218.12", ["192.168.1.5"]), true);
207
+ });
208
+
209
+ test("主机名(非 IPv4 字面量)要探测", () => {
210
+ assert.equal(needsReachabilityProbe("peer.example", ["192.168.1.5"]), true);
211
+ });
212
+
213
+ test("没有任何本机地址时,IPv4 也要探测", () => {
214
+ assert.equal(needsReachabilityProbe("192.168.1.8", []), true);
215
+ });
216
+
217
+ console.log(`\n${failed === 0 ? "全部通过" : "有失败"}:${passed} 通过 / ${failed} 失败\n`);
218
+ process.exit(failed === 0 ? 0 : 1);