@bom0792/herdgate 0.0.0-stage → 0.9.5
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 +167 -2
- package/app-mod/herdgate/main.js +395 -0
- package/app-mod/herdgate/mod.json +5 -0
- package/bin/herdgate.mjs +92 -0
- package/dist-readme.md +48 -0
- package/package.json +36 -3
- package/plugin/herdr-plugin.toml +36 -0
- package/plugin/server/common.mjs +79 -0
- package/plugin/server/e2e.d.mts +43 -0
- package/plugin/server/e2e.mjs +268 -0
- package/plugin/server/index.mjs +454 -0
- package/plugin/server/ssh.mjs +241 -0
- package/scripts/build-host-bundle.sh +12 -0
- package/scripts/deploy-vps.sh +74 -0
- package/scripts/patch-ssh2.mjs +32 -0
- package/src/agent/adapter.ts +23 -0
- package/src/agent/index.ts +183 -0
- package/src/cloud/gateway.ts +946 -0
- package/src/cloud/main.ts +88 -0
- package/src/cloud/pairing.ts +81 -0
- package/src/cloud/ssh-bridge.ts +212 -0
- package/src/cloud/sshd.ts +412 -0
- package/src/cloud/static.ts +142 -0
- package/src/cloud/stores.ts +180 -0
- package/src/cloud/util.ts +47 -0
- package/src/cloud/vapid.ts +105 -0
- package/src/dev/edge-sim.ts +99 -0
- package/src/dev/main.ts +33 -0
- package/src/host/herdr.ts +316 -0
- package/src/host/main.ts +105 -0
- package/src/host/ssh.ts +317 -0
- package/src/index.ts +5 -0
- package/src/protocol/agent.ts +84 -0
- package/src/protocol/device.ts +90 -0
- package/src/protocol/envelope.ts +58 -0
- package/src/protocol/index.ts +3 -0
- package/src/web/app.css +312 -0
- package/src/web/app.js +835 -0
- package/src/web/app.webmanifest +15 -0
- package/src/web/e2e.js +268 -0
- package/src/web/herdgate-host.zip +0 -0
- package/src/web/icons/icon-192.png +0 -0
- package/src/web/icons/icon-512.png +0 -0
- package/src/web/icons/icon.svg +6 -0
- package/src/web/index.html +99 -0
- package/src/web/sw.js +98 -0
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Herdr adapter: official Herdr (https://herdr.dev) as a herdgate host.
|
|
3
|
+
*
|
|
4
|
+
* Upstream contract used here — and nothing else:
|
|
5
|
+
* - `herdr api snapshot` → one JSON object `{id, result:{snapshot}}`, printed by the
|
|
6
|
+
* Herdr CLI without a TUI (`herdr api --help`, protocol 22 on 0.9.5). The snapshot
|
|
7
|
+
* carries `version`, `protocol`, `workspaces[]`, `tabs[]`, `panes[]`, `agents[]`
|
|
8
|
+
* and the focused ids. Verified against a live server; shapes are read per field,
|
|
9
|
+
* never predicted.
|
|
10
|
+
* - `herdr status --json` (optional) distinguishes "no server running" from a real
|
|
11
|
+
* failure so the device sees an honest state instead of an empty list.
|
|
12
|
+
*
|
|
13
|
+
* This adapter is **metadata-only and read-only**. It never runs `pane read`, never
|
|
14
|
+
* writes a keystroke, never prompts an agent, and never starts or splits layout — the
|
|
15
|
+
* device view is workspaces/tabs/panes/agents/status and nothing else. Terminal
|
|
16
|
+
* content is deliberately out of scope for the first slice.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { execFile } from "node:child_process";
|
|
20
|
+
import { promisify } from "node:util";
|
|
21
|
+
import type { AgentAdapter, PairAsk } from "../agent/adapter.ts";
|
|
22
|
+
import type { DeviceView } from "../protocol/agent.ts";
|
|
23
|
+
|
|
24
|
+
const run = promisify(execFile);
|
|
25
|
+
|
|
26
|
+
/** Biggest snapshot we accept from the CLI (a session with thousands of panes). */
|
|
27
|
+
const MAX_SNAPSHOT_BYTES = 8 * 1024 * 1024;
|
|
28
|
+
|
|
29
|
+
export interface HerdrWorkspace {
|
|
30
|
+
id: string;
|
|
31
|
+
label: string;
|
|
32
|
+
number: number;
|
|
33
|
+
tabs: number;
|
|
34
|
+
panes: number;
|
|
35
|
+
agentStatus: string;
|
|
36
|
+
focused: boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface HerdrAgent {
|
|
40
|
+
name: string;
|
|
41
|
+
/** Herdr's recognized agent kind (`pi`, `claude`, `codex`, …). */
|
|
42
|
+
kind: string;
|
|
43
|
+
/** `idle` | `working` | `blocked` | `done` | `unknown` — upstream's own words. */
|
|
44
|
+
status: string;
|
|
45
|
+
workspaceId: string;
|
|
46
|
+
tabId: string;
|
|
47
|
+
paneId: string;
|
|
48
|
+
cwd: string;
|
|
49
|
+
title: string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface HerdrView {
|
|
53
|
+
v: 1;
|
|
54
|
+
source: "herdr";
|
|
55
|
+
herdrVersion: string;
|
|
56
|
+
protocol: number;
|
|
57
|
+
takenAt: string;
|
|
58
|
+
focused: { workspaceId: string | null; tabId: string | null; paneId: string | null };
|
|
59
|
+
workspaces: HerdrWorkspace[];
|
|
60
|
+
agents: HerdrAgent[];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const str = (v: unknown, fallback = ""): string => (typeof v === "string" ? v : fallback);
|
|
64
|
+
const num = (v: unknown, fallback = 0): number => (typeof v === "number" && Number.isFinite(v) ? v : fallback);
|
|
65
|
+
const bool = (v: unknown): boolean => v === true;
|
|
66
|
+
const arr = (v: unknown): unknown[] => (Array.isArray(v) ? v : []);
|
|
67
|
+
/** Only object entries: a junk element in `workspaces[]` is dropped, not shown empty. */
|
|
68
|
+
const objs = (v: unknown): Record<string, unknown>[] =>
|
|
69
|
+
arr(v).filter((x): x is Record<string, unknown> => typeof x === "object" && x !== null && !Array.isArray(x));
|
|
70
|
+
const obj = (v: unknown): Record<string, unknown> => (typeof v === "object" && v !== null ? (v as Record<string, unknown>) : {});
|
|
71
|
+
|
|
72
|
+
function unwrapSnapshot(raw: unknown): Record<string, unknown> {
|
|
73
|
+
const top = obj(raw);
|
|
74
|
+
const result = obj(top.result);
|
|
75
|
+
if (result.snapshot !== undefined) return obj(result.snapshot);
|
|
76
|
+
if (top.snapshot !== undefined) return obj(top.snapshot);
|
|
77
|
+
return top; // already the snapshot itself
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Map a `herdr api snapshot` document onto the device view. Pure and tolerant: every
|
|
82
|
+
* field is read defensively, so a newer Herdr that adds keys keeps working and a
|
|
83
|
+
* missing key degrades to an empty/honest value rather than throwing.
|
|
84
|
+
*/
|
|
85
|
+
export function mapSnapshot(raw: unknown, takenAt: string = new Date().toISOString()): HerdrView {
|
|
86
|
+
const snap = unwrapSnapshot(raw);
|
|
87
|
+
const workspaces = objs(snap.workspaces).map((o): HerdrWorkspace => {
|
|
88
|
+
return {
|
|
89
|
+
id: str(o.workspace_id),
|
|
90
|
+
label: str(o.label),
|
|
91
|
+
number: num(o.number),
|
|
92
|
+
tabs: num(o.tab_count),
|
|
93
|
+
panes: num(o.pane_count),
|
|
94
|
+
agentStatus: str(o.agent_status, "unknown"),
|
|
95
|
+
focused: bool(o.focused),
|
|
96
|
+
};
|
|
97
|
+
});
|
|
98
|
+
const agents = objs(snap.agents).map((o): HerdrAgent => {
|
|
99
|
+
return {
|
|
100
|
+
name: str(o.agent),
|
|
101
|
+
kind: str(obj(o.agent_session).agent, str(o.agent)),
|
|
102
|
+
status: str(o.agent_status, "unknown"),
|
|
103
|
+
workspaceId: str(o.workspace_id),
|
|
104
|
+
tabId: str(o.tab_id),
|
|
105
|
+
paneId: str(o.pane_id),
|
|
106
|
+
cwd: str(o.cwd),
|
|
107
|
+
title: str(o.terminal_title_stripped, str(o.terminal_title)),
|
|
108
|
+
};
|
|
109
|
+
});
|
|
110
|
+
const focused = obj(snap);
|
|
111
|
+
return {
|
|
112
|
+
v: 1,
|
|
113
|
+
source: "herdr",
|
|
114
|
+
herdrVersion: str(focused.version),
|
|
115
|
+
protocol: num(focused.protocol),
|
|
116
|
+
takenAt,
|
|
117
|
+
focused: {
|
|
118
|
+
workspaceId: typeof focused.focused_workspace_id === "string" ? focused.focused_workspace_id : null,
|
|
119
|
+
tabId: typeof focused.focused_tab_id === "string" ? focused.focused_tab_id : null,
|
|
120
|
+
paneId: typeof focused.focused_pane_id === "string" ? focused.focused_pane_id : null,
|
|
121
|
+
},
|
|
122
|
+
workspaces,
|
|
123
|
+
agents,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export interface HerdrReadOptions {
|
|
128
|
+
/** `herdr` on PATH by default (or $HERDGATE_HERDR_BIN). */
|
|
129
|
+
bin?: string;
|
|
130
|
+
timeoutMs?: number;
|
|
131
|
+
env?: NodeJS.ProcessEnv;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Run `herdr api snapshot` once and map it. Throws with the CLI's own words. */
|
|
135
|
+
export async function readHerdrView(o: HerdrReadOptions = {}): Promise<HerdrView> {
|
|
136
|
+
const bin = o.bin ?? process.env.HERDGATE_HERDR_BIN ?? "herdr";
|
|
137
|
+
let stdout: string;
|
|
138
|
+
try {
|
|
139
|
+
({ stdout } = await run(bin, ["api", "snapshot"], {
|
|
140
|
+
timeout: o.timeoutMs ?? 5_000,
|
|
141
|
+
maxBuffer: MAX_SNAPSHOT_BYTES,
|
|
142
|
+
env: o.env ?? process.env,
|
|
143
|
+
}));
|
|
144
|
+
} catch (e) {
|
|
145
|
+
const err = e as { stderr?: string; message?: string; code?: string };
|
|
146
|
+
const detail = (err.stderr ?? "").trim() || err.message || "herdr api snapshot failed";
|
|
147
|
+
throw new Error(`herdr: ${detail}`);
|
|
148
|
+
}
|
|
149
|
+
let parsed: unknown;
|
|
150
|
+
try {
|
|
151
|
+
parsed = JSON.parse(stdout);
|
|
152
|
+
} catch {
|
|
153
|
+
throw new Error("herdr: `api snapshot` did not print JSON");
|
|
154
|
+
}
|
|
155
|
+
return mapSnapshot(parsed);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** What the host can push/drive; the agent supplies it (see `HerdrAdapter.attach`). */
|
|
159
|
+
export interface HostLink {
|
|
160
|
+
send(device: string, payload: unknown): void;
|
|
161
|
+
readonly devices: readonly DeviceView[];
|
|
162
|
+
scopeOf(device: string): "read" | "act" | null;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export interface HerdrAdapterOptions {
|
|
166
|
+
/** Report name; defaults to the machine's hostname-ish label. */
|
|
167
|
+
name: string;
|
|
168
|
+
/** Snapshot poll interval. Default 5s. */
|
|
169
|
+
pollMs?: number;
|
|
170
|
+
/** Pair `act` devices without asking a human. Default **false**. */
|
|
171
|
+
allowAct?: boolean;
|
|
172
|
+
log?: (line: string) => void;
|
|
173
|
+
/** Injectable reader (tests). Default: the real CLI. */
|
|
174
|
+
read?: () => Promise<HerdrView>;
|
|
175
|
+
now?: () => Date;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* The payload contract between a paired device and this host. The cloud never looks
|
|
180
|
+
* inside `payload`, so this is herdgate's own business — versioned with `op` strings.
|
|
181
|
+
*
|
|
182
|
+
* device → host {op:"view"} → {op:"view", view}
|
|
183
|
+
* {op:"ping", n?} → {op:"pong", n, at}
|
|
184
|
+
* host → device {op:"view", view} (pushed when the view changes)
|
|
185
|
+
* {op:"error", code, message} (unknown/refused op; `read-only` today)
|
|
186
|
+
*/
|
|
187
|
+
export const HOST_PROTOCOL = "herdgate.herdr.v1" as const;
|
|
188
|
+
|
|
189
|
+
export class HerdrAdapter implements AgentAdapter {
|
|
190
|
+
readonly #o: HerdrAdapterOptions;
|
|
191
|
+
readonly #log: (line: string) => void;
|
|
192
|
+
#link: HostLink | null = null;
|
|
193
|
+
#view: HerdrView | null = null;
|
|
194
|
+
#sig = "";
|
|
195
|
+
#lastError: string | null = null;
|
|
196
|
+
#timer: NodeJS.Timeout | null = null;
|
|
197
|
+
#reading = false;
|
|
198
|
+
|
|
199
|
+
constructor(o: HerdrAdapterOptions) {
|
|
200
|
+
this.#o = o;
|
|
201
|
+
this.#log = o.log ?? (() => {});
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** Wire the adapter to the live agent connection (idempotent). */
|
|
205
|
+
attach(link: HostLink): void {
|
|
206
|
+
this.#link = link;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
info(): { name: string; version: string; capabilities?: string[] } {
|
|
210
|
+
return { name: this.#o.name, version: "0.0.1", capabilities: [HOST_PROTOCOL, "read-only"] };
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Latest view pushed (or `null` before the first successful read). */
|
|
214
|
+
get view(): HerdrView | null {
|
|
215
|
+
return this.#view;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Read Herdr once and push to devices if the view changed. Public so a host can
|
|
220
|
+
* refresh on demand (and so tests drive it without timers).
|
|
221
|
+
*/
|
|
222
|
+
refresh(): Promise<void> {
|
|
223
|
+
return this.#tick();
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** Begin polling. Safe to call once; `stop()` then `start()` restarts. */
|
|
227
|
+
start(): void {
|
|
228
|
+
if (this.#timer) return;
|
|
229
|
+
void this.#tick();
|
|
230
|
+
this.#timer = setInterval(() => void this.#tick(), this.#o.pollMs ?? 5_000);
|
|
231
|
+
this.#timer.unref?.();
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
stop(): void {
|
|
235
|
+
if (this.#timer) clearInterval(this.#timer);
|
|
236
|
+
this.#timer = null;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
onDevices(devices: DeviceView[]): void {
|
|
240
|
+
this.#log(`devices: ${devices.length === 0 ? "none" : devices.map((d) => `${d.name}:${d.scope}`).join(", ")}`);
|
|
241
|
+
// A new device gets the current view at once instead of waiting for the next change.
|
|
242
|
+
if (this.#view) this.#pushAll();
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
onFrame(device: string, payload: unknown): void {
|
|
246
|
+
const link = this.#link;
|
|
247
|
+
if (!link) return;
|
|
248
|
+
const op = typeof payload === "object" && payload !== null ? (payload as { op?: unknown }).op : undefined;
|
|
249
|
+
switch (op) {
|
|
250
|
+
case "view": {
|
|
251
|
+
const view = this.#view;
|
|
252
|
+
if (view) return link.send(device, { op: "view", view });
|
|
253
|
+
// No snapshot yet (fresh adapter, or polling off): read on demand once, then
|
|
254
|
+
// answer — a device request must never hang on the host's poll schedule.
|
|
255
|
+
void this.#tick({ push: false }).then(() => this.#replyView(device));
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
case "ping":
|
|
259
|
+
return link.send(device, { op: "pong", n: (payload as { n?: unknown }).n ?? null, at: (this.#o.now?.() ?? new Date()).toISOString() });
|
|
260
|
+
default:
|
|
261
|
+
// The read-only invariant lives HERE: the cloud cannot inspect payloads, so the
|
|
262
|
+
// host is the last trusted decision point for "this device may not do that".
|
|
263
|
+
return link.send(device, { op: "error", code: "read-only", message: `${HOST_PROTOCOL} has no op ${String(op)}` });
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
onPairAsk(ask: PairAsk): boolean | Promise<boolean> {
|
|
268
|
+
const ok = ask.scope === "act" && this.#o.allowAct === true;
|
|
269
|
+
this.#log(`pair request from ${ask.device.name} for ${ask.scope}: ${ok ? "approved" : "denied"}`);
|
|
270
|
+
return ok;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
async #tick(o: { push?: boolean } = {}): Promise<void> {
|
|
274
|
+
if (this.#reading) return; // one read at a time; a slow CLI must not pile up
|
|
275
|
+
this.#reading = true;
|
|
276
|
+
try {
|
|
277
|
+
const view = await (this.#o.read ?? (() => readHerdrView()))();
|
|
278
|
+
this.#lastError = null;
|
|
279
|
+
const sig = JSON.stringify([view.workspaces, view.agents, view.focused]);
|
|
280
|
+
const changed = sig !== this.#sig;
|
|
281
|
+
this.#view = view;
|
|
282
|
+
this.#sig = sig;
|
|
283
|
+
if (changed && o.push !== false) this.#pushAll();
|
|
284
|
+
} catch (e) {
|
|
285
|
+
const message = e instanceof Error ? e.message : String(e);
|
|
286
|
+
if (message !== this.#lastError) {
|
|
287
|
+
this.#lastError = message;
|
|
288
|
+
this.#log(`herdr read failed: ${message}`);
|
|
289
|
+
// Tell devices the host is reachable but Herdr's state is not readable.
|
|
290
|
+
if (this.#link) for (const d of this.#link.devices) this.#link.send(d.id, { op: "error", code: "host-unavailable", message });
|
|
291
|
+
}
|
|
292
|
+
} finally {
|
|
293
|
+
this.#reading = false;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
#replyView(device: string): void {
|
|
298
|
+
const link = this.#link;
|
|
299
|
+
if (!link) return;
|
|
300
|
+
const view = this.#view;
|
|
301
|
+
if (view) link.send(device, { op: "view", view });
|
|
302
|
+
else
|
|
303
|
+
link.send(device, {
|
|
304
|
+
op: "error",
|
|
305
|
+
code: "host-unavailable",
|
|
306
|
+
message: this.#lastError ?? "the host has not read a Herdr snapshot yet",
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
#pushAll(): void {
|
|
311
|
+
const link = this.#link;
|
|
312
|
+
const view = this.#view;
|
|
313
|
+
if (!link || !view) return;
|
|
314
|
+
for (const d of link.devices) link.send(d.id, { op: "view", view });
|
|
315
|
+
}
|
|
316
|
+
}
|
package/src/host/main.ts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/** The host CLI: run this on the machine where Herdr runs.
|
|
2
|
+
*
|
|
3
|
+
* HERDGATE_URL https://your-cloud (required)
|
|
4
|
+
* HERDGATE_ROOM_TOKEN this host's room token (required once paired)
|
|
5
|
+
* HERDGATE_PROVISION "1" to pair via device flow, then print the room token
|
|
6
|
+
* HERDGATE_HOST_NAME label shown to devices (default: os hostname)
|
|
7
|
+
* HERDGATE_HERDR_POLL_MS snapshot poll interval (default 5000)
|
|
8
|
+
* HERDGATE_ALLOW_ACT "1" to auto-approve `act` devices (default: deny)
|
|
9
|
+
* HERDGATE_HERDR_BIN path to the herdr binary (default: `herdr` on PATH)
|
|
10
|
+
*
|
|
11
|
+
* Read-only: this process only ever runs `herdr api snapshot`.
|
|
12
|
+
*/
|
|
13
|
+
import { hostname } from "node:os";
|
|
14
|
+
import { HerdGateAgent, provision } from "../agent/index.ts";
|
|
15
|
+
import { HerdrAdapter } from "./herdr.ts";
|
|
16
|
+
import { createSshBridge } from "./ssh.ts";
|
|
17
|
+
import type { AgentAdapter, PairAsk } from "../agent/adapter.ts";
|
|
18
|
+
import type { DeviceView } from "../protocol/agent.ts";
|
|
19
|
+
|
|
20
|
+
const env = process.env;
|
|
21
|
+
const url = env.HERDGATE_URL;
|
|
22
|
+
if (!url) {
|
|
23
|
+
console.error("herdgate-host: HERDGATE_URL is required (e.g. https://herd.example.com)");
|
|
24
|
+
process.exit(2);
|
|
25
|
+
}
|
|
26
|
+
const name = env.HERDGATE_HOST_NAME ?? hostname();
|
|
27
|
+
const log = (line: string): void => console.error(`herdgate-host: ${line}`);
|
|
28
|
+
const base = url.replace(/\/$/, "");
|
|
29
|
+
|
|
30
|
+
let roomToken = env.HERDGATE_ROOM_TOKEN ?? "";
|
|
31
|
+
if (!roomToken) {
|
|
32
|
+
if (env.HERDGATE_PROVISION !== "1") {
|
|
33
|
+
console.error("herdgate-host: HERDGATE_ROOM_TOKEN is required (or set HERDGATE_PROVISION=1 to pair)");
|
|
34
|
+
process.exit(2);
|
|
35
|
+
}
|
|
36
|
+
const paired = await provision({
|
|
37
|
+
baseUrl: base,
|
|
38
|
+
name,
|
|
39
|
+
onCode: (u) => console.error(`herdgate-host: approve this host at ${u} (expires in 10 minutes)`),
|
|
40
|
+
});
|
|
41
|
+
roomToken = paired.roomToken;
|
|
42
|
+
console.error(`herdgate-host: paired into room ${paired.roomId}`);
|
|
43
|
+
console.error(`herdgate-host: store this token: HERDGATE_ROOM_TOKEN=${paired.roomToken}`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const adapter = new HerdrAdapter({
|
|
47
|
+
name,
|
|
48
|
+
pollMs: Number(env.HERDGATE_HERDR_POLL_MS ?? 5_000),
|
|
49
|
+
allowAct: env.HERDGATE_ALLOW_ACT === "1",
|
|
50
|
+
log,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// The SSH leg (phones → herdr-core through the gate). The bridge owns every `ssh.*`
|
|
54
|
+
// op; the Herdr adapter never sees them. Scope is enforced inside the bridge.
|
|
55
|
+
const sshBridge = createSshBridge({
|
|
56
|
+
send: (device, payload) => agent.send(device, payload),
|
|
57
|
+
scopeOf: (device) => agent.scopeOf(device),
|
|
58
|
+
log,
|
|
59
|
+
});
|
|
60
|
+
let knownDevices = new Set<string>();
|
|
61
|
+
const adapterWithSsh: AgentAdapter = {
|
|
62
|
+
info: () => adapter.info(),
|
|
63
|
+
onFrame: (device: string, payload: unknown): void => {
|
|
64
|
+
if (sshBridge.handle(device, payload)) return;
|
|
65
|
+
adapter.onFrame(device, payload);
|
|
66
|
+
},
|
|
67
|
+
onDevices: (devices: DeviceView[]) => {
|
|
68
|
+
// A device that left the room must not keep an SSH channel running.
|
|
69
|
+
const fresh = new Set(devices.map((d) => d.id));
|
|
70
|
+
for (const gone of knownDevices) if (!fresh.has(gone)) sshBridge.dropDevice(gone);
|
|
71
|
+
knownDevices = fresh;
|
|
72
|
+
adapter.onDevices(devices);
|
|
73
|
+
},
|
|
74
|
+
onPairAsk: (ask: PairAsk) => adapter.onPairAsk(ask),
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const agent = new HerdGateAgent({
|
|
78
|
+
endpoint: `${base.replace(/^http/, "ws")}/v1/agent/ws`,
|
|
79
|
+
roomToken,
|
|
80
|
+
adapter: adapterWithSsh,
|
|
81
|
+
log,
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
adapter.attach({
|
|
85
|
+
send: (device, payload) => agent.send(device, payload),
|
|
86
|
+
get devices() {
|
|
87
|
+
return agent.devices;
|
|
88
|
+
},
|
|
89
|
+
scopeOf: (device) => agent.scopeOf(device),
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
agent.start();
|
|
93
|
+
adapter.start();
|
|
94
|
+
await agent.firstHello;
|
|
95
|
+
log(`connected; serving Herdr metadata read-only (${name})`);
|
|
96
|
+
|
|
97
|
+
const bye = (): void => {
|
|
98
|
+
log("shutting down");
|
|
99
|
+
adapter.stop();
|
|
100
|
+
sshBridge.closeAll();
|
|
101
|
+
agent.stop();
|
|
102
|
+
process.exit(0);
|
|
103
|
+
};
|
|
104
|
+
process.on("SIGINT", bye);
|
|
105
|
+
process.on("SIGTERM", bye);
|