dsh-hiss-plugins 1.5.1 → 3.0.0
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/README.md +15 -117
- package/cordis.patch.yml +2 -15
- package/lib/client.js +21 -278
- package/lib/project-mcp/index.js +2 -2
- package/locale/en.json +3 -17
- package/locale/zh.json +3 -17
- package/package.json +13 -23
- package/lib/codegraph/index.js +0 -62
- package/lib/colgrep/index.js +0 -247
- package/lib/lan-access/gateway.js +0 -348
- package/lib/lan-access/index.js +0 -340
- package/lib/lan-access/qr.js +0 -442
package/lib/lan-access/index.js
DELETED
|
@@ -1,340 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The lan-access row: one LAN listener that relays this Web Host, the
|
|
3
|
-
* `/lan` command that hands a phone the tokenized URL, and the loopback page
|
|
4
|
-
* that shows that URL as a QR code.
|
|
5
|
-
*
|
|
6
|
-
* The Desktop Host always binds the Host to loopback and reports
|
|
7
|
-
* `http://127.0.0.1:<port>` to its window, so the LAN surface cannot be the
|
|
8
|
-
* Host's own listener: it is a gateway beside it. See `./gateway.js` for why
|
|
9
|
-
* the forwarded request claims the loopback authority.
|
|
10
|
-
*
|
|
11
|
-
* @module dsh-hiss-plugins/lan-access
|
|
12
|
-
*/
|
|
13
|
-
import { randomBytes } from "node:crypto";
|
|
14
|
-
import { appendFileSync, mkdirSync, statSync } from "node:fs";
|
|
15
|
-
import { homedir } from "node:os";
|
|
16
|
-
import { join } from "node:path";
|
|
17
|
-
import { lanAddresses, startGateway } from "./gateway.js";
|
|
18
|
-
import { qrMatrix, qrSvg } from "./qr.js";
|
|
19
|
-
|
|
20
|
-
export const name = "lan-access";
|
|
21
|
-
export const inject = ["webServer"];
|
|
22
|
-
|
|
23
|
-
/** The stable LAN port a phone bookmark can keep. */
|
|
24
|
-
const DEFAULT_PORT = 7505;
|
|
25
|
-
/** Every IPv4 interface, so a DHCP address change never invalidates the bind. */
|
|
26
|
-
const WILDCARD = "0.0.0.0";
|
|
27
|
-
/** Loopback page that renders the pairing URL as a QR code. */
|
|
28
|
-
const PAGE_PATH = "/lan-access";
|
|
29
|
-
/** Diagnostics log: only refusals and pairing events, capped. */
|
|
30
|
-
const LOG_PATH = join(homedir(), ".dsh", "lan-access.log");
|
|
31
|
-
const LOG_LIMIT_BYTES = 256 * 1024;
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Mount the gateway, the QR page, and the command.
|
|
35
|
-
* @param {import("@deepseek-ai/cordis").Context} ctx - host context carrying `webServer`.
|
|
36
|
-
* @param {{ host?: string, port?: number | string }} [config] - profile-patch overrides.
|
|
37
|
-
*/
|
|
38
|
-
export function apply(ctx, config = {}) {
|
|
39
|
-
const port = portOf(config?.port);
|
|
40
|
-
const host = typeof config?.host === "string" && config.host !== "" ? config.host : undefined;
|
|
41
|
-
// Off by default: tests and ordinary users must not grow a log file; turning it
|
|
42
|
-
// on in the profile patch is what makes a phone-side refusal diagnosable.
|
|
43
|
-
const debug = config?.debug === true;
|
|
44
|
-
const pair = randomBytes(18).toString("base64url");
|
|
45
|
-
const state = { status: "starting", error: undefined, bindHost: undefined, port: undefined, pageUrl: undefined, pair };
|
|
46
|
-
|
|
47
|
-
ctx.inject(["connection"], (connectionCtx) => {
|
|
48
|
-
const connection = connectionCtx.connection;
|
|
49
|
-
const webServer = connectionCtx.get("webServer");
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* The URL a client opens to receive a browser cookie. DSH 0.2.2 moved browser
|
|
53
|
-
* auth into the Token provider and removed Connection's `authenticatedUrl`, so
|
|
54
|
-
* the signed URL is collected the documented way: emit `webserver/entry-url`
|
|
55
|
-
* and take the entry carrying the launch token (WebApp and the remote
|
|
56
|
-
* connection collect their entry URLs the same way).
|
|
57
|
-
* @param base - the advertised root without credentials.
|
|
58
|
-
* @returns the tokenized URL, or `base` when nothing signs one.
|
|
59
|
-
*/
|
|
60
|
-
const authenticatedUrl = (base) => {
|
|
61
|
-
if (webServer === undefined || typeof ctx.emit !== "function") return base;
|
|
62
|
-
const entries = [];
|
|
63
|
-
try {
|
|
64
|
-
ctx.emit(webServer, "webserver/entry-url", base, entries);
|
|
65
|
-
} catch {
|
|
66
|
-
return base;
|
|
67
|
-
}
|
|
68
|
-
return pickTokenUrl(entries, base);
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
if (webServer !== undefined && typeof webServer.register === "function") {
|
|
72
|
-
connectionCtx.effect(() => webServer.register({
|
|
73
|
-
kind: "exact",
|
|
74
|
-
path: PAGE_PATH,
|
|
75
|
-
// Hosts from 0.2.2 on admit every route themselves and hand the handler the
|
|
76
|
-
// admitted Peer; the page carries a pairing secret, so the requirement is
|
|
77
|
-
// declared here instead of being re-derived inside the handler.
|
|
78
|
-
auth: "required",
|
|
79
|
-
handler: (request, response, peer) => servePage(request, response, { state, connection, addresses: lanAddresses(), peer }),
|
|
80
|
-
}), "lan-access.page");
|
|
81
|
-
try {
|
|
82
|
-
state.pageUrl = new URL(PAGE_PATH, webServer.getOrigin()).href;
|
|
83
|
-
} catch {
|
|
84
|
-
state.pageUrl = undefined;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
connectionCtx.effect(async () => {
|
|
89
|
-
const server = connectionCtx.get("webServer");
|
|
90
|
-
if (server === undefined) return;
|
|
91
|
-
if (!isLoopback(server.host)) {
|
|
92
|
-
// Someone already ran `dsh web --host <address>`; a second listener
|
|
93
|
-
// would only add a surface without adding reach.
|
|
94
|
-
state.status = "direct";
|
|
95
|
-
state.bindHost = server.host;
|
|
96
|
-
state.port = server.port;
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
try {
|
|
100
|
-
const gateway = await bindGateway(server, port, host, {
|
|
101
|
-
pair,
|
|
102
|
-
tokenUrl: () => authenticatedUrl(`${server.getOrigin()}/`),
|
|
103
|
-
log: debug ? writeLog : undefined,
|
|
104
|
-
});
|
|
105
|
-
if (debug) writeLog({ event: "listen", address: gateway.address, port: gateway.port });
|
|
106
|
-
state.status = "listening";
|
|
107
|
-
state.bindHost = gateway.address;
|
|
108
|
-
state.port = gateway.port;
|
|
109
|
-
connectionCtx.logger.warn(`lan-access: serving the browser surface on port ${String(gateway.port)} over plain HTTP; disable the row to stop it`);
|
|
110
|
-
return async () => {
|
|
111
|
-
await gateway.close();
|
|
112
|
-
state.status = "closed";
|
|
113
|
-
};
|
|
114
|
-
} catch (error) {
|
|
115
|
-
state.status = "failed";
|
|
116
|
-
state.error = messageOf(error);
|
|
117
|
-
connectionCtx.logger.warn(`lan-access: ${state.error}`);
|
|
118
|
-
}
|
|
119
|
-
}, "lan-access.listen");
|
|
120
|
-
|
|
121
|
-
ctx.inject(["commands"], (commandCtx) => {
|
|
122
|
-
commandCtx.commands.register({
|
|
123
|
-
name: "lan",
|
|
124
|
-
description: "Show the LAN URL (and QR page) a phone can use for this Web GUI",
|
|
125
|
-
handler: (invocation) => (invocation.rawInput.trim() === ""
|
|
126
|
-
? report(state, authenticatedUrl, lanAddresses())
|
|
127
|
-
: { kind: "error", text: "Usage: /lan (no arguments)" }),
|
|
128
|
-
});
|
|
129
|
-
});
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Bind the LAN gateway, falling back to one concrete address when the wildcard
|
|
135
|
-
* port is already taken by this Host's own loopback listener on the same port.
|
|
136
|
-
* @param {{ host: string, port: number }} server - the Host's web server facts.
|
|
137
|
-
* @param {number} port - the configured LAN port.
|
|
138
|
-
* @param {string | undefined} pinned - an explicitly configured bind address.
|
|
139
|
-
* @returns the started gateway.
|
|
140
|
-
*/
|
|
141
|
-
async function bindGateway(server, port, pinned, pairing) {
|
|
142
|
-
const listenHost = pinned ?? WILDCARD;
|
|
143
|
-
const options = { targetHost: server.host, targetPort: server.port, listenPort: port, ...pairing };
|
|
144
|
-
try {
|
|
145
|
-
return await startGateway({ ...options, listenHost });
|
|
146
|
-
} catch (error) {
|
|
147
|
-
const fallback = pinned === undefined && error?.code === "EADDRINUSE"
|
|
148
|
-
? lanAddresses().find((entry) => entry.virtual !== true)?.address
|
|
149
|
-
: undefined;
|
|
150
|
-
if (fallback === undefined) throw error;
|
|
151
|
-
return startGateway({ ...options, listenHost: fallback });
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Build the `/lan` answer from the listener's current state; LAN addresses are
|
|
157
|
-
* read at call time, not at boot, so a DHCP change shows up instead of going stale.
|
|
158
|
-
* @param {{ status: string, error?: string, bindHost?: string, port?: number, pageUrl?: string }} state - the listener state.
|
|
159
|
-
* @param {(base: string) => string} authenticatedUrl - signs one URL with this process's launch token.
|
|
160
|
-
* @param {{ name: string, address: string }[]} addresses - the current LAN addresses.
|
|
161
|
-
* @returns {{ kind: "success" | "error", text: string }} the command result.
|
|
162
|
-
*/
|
|
163
|
-
export function report(state, authenticatedUrl, addresses) {
|
|
164
|
-
if (state.status === "listening") {
|
|
165
|
-
const physical = addresses.filter((entry) => entry.virtual !== true);
|
|
166
|
-
const ignored = addresses.filter((entry) => entry.virtual === true);
|
|
167
|
-
if (physical.length === 0) return { kind: "error", text: "LAN access is listening but this machine has no physical LAN IPv4 address right now." };
|
|
168
|
-
const url = `http://${physical[0].address}:${String(state.port)}/?pair=${String(state.pair)}`;
|
|
169
|
-
const lines = [
|
|
170
|
-
`LAN access: ${url}`,
|
|
171
|
-
state.pageUrl === undefined ? "Open this URL once on a phone on the same network; the signed browser cookie then lasts 30 days." : `QR code: open ${state.pageUrl} on this computer and scan it with the phone.`,
|
|
172
|
-
`Plain HTTP on the LAN: only use it on a network you trust (Windows firewall must allow TCP ${String(state.port)} for this app).`,
|
|
173
|
-
];
|
|
174
|
-
if (physical.length > 1) lines.push(`Other addresses: ${physical.slice(1).map((entry) => `${entry.address} (${entry.name})`).join(", ")}.`);
|
|
175
|
-
if (ignored.length > 0) lines.push(`Ignored virtual adapters (a phone cannot reach them): ${ignored.map((entry) => `${entry.address} (${entry.name})`).join(", ")}.`);
|
|
176
|
-
return { kind: "success", text: lines.join("\n") };
|
|
177
|
-
}
|
|
178
|
-
if (state.status === "direct") {
|
|
179
|
-
const url = authenticatedUrl(`http://${state.bindHost}:${String(state.port)}/`);
|
|
180
|
-
return { kind: "success", text: `The Host already listens on a network address: ${url}` };
|
|
181
|
-
}
|
|
182
|
-
if (state.status === "failed") return { kind: "error", text: `LAN access is not running: ${state.error}. Set \`host\` or \`port\` in the profile patch and reload the row.` };
|
|
183
|
-
if (state.status === "closed") return { kind: "error", text: "LAN access is disabled; enable the lan-access row in the Plugins page." };
|
|
184
|
-
return { kind: "error", text: "LAN access is still starting; run /lan again in a moment." };
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* Pick the entry URL that carries this process's launch token.
|
|
189
|
-
* @param {readonly { url?: string }[]} entries - rows `webserver/entry-url` collected.
|
|
190
|
-
* @param {string} base - the URL to keep when no provider signs one.
|
|
191
|
-
* @returns the tokenized URL, or `base`.
|
|
192
|
-
*/
|
|
193
|
-
export function pickTokenUrl(entries, base) {
|
|
194
|
-
for (const entry of entries) {
|
|
195
|
-
if (typeof entry?.url === "string" && /[?&]token=/u.test(entry.url)) return entry.url;
|
|
196
|
-
}
|
|
197
|
-
return base;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Serve the QR page to an authenticated owner only: it carries the pairing secret.
|
|
202
|
-
* @param {import("node:http").IncomingMessage} request - the page request.
|
|
203
|
-
* @param {import("node:http").ServerResponse} response - the answer.
|
|
204
|
-
* @param {{ state: object, connection: object, addresses: { name: string, address: string }[], peer?: object }} deps - listener state, browser auth, the LAN addresses to show, and the Peer the Host already admitted.
|
|
205
|
-
*/
|
|
206
|
-
export function servePage(request, response, { state, connection, addresses, peer }) {
|
|
207
|
-
// Hosts from 0.2.2 on move browser auth into the route and hand the handler the
|
|
208
|
-
// admitted Peer; asking `connection.admit` again for that same request fails there.
|
|
209
|
-
// An older Host admits no route, so the handler stays the only gate and checks.
|
|
210
|
-
if (peer === undefined) {
|
|
211
|
-
let admission;
|
|
212
|
-
try {
|
|
213
|
-
admission = connection.admit(request);
|
|
214
|
-
} catch {
|
|
215
|
-
admission = { rejection: 401 };
|
|
216
|
-
}
|
|
217
|
-
if (admission.rejection !== undefined) {
|
|
218
|
-
response.writeHead(admission.rejection, { "content-type": "text/plain; charset=utf-8", "cache-control": "no-store" });
|
|
219
|
-
response.end(admission.rejection === 401 ? "Open this page from the DSH window on this computer.\n" : "forbidden\n");
|
|
220
|
-
return;
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
let embed = false;
|
|
224
|
-
try {
|
|
225
|
-
embed = new URL(request.url ?? "/", "http://lan.invalid").searchParams.get("embed") === "1";
|
|
226
|
-
} catch {
|
|
227
|
-
embed = false;
|
|
228
|
-
}
|
|
229
|
-
response.writeHead(200, {
|
|
230
|
-
"content-type": "text/html; charset=utf-8",
|
|
231
|
-
"cache-control": "no-store",
|
|
232
|
-
"referrer-policy": "no-referrer",
|
|
233
|
-
});
|
|
234
|
-
response.end(renderPage(state, connection, addresses, { embed }));
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
/**
|
|
238
|
-
* Render the QR page.
|
|
239
|
-
* @param {{ status: string, error?: string, bindHost?: string, port?: number }} state - the listener state.
|
|
240
|
-
* @param {{ authenticatedUrl: (base: string) => string }} connection - the browser-auth carrier.
|
|
241
|
-
* @param {{ name: string, address: string }[]} addresses - the current LAN addresses.
|
|
242
|
-
* @param {{ embed?: boolean }} [options] - `embed` drops the page chrome for the in-app popup.
|
|
243
|
-
* @returns the HTML document.
|
|
244
|
-
*/
|
|
245
|
-
export function renderPage(state, connection, addresses, { embed = false } = {}) {
|
|
246
|
-
const parts = [
|
|
247
|
-
"<!doctype html><html lang=\"zh\"><head><meta charset=\"utf-8\">",
|
|
248
|
-
"<meta name=\"viewport\" content=\"width=device-width,initial-scale=1\">",
|
|
249
|
-
"<title>DSH 手机访问</title><style>body{font:15px/1.6 system-ui,sans-serif;margin:24px;color:#111;background:#fafafa}",
|
|
250
|
-
"section{background:#fff;border:1px solid #e4e4e7;border-radius:12px;padding:16px;margin:16px 0;max-width:420px}",
|
|
251
|
-
"h1{font-size:20px}h2{font-size:14px;color:#52525b;margin:0 0 12px}",
|
|
252
|
-
// The URL is read, not copied by sight: keep the body face at a readable size.
|
|
253
|
-
"code{font:inherit;font-size:14px;line-height:1.5;color:#3f3f46;word-break:break-all}svg{max-width:100%;height:auto}",
|
|
254
|
-
embed ? "body{margin:0;padding:8px;background:#fff;overflow:hidden}section{margin:0;border:none;padding:0;max-width:none}svg{display:block;margin:0 auto}" : "",
|
|
255
|
-
"</style></head><body>",
|
|
256
|
-
];
|
|
257
|
-
if (!embed) parts.push("<h1>手机访问本机 DSH</h1>");
|
|
258
|
-
if (state.status === "listening") {
|
|
259
|
-
const physical = addresses.filter((entry) => entry.virtual !== true);
|
|
260
|
-
if (physical.length === 0) parts.push("<p>本机当前只有虚拟网卡(Hyper-V / VPN),手机无法连接。</p>");
|
|
261
|
-
for (const entry of physical) {
|
|
262
|
-
const url = `http://${entry.address}:${String(state.port)}/?pair=${String(state.pair)}`;
|
|
263
|
-
parts.push(
|
|
264
|
-
`<section><h2>${escapeHtml(entry.name)} · ${escapeHtml(entry.address)}</h2>`,
|
|
265
|
-
qrSvg(qrMatrix(url), { scale: embed ? 6 : 8, border: 4 }),
|
|
266
|
-
`<p><code>${escapeHtml(url)}</code></p></section>`,
|
|
267
|
-
);
|
|
268
|
-
}
|
|
269
|
-
// No instructions here: the popup below the frame and /lan already say it,
|
|
270
|
-
// and a virtual adapter with no phone-reachable route is simply not listed.
|
|
271
|
-
} else if (state.status === "direct") {
|
|
272
|
-
parts.push(`<p>Host 已监听网络地址 <code>http://${escapeHtml(String(state.bindHost))}:${String(state.port)}/</code>,无需网关。</p>`);
|
|
273
|
-
} else if (state.status === "failed") {
|
|
274
|
-
parts.push(`<p role=\"alert\"><strong>局域网监听失败:</strong>${escapeHtml(String(state.error))}</p>`, "<p>在 profile 的 <code>cordis.patch.yml</code> 里给 <code>dsh-hiss-plugins-lan-access</code> 行设置 <code>config.host</code> / <code>config.port</code> 后重启。</p>");
|
|
275
|
-
} else {
|
|
276
|
-
parts.push("<p>正在启动局域网监听,稍后刷新本页。</p>");
|
|
277
|
-
}
|
|
278
|
-
parts.push("</body></html>");
|
|
279
|
-
return parts.join("");
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
/**
|
|
283
|
-
* Escape text for HTML text and attribute contexts.
|
|
284
|
-
* @param value - the text.
|
|
285
|
-
* @returns the escaped text.
|
|
286
|
-
*/
|
|
287
|
-
function escapeHtml(value) {
|
|
288
|
-
return String(value).replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll('"', """);
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
/**
|
|
292
|
-
* Append one diagnostic line, dropping the file instead of growing without
|
|
293
|
-
* bound. Every write failure is ignored: diagnostics never break the listener.
|
|
294
|
-
* @param entry - the event record.
|
|
295
|
-
*/
|
|
296
|
-
function writeLog(entry) {
|
|
297
|
-
try {
|
|
298
|
-
mkdirSync(join(homedir(), ".dsh"), { recursive: true });
|
|
299
|
-
let size = 0;
|
|
300
|
-
try {
|
|
301
|
-
size = statSync(LOG_PATH).size;
|
|
302
|
-
} catch {
|
|
303
|
-
size = 0;
|
|
304
|
-
}
|
|
305
|
-
const stamp = new Date().toISOString();
|
|
306
|
-
appendFileSync(LOG_PATH, `${stamp} ${JSON.stringify(entry)}\n`, { flag: size > LOG_LIMIT_BYTES ? "w" : "a" });
|
|
307
|
-
} catch {
|
|
308
|
-
// Diagnostics are optional.
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
/**
|
|
313
|
-
* Read the configured LAN port.
|
|
314
|
-
* @param value - the profile patch value, or undefined.
|
|
315
|
-
* @returns the listening port; 0 asks the OS to pick one.
|
|
316
|
-
*/
|
|
317
|
-
function portOf(value) {
|
|
318
|
-
if (value === undefined) return DEFAULT_PORT;
|
|
319
|
-
const port = Number(value);
|
|
320
|
-
if (!Number.isInteger(port) || port < 0 || port > 65535) throw new Error(`lan-access: port must be an integer 0-65535, got ${JSON.stringify(value)}`);
|
|
321
|
-
return port;
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
/**
|
|
325
|
-
* Whether an address is the loopback authority.
|
|
326
|
-
* @param host - a bind address literal.
|
|
327
|
-
* @returns true for `localhost`, `::1`, and 127/8.
|
|
328
|
-
*/
|
|
329
|
-
function isLoopback(host) {
|
|
330
|
-
return host === "localhost" || host === "::1" || /^127\./u.test(host);
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
/**
|
|
334
|
-
* Readable text for anything thrown.
|
|
335
|
-
* @param error - the thrown value.
|
|
336
|
-
* @returns its message.
|
|
337
|
-
*/
|
|
338
|
-
function messageOf(error) {
|
|
339
|
-
return error instanceof Error ? error.message : String(error);
|
|
340
|
-
}
|