@bom0792/herdgate 0.9.19 → 0.9.21
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 +72 -1
- package/bin/herdgate.mjs +37 -5
- package/dist-readme.md +27 -2
- package/package.json +3 -2
- package/plugin/server/common.mjs +27 -0
- package/plugin/server/herdr-ssh-win.mjs +43 -0
- package/plugin/server/herdr-ssh.mjs +102 -0
- package/plugin/server/index.mjs +78 -14
- package/plugin/server/publish-tui.mjs +89 -0
- package/plugin/server/publisher-tui-lifecycle.mjs +117 -0
- package/plugin/server/ssh.d.mts +23 -0
- package/plugin/server/ssh.mjs +122 -37
- package/scripts/build-host-bundle.sh +3 -3
- package/src/cloud/gateway.ts +4 -1
- package/src/cloud/sshd.ts +27 -23
- package/src/host/herdr.ts +66 -0
- package/src/host/ssh.ts +5 -315
- package/src/web/app.css +19 -0
- package/src/web/app.js +199 -1
- package/src/web/herdgate-host.zip +0 -0
- package/src/web/sw.js +1 -1
- package/windows/cli.mjs +46 -0
- package/windows/install-cli.mjs +129 -0
package/src/host/ssh.ts
CHANGED
|
@@ -1,317 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* The cloud relays `ssh.*` payloads opaquely; THIS module is the trusted decision point:
|
|
5
|
-
*
|
|
6
|
-
* - a channel runs exactly one command, and only `hc …` (argv split, `shell: false`) —
|
|
7
|
-
* never a shell, never anything else;
|
|
8
|
-
* - a `read` device gets a filtered plain-mode channel: mutating JSON-RPC methods are
|
|
9
|
-
* refused AT THIS GATE (the phone sees an honest JSON-RPC error; herdr-core never
|
|
10
|
-
* sees the call), a `framed: true` hello is downgraded to plain mode, and a stream
|
|
11
|
-
* of garbage is cut off;
|
|
12
|
-
* - an `act` device (approved by a human on this host) gets byte-transparent access —
|
|
13
|
-
* framed mode, attach, everything herdr-core offers;
|
|
14
|
-
* - output back to the phone is coalesced (~20 ms) so a chatty terminal stays under
|
|
15
|
-
* the wire's frame-rate limits.
|
|
16
|
-
*/
|
|
1
|
+
// The standalone host and self-contained plugin enforce the same SSH policy.
|
|
2
|
+
import { createSshBridge as create, type SshBridgeOptions } from "../../plugin/server/ssh.mjs";
|
|
3
|
+
export { READ_METHODS, translateBootstrap, type SshBridge } from "../../plugin/server/ssh.mjs";
|
|
17
4
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
import { homedir } from "node:os";
|
|
21
|
-
import { join } from "node:path";
|
|
22
|
-
|
|
23
|
-
/** Methods a `read` device may call on herdr-core. Everything else is refused here. */
|
|
24
|
-
export const READ_METHODS = new Set([
|
|
25
|
-
"hello",
|
|
26
|
-
"rpc.discover",
|
|
27
|
-
"server.status",
|
|
28
|
-
"terminal.list",
|
|
29
|
-
"terminal.get",
|
|
30
|
-
"terminal.read",
|
|
31
|
-
"terminal.wait",
|
|
32
|
-
"templates.list",
|
|
33
|
-
"events.subscribe",
|
|
34
|
-
"doc.get",
|
|
35
|
-
"machine.list",
|
|
36
|
-
]);
|
|
37
|
-
|
|
38
|
-
const FLUSH_MS = 20;
|
|
39
|
-
const FLUSH_BYTES = 64 * 1024;
|
|
40
|
-
const MAX_MALFORMED = 50;
|
|
41
|
-
|
|
42
|
-
export interface SshBridgeOptions {
|
|
43
|
-
/** Route an opaque payload to one device. */
|
|
44
|
-
send(device: string, payload: Record<string, unknown>): void;
|
|
45
|
-
/** The device's scope in this room (`null` when unknown). */
|
|
46
|
-
scopeOf(device: string): "read" | "act" | null;
|
|
47
|
-
log?(line: string): void;
|
|
48
|
-
/** Where herdr-core is; resolved once per channel by default. */
|
|
49
|
-
resolveHc?: () => string | null;
|
|
50
|
-
spawnImpl?: (bin: string, args: string[], opts: { stdio: ("pipe" | null)[]; shell: boolean }) => ChildProcess;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
interface HostChannel {
|
|
54
|
-
device: string;
|
|
55
|
-
ch: string;
|
|
56
|
-
scope: "read" | "act";
|
|
57
|
-
proc: ChildProcess | null;
|
|
58
|
-
/** Bytes from the phone awaiting a newline (read scope parses line by line). */
|
|
59
|
-
inBuf: Buffer;
|
|
60
|
-
/** Bytes from hc awaiting the next flush. */
|
|
61
|
-
outBuf: Buffer;
|
|
62
|
-
flushTimer: NodeJS.Timeout | null;
|
|
63
|
-
malformed: number;
|
|
64
|
-
closed: boolean;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export interface SshBridge {
|
|
68
|
-
/** Handle one frame payload; returns true when it was an ssh.* op this module owns. */
|
|
69
|
-
handle(device: string, payload: unknown): boolean;
|
|
70
|
-
/** Drop every channel of a device (device disconnected from the cloud). */
|
|
71
|
-
dropDevice(device: string): void;
|
|
72
|
-
closeAll(): void;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
/** Extract `hc <verb>` from herdr-core's known machine-add probes (null = not one). */
|
|
77
|
-
export function translateBootstrap(cmd: string): string | null {
|
|
78
|
-
// POSIX: sh -c 'p=$(command -v herdr-core) …; exec "$p" connect'
|
|
79
|
-
const posix = cmd.match(/exec "\$p"\s+([^'"\n]+?)['"]?\s*$/);
|
|
80
|
-
if (posix) {
|
|
81
|
-
const args = posix[1].trim();
|
|
82
|
-
// Strict whitelist: flags, words, equals — never shell metacharacters.
|
|
83
|
-
if (/^[-a-z0-9][-a-z0-9 =]*$/i.test(args)) return `hc ${args}`;
|
|
84
|
-
}
|
|
85
|
-
// Windows: cmd /d /c "…herdr-core.exe connect) …"
|
|
86
|
-
const win = cmd.match(/herdr-core\.exe\)?\s+\(?([a-z][a-z0-9-]*)\)/);
|
|
87
|
-
if (win) return `hc ${win[1]}`;
|
|
88
|
-
return null;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export function createSshBridge(o: SshBridgeOptions): SshBridge {
|
|
92
|
-
const log = o.log ?? (() => {});
|
|
93
|
-
const spawnFn = o.spawnImpl ?? spawn;
|
|
94
|
-
const channels = new Map<string, HostChannel>();
|
|
95
|
-
const key = (device: string, ch: string): string => `${device}:${ch}`;
|
|
96
|
-
|
|
97
|
-
const resolveHc =
|
|
98
|
-
o.resolveHc ??
|
|
99
|
-
((): string | null => {
|
|
100
|
-
const env = process.env.HERDGATE_HC_BIN?.trim();
|
|
101
|
-
if (env && existsSync(env)) return env;
|
|
102
|
-
const local = join(homedir(), ".local", "bin", "hc");
|
|
103
|
-
if (existsSync(local)) return local;
|
|
104
|
-
return existsSync("/usr/bin/hc") ? "/usr/bin/hc" : "hc"; // let spawn report ENOENT honestly
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
const sendOut = (c: HostChannel, final: boolean, code?: number, reason?: string): void => {
|
|
108
|
-
if (c.outBuf.length > 0) {
|
|
109
|
-
o.send(c.device, { op: "ssh.data", ch: c.ch, b64: c.outBuf.toString("base64") });
|
|
110
|
-
c.outBuf = Buffer.alloc(0);
|
|
111
|
-
}
|
|
112
|
-
if (final) {
|
|
113
|
-
o.send(c.device, { op: "ssh.closed", ch: c.ch, ...(code !== undefined ? { code } : {}), ...(reason ? { reason } : {}) });
|
|
114
|
-
}
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
const scheduleFlush = (c: HostChannel): void => {
|
|
118
|
-
if (c.flushTimer) return;
|
|
119
|
-
c.flushTimer = setTimeout(() => {
|
|
120
|
-
c.flushTimer = null;
|
|
121
|
-
if (!c.closed) sendOut(c, false);
|
|
122
|
-
}, FLUSH_MS);
|
|
123
|
-
c.flushTimer.unref?.();
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
const deny = (c: HostChannel, reason: string): void => {
|
|
127
|
-
o.send(c.device, { op: "ssh.denied", ch: c.ch, reason });
|
|
128
|
-
};
|
|
129
|
-
|
|
130
|
-
/** A refused call never reaches herdr-core: the phone gets a real JSON-RPC error. */
|
|
131
|
-
const refuseCall = (c: HostChannel, id: unknown, method: string): void => {
|
|
132
|
-
const err = JSON.stringify({
|
|
133
|
-
jsonrpc: "2.0",
|
|
134
|
-
id: id ?? null,
|
|
135
|
-
error: { code: -32600, message: `herdgate: "${method}" needs the act scope (this device is read-only)`, data: { kind: "forbidden", method } },
|
|
136
|
-
});
|
|
137
|
-
c.outBuf = Buffer.concat([c.outBuf, Buffer.from(`${err}\n`)]);
|
|
138
|
-
scheduleFlush(c);
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
/** Feed phone bytes into the channel; `read` scope parses, `act` pipes blind. */
|
|
142
|
-
const feed = (c: HostChannel, buf: Buffer): void => {
|
|
143
|
-
if (c.closed || !c.proc?.stdin) return;
|
|
144
|
-
if (c.scope === "act") {
|
|
145
|
-
c.proc.stdin.write(buf);
|
|
146
|
-
return;
|
|
147
|
-
}
|
|
148
|
-
c.inBuf = Buffer.concat([c.inBuf, buf]);
|
|
149
|
-
for (;;) {
|
|
150
|
-
const nl = c.inBuf.indexOf(0x0a);
|
|
151
|
-
if (nl < 0) break;
|
|
152
|
-
const line = c.inBuf.subarray(0, nl);
|
|
153
|
-
c.inBuf = c.inBuf.subarray(nl + 1);
|
|
154
|
-
const text = line.toString("utf8").trim();
|
|
155
|
-
if (!text) continue;
|
|
156
|
-
let msg: { method?: unknown; id?: unknown; params?: Record<string, unknown> } | null = null;
|
|
157
|
-
try {
|
|
158
|
-
msg = JSON.parse(text);
|
|
159
|
-
} catch {
|
|
160
|
-
msg = null;
|
|
161
|
-
}
|
|
162
|
-
if (!msg || typeof msg !== "object" || typeof msg.method !== "string") {
|
|
163
|
-
c.malformed += 1;
|
|
164
|
-
if (c.malformed > MAX_MALFORMED) {
|
|
165
|
-
log(`ssh: ${c.device}/${c.ch} cut off after ${c.malformed} malformed lines`);
|
|
166
|
-
kill(c, "too much malformed input");
|
|
167
|
-
return;
|
|
168
|
-
}
|
|
169
|
-
continue;
|
|
170
|
-
}
|
|
171
|
-
const method = msg.method;
|
|
172
|
-
if (method === "hello" && msg.params && typeof msg.params === "object") {
|
|
173
|
-
// Live terminals (framed attach) are byte streams a read-only device must not
|
|
174
|
-
// write into; downgrade to plain mode so list/read still work honestly.
|
|
175
|
-
if (msg.params.framed === true) {
|
|
176
|
-
msg.params.framed = false;
|
|
177
|
-
log(`ssh: ${c.device}/${c.ch} hello framed:true downgraded (read scope)`);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
if (!READ_METHODS.has(method)) {
|
|
181
|
-
refuseCall(c, msg.id, method);
|
|
182
|
-
continue;
|
|
183
|
-
}
|
|
184
|
-
c.proc.stdin.write(`${JSON.stringify(msg)}\n`);
|
|
185
|
-
}
|
|
186
|
-
};
|
|
187
|
-
|
|
188
|
-
const kill = (c: HostChannel, reason: string): void => {
|
|
189
|
-
c.closed = true;
|
|
190
|
-
if (c.flushTimer) clearTimeout(c.flushTimer);
|
|
191
|
-
c.proc?.kill("SIGKILL");
|
|
192
|
-
channels.delete(key(c.device, c.ch));
|
|
193
|
-
sendOut(c, true, 130, reason);
|
|
194
|
-
};
|
|
195
|
-
|
|
196
|
-
const handle = (device: string, payload: unknown): boolean => {
|
|
197
|
-
if (typeof payload !== "object" || payload === null) return false;
|
|
198
|
-
const op = (payload as { op?: unknown }).op;
|
|
199
|
-
if (typeof op !== "string" || !op.startsWith("ssh.")) return false;
|
|
200
|
-
const p = payload as Record<string, unknown>;
|
|
201
|
-
const ch = typeof p.ch === "string" ? p.ch : "";
|
|
202
|
-
|
|
203
|
-
if (op === "ssh.open") {
|
|
204
|
-
const scope = o.scopeOf(device);
|
|
205
|
-
if (!scope) {
|
|
206
|
-
o.send(device, { op: "ssh.denied", ch, reason: "this device is not paired to this room" });
|
|
207
|
-
return true;
|
|
208
|
-
}
|
|
209
|
-
const cmd = typeof p.cmd === "string" ? p.cmd.trim() : "";
|
|
210
|
-
// herdr-core's `machine add` probes with shell wrappers that locate a binary
|
|
211
|
-
// named `herdr-core` and `exec` it (POSIX and Windows templates). We never run
|
|
212
|
-
// shell lines: translate the KNOWN templates to the equivalent `hc <verb>` and
|
|
213
|
-
// let the normal path spawn it. Anything else shell-shaped is refused below.
|
|
214
|
-
const bootstrap = cmd.includes("herdr-core") ? translateBootstrap(cmd) : null;
|
|
215
|
-
const parts = (bootstrap ?? cmd).split(/\s+/).filter(Boolean);
|
|
216
|
-
if (parts.length > 0 && parts[0] === "exit" && (parts.length === 1 || parts[1] === "0")) {
|
|
217
|
-
// SSH clients end probe sessions with `exit` — acknowledge and close clean.
|
|
218
|
-
o.send(device, { op: "ssh.granted", ch, scope });
|
|
219
|
-
o.send(device, { op: "ssh.closed", ch, code: 0 });
|
|
220
|
-
return true;
|
|
221
|
-
}
|
|
222
|
-
if (parts.length === 0 || parts[0] !== "hc") {
|
|
223
|
-
// One command family only: the phone speaks herdr-core through this leg.
|
|
224
|
-
o.send(device, { op: "ssh.denied", ch, reason: `herdgate bridges "hc …" only, got "${cmd.slice(0, 60) || "(empty)"}"` });
|
|
225
|
-
return true;
|
|
226
|
-
}
|
|
227
|
-
const bin = resolveHc();
|
|
228
|
-
const args = parts.slice(1);
|
|
229
|
-
let proc: ChildProcess;
|
|
230
|
-
try {
|
|
231
|
-
proc = spawnFn(bin ?? "hc", args, { stdio: ["pipe", "pipe", "pipe"], shell: false });
|
|
232
|
-
} catch (e) {
|
|
233
|
-
o.send(device, { op: "ssh.denied", ch, reason: `could not start herdr-core: ${e instanceof Error ? e.message : String(e)}` });
|
|
234
|
-
return true;
|
|
235
|
-
}
|
|
236
|
-
const c: HostChannel = { device, ch, scope, proc, inBuf: Buffer.alloc(0), outBuf: Buffer.alloc(0), flushTimer: null, malformed: 0, closed: false };
|
|
237
|
-
channels.set(key(device, ch), c);
|
|
238
|
-
|
|
239
|
-
proc.stdout?.on("data", (chunk: Buffer) => {
|
|
240
|
-
c.outBuf = Buffer.concat([c.outBuf, chunk]);
|
|
241
|
-
scheduleFlush(c);
|
|
242
|
-
});
|
|
243
|
-
proc.stderr?.on("data", (chunk: Buffer) => {
|
|
244
|
-
// herdr-core writes diagnostics to stderr; the phone shows them as output too.
|
|
245
|
-
c.outBuf = Buffer.concat([c.outBuf, chunk]);
|
|
246
|
-
scheduleFlush(c);
|
|
247
|
-
});
|
|
248
|
-
proc.on("error", (e) => {
|
|
249
|
-
const enoent = (e as NodeJS.ErrnoException).code === "ENOENT";
|
|
250
|
-
log(`ssh: spawn failed for ${device}/${ch}: ${e.message}`);
|
|
251
|
-
channels.delete(key(device, ch));
|
|
252
|
-
c.closed = true;
|
|
253
|
-
if (c.flushTimer) clearTimeout(c.flushTimer);
|
|
254
|
-
o.send(device, {
|
|
255
|
-
op: "ssh.denied",
|
|
256
|
-
ch,
|
|
257
|
-
reason: enoent ? "herdr-core (hc) was not found on this host" : `could not start herdr-core: ${e.message}`,
|
|
258
|
-
});
|
|
259
|
-
});
|
|
260
|
-
proc.on("close", (code, signal) => {
|
|
261
|
-
if (c.closed) return;
|
|
262
|
-
c.closed = true;
|
|
263
|
-
if (c.flushTimer) clearTimeout(c.flushTimer);
|
|
264
|
-
channels.delete(key(device, ch));
|
|
265
|
-
sendOut(c, true, code ?? (signal ? 130 : 1), signal ? `terminated by ${signal}` : undefined);
|
|
266
|
-
});
|
|
267
|
-
|
|
268
|
-
o.send(device, { op: "ssh.granted", ch, scope });
|
|
269
|
-
log(`ssh: ${device}/${ch} → ${cmd} (${scope})`);
|
|
270
|
-
return true;
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
if (op === "ssh.data") {
|
|
274
|
-
const c = channels.get(key(device, ch));
|
|
275
|
-
if (!c) return true; // unknown or already closed: drop
|
|
276
|
-
const b64 = typeof p.b64 === "string" ? p.b64 : "";
|
|
277
|
-
if (b64.length > 400_000) return true;
|
|
278
|
-
feed(c, Buffer.from(b64, "base64"));
|
|
279
|
-
return true;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
if (op === "ssh.close") {
|
|
283
|
-
const c = channels.get(key(device, ch));
|
|
284
|
-
if (!c) return true;
|
|
285
|
-
c.closed = true;
|
|
286
|
-
if (c.flushTimer) clearTimeout(c.flushTimer);
|
|
287
|
-
c.proc?.kill("SIGTERM");
|
|
288
|
-
channels.delete(key(device, ch));
|
|
289
|
-
sendOut(c, true, 0, "closed by the device");
|
|
290
|
-
return true;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
return false; // an ssh.* op we don't know: tell the adapter layer it's not ours to read
|
|
294
|
-
};
|
|
295
|
-
|
|
296
|
-
return {
|
|
297
|
-
handle,
|
|
298
|
-
dropDevice(device: string): void {
|
|
299
|
-
for (const [k, c] of [...channels]) {
|
|
300
|
-
if (c.device === device) {
|
|
301
|
-
c.closed = true;
|
|
302
|
-
if (c.flushTimer) clearTimeout(c.flushTimer);
|
|
303
|
-
c.proc?.kill("SIGTERM");
|
|
304
|
-
channels.delete(k);
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
},
|
|
308
|
-
closeAll(): void {
|
|
309
|
-
for (const [, c] of [...channels]) {
|
|
310
|
-
c.closed = true;
|
|
311
|
-
if (c.flushTimer) clearTimeout(c.flushTimer);
|
|
312
|
-
c.proc?.kill("SIGTERM");
|
|
313
|
-
}
|
|
314
|
-
channels.clear();
|
|
315
|
-
},
|
|
316
|
-
};
|
|
5
|
+
export function createSshBridge(options: Omit<SshBridgeOptions, "resolve"> & { resolveHc?: SshBridgeOptions["resolve"] }) {
|
|
6
|
+
return create({ ...options, resolve: options.resolveHc });
|
|
317
7
|
}
|
package/src/web/app.css
CHANGED
|
@@ -310,3 +310,22 @@ dialog::backdrop {
|
|
|
310
310
|
border-radius: 6px;
|
|
311
311
|
}
|
|
312
312
|
.sshkeys .keyrow { margin-top: 8px; }
|
|
313
|
+
|
|
314
|
+
/* Live terminals (read-only) */
|
|
315
|
+
.termbody { display: flex; flex-direction: column; gap: 8px; }
|
|
316
|
+
.termlist .termname { text-align: left; }
|
|
317
|
+
.termrow { padding: 2px 0; }
|
|
318
|
+
.termbar { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
|
|
319
|
+
.termscreen {
|
|
320
|
+
max-height: 60vh;
|
|
321
|
+
overflow: auto;
|
|
322
|
+
background: #14161a; /* a terminal is dark: keep it dark in light mode too */
|
|
323
|
+
color: #cfd3da;
|
|
324
|
+
border: 1px solid #2a2d33;
|
|
325
|
+
border-radius: 8px;
|
|
326
|
+
padding: 10px 12px;
|
|
327
|
+
font: 12px/1.45 ui-monospace, SFMono-Regular, Menlo, monospace;
|
|
328
|
+
white-space: pre-wrap;
|
|
329
|
+
word-break: break-word;
|
|
330
|
+
margin: 0;
|
|
331
|
+
}
|
package/src/web/app.js
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
// - the token lives in localStorage on this device and is never sent anywhere but the
|
|
12
12
|
// cloud's own WS endpoint.
|
|
13
13
|
|
|
14
|
-
const BUILD = "v0.9.
|
|
14
|
+
const BUILD = "v0.9.20";
|
|
15
15
|
|
|
16
16
|
const $ = (id) => document.getElementById(id);
|
|
17
17
|
const TOKEN_KEY = "herdgate.token";
|
|
@@ -107,6 +107,12 @@ const state = {
|
|
|
107
107
|
roomId: null,
|
|
108
108
|
view: null,
|
|
109
109
|
viewError: null,
|
|
110
|
+
terms: null,
|
|
111
|
+
termTarget: null,
|
|
112
|
+
termAnsi: null,
|
|
113
|
+
termTimer: null,
|
|
114
|
+
termBody: null,
|
|
115
|
+
termPre: null,
|
|
110
116
|
retry: 0,
|
|
111
117
|
closedByUs: false,
|
|
112
118
|
};
|
|
@@ -267,6 +273,30 @@ function renderRoom() {
|
|
|
267
273
|
box.append(p);
|
|
268
274
|
}
|
|
269
275
|
|
|
276
|
+
// Live terminals (read-only): the host's own panes — pick one and watch its
|
|
277
|
+
// ANSI screen. Everything rides the same frames (E2E when paired), no new trust.
|
|
278
|
+
if (room?.online) {
|
|
279
|
+
const tblock = document.createElement("details");
|
|
280
|
+
tblock.className = "terms";
|
|
281
|
+
const ts = document.createElement("summary");
|
|
282
|
+
ts.textContent = "Live terminals — this machine's panes, read-only";
|
|
283
|
+
tblock.append(ts);
|
|
284
|
+
const tbody = document.createElement("div");
|
|
285
|
+
tbody.className = "termbody";
|
|
286
|
+
tblock.append(tbody);
|
|
287
|
+
tblock.addEventListener("toggle", () => {
|
|
288
|
+
if (tblock.open && !state.terms) send({ t: "frame", payload: { op: "term.list" } });
|
|
289
|
+
});
|
|
290
|
+
box.append(tblock);
|
|
291
|
+
state.termBody = tbody;
|
|
292
|
+
renderTerms();
|
|
293
|
+
if (state.termTarget) {
|
|
294
|
+
// The view rebuilds this node; keep the live pane ticking without a new poll.
|
|
295
|
+
paintTerm();
|
|
296
|
+
if (!state.termTimer) startTerm(state.termTarget);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
270
300
|
// The SSH leg: this device's own key dials the gate. In the Herdr phone app, add a
|
|
271
301
|
// machine with this host/port and the key shown once at pair time.
|
|
272
302
|
if (state.deviceSecret && state.deviceId) {
|
|
@@ -474,6 +504,162 @@ function renderRoom() {
|
|
|
474
504
|
}
|
|
475
505
|
}
|
|
476
506
|
|
|
507
|
+
// ---- live terminals (read-only) ---------------------------------------------
|
|
508
|
+
|
|
509
|
+
function startTerm(target) {
|
|
510
|
+
stopTerm(false);
|
|
511
|
+
state.termTarget = target;
|
|
512
|
+
state.termAnsi = null;
|
|
513
|
+
send({ t: "frame", payload: { op: "term.read", target, lines: 40 } });
|
|
514
|
+
state.termTimer = setInterval(() => {
|
|
515
|
+
if (!state.termTarget || !state.roomId) return stopTerm();
|
|
516
|
+
send({ t: "frame", payload: { op: "term.read", target: state.termTarget, lines: 40 } });
|
|
517
|
+
}, 2_000);
|
|
518
|
+
renderTerms();
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
function stopTerm(rerender = true) {
|
|
522
|
+
if (state.termTimer) clearInterval(state.termTimer);
|
|
523
|
+
state.termTimer = null;
|
|
524
|
+
state.termTarget = null;
|
|
525
|
+
state.termAnsi = null;
|
|
526
|
+
if (rerender) renderTerms();
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/** The panes list + the live pane, inside the details' body (survives view re-renders). */
|
|
530
|
+
function renderTerms() {
|
|
531
|
+
const body = state.termBody;
|
|
532
|
+
if (!body) return;
|
|
533
|
+
body.replaceChildren();
|
|
534
|
+
if (state.termTarget) {
|
|
535
|
+
const bar = document.createElement("div");
|
|
536
|
+
bar.className = "termbar";
|
|
537
|
+
const name = document.createElement("span");
|
|
538
|
+
name.className = "muted small";
|
|
539
|
+
name.textContent = `${state.termTarget} — live, refreshes every 2s`;
|
|
540
|
+
const stop = document.createElement("button");
|
|
541
|
+
stop.type = "button";
|
|
542
|
+
stop.className = "link";
|
|
543
|
+
stop.textContent = "close";
|
|
544
|
+
stop.addEventListener("click", () => stopTerm());
|
|
545
|
+
bar.append(name, stop);
|
|
546
|
+
body.append(bar);
|
|
547
|
+
const pre = document.createElement("pre");
|
|
548
|
+
pre.className = "termscreen";
|
|
549
|
+
pre.textContent = state.termAnsi ?? "waiting for the pane…";
|
|
550
|
+
body.append(pre);
|
|
551
|
+
state.termPre = pre;
|
|
552
|
+
paintTerm();
|
|
553
|
+
return;
|
|
554
|
+
}
|
|
555
|
+
const list = document.createElement("div");
|
|
556
|
+
list.className = "termlist";
|
|
557
|
+
const btn = document.createElement("button");
|
|
558
|
+
btn.type = "button";
|
|
559
|
+
btn.className = "link";
|
|
560
|
+
btn.textContent = "refresh the pane list";
|
|
561
|
+
btn.addEventListener("click", () => send({ t: "frame", payload: { op: "term.list" } }));
|
|
562
|
+
list.append(btn);
|
|
563
|
+
const panes = state.terms ?? null;
|
|
564
|
+
if (panes === null) {
|
|
565
|
+
list.append(note("loading…", "muted"));
|
|
566
|
+
} else if (!panes.length) {
|
|
567
|
+
list.append(note("no panes — is a workspace open on the machine?", "muted"));
|
|
568
|
+
} else {
|
|
569
|
+
const labels = new Map((state.view?.workspaces ?? []).map((w) => [w.id, w.label]));
|
|
570
|
+
for (const p of panes) {
|
|
571
|
+
const row = document.createElement("div");
|
|
572
|
+
row.className = "keyrow termrow";
|
|
573
|
+
const label = document.createElement("button");
|
|
574
|
+
label.type = "button";
|
|
575
|
+
label.className = "link termname";
|
|
576
|
+
const ws = labels.get(p.workspace) || p.workspace;
|
|
577
|
+
label.textContent = `${ws} · ${p.agent ? `${p.agent} (${p.status})` : p.title || "terminal"}`;
|
|
578
|
+
label.title = `${p.cwd} — ${p.id}`;
|
|
579
|
+
label.addEventListener("click", () => startTerm(p.id));
|
|
580
|
+
row.append(label);
|
|
581
|
+
list.append(row);
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
body.append(list);
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
function paintTerm() {
|
|
588
|
+
const pre = state.termPre;
|
|
589
|
+
if (!pre || !state.termAnsi) return;
|
|
590
|
+
pre.replaceChildren(...ansiToNodes(state.termAnsi));
|
|
591
|
+
pre.scrollTop = pre.scrollHeight;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
/** SGR-only ANSI → DOM nodes: colors (palette + truecolor), bold. Cursor and other
|
|
595
|
+
* sequences are dropped — this is a reading view, not an emulator. No innerHTML:
|
|
596
|
+
* every byte from the wire becomes text nodes, never markup. */
|
|
597
|
+
function ansiToNodes(s) {
|
|
598
|
+
const pal = ["#5b5b5b", "#d54e53", "#5db542", "#e5c07b", "#61afef", "#c678dd", "#56b6c2", "#acacac", "#8a8a8a", "#ff6f6f", "#8ce07d", "#ffe08a", "#93c5fd", "#e2a2ff", "#8be9fd", "#ffffff"];
|
|
599
|
+
const out = [];
|
|
600
|
+
let fg = null;
|
|
601
|
+
let bg = null;
|
|
602
|
+
let bold = false;
|
|
603
|
+
let i = 0;
|
|
604
|
+
s = String(s).replace(/\r\n?/g, "\n");
|
|
605
|
+
const flush = (text) => {
|
|
606
|
+
if (!text) return;
|
|
607
|
+
if (fg === null && bg === null && !bold) {
|
|
608
|
+
out.push(document.createTextNode(text));
|
|
609
|
+
return;
|
|
610
|
+
}
|
|
611
|
+
const span = document.createElement("span");
|
|
612
|
+
if (fg) span.style.color = bold ? `color-mix(in srgb, ${fg} 70%, white)` : fg;
|
|
613
|
+
if (bg) span.style.background = bg;
|
|
614
|
+
if (bold && !fg) span.style.fontWeight = "600";
|
|
615
|
+
span.textContent = text;
|
|
616
|
+
out.push(span);
|
|
617
|
+
};
|
|
618
|
+
while (i < s.length) {
|
|
619
|
+
if (s.charCodeAt(i) === 27) {
|
|
620
|
+
const rest = s.slice(i);
|
|
621
|
+
let m = /^\x1b\[([0-9;]*)([A-Za-z])/.exec(rest);
|
|
622
|
+
if (m) {
|
|
623
|
+
if (m[2] === "m") {
|
|
624
|
+
const parts = m[1] === "" ? [0] : m[1].split(";").map((x) => (x === "" ? 0 : Number(x)));
|
|
625
|
+
for (let k = 0; k < parts.length; k += 1) {
|
|
626
|
+
const p = parts[k];
|
|
627
|
+
if (p === 0) { fg = bg = null; bold = false; }
|
|
628
|
+
else if (p === 1) bold = true;
|
|
629
|
+
else if (p === 22) bold = false;
|
|
630
|
+
else if (p === 39) fg = null;
|
|
631
|
+
else if (p === 49) bg = null;
|
|
632
|
+
else if (p >= 30 && p <= 37) fg = pal[p - 30];
|
|
633
|
+
else if (p >= 90 && p <= 97) fg = pal[p - 90 + 8];
|
|
634
|
+
else if (p >= 40 && p <= 47) bg = pal[p - 40];
|
|
635
|
+
else if (p >= 100 && p <= 107) bg = pal[p - 100 + 8];
|
|
636
|
+
else if ((p === 38 || p === 48) && parts[k + 1] === 2) {
|
|
637
|
+
const c = `rgb(${parts[k + 2] || 0},${parts[k + 3] || 0},${parts[k + 4] || 0})`;
|
|
638
|
+
if (p === 38) fg = c; else bg = c;
|
|
639
|
+
k += 4;
|
|
640
|
+
} else if ((p === 38 || p === 48) && parts[k + 1] === 5) {
|
|
641
|
+
const n8 = parts[k + 2] || 0;
|
|
642
|
+
const c = pal[n8 % 16];
|
|
643
|
+
if (p === 38) fg = c; else bg = c;
|
|
644
|
+
k += 2;
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
i += m[0].length;
|
|
649
|
+
continue;
|
|
650
|
+
}
|
|
651
|
+
m = /^\x1b\][^\x07]*(?:\x07|\x1b\\)/.exec(rest) || /^\x1b./.exec(rest);
|
|
652
|
+
i += m ? m[0].length : 1;
|
|
653
|
+
continue;
|
|
654
|
+
}
|
|
655
|
+
let j = s.indexOf("\x1b", i);
|
|
656
|
+
if (j < 0) j = s.length;
|
|
657
|
+
flush(s.slice(i, j));
|
|
658
|
+
i = j;
|
|
659
|
+
}
|
|
660
|
+
return out;
|
|
661
|
+
}
|
|
662
|
+
|
|
477
663
|
function note(message, kind) {
|
|
478
664
|
const p = document.createElement("p");
|
|
479
665
|
p.className = kind === "error" ? "error" : "muted";
|
|
@@ -688,6 +874,18 @@ function applyPayload(payload) {
|
|
|
688
874
|
if (payload && typeof payload === "object" && payload.op === "view") {
|
|
689
875
|
state.view = payload.view;
|
|
690
876
|
state.viewError = null;
|
|
877
|
+
} else if (payload && typeof payload === "object" && payload.op === "term.list") {
|
|
878
|
+
state.terms = Array.isArray(payload.panes) ? payload.panes : [];
|
|
879
|
+
renderTerms();
|
|
880
|
+
return;
|
|
881
|
+
} else if (payload && typeof payload === "object" && payload.op === "term.read") {
|
|
882
|
+
if (payload.target === state.termTarget && typeof payload.ansi === "string") {
|
|
883
|
+
state.termAnsi = payload.ansi;
|
|
884
|
+
paintTerm();
|
|
885
|
+
}
|
|
886
|
+
return;
|
|
887
|
+
} else if (payload && typeof payload === "object" && payload.op === "error" && payload.code === "busy") {
|
|
888
|
+
return; // our poll raced the host's 1/s throttle — the next tick catches up
|
|
691
889
|
} else if (payload && typeof payload === "object" && payload.op === "error") {
|
|
692
890
|
// The host refused or could not answer: an op we may not use, or its Herdr is down.
|
|
693
891
|
state.viewError = payload.message ?? payload.code ?? "the host refused";
|
|
Binary file
|
package/src/web/sw.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
//
|
|
4
4
|
// Bump VERSION when a shell file changes; old caches are dropped on activate.
|
|
5
5
|
|
|
6
|
-
const VERSION = "herdgate-
|
|
6
|
+
const VERSION = "herdgate-v11";
|
|
7
7
|
const SHELL = ["/", "/app.css", "/e2e.js", "/app.js", "/app.webmanifest", "/icons/icon.svg"];
|
|
8
8
|
|
|
9
9
|
self.addEventListener("install", (event) => {
|
package/windows/cli.mjs
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { existsSync, readFileSync, readdirSync, realpathSync } from "node:fs";
|
|
3
|
+
import { basename, dirname, join } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
|
|
6
|
+
const release = realpathSync(fileURLToPath(new URL("../", import.meta.url)));
|
|
7
|
+
const releases = dirname(release);
|
|
8
|
+
const root = dirname(releases);
|
|
9
|
+
const usage = `herdgate-tui start [path-to-herdr.exe]
|
|
10
|
+
herdgate-tui status | stop | version | versions
|
|
11
|
+
herdgate-tui use <release-id>`;
|
|
12
|
+
|
|
13
|
+
function run(file, args) {
|
|
14
|
+
const result = spawnSync(process.execPath, [join(release, file), ...args], { stdio: "inherit", windowsHide: true, shell: false });
|
|
15
|
+
if (result.error) throw result.error;
|
|
16
|
+
process.exitCode = result.status ?? 1;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
try {
|
|
20
|
+
const [cmd, ...args] = process.argv.slice(2);
|
|
21
|
+
if ((!cmd || cmd === "help" || cmd === "--help") && !args.length) {
|
|
22
|
+
console.log(usage);
|
|
23
|
+
} else if (cmd === "version" && !args.length) {
|
|
24
|
+
console.log(readFileSync(join(release, "release.txt"), "utf8").trim());
|
|
25
|
+
} else if (cmd === "versions" && !args.length) {
|
|
26
|
+
for (const entry of readdirSync(releases, { withFileTypes: true })) {
|
|
27
|
+
if (entry.isDirectory() && /^tui-[a-f0-9]{16}$/.test(entry.name)) {
|
|
28
|
+
console.log(`${entry.name}${entry.name === basename(release) ? " (current)" : ""}`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
} else if (cmd === "use" && args.length === 1) {
|
|
32
|
+
run("windows/install-cli.mjs", ["--root", root, "--use", args[0]]);
|
|
33
|
+
} else if ((cmd === "status" || cmd === "stop") && !args.length) {
|
|
34
|
+
run("plugin/server/publish-tui.mjs", [cmd]);
|
|
35
|
+
} else if (cmd === "start" && args.length <= 1) {
|
|
36
|
+
const pilot = process.env.LOCALAPPDATA && join(process.env.LOCALAPPDATA, "HerdrPilot", "v0.9.5", "herdr.exe");
|
|
37
|
+
const bin = args[0] || process.env.HERDGATE_HERDR_BIN || (pilot && existsSync(pilot) ? pilot : null);
|
|
38
|
+
if (!bin) throw new Error("give the full path to Herdr 0.9.5: herdgate-tui start <path-to-herdr.exe>");
|
|
39
|
+
run("plugin/server/publish-tui.mjs", [cmd, bin]);
|
|
40
|
+
} else {
|
|
41
|
+
throw new Error(usage);
|
|
42
|
+
}
|
|
43
|
+
} catch (e) {
|
|
44
|
+
console.error(`herdgate-tui: ${e instanceof Error ? e.message : String(e)}`);
|
|
45
|
+
process.exitCode = 1;
|
|
46
|
+
}
|