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.
- package/client/client.js +55 -9
- package/lib/_selftest-peer-web.mjs +317 -0
- package/lib/_selftest-ssh-target.mjs +106 -0
- package/lib/_selftest-web-target.mjs +218 -0
- package/lib/index.js +378 -22
- package/lib/index.js.ws.bak +4589 -0
- package/lib/peer-web.js +226 -0
- package/lib/ssh-host.js +124 -19
- package/lib/ssh-target.js +98 -0
- package/lib/tsnet/manager.js +119 -0
- package/lib/web-proxy.js +57 -6
- package/lib/web-target.js +191 -0
- package/llms.txt +27 -0
- package/package.json +7 -4
- package/sidecar/main.go +242 -1
package/lib/tsnet/manager.js
CHANGED
|
@@ -274,6 +274,8 @@ export class TsnetNode extends EventEmitter {
|
|
|
274
274
|
#restarts = 0;
|
|
275
275
|
#stopping = false;
|
|
276
276
|
#forwards = new Set();
|
|
277
|
+
#streams = new Map();
|
|
278
|
+
#exposed = new Set();
|
|
277
279
|
#hostname;
|
|
278
280
|
|
|
279
281
|
/**
|
|
@@ -465,6 +467,104 @@ export class TsnetNode extends EventEmitter {
|
|
|
465
467
|
this.#log(`tailnet 入站已开启::${port} → 127.0.0.1:${port}`);
|
|
466
468
|
}
|
|
467
469
|
|
|
470
|
+
/**
|
|
471
|
+
* 在本机开一个回环端口,把每个连上来的字节流打进 tailnet。
|
|
472
|
+
*
|
|
473
|
+
* 与 `fetchOverTailnet` 的分工:那个只做"一问一答、整包缓冲"的 HTTP,WebSocket
|
|
474
|
+
* 升级和长连接一律不行(`/api/remote.mux` 就是 WS)。要看对端 GUI 就必须有
|
|
475
|
+
* 裸字节流,所以这里让 sidecar 监听 127.0.0.1 并双向 copy —— 拿到端口之后,
|
|
476
|
+
* 上层(web-proxy)当普通 http 源用即可,ES/WS 全自动成立。
|
|
477
|
+
*
|
|
478
|
+
* 同一个地址复用同一只端口:重开面板不该每次换端口(换了就等于换了一个源,
|
|
479
|
+
* 对端签的会话 cookie 也跟着作废)。
|
|
480
|
+
* @param {string} address - 形如 `100.64.0.5:3080` 的 tailnet 目标
|
|
481
|
+
* @returns {Promise<number>} 本机回环端口
|
|
482
|
+
*/
|
|
483
|
+
async openStream(address) {
|
|
484
|
+
const key = String(address ?? "").trim();
|
|
485
|
+
if (!key) throw new Error("openStream 需要 tailnet 目标地址");
|
|
486
|
+
const cached = this.#streams.get(key);
|
|
487
|
+
if (cached?.port) return cached.port;
|
|
488
|
+
if (cached?.pending) return cached.pending;
|
|
489
|
+
const pending = (async () => {
|
|
490
|
+
let res;
|
|
491
|
+
try {
|
|
492
|
+
res = await this.#call("/__tsnet/dial", { method: "POST", body: { address: key }, timeoutMs: 15000 });
|
|
493
|
+
} catch (err) {
|
|
494
|
+
// 老 sidecar 没有这个接口:Go 的 mux 直接回 404 文本,#call 会报"非 JSON"。
|
|
495
|
+
// 说清楚是**本机二进制过旧**,不是对端不通(AGENTS.md 硬约束 1)。
|
|
496
|
+
const text = String(err?.message ?? err);
|
|
497
|
+
if (/非 JSON|404/.test(text)) {
|
|
498
|
+
throw new Error(`本机 tsnet sidecar 版本过旧(没有出站流接口 /__tsnet/dial),需要随插件一起升级 sidecar`);
|
|
499
|
+
}
|
|
500
|
+
throw err;
|
|
501
|
+
}
|
|
502
|
+
if (!res?.ok) throw new Error(res?.error ?? `tailnet 拨号 ${key} 失败`);
|
|
503
|
+
const port = Number(res.port);
|
|
504
|
+
if (!port) throw new Error(`tailnet 拨号 ${key} 没有返回端口`);
|
|
505
|
+
this.#streams.set(key, { port });
|
|
506
|
+
this.#log(`tailnet 出站流已开启:127.0.0.1:${port} → ${key}`);
|
|
507
|
+
return port;
|
|
508
|
+
})();
|
|
509
|
+
this.#streams.set(key, { pending });
|
|
510
|
+
try {
|
|
511
|
+
return await pending;
|
|
512
|
+
} catch (err) {
|
|
513
|
+
this.#streams.delete(key);
|
|
514
|
+
throw err;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
/** 关闭某个目标地址的出站流(地址不认识时静默,幂等)。 */
|
|
519
|
+
async closeStream(address) {
|
|
520
|
+
const key = String(address ?? "").trim();
|
|
521
|
+
const entry = this.#streams.get(key);
|
|
522
|
+
if (!entry?.port) {
|
|
523
|
+
this.#streams.delete(key);
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
526
|
+
this.#streams.delete(key);
|
|
527
|
+
await this.#call("/__tsnet/undial", { method: "POST", body: { port: entry.port }, timeoutMs: 8000 }).catch(() => {});
|
|
528
|
+
this.#log(`tailnet 出站流已关闭:127.0.0.1:${entry.port} → ${key}`);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* 把本机某个回环端口**暴露到 tailnet**(tailnet :port → 127.0.0.1:port,裸字节流)。
|
|
533
|
+
*
|
|
534
|
+
* 与 `registerForward` 的区别:那个是 HTTP 反代(SSH 的握手字节进不去);
|
|
535
|
+
* 这个是纯 TCP 管道。两侧合起来才有「SSH 走 tailnet」——本机用 openStream
|
|
536
|
+
* 拨过去,对端用 exposePort 暴露自己的 sshd。
|
|
537
|
+
*
|
|
538
|
+
* 注意:暴露只发生在 userspace tailnet 里,宿主与局域网看不到它;但 tailnet
|
|
539
|
+
* 内部按 ACL 可达,所以只该暴露确实要共享的端口(SSH),别当通用开关用。
|
|
540
|
+
* @param {number} port - 本机监听端口(通常 22 或 8022)
|
|
541
|
+
*/
|
|
542
|
+
async exposePort(port) {
|
|
543
|
+
const value = Number(port);
|
|
544
|
+
if (!Number.isInteger(value) || value <= 0 || value > 65535) throw new Error(`端口不合法:${port}`);
|
|
545
|
+
if (this.#exposed.has(value)) return { port: value, already: true };
|
|
546
|
+
const res = await this.#call("/__tsnet/listen", { method: "POST", body: { port: value }, timeoutMs: 15000 });
|
|
547
|
+
if (!res?.ok) throw new Error(res?.error ?? `暴露 tailnet :${value} 失败`);
|
|
548
|
+
this.#exposed.add(value);
|
|
549
|
+
this.#log(`tailnet 入站裸流已开启::${value} → 127.0.0.1:${value}`);
|
|
550
|
+
return { port: value, already: res.already === true };
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
/** 收回某个已暴露的端口(幂等)。 */
|
|
554
|
+
async unexposePort(port) {
|
|
555
|
+
const value = Number(port);
|
|
556
|
+
if (!this.#exposed.has(value)) return { port: value, absent: true };
|
|
557
|
+
this.#exposed.delete(value);
|
|
558
|
+
await this.#call("/__tsnet/unlisten", { method: "POST", body: { port: value }, timeoutMs: 8000 }).catch(() => {});
|
|
559
|
+
this.#log(`tailnet 入站裸流已关闭::${value}`);
|
|
560
|
+
return { port: value, absent: false };
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
/** 当前暴露到 tailnet 的端口(诊断用)。 */
|
|
564
|
+
exposedPorts() {
|
|
565
|
+
return [...this.#exposed];
|
|
566
|
+
}
|
|
567
|
+
|
|
468
568
|
// ───────────────────────── 启动 ─────────────────────────
|
|
469
569
|
|
|
470
570
|
/**
|
|
@@ -882,6 +982,25 @@ export class TsnetNode extends EventEmitter {
|
|
|
882
982
|
}
|
|
883
983
|
}
|
|
884
984
|
this.#forwards.clear();
|
|
985
|
+
// 出站流同理:只关自己开的那几只端口。
|
|
986
|
+
for (const entry of this.#streams.values()) {
|
|
987
|
+
if (!entry?.port) continue;
|
|
988
|
+
try {
|
|
989
|
+
await this.#call("/__tsnet/undial", { method: "POST", body: { port: entry.port }, timeoutMs: 5000 });
|
|
990
|
+
} catch {
|
|
991
|
+
/* 尽力而为 */
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
this.#streams.clear();
|
|
995
|
+
// 出站监听同理:只收自己开的那几个端口。
|
|
996
|
+
for (const port of this.#exposed) {
|
|
997
|
+
try {
|
|
998
|
+
await this.#call("/__tsnet/unlisten", { method: "POST", body: { port }, timeoutMs: 5000 });
|
|
999
|
+
} catch {
|
|
1000
|
+
/* 尽力而为 */
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
this.#exposed.clear();
|
|
885
1004
|
|
|
886
1005
|
if (!this.#ownsProcess || keepRemote) {
|
|
887
1006
|
this.#log("保留共享的 sidecar(非本实例拉起)");
|
package/lib/web-proxy.js
CHANGED
|
@@ -92,7 +92,8 @@ function hasGate(req, nonce) {
|
|
|
92
92
|
/**
|
|
93
93
|
* 起一个指向 `remoteBase` 的回环代理。
|
|
94
94
|
* @param options - remoteBase(对端 http://host:port/)、token(启动 token,可空)、
|
|
95
|
-
* nonce、initialCookie(上次持久化下来的对端会话 cookie)、onCookie(换到新 cookie 时的落盘回调)、
|
|
95
|
+
* nonce、initialCookie(上次持久化下来的对端会话 cookie)、onCookie(换到新 cookie 时的落盘回调)、
|
|
96
|
+
* upstreamHeaders(每个转发请求都要附带的额外头,例如 mesh 口隧道的票据)、log、warn
|
|
96
97
|
* @returns {Promise<{ port: number, url: string, remote: string, hasSession: () => boolean, close: () => Promise<void> }>}
|
|
97
98
|
*/
|
|
98
99
|
export async function createWebProxy({
|
|
@@ -103,6 +104,7 @@ export async function createWebProxy({
|
|
|
103
104
|
onCookie = () => {},
|
|
104
105
|
pathPrefix = "",
|
|
105
106
|
passthrough = false,
|
|
107
|
+
upstreamHeaders = {},
|
|
106
108
|
log = () => {},
|
|
107
109
|
warn = () => {},
|
|
108
110
|
}) {
|
|
@@ -115,6 +117,14 @@ export async function createWebProxy({
|
|
|
115
117
|
// 对端 cookie 是 30 天、由对端持久密钥签名的,跨对端重启仍有效 —— 所以
|
|
116
118
|
// 优先复用上次存下来的,token 只在"没 cookie 或 cookie 被拒"时才需要。
|
|
117
119
|
const prefix = String(pathPrefix || "").replace(/\/+$/, "");
|
|
120
|
+
|
|
121
|
+
/** 隧道票据一类的固定头:值为空的一律不发(别把 "undefined" 写进请求)。 */
|
|
122
|
+
const extraHeaders = () =>
|
|
123
|
+
Object.fromEntries(
|
|
124
|
+
Object.entries(upstreamHeaders ?? {}).filter(
|
|
125
|
+
([name, value]) => typeof name === "string" && name && value !== undefined && value !== null && value !== "",
|
|
126
|
+
),
|
|
127
|
+
);
|
|
118
128
|
let cookie = initialCookie || "";
|
|
119
129
|
// cookie 在手里不等于对端认它:真收到 401 才翻成 false,供 UI 如实提示。
|
|
120
130
|
let healthy = true;
|
|
@@ -152,9 +162,47 @@ export async function createWebProxy({
|
|
|
152
162
|
return minting;
|
|
153
163
|
}
|
|
154
164
|
|
|
155
|
-
/**
|
|
165
|
+
/**
|
|
166
|
+
* 把「浏览器来源」改写成对端自己的源。
|
|
167
|
+
*
|
|
168
|
+
* 浏览器访问的是本机回环代理(`http://127.0.0.1:<port>`),而对端的
|
|
169
|
+
* browser-trust 围栏是拿 `Origin` 与 `Host` 比同源的
|
|
170
|
+
* (`dsh-client-connection` 的 `isTrustedApiRequest`:`new URL(origin).host === host.host`)。
|
|
171
|
+
* 只换 Host 不换 Origin,等于告诉对端"这个请求来自别的站点",会被拒成 403:
|
|
172
|
+
* · `/api/dynamicCordisRunner/*` → 403,Cordis/插件面板读不到清单;
|
|
173
|
+
* · `/api/remote.mux` 的 WebSocket 升级被拒 → 界面永远停在「重新连接中…」。
|
|
174
|
+
* 我们本来就是**替浏览器**发请求(`desktop-web.js` 对另一个引擎做的是同一件事),
|
|
175
|
+
* 所以这里照那个写法把来源也改写掉。
|
|
176
|
+
*/
|
|
177
|
+
function rewriteProvenance(headers, upstreamOrigin) {
|
|
178
|
+
headers.origin = upstreamOrigin;
|
|
179
|
+
if (headers.referer !== undefined) headers.referer = `${upstreamOrigin}/`;
|
|
180
|
+
if (String(headers["sec-fetch-site"] ?? "") === "cross-site") headers["sec-fetch-site"] = "same-origin";
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* 把门禁参数 `k` 从转发路径里摘掉。
|
|
185
|
+
*
|
|
186
|
+
* 它是**本机**回环代理的一次性令牌,对上游没有任何意义:留着等于把我们的
|
|
187
|
+
* nonce 送进对端的访问日志与 URL 解析。其余参数原样保留。
|
|
188
|
+
*/
|
|
189
|
+
function stripGate(rawUrl) {
|
|
190
|
+
const text = String(rawUrl ?? "/");
|
|
191
|
+
const at = text.indexOf("?");
|
|
192
|
+
if (at === -1) return text;
|
|
193
|
+
const query = text.slice(at + 1);
|
|
194
|
+
// 只认一个**完整**的 k 参数;其余部分逐字节保留 —— 对端的插件脚本地址是
|
|
195
|
+
// `/plugins/??a,b&rev=…` 这种多问号/逗号形式,用 URLSearchParams 重写会把它编码坏。
|
|
196
|
+
if (!/(^|&)k=/.test(query)) return text;
|
|
197
|
+
const kept = query.split("&").filter((part) => part !== "k" && !part.startsWith("k="));
|
|
198
|
+
return kept.length ? `${text.slice(0, at)}?${kept.join("&")}` : text.slice(0, at);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** 一次转发;不改写路径,只是把 Host/Cookie/来源换掉。 */
|
|
156
202
|
function forward(req, res, retried = false) {
|
|
157
|
-
|
|
203
|
+
// upstreamHeaders 每次都要带(例如 mesh 口隧道的票据);浏览器的同名头不能覆盖它。
|
|
204
|
+
const headers = { ...req.headers, ...extraHeaders(), host: authority };
|
|
205
|
+
rewriteProvenance(headers, `http://${authority}`);
|
|
158
206
|
const merged = mergeCookie(cookie, headers.cookie);
|
|
159
207
|
if (merged) headers.cookie = merged;
|
|
160
208
|
else delete headers.cookie;
|
|
@@ -163,7 +211,7 @@ export async function createWebProxy({
|
|
|
163
211
|
if (wantsHtml) headers["accept-encoding"] = "identity";
|
|
164
212
|
|
|
165
213
|
const up = httpRequest(
|
|
166
|
-
{ host: hostname, port: remotePort, method: req.method, path: `${prefix}${req.url
|
|
214
|
+
{ host: hostname, port: remotePort, method: req.method, path: `${prefix}${stripGate(req.url)}`, headers },
|
|
167
215
|
(reply) => {
|
|
168
216
|
// 401 = 我们的服务端 cookie 过期/轮换过:换一次再重试(只重试幂等方法)。
|
|
169
217
|
const idempotent = req.method === "GET" || req.method === "HEAD";
|
|
@@ -284,8 +332,11 @@ export async function createWebProxy({
|
|
|
284
332
|
const target = netConnect(remotePort, hostname, () => {
|
|
285
333
|
audit(`UPGRADE-CONNECTED upstream=${hostname}:${remotePort} path=${prefix}${req.url}`);
|
|
286
334
|
// 前缀必须带上,否则对端的 upgrade 路由匹配不上(WS 直接握手失败)
|
|
287
|
-
const lines = [`${req.method} ${prefix}${req.url
|
|
288
|
-
|
|
335
|
+
const lines = [`${req.method} ${prefix}${stripGate(req.url)} HTTP/1.1`];
|
|
336
|
+
// 升级请求同样要过对端的同源围栏:Origin 不改写就会被拒,前端一直"重新连接中"。
|
|
337
|
+
const clean = { ...req.headers, ...extraHeaders() };
|
|
338
|
+
rewriteProvenance(clean, `http://${authority}`);
|
|
339
|
+
for (const [key, value] of Object.entries(clean)) {
|
|
289
340
|
if (value === undefined) continue;
|
|
290
341
|
const name = key.toLowerCase();
|
|
291
342
|
if (name === "host" || name === "cookie") continue;
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 「在我这台机器上,怎么才能真的打开对端的 DSH 界面」—— 纯决策,不做 IO。
|
|
3
|
+
*
|
|
4
|
+
* 为什么需要单独一层:对端 `GET /mesh/v1/web-url` 的自报地址是**站在它自己角度**
|
|
5
|
+
* 算出来的(`preferredAddress()`),手机在移动数据下那会是运营商 CGNAT 的 10.x;
|
|
6
|
+
* 它再顺带自报一个 `lanReachable`(判据只是"我绑了 0.0.0.0")。这两样都不是
|
|
7
|
+
* "观看端能不能到"的证据,照单全收的结果就是面板永远 502:
|
|
8
|
+
*
|
|
9
|
+
* 连不上对端 10.12.225.124:3080:socket hang up
|
|
10
|
+
*
|
|
11
|
+
* 所以判断权必须留在观看端:
|
|
12
|
+
* 1. 地址能拨通(真的拿到 HTTP 响应)→ 直接用它,行为和以前一致;
|
|
13
|
+
* 2. 拨不通,但对端有一条 tailnet 通道 → 在本机开一个回环端口,由 sidecar
|
|
14
|
+
* 把字节流打进 tailnet(宿主机路由不到 100.x,且 WebSocket 升级也走这条);
|
|
15
|
+
* 3. 两条都不可用 → 如实说"哪一条试过、怎么失败的",不许把探测失败伪装成
|
|
16
|
+
* 对端已关闭(AGENTS.md 硬约束 1)。
|
|
17
|
+
*
|
|
18
|
+
* @typedef {{ prefix: string, ticket: string, header: string } | null} TunnelInfo
|
|
19
|
+
* @typedef {{ mode: "direct" | "stream" | "none", remoteBase?: string, address?: string,
|
|
20
|
+
* prefix?: string, ticket?: string, header?: string, note: string }} WebTargetPlan
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 从对端给的 tunnel 描述里取出**相对前缀**。
|
|
25
|
+
*
|
|
26
|
+
* 新版本对端发 `{ prefix: "/mesh/peer-web", ticket }`;老版本发的是一条绝对地址
|
|
27
|
+
* `{ url: "http://<它自己的地址>:<mesh端口>/mesh/peer-web/" }` —— 那种地址的主机
|
|
28
|
+
* 部分同样可能是不可达的 10.x,所以**只取路径**,主机由观看端用自己的通道决定。
|
|
29
|
+
* @returns {string} 带前导斜杠、不带尾斜杠的前缀;无则空串。
|
|
30
|
+
*/
|
|
31
|
+
export function tunnelPrefixOf(tunnel) {
|
|
32
|
+
if (!tunnel || typeof tunnel !== "object") return "";
|
|
33
|
+
const raw = String(tunnel.prefix ?? "").trim();
|
|
34
|
+
if (raw) {
|
|
35
|
+
const withSlash = raw.startsWith("/") ? raw : `/${raw}`;
|
|
36
|
+
return withSlash.replace(/\/+$/, "");
|
|
37
|
+
}
|
|
38
|
+
const url = String(tunnel.url ?? "").trim();
|
|
39
|
+
if (!url) return "";
|
|
40
|
+
try {
|
|
41
|
+
return new URL(url).pathname.replace(/\/+$/, "");
|
|
42
|
+
} catch {
|
|
43
|
+
// 解析不了的地址当"没有前缀",不要凭空猜一个出来。
|
|
44
|
+
return "";
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 决定观看端用哪条路。
|
|
50
|
+
*
|
|
51
|
+
* @param {object} input
|
|
52
|
+
* @param {string} input.reportedBase 对端自报的绝对地址(`http://host:port/`)
|
|
53
|
+
* @param {number} [input.reportedPort] 对端 GUI 端口,缺省取地址里的端口
|
|
54
|
+
* @param {boolean} [input.reachable] 本机真的拨通过没有;`undefined` = 没测(按可达处理,保持旧行为)
|
|
55
|
+
* @param {TunnelInfo} [input.tunnel] 对端 mesh 口隧道
|
|
56
|
+
* @param {string} [input.tsnetHost] 对端的 tailnet 地址(100.64/10);没有就走不了公网
|
|
57
|
+
* @param {number} [input.meshPort] 对端 mesh 口端口(隧道挂在它上面)
|
|
58
|
+
* @param {boolean} [input.guiBoundEverywhere] 对端 GUI 是否绑在所有网卡(bind=0.0.0.0);
|
|
59
|
+
* `undefined` = 对端没报(老版本),按"绑了"处理以保持旧行为
|
|
60
|
+
* @param {string} [input.probeNote] 探测失败时的原始描述,需原样带进 note
|
|
61
|
+
* @returns {WebTargetPlan}
|
|
62
|
+
*/
|
|
63
|
+
export function planWebTarget({
|
|
64
|
+
reportedBase,
|
|
65
|
+
reportedPort,
|
|
66
|
+
reachable,
|
|
67
|
+
tunnel = null,
|
|
68
|
+
tsnetHost = "",
|
|
69
|
+
meshPort = 0,
|
|
70
|
+
guiBoundEverywhere,
|
|
71
|
+
probeNote = "",
|
|
72
|
+
} = {}) {
|
|
73
|
+
const base = String(reportedBase ?? "");
|
|
74
|
+
const port = Number(reportedPort) || portOf(base) || 0;
|
|
75
|
+
const reportedHost = hostOf(base);
|
|
76
|
+
|
|
77
|
+
// 对端自报 `127.0.0.1` 表示「我的 GUI 只听回环」——那不是本机能直连的地址,
|
|
78
|
+
// 直连过去只会打到**本机自己**的 3080(一个非常难查的假成功)。
|
|
79
|
+
// 所以这类地址一律不按直连处理,只能走它给的 mesh 口隧道。
|
|
80
|
+
const loopbackReport = isLoopbackHost(reportedHost);
|
|
81
|
+
const direct = reachable !== false && !loopbackReport;
|
|
82
|
+
|
|
83
|
+
// 没探测 / 探测通过 → 照旧直连。局域网路径上不多花一毫秒。
|
|
84
|
+
if (direct) {
|
|
85
|
+
return { mode: "direct", remoteBase: base, note: "" };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const host = String(tsnetHost ?? "").trim();
|
|
89
|
+
if (!host) {
|
|
90
|
+
return {
|
|
91
|
+
mode: "none",
|
|
92
|
+
remoteBase: base,
|
|
93
|
+
note: loopbackReport
|
|
94
|
+
? `对端只报了回环地址 ${base}(它的 GUI 只听 127.0.0.1),且没有 tailnet 通道可用`
|
|
95
|
+
: `对端自报地址 ${base} 从本机拨不通(${probeNote || "无响应"}),且没有 tailnet 通道可用`,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const prefix = tunnelPrefixOf(tunnel);
|
|
100
|
+
const ticket = String(tunnel?.ticket ?? "");
|
|
101
|
+
const header = String(tunnel?.header ?? "x-dsh-peer-web");
|
|
102
|
+
|
|
103
|
+
// 隧道挂在**对端的 mesh 口**上:目标是对端 mesh 端口 + 相对前缀。
|
|
104
|
+
// 这条路把 Host/Origin/会话 cookie 的改写留在对端进程里完成,所以对端 GUI
|
|
105
|
+
// 只绑回环也能看;WebSocket(/api/remote.mux)同样走这条。
|
|
106
|
+
if (prefix && ticket && meshPort > 0) {
|
|
107
|
+
return {
|
|
108
|
+
mode: "stream",
|
|
109
|
+
address: `${host}:${meshPort}`,
|
|
110
|
+
prefix,
|
|
111
|
+
ticket,
|
|
112
|
+
header,
|
|
113
|
+
note: `经对端 mesh 口隧道(对端自报地址 ${base} 不可达)`,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// 没有隧道(对端认为它"局域网可达",其实只有运营商地址):直接打通它的 GUI。
|
|
118
|
+
// 前提是**对端 GUI 真的绑在所有网卡上**(它报的 bind=0.0.0.0)。只绑回环的
|
|
119
|
+
// 宿主连它自己的 tailnet 地址都不听,拨过去只会被拒。
|
|
120
|
+
const boundEverywhere = guiBoundEverywhere !== false;
|
|
121
|
+
if (port > 0 && boundEverywhere && !loopbackReport) {
|
|
122
|
+
return {
|
|
123
|
+
mode: "stream",
|
|
124
|
+
address: `${host}:${port}`,
|
|
125
|
+
prefix: "",
|
|
126
|
+
ticket: "",
|
|
127
|
+
header,
|
|
128
|
+
note: `经本机 tailnet 通道直连对端 GUI ${host}:${port}(对端自报地址 ${base} 不可达)`,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return {
|
|
133
|
+
mode: "none",
|
|
134
|
+
remoteBase: base,
|
|
135
|
+
note: loopbackReport || !boundEverywhere
|
|
136
|
+
? `对端 GUI 只听回环(${base}),且没给 mesh 口隧道 —— 需要把对端插件升级到带隧道的版本`
|
|
137
|
+
: `对端自报地址 ${base} 从本机拨不通(${probeNote || "无响应"}),且没拿到对端 GUI 端口`,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** 从 `http://host:port/` 里取主机名;取不到返回空串。 */
|
|
142
|
+
function hostOf(base) {
|
|
143
|
+
try {
|
|
144
|
+
return new URL(String(base ?? "")).hostname;
|
|
145
|
+
} catch {
|
|
146
|
+
return "";
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** 这个主机名是不是"本机回环"(对端自称只听回环时会给它)。 */
|
|
151
|
+
export function isLoopbackHost(host) {
|
|
152
|
+
const text = String(host ?? "").trim().toLowerCase();
|
|
153
|
+
if (!text) return false;
|
|
154
|
+
if (text === "localhost" || text.endsWith(".localhost")) return true;
|
|
155
|
+
if (text === "::1" || text === "0:0:0:0:0:0:0:1") return true;
|
|
156
|
+
if (/^127\./.test(text)) return true;
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** 从 `http://host:port/` 里取端口;取不到返回 0。 */
|
|
161
|
+
function portOf(base) {
|
|
162
|
+
try {
|
|
163
|
+
const parsed = new URL(base);
|
|
164
|
+
if (parsed.port) return Number(parsed.port);
|
|
165
|
+
return parsed.protocol === "https:" ? 443 : 80;
|
|
166
|
+
} catch {
|
|
167
|
+
return 0;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* 这个主机名需不需要先探测一次可达性。
|
|
173
|
+
*
|
|
174
|
+
* 只用来决定"要不要探测":同网段地址拨不通会在面板里立刻明确报出来,没必要
|
|
175
|
+
* 每次都先花 1.5 秒探测;跨网段的地址(运营商 CGNAT、别的子网)才是对端自报
|
|
176
|
+
* 地址最容易骗人的地方 —— 10.x 尤其:手机在移动数据下的自报地址就长这样。
|
|
177
|
+
* @param {string} host
|
|
178
|
+
* @param {string[]} localAddresses 本机 IPv4 列表
|
|
179
|
+
*/
|
|
180
|
+
export function needsReachabilityProbe(host, localAddresses = []) {
|
|
181
|
+
const m = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(String(host ?? ""));
|
|
182
|
+
if (!m) return true;
|
|
183
|
+
if (m[1] === "127") return false;
|
|
184
|
+
const wanted = `${m[1]}.${m[2]}.${m[3]}`;
|
|
185
|
+
for (const address of localAddresses) {
|
|
186
|
+
const n = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.\d{1,3}$/.exec(String(address ?? ""));
|
|
187
|
+
if (!n) continue;
|
|
188
|
+
if (`${n[1]}.${n[2]}.${n[3]}` === wanted) return false;
|
|
189
|
+
}
|
|
190
|
+
return true;
|
|
191
|
+
}
|
package/llms.txt
CHANGED
|
@@ -124,6 +124,33 @@ the home board, enabled while the peer is online):
|
|
|
124
124
|
- The private key and credentials live under the profile's mesh directory (mode 0600);
|
|
125
125
|
they are never broadcast and never included in the peer card.
|
|
126
126
|
|
|
127
|
+
### Viewing a peer whose GUI only listens on loopback
|
|
128
|
+
|
|
129
|
+
A DSH Web GUI usually binds `127.0.0.1` — and DSH Desktop hardcodes that
|
|
130
|
+
(`desktopWebServerHost()` ignores `networkExposure`), so a profile patch cannot
|
|
131
|
+
change it. In that case `GET /mesh/v1/web-url` also returns a `tunnel` object and
|
|
132
|
+
the panel reaches the peer through the **already-open mesh port** instead:
|
|
133
|
+
|
|
134
|
+
```
|
|
135
|
+
viewer -> <peer>:<mesh port>/mesh/peer-web/<original path> (header: x-dsh-peer-web)
|
|
136
|
+
peer -> 127.0.0.1:<its own GUI port> (Host/Origin rewritten,
|
|
137
|
+
session cookie signed here)
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
- No certificate, no extra listener, no second profile: forwarding happens inside the
|
|
141
|
+
peer process, so a loopback-only GUI (including DSH Desktop) can be viewed.
|
|
142
|
+
- The ticket is **minted by the viewed machine** (`HMAC` with its own mesh secret),
|
|
143
|
+
lives 12 hours, is never persisted, and is only handed out by the authenticated
|
|
144
|
+
`/mesh/v1/web-url`. Changing the pairing code invalidates it immediately.
|
|
145
|
+
- Security policy is the **same as every other mesh route**: the one-click LAN/internet
|
|
146
|
+
kill switch answers 503; with `requirePairing` on, every tunnel request must carry the
|
|
147
|
+
shared secret (`x-dsh-mesh-token`, attached by the viewer's proxy). Neither the ticket
|
|
148
|
+
nor that secret is forwarded to the upstream GUI.
|
|
149
|
+
- Transport is plaintext HTTP — the same level as the mesh port itself. Use a pairing
|
|
150
|
+
code (or an SSH/tunnel wrapper) on untrusted networks.
|
|
151
|
+
- Both ends must run a version that knows the tunnel: an older peer never reports one,
|
|
152
|
+
so the panel falls back to the loopback URL and fails with `ECONNREFUSED`.
|
|
153
|
+
|
|
127
154
|
## Configuration
|
|
128
155
|
|
|
129
156
|
Override by id in the profile's `cordis.patch.yml`:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-plugin-mesh",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.14",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "把跑不完的活交给另一台电脑;跑完,结果回到你面前。两台装了 DSH 的电脑各装一次,自动互相发现,任务派过去由对端的 agent 执行,结果派回来。Hand a long task to another machine: your DSH instances find each other, the peer runs it, and the result comes back. No pairing codes, no IPs, no Tailscale client.",
|
|
6
6
|
"license": "MIT",
|
|
@@ -69,8 +69,8 @@
|
|
|
69
69
|
"selftest:update": "node lib/_selftest-update.mjs",
|
|
70
70
|
"selftest:upgrade": "node lib/_selftest-upgrade.mjs",
|
|
71
71
|
"selftest:approval": "node lib/_selftest-approval.mjs",
|
|
72
|
-
"test": "npm run check && npm run selftest && npm run selftest:prompt && npm run selftest:at && npm run selftest:at:host && npm run selftest:pairing && npm run selftest:approval && npm run selftest:switch && npm run selftest:client && npm run selftest:caps && npm run selftest:release && npm run selftest:reply && npm run selftest:reasoning && npm run selftest:resume && npm run selftest:persist && npm run selftest:layout && npm run selftest:diagnostics && npm run selftest:osinfo && npm run selftest:update && npm run selftest:upgrade",
|
|
73
|
-
"check": "node scripts/check-version.mjs && node scripts/check-tdz.mjs && node --check lib/index.js && node --check lib/layout.js && node --check lib/discovery.js && node --check lib/conversation.js && node --check lib/identity.js && node --check lib/capabilities.js && node --check lib/prompt.js && node --check lib/at-mention.js && node --check lib/pairing.js && node --check lib/switch.js && node --check lib/scan.js && node --check lib/update.js && node --check lib/upgrade.js && node --check lib/tsnet/download.js && node --check lib/tsnet/manager.js && node --check lib/tsnet/bridge.js && node --check lib/tsnet/channels.js && node --check client/client.js && node --check scripts/release-plan.mjs && npm run check:loadable && npm run check:docs",
|
|
72
|
+
"test": "npm run check && npm run selftest && npm run selftest:prompt && npm run selftest:at && npm run selftest:at:host && npm run selftest:pairing && npm run selftest:approval && npm run selftest:switch && npm run selftest:client && npm run selftest:caps && npm run selftest:release && npm run selftest:reply && npm run selftest:reasoning && npm run selftest:resume && npm run selftest:persist && npm run selftest:layout && npm run selftest:diagnostics && npm run selftest:osinfo && npm run selftest:peerweb && npm run selftest:webtarget && npm run selftest:sshtarget && npm run selftest:update && npm run selftest:upgrade",
|
|
73
|
+
"check": "node scripts/check-version.mjs && node scripts/check-tdz.mjs && node --check lib/index.js && node --check lib/layout.js && node --check lib/discovery.js && node --check lib/conversation.js && node --check lib/identity.js && node --check lib/capabilities.js && node --check lib/prompt.js && node --check lib/at-mention.js && node --check lib/pairing.js && node --check lib/switch.js && node --check lib/scan.js && node --check lib/update.js && node --check lib/upgrade.js && node --check lib/peer-web.js && node --check lib/web-target.js && node --check lib/ssh-target.js && node --check lib/tsnet/download.js && node --check lib/tsnet/manager.js && node --check lib/tsnet/bridge.js && node --check lib/tsnet/channels.js && node --check client/client.js && node --check scripts/release-plan.mjs && npm run check:loadable && npm run check:docs",
|
|
74
74
|
"build:sidecar": "bash scripts/build-sidecar.sh",
|
|
75
75
|
"verify:mirror": "node scripts/verify-lan-mirror.mjs",
|
|
76
76
|
"release:plan": "node scripts/release-plan.mjs",
|
|
@@ -83,7 +83,10 @@
|
|
|
83
83
|
"selftest:pairing": "node lib/_selftest-pairing.mjs",
|
|
84
84
|
"selftest:switch": "node lib/_selftest-switch.mjs",
|
|
85
85
|
"selftest:at": "node lib/_selftest-at-mention.mjs",
|
|
86
|
-
"selftest:at:host": "node --experimental-vm-modules lib/_selftest-at-mention-host.mjs"
|
|
86
|
+
"selftest:at:host": "node --experimental-vm-modules lib/_selftest-at-mention-host.mjs",
|
|
87
|
+
"selftest:peerweb": "node lib/_selftest-peer-web.mjs",
|
|
88
|
+
"selftest:webtarget": "node lib/_selftest-web-target.mjs",
|
|
89
|
+
"selftest:sshtarget": "node lib/_selftest-ssh-target.mjs"
|
|
87
90
|
},
|
|
88
91
|
"publishConfig": {
|
|
89
92
|
"registry": "https://registry.npmjs.org/",
|