dsh-plugin-mesh 0.5.4 → 0.5.6
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/AGENTS.md +4 -2
- package/README.md +61 -1
- package/client/client.js +1122 -17
- package/lib/_selftest-reply-route.mjs +80 -2
- package/lib/_selftest-switch.mjs +142 -0
- package/lib/_selftest-tsnet.mjs +86 -1
- package/lib/_selftest-workbench.mjs +236 -0
- package/lib/desktop-auth.js +88 -0
- package/lib/desktop-web.js +146 -0
- package/lib/index.js +1022 -109
- package/lib/ssh-host.js +380 -0
- package/lib/switch.js +110 -0
- package/lib/tsnet/manager.js +129 -3
- package/lib/web-proxy.js +333 -0
- package/lib/ws-mini.js +156 -0
- package/llms.txt +61 -1
- package/package.json +8 -4
- package/scripts/check-tarball.mjs +1 -0
|
@@ -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
|
+
}
|