dsh-plugin-mesh 0.5.12 → 0.5.14

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.
@@ -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);