@bom0792/herdgate 0.9.31 → 0.9.35
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/DEPLOY.md +85 -126
- package/DESIGN.md +9 -4
- package/README.md +23 -2
- package/bin/cloud-config.mjs +9 -2
- package/bin/herdgate.mjs +216 -8
- package/dist-readme.md +3 -1
- package/package.json +35 -9
- package/plugin/server/e2e.d.mts +2 -2
- package/plugin/server/e2e.mjs +51 -32
- package/plugin/server/index.mjs +128 -32
- package/plugin/server/pi-receipts.d.mts +28 -0
- package/plugin/server/pi-receipts.mjs +77 -0
- package/plugin/server/pi-worker.d.mts +47 -0
- package/plugin/server/pi-worker.mjs +127 -0
- package/scripts/build-host-bundle.sh +14 -8
- package/scripts/build-release.sh +47 -0
- package/scripts/deploy-vps.sh +5 -78
- package/src/cloud/gateway.ts +163 -134
- package/src/cloud/main.ts +72 -15
- package/src/cloud/pairing.ts +7 -2
- package/src/cloud/push.ts +54 -0
- package/src/cloud/sshd.ts +76 -38
- package/src/cloud/util.ts +5 -5
- package/src/web/app.js +88 -61
- package/src/web/e2e.js +51 -32
- package/src/web/sw.js +1 -1
- package/templates/cloud/.dockerignore +7 -0
- package/templates/cloud/Caddyfile.example +3 -0
- package/templates/cloud/Dockerfile +10 -0
- package/templates/cloud/README.md +74 -0
- package/templates/cloud/compose.env.example +18 -0
- package/templates/cloud/compose.yml +41 -0
- package/templates/cloud/gitignore.example +4 -0
- package/templates/cloud/herdgate-cloud-edge.service +18 -0
- package/templates/cloud/herdgate-cloud.service +26 -0
- package/tsconfig.cloud.json +11 -0
- package/tsconfig.json +18 -0
- package/src/web/herdgate-host.zip +0 -0
package/bin/herdgate.mjs
CHANGED
|
@@ -6,11 +6,12 @@
|
|
|
6
6
|
// Installs the herdr-app mod and the publisher into the app's home, and
|
|
7
7
|
// prefills the panel's settings so the only thing left is Pair → Publish.
|
|
8
8
|
import { cpSync, mkdirSync, writeFileSync, existsSync, readFileSync, realpathSync, rmdirSync } from "node:fs";
|
|
9
|
+
import readline from "node:readline";
|
|
9
10
|
import { join, dirname, basename } from "node:path";
|
|
10
11
|
import { homedir, hostname } from "node:os";
|
|
11
12
|
import { spawnSync } from "node:child_process";
|
|
12
13
|
import { fileURLToPath } from "node:url";
|
|
13
|
-
import { manageCloud, resolveCloud } from "./cloud-config.mjs";
|
|
14
|
+
import { listClouds, manageCloud, resolveCloud } from "./cloud-config.mjs";
|
|
14
15
|
import { trySyncPiSkills } from "./pi-skills.mjs";
|
|
15
16
|
import { readConfig } from "../plugin/server/common.mjs";
|
|
16
17
|
import { withLatest } from "../windows/npm-update.mjs";
|
|
@@ -25,6 +26,11 @@ const help = `herdgate — every machine, one herd
|
|
|
25
26
|
herdgate pi-skills Sync the bundled skills into your Pi profile.
|
|
26
27
|
herdgate cloud add <name> <https-url>
|
|
27
28
|
herdgate cloud list
|
|
29
|
+
herdgate cloud serve Run a self-hosted Gate from the full npm package (uses HERDGATE_* env).
|
|
30
|
+
herdgate connect [--cloud <name>] [--room <id|name>]... [--scope read|act]
|
|
31
|
+
Pair THIS machine as a controller of machines already in the herd: one SSH
|
|
32
|
+
key per cloud, one ~/.ssh/config Host per machine. Asks for the owner token
|
|
33
|
+
once on a hidden prompt; the token and device secrets are never stored.
|
|
28
34
|
herdgate pair --cloud <name> Pair this machine with owner approval.
|
|
29
35
|
herdgate setup [--cloud <name>] [--mobile-act <device-id>]
|
|
30
36
|
Install/configure the publisher for this OS.
|
|
@@ -119,15 +125,16 @@ code in the PWA (${cloud}) and click Publish. Requires node >= 22 on PATH
|
|
|
119
125
|
for the publisher (node -v).`);
|
|
120
126
|
if (!arg("no-integrate")) integrate(home);
|
|
121
127
|
// The other direction — THIS machine controlling one that is already paired —
|
|
122
|
-
//
|
|
128
|
+
// is one command:
|
|
123
129
|
const viaPath = join(base, "bin", "herdgate-via.mjs");
|
|
124
130
|
if (existsSync(viaPath)) {
|
|
125
131
|
console.log(`
|
|
126
132
|
To connect FROM this machine to another machine in the herd (no admin,
|
|
127
|
-
works on IPv4-only networks — ssh over wss/443),
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
133
|
+
works on IPv4-only networks — ssh over wss/443), run instead:
|
|
134
|
+
herdgate connect --room <name> --scope act
|
|
135
|
+
It pairs the device, keeps one SSH key per cloud and writes the
|
|
136
|
+
~/.ssh/config blocks; the owner token is asked on a hidden prompt and never
|
|
137
|
+
stored. (Add --cloud <name> when more than one cloud is configured.)`);
|
|
131
138
|
}
|
|
132
139
|
}
|
|
133
140
|
|
|
@@ -347,16 +354,217 @@ function managePublisher(cmd, args) {
|
|
|
347
354
|
return runNode(join(HERE, "home-tui.mjs"), [cmd, ...options]);
|
|
348
355
|
}
|
|
349
356
|
|
|
357
|
+
// ---- connect: this machine as a CONTROLLER of machines already in the herd ----
|
|
358
|
+
//
|
|
359
|
+
// `pair`/`setup` publish this machine's own TUI; `connect` is the other
|
|
360
|
+
// direction in one command: pair this machine as a device into other machines'
|
|
361
|
+
// rooms, register one SSH key per cloud, and write the matching ~/.ssh/config
|
|
362
|
+
// Host blocks. The owner token is asked for once on a hidden prompt and never
|
|
363
|
+
// stored — afterwards this machine holds only per-device SSH keys, each
|
|
364
|
+
// independently revocable from the PWA roster.
|
|
365
|
+
|
|
366
|
+
const connectUsage = "usage: herdgate connect [--cloud <name>] [--room <id|name>]... [--scope read|act] [--name <label>]";
|
|
367
|
+
|
|
368
|
+
function repeatArg(name) {
|
|
369
|
+
const out = [];
|
|
370
|
+
for (let i = 2; i < process.argv.length - 1; i++) {
|
|
371
|
+
if (process.argv[i] === `--${name}` && process.argv[i + 1]) out.push(process.argv[i + 1]);
|
|
372
|
+
}
|
|
373
|
+
return out;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/** Hidden prompt on a terminal; plain stdin read when piped. Never a flag/env value. */
|
|
377
|
+
function promptSecret(question) {
|
|
378
|
+
return new Promise((resolve, reject) => {
|
|
379
|
+
let rl;
|
|
380
|
+
try { rl = readline.createInterface({ input: process.stdin, output: process.stdout }); }
|
|
381
|
+
catch (e) { return reject(e); }
|
|
382
|
+
process.stdout.write(question);
|
|
383
|
+
if (process.stdout.isTTY) rl._writeToOutput = () => {};
|
|
384
|
+
rl.question("", (answer) => { try { rl.close(); } catch { /* already gone */ } resolve(answer.trim()); });
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
const escapeRe = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
389
|
+
|
|
390
|
+
function gateWs(origin) {
|
|
391
|
+
const url = origin.replace(/^http/, "ws") + "/v1/device/ws";
|
|
392
|
+
const ws = new WebSocket(url);
|
|
393
|
+
const opened = new Promise((resolve, reject) => {
|
|
394
|
+
const timer = setTimeout(() => reject(new Error(`cannot reach ${url} in time`)), 8_000);
|
|
395
|
+
ws.addEventListener("open", () => { clearTimeout(timer); resolve(); }, { once: true });
|
|
396
|
+
ws.addEventListener("error", () => { clearTimeout(timer); reject(new Error(`cannot reach ${url}`)); }, { once: true });
|
|
397
|
+
});
|
|
398
|
+
return { ws, opened };
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
function gateNext(ws, wanted, timeoutMs = 70_000) {
|
|
402
|
+
const want = Array.isArray(wanted) ? wanted : [wanted];
|
|
403
|
+
return new Promise((resolve, reject) => {
|
|
404
|
+
const timer = setTimeout(() => done(new Error(`the gate did not answer (${want.join("/")}) in time`)), timeoutMs);
|
|
405
|
+
const onMessage = (ev) => {
|
|
406
|
+
let m;
|
|
407
|
+
try { m = JSON.parse(String(ev.data)); } catch { return; }
|
|
408
|
+
if (m.t === "error") return done(new Error(`the gate refused: ${m.message ?? m.code}`));
|
|
409
|
+
if (want.includes(m.t)) done(null, m);
|
|
410
|
+
};
|
|
411
|
+
const onClose = () => done(new Error("the gate closed the connection"));
|
|
412
|
+
function done(err, m) {
|
|
413
|
+
clearTimeout(timer);
|
|
414
|
+
ws.removeEventListener("message", onMessage);
|
|
415
|
+
ws.removeEventListener("close", onClose);
|
|
416
|
+
if (err) reject(err);
|
|
417
|
+
else resolve(m);
|
|
418
|
+
}
|
|
419
|
+
ws.addEventListener("message", onMessage);
|
|
420
|
+
ws.addEventListener("close", onClose);
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/** Replace (or add) this room's managed block; returns the previous alias if any. */
|
|
425
|
+
function upsertSshBlock({ sshConfig, cloudKey, roomId, alias, deviceId, keyFile, viaPath, origin }) {
|
|
426
|
+
const begin = `# herdgate connect:begin ${cloudKey} ${roomId}`;
|
|
427
|
+
const end = `# herdgate connect:end ${cloudKey} ${roomId}`;
|
|
428
|
+
let text = existsSync(sshConfig) ? readFileSync(sshConfig, "utf8") : "";
|
|
429
|
+
let previousAlias = null;
|
|
430
|
+
text = text.replace(new RegExp(`${escapeRe(begin)}\\n([\\s\\S]*?)${escapeRe(end)}\\n?`, "g"), (_all, body) => {
|
|
431
|
+
const hit = body.match(/^Host\s+(\\S+)/m);
|
|
432
|
+
if (hit) previousAlias = hit[1];
|
|
433
|
+
return "";
|
|
434
|
+
});
|
|
435
|
+
const taken = new Set((text.match(/^Host\s+(\\S+)/gm) ?? []).map((l) => l.replace(/^Host\s+/, "").split(/\\s+/)[0]));
|
|
436
|
+
let chosen = previousAlias && !taken.has(previousAlias) ? previousAlias : alias;
|
|
437
|
+
let n = 2;
|
|
438
|
+
while (taken.has(chosen)) chosen = `${alias}-${n++}`;
|
|
439
|
+
const block = [
|
|
440
|
+
`Host ${chosen}`,
|
|
441
|
+
` HostName ${chosen}`,
|
|
442
|
+
` User ${deviceId}`,
|
|
443
|
+
` IdentityFile ${keyFile}`,
|
|
444
|
+
` HostKeyAlias herdgate-${cloudKey}`,
|
|
445
|
+
` ProxyCommand node ${viaPath} --tunnel --gate ${origin}`,
|
|
446
|
+
].join("\n");
|
|
447
|
+
const trimmed = text.replace(/\n*$/, "");
|
|
448
|
+
mkdirSync(dirname(sshConfig), { recursive: true, mode: 0o700 });
|
|
449
|
+
writeFileSync(sshConfig, `${trimmed ? trimmed + "\n\n" : ""}${begin}\n${block}\n${end}\n`, { mode: 0o600 });
|
|
450
|
+
return chosen;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
async function connectController() {
|
|
454
|
+
const clouds = listClouds();
|
|
455
|
+
const cloudInput = arg("cloud") ?? (clouds.length === 1 ? clouds[0][0] : null);
|
|
456
|
+
if (!cloudInput) throw new Error(`${connectUsage}\n no --cloud given and ${clouds.length} clouds configured; see herdgate cloud list`);
|
|
457
|
+
const origin = resolveCloud(cloudInput);
|
|
458
|
+
const cloudKey = (/^[a-z0-9][a-z0-9_-]{0,31}$/.test(cloudInput) ? cloudInput : new URL(origin).hostname)
|
|
459
|
+
.toLowerCase().replace(/[^a-z0-9-]+/g, "-");
|
|
460
|
+
const scope = arg("scope") ?? "read";
|
|
461
|
+
if (scope !== "read" && scope !== "act") throw new Error("--scope must be read or act");
|
|
462
|
+
const deviceName = (arg("name") ?? hostname()).slice(0, 64);
|
|
463
|
+
|
|
464
|
+
const token = await promptSecret(`Owner token for ${origin} (hidden, not stored): `);
|
|
465
|
+
if (!token) throw new Error("an owner token is required to pair this machine");
|
|
466
|
+
|
|
467
|
+
const { ws, opened } = gateWs(origin);
|
|
468
|
+
await opened;
|
|
469
|
+
try {
|
|
470
|
+
ws.send(JSON.stringify({ v: 1, t: "hello", accountToken: token }));
|
|
471
|
+
const hello = await gateNext(ws, "hello", 10_000);
|
|
472
|
+
const agents = Array.isArray(hello.agents) ? hello.agents : [];
|
|
473
|
+
if (!agents.length) throw new Error("this account has no machines to manage yet; pair a host first (herdgate pair)");
|
|
474
|
+
|
|
475
|
+
const wanted = repeatArg("room");
|
|
476
|
+
const rooms = [];
|
|
477
|
+
if (wanted.length) {
|
|
478
|
+
for (const w of wanted) {
|
|
479
|
+
const hit = agents.find((a) => a.roomId === w || String(a.name ?? "").toLowerCase() === w.toLowerCase());
|
|
480
|
+
if (!hit) throw new Error(`no machine matches --room '${w}'; available: ${agents.map((a) => `${a.name} (${a.roomId})`).join(", ")}`);
|
|
481
|
+
if (!rooms.includes(hit)) rooms.push(hit);
|
|
482
|
+
}
|
|
483
|
+
} else if (process.stdin.isTTY) {
|
|
484
|
+
console.log("Machines in this herd:");
|
|
485
|
+
agents.forEach((a, i) => console.log(` ${i + 1}. ${a.name} (${a.roomId})${a.online ? "" : " — host offline"}`));
|
|
486
|
+
const answer = await new Promise((resolve) => {
|
|
487
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
488
|
+
rl.question("Connect to (numbers or names, comma-separated): ", (a) => { rl.close(); resolve(a); });
|
|
489
|
+
});
|
|
490
|
+
for (const part of answer.split(/[\s,]+/).filter(Boolean)) {
|
|
491
|
+
const n = Number(part);
|
|
492
|
+
const hit = (Number.isInteger(n) && n >= 1 && n <= agents.length) ? agents[n - 1]
|
|
493
|
+
: agents.find((a) => a.roomId === part || String(a.name ?? "").toLowerCase() === part.toLowerCase());
|
|
494
|
+
if (!hit) throw new Error(`nothing selected matches '${part}'`);
|
|
495
|
+
if (!rooms.includes(hit)) rooms.push(hit);
|
|
496
|
+
}
|
|
497
|
+
if (!rooms.length) throw new Error("no machine selected");
|
|
498
|
+
} else {
|
|
499
|
+
throw new Error(`${connectUsage}\n stdin is not a terminal; pass --room per machine`);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
// One key per cloud on this machine. The same public key may serve several
|
|
503
|
+
// devices (like the phone app does); each device stays independently revocable.
|
|
504
|
+
const sshDir = join(homedir(), ".ssh");
|
|
505
|
+
mkdirSync(sshDir, { recursive: true, mode: 0o700 });
|
|
506
|
+
const keyFile = join(sshDir, `herdgate_${cloudKey}`);
|
|
507
|
+
if (!existsSync(keyFile)) {
|
|
508
|
+
const r = spawnSync("ssh-keygen", ["-t", "ed25519", "-N", "", "-q", "-f", keyFile], { shell: false });
|
|
509
|
+
if (r.status !== 0) throw new Error(`could not create ${keyFile} with ssh-keygen; create an ed25519 key there and re-run`);
|
|
510
|
+
}
|
|
511
|
+
const pubKey = readFileSync(`${keyFile}.pub`, "utf8").trim();
|
|
512
|
+
|
|
513
|
+
// The --via transport ships with this package; keep a copy beside the key.
|
|
514
|
+
const viaPath = join(sshDir, `herdgate-via-${cloudKey}.mjs`);
|
|
515
|
+
cpSync(join(ROOT, "templates", "herdgate-via.mjs"), viaPath);
|
|
516
|
+
if (/\s/.test(viaPath)) {
|
|
517
|
+
console.error(`herdgate: warning: ${viaPath} contains spaces; OpenSSH's ProxyCommand cannot quote it — move the profile or configure Host manually`);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
const sshConfig = join(sshDir, "config");
|
|
521
|
+
const results = [];
|
|
522
|
+
for (const room of rooms) {
|
|
523
|
+
ws.send(JSON.stringify({ v: 1, t: "pair.begin", roomId: room.roomId, scope, name: `${deviceName} (cli)` }));
|
|
524
|
+
const answer = await gateNext(ws, ["pair.granted", "pair.denied"], 70_000);
|
|
525
|
+
if (answer.t === "pair.denied") throw new Error(`pairing with ${room.name} was denied: ${answer.reason ?? "denied"}`);
|
|
526
|
+
const deviceId = answer.device?.id;
|
|
527
|
+
const deviceSecret = answer.deviceSecret;
|
|
528
|
+
if (!deviceId || !deviceSecret) throw new Error("the gate granted the pair without device credentials");
|
|
529
|
+
const res = await fetch(`${origin}/v1/device/sshkey`, {
|
|
530
|
+
method: "POST",
|
|
531
|
+
headers: { "content-type": "application/json" },
|
|
532
|
+
body: JSON.stringify({ device: deviceId, deviceSecret, pubkey: pubKey }),
|
|
533
|
+
});
|
|
534
|
+
const out = await res.json().catch(() => ({}));
|
|
535
|
+
if (!res.ok || out.ok !== true) throw new Error(`registering the SSH key for ${room.name} failed (HTTP ${res.status})`);
|
|
536
|
+
const base = String(room.name || room.roomId).toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "") || "herd";
|
|
537
|
+
const alias = upsertSshBlock({ sshConfig, cloudKey, roomId: room.roomId, alias: base, deviceId, keyFile, viaPath, origin });
|
|
538
|
+
results.push({ room, alias, deviceId, fp: out.fp });
|
|
539
|
+
}
|
|
540
|
+
console.log(`paired this machine with ${results.length} machine(s) on ${origin} (scope ${scope}):`);
|
|
541
|
+
for (const r of results) {
|
|
542
|
+
console.log(` ${r.room.name} → ssh host '${r.alias}' (device ${r.deviceId}, key ${r.fp})`);
|
|
543
|
+
console.log(` herdr machine add ${r.alias} --label ${JSON.stringify(String(r.room.name))} --remote-session default`);
|
|
544
|
+
}
|
|
545
|
+
console.log("The owner token and device secrets were not stored. The first ssh connection pins the gate's host key.");
|
|
546
|
+
console.log("If this machine was connected before, revoke its previous device in the PWA roster.");
|
|
547
|
+
} finally {
|
|
548
|
+
try { ws.close(); } catch { /* already gone */ }
|
|
549
|
+
}
|
|
550
|
+
process.exit(0);
|
|
551
|
+
}
|
|
552
|
+
|
|
350
553
|
const [cmd, ...args] = process.argv.slice(2);
|
|
351
554
|
try {
|
|
352
555
|
if (cmd === "setup-host") {
|
|
353
556
|
setupHost();
|
|
354
557
|
trySyncPiSkills();
|
|
355
558
|
} else if (cmd === "pi-skills" && !args.length) trySyncPiSkills();
|
|
356
|
-
else if (cmd === "cloud"
|
|
559
|
+
else if (cmd === "cloud" && args.length === 1 && args[0] === "serve") {
|
|
560
|
+
const entry = join(ROOT, "src", "cloud", "main.ts");
|
|
561
|
+
if (!existsSync(entry)) throw new Error("cloud serve requires the full npm package; use the Cloud archive for Docker or systemd");
|
|
562
|
+
runNode(entry, []);
|
|
563
|
+
} else if (cmd === "cloud") manageCloud(args);
|
|
564
|
+
else if (cmd === "connect") await connectController();
|
|
357
565
|
else if (cmd === "pair") pairHost(args);
|
|
358
566
|
else if (process.platform === "win32" && (
|
|
359
|
-
["install", "setup", "migrate", "start", "update", "install-tui"].includes(cmd) ||
|
|
567
|
+
["install", "setup", "migrate", "start", "update", "install-tui", "connect"].includes(cmd) ||
|
|
360
568
|
(cmd === "tui" && ["setup", "migrate", "start", "update"].includes(args[0]))
|
|
361
569
|
) && (Number(process.versions.node.split(".")[0]) < 22 || typeof globalThis.WebSocket !== "function")) {
|
|
362
570
|
const node = windowsNode();
|
package/dist-readme.md
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Bộ này dùng cho máy đã cài **herdr** (app desktop hoặc TUI): cài mod + publisher,
|
|
4
4
|
máy sẽ tự kết nối **out** tới gate (không mở port, không VPN) và xuất hiện trên
|
|
5
|
-
phone/PWA của bạn.
|
|
5
|
+
phone/PWA của bạn. ZIP này không chạy Gate; để tự host Gate mới, dùng
|
|
6
|
+
`herdgate-cloud-<version>.tar.gz` trong cùng bộ phát hành.
|
|
6
7
|
|
|
7
8
|
## Yêu cầu
|
|
8
9
|
|
|
@@ -86,6 +87,7 @@ Cài ZIP không tự migrate hay nâng cấp máy nhà, Gate hoặc VPS.
|
|
|
86
87
|
plugin/server/*.mjs → publisher (mod tự spawn bằng node)
|
|
87
88
|
plugin/herdr-plugin.toml → nếu bạn dùng herdr TUI thay vì app
|
|
88
89
|
windows/ → bộ cài và CLI publisher Windows
|
|
90
|
+
templates/herdgate-via.mjs → cầu SSH qua WSS/443
|
|
89
91
|
|
|
90
92
|
## Gỡ
|
|
91
93
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bom0792/herdgate",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.35",
|
|
4
4
|
"description": "Cloud relay hub for herdr-core: every machine dials out, phones and browsers follow (PWA + SSH leg, E2E payloads).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -12,6 +12,8 @@
|
|
|
12
12
|
"edge": "node src/dev/main.ts",
|
|
13
13
|
"test": "node --test test/*.test.ts",
|
|
14
14
|
"check": "tsc -p tsconfig.json",
|
|
15
|
+
"build:cloud": "tsc -p tsconfig.cloud.json",
|
|
16
|
+
"build:release": "bash scripts/build-release.sh",
|
|
15
17
|
"ssh-bridge": "node src/cloud/ssh-bridge.ts",
|
|
16
18
|
"ssh-tcp-edge": "node src/cloud/ssh-tcp-edge.ts",
|
|
17
19
|
"mobile-tui": "node src/host/mobile-tui.ts",
|
|
@@ -31,18 +33,42 @@
|
|
|
31
33
|
"herdgate": "bin/herdgate.mjs"
|
|
32
34
|
},
|
|
33
35
|
"files": [
|
|
34
|
-
"bin",
|
|
36
|
+
"bin/*.mjs",
|
|
35
37
|
".agents/skills/herdr*/SKILL.md",
|
|
36
38
|
".agents/skills/herdr/herdr_pi.py",
|
|
37
39
|
".agents/skills/herdr/profiles.example.toml",
|
|
38
|
-
"src",
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
40
|
+
"src/**/*.ts",
|
|
41
|
+
"src/host/*.mjs",
|
|
42
|
+
"src/web/index.html",
|
|
43
|
+
"src/web/*.css",
|
|
44
|
+
"src/web/*.js",
|
|
45
|
+
"src/web/app.webmanifest",
|
|
46
|
+
"src/web/icons/icon.svg",
|
|
47
|
+
"src/web/icons/icon-192.png",
|
|
48
|
+
"src/web/icons/icon-512.png",
|
|
49
|
+
"app-mod/herdgate/main.js",
|
|
50
|
+
"app-mod/herdgate/mod.json",
|
|
51
|
+
"app-mod/herdgate/herdgate.svg",
|
|
52
|
+
"plugin/herdr-plugin.toml",
|
|
53
|
+
"plugin/server/*.mjs",
|
|
54
|
+
"plugin/server/*.d.mts",
|
|
55
|
+
"scripts/*.mjs",
|
|
56
|
+
"scripts/*.sh",
|
|
42
57
|
"DEPLOY.md",
|
|
43
58
|
"DESIGN.md",
|
|
44
59
|
"dist-readme.md",
|
|
45
|
-
"templates",
|
|
46
|
-
"
|
|
60
|
+
"templates/herdgate-via.mjs",
|
|
61
|
+
"templates/herdgate-tcp-edge.service",
|
|
62
|
+
"templates/cloud/Caddyfile.example",
|
|
63
|
+
"templates/cloud/Dockerfile",
|
|
64
|
+
"templates/cloud/.dockerignore",
|
|
65
|
+
"templates/cloud/gitignore.example",
|
|
66
|
+
"templates/cloud/README.md",
|
|
67
|
+
"templates/cloud/compose.env.example",
|
|
68
|
+
"templates/cloud/compose.yml",
|
|
69
|
+
"templates/cloud/*.service",
|
|
70
|
+
"tsconfig.json",
|
|
71
|
+
"tsconfig.cloud.json",
|
|
72
|
+
"windows/*.mjs"
|
|
47
73
|
]
|
|
48
|
-
}
|
|
74
|
+
}
|
package/plugin/server/e2e.d.mts
CHANGED
|
@@ -12,7 +12,7 @@ export declare function b64d(s: string): Uint8Array;
|
|
|
12
12
|
export declare function utf8e(s: string): Uint8Array;
|
|
13
13
|
export declare function utf8d(u8: Uint8Array): string;
|
|
14
14
|
export declare function concat(...parts: Uint8Array[]): Uint8Array;
|
|
15
|
-
export declare function fingerprint(pub: Uint8Array): string
|
|
15
|
+
export declare function fingerprint(pub: Uint8Array): Promise<string>;
|
|
16
16
|
export declare function generateKeyPair(): Promise<{ jwk: JsonWebKey; pub: Uint8Array }>;
|
|
17
17
|
|
|
18
18
|
export interface NoiseWire {
|
|
@@ -25,7 +25,7 @@ export interface NoiseWire {
|
|
|
25
25
|
export declare class DeviceChannel {
|
|
26
26
|
constructor(self: { jwk: JsonWebKey; pub: Uint8Array });
|
|
27
27
|
begin(): Promise<{ e: string }>;
|
|
28
|
-
acceptReply(msg: { e: string; c: string }): Promise<{ c: string }>;
|
|
28
|
+
acceptReply(msg: { e: string; c: string }, expectedHost?: string | null): Promise<{ c: string }>;
|
|
29
29
|
ready(): boolean;
|
|
30
30
|
hostFingerprint: string | null;
|
|
31
31
|
send(obj: unknown): Promise<{ op: string; n: number; c: string }>;
|
package/plugin/server/e2e.mjs
CHANGED
|
@@ -47,11 +47,15 @@ export function concat(...parts) {
|
|
|
47
47
|
return out;
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
/**
|
|
51
|
-
|
|
50
|
+
/** Legacy pin matching is used only to upgrade existing browser pins. */
|
|
51
|
+
function legacyFingerprint(pub) {
|
|
52
52
|
return b64e(pub.slice(0, 8)).replace(/[^A-Za-z0-9]/g, "").toLowerCase().slice(0, 12);
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
+
export async function fingerprint(pub) {
|
|
56
|
+
return "SHA256:" + b64e(await sha256(pub)).replace(/=+$/, "");
|
|
57
|
+
}
|
|
58
|
+
|
|
55
59
|
// ---- WebCrypto plumbing ----------------------------------------------------
|
|
56
60
|
|
|
57
61
|
const subtle = globalThis.crypto.subtle;
|
|
@@ -90,31 +94,38 @@ export async function generateKeyPair() {
|
|
|
90
94
|
class CipherState {
|
|
91
95
|
k = null;
|
|
92
96
|
n = 0;
|
|
97
|
+
#tail = Promise.resolve();
|
|
93
98
|
setKey(k) {
|
|
94
99
|
this.k = k;
|
|
95
100
|
}
|
|
96
|
-
nonce() {
|
|
97
|
-
// 96-bit: 4 zero bytes ‖ BE64(n)
|
|
101
|
+
nonce(n) {
|
|
102
|
+
// 96-bit: 4 zero bytes ‖ BE64(n); sessions are bounded below 2^32.
|
|
98
103
|
const iv = new Uint8Array(12);
|
|
99
|
-
new DataView(iv.buffer).setUint32(8,
|
|
104
|
+
new DataView(iv.buffer).setUint32(8, n);
|
|
100
105
|
return iv;
|
|
101
106
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
await subtle.encrypt({ name: "AES-GCM", iv: this.nonce(), additionalData: ad, tagLength: 128 }, key, plaintext),
|
|
107
|
-
);
|
|
108
|
-
this.n += 1;
|
|
109
|
-
return ct;
|
|
107
|
+
#enqueue(operation) {
|
|
108
|
+
const result = this.#tail.then(operation);
|
|
109
|
+
this.#tail = result.catch(() => {});
|
|
110
|
+
return result;
|
|
110
111
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
112
|
+
encrypt(ad, plaintext) {
|
|
113
|
+
return this.#enqueue(async () => {
|
|
114
|
+
if (this.n >= 0xffffffff) throw new Error("e2e: nonce exhausted");
|
|
115
|
+
const n = this.n++;
|
|
116
|
+
const key = await subtle.importKey("raw", this.k, "AES-GCM", false, ["encrypt"]);
|
|
117
|
+
const ct = new Uint8Array(await subtle.encrypt({ name: "AES-GCM", iv: this.nonce(n), additionalData: ad, tagLength: 128 }, key, plaintext));
|
|
118
|
+
return { ct, n };
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
decrypt(ad, ciphertext, n) {
|
|
122
|
+
return this.#enqueue(async () => {
|
|
123
|
+
if (this.n >= 0xffffffff || (n !== undefined && n !== this.n)) throw new Error("e2e: invalid nonce counter");
|
|
124
|
+
const key = await subtle.importKey("raw", this.k, "AES-GCM", false, ["decrypt"]);
|
|
125
|
+
const pt = new Uint8Array(await subtle.decrypt({ name: "AES-GCM", iv: this.nonce(this.n), additionalData: ad, tagLength: 128 }, key, ciphertext));
|
|
126
|
+
this.n += 1;
|
|
127
|
+
return pt;
|
|
128
|
+
});
|
|
118
129
|
}
|
|
119
130
|
}
|
|
120
131
|
|
|
@@ -151,7 +162,7 @@ class Handshake {
|
|
|
151
162
|
}
|
|
152
163
|
/** The `s` token: transport under the current key, else in the clear (ENC0). */
|
|
153
164
|
async writeTokenStatic(pubRaw) {
|
|
154
|
-
const out = this.cs.k ? await this.cs.encrypt(this.h, pubRaw) : pubRaw;
|
|
165
|
+
const out = this.cs.k ? (await this.cs.encrypt(this.h, pubRaw)).ct : pubRaw;
|
|
155
166
|
await this.mixHash(out);
|
|
156
167
|
return out;
|
|
157
168
|
}
|
|
@@ -189,14 +200,18 @@ export class DeviceChannel {
|
|
|
189
200
|
return { e: b64e(this.#e.pub) };
|
|
190
201
|
}
|
|
191
202
|
/** Feed the host's `{op:"e2e.reply", e, c}`; returns the msg3 payload. */
|
|
192
|
-
async acceptReply(msg) {
|
|
203
|
+
async acceptReply(msg, expectedHost = null) {
|
|
193
204
|
const eHost = b64d(msg.e);
|
|
194
205
|
if (eHost.length !== PUB_LEN) throw new Error("e2e: bad host ephemeral");
|
|
195
206
|
await this.#hs.mixHash(eHost);
|
|
196
207
|
await this.#hs.mixKey(await dh(this.#e.jwk, eHost)); // ee
|
|
197
208
|
const sHost = await this.#hs.readTokenStatic(b64d(msg.c)); // s
|
|
198
209
|
if (sHost.length !== PUB_LEN) throw new Error("e2e: bad host static");
|
|
199
|
-
|
|
210
|
+
const fp = await fingerprint(sHost);
|
|
211
|
+
if (expectedHost && expectedHost !== fp && (expectedHost.startsWith("SHA256:") || expectedHost !== legacyFingerprint(sHost))) {
|
|
212
|
+
throw Object.assign(new Error("e2e: host key changed"), { code: "host-key-changed" });
|
|
213
|
+
}
|
|
214
|
+
this.hostFingerprint = fp;
|
|
200
215
|
await this.#hs.mixKey(await dh(this.#e.jwk, sHost)); // es (e_dev × s_host)
|
|
201
216
|
const c = await this.#hs.writeTokenStatic(this.#self.pub); // s_dev
|
|
202
217
|
await this.#hs.mixKey(await dh(this.#self.jwk, eHost)); // se (e_host × s_dev)
|
|
@@ -210,12 +225,11 @@ export class DeviceChannel {
|
|
|
210
225
|
return Boolean(this.sendCs && this.recvCs);
|
|
211
226
|
}
|
|
212
227
|
async send(obj) {
|
|
213
|
-
const ct = await this.sendCs.encrypt(this.#hs.h, utf8e(JSON.stringify(obj)));
|
|
214
|
-
return { op: "e2e", n
|
|
228
|
+
const { ct, n } = await this.sendCs.encrypt(this.#hs.h, utf8e(JSON.stringify(obj)));
|
|
229
|
+
return { op: "e2e", n, c: b64e(ct) };
|
|
215
230
|
}
|
|
216
231
|
async recv(msg) {
|
|
217
|
-
|
|
218
|
-
return JSON.parse(utf8d(pt));
|
|
232
|
+
return receive(this.recvCs, this.#hs.h, msg);
|
|
219
233
|
}
|
|
220
234
|
}
|
|
221
235
|
|
|
@@ -247,7 +261,7 @@ export class HostChannel {
|
|
|
247
261
|
async acceptFinish(msg) {
|
|
248
262
|
const sDev = await this.#hs.readTokenStatic(b64d(msg.c)); // s_dev
|
|
249
263
|
if (sDev.length !== PUB_LEN) throw new Error("e2e: bad device static");
|
|
250
|
-
this.deviceFingerprint = fingerprint(sDev);
|
|
264
|
+
this.deviceFingerprint = await fingerprint(sDev);
|
|
251
265
|
await this.#hs.mixKey(await dh(this.#e.jwk, sDev)); // se (e_host × s_dev)
|
|
252
266
|
const [c1, c2] = await this.#hs.split();
|
|
253
267
|
this.recvCs = c1; // device → host (c1 encrypts initiator→responder)
|
|
@@ -258,11 +272,16 @@ export class HostChannel {
|
|
|
258
272
|
return Boolean(this.sendCs && this.recvCs);
|
|
259
273
|
}
|
|
260
274
|
async send(obj) {
|
|
261
|
-
const ct = await this.sendCs.encrypt(this.#hs.h, utf8e(JSON.stringify(obj)));
|
|
262
|
-
return { op: "e2e", n
|
|
275
|
+
const { ct, n } = await this.sendCs.encrypt(this.#hs.h, utf8e(JSON.stringify(obj)));
|
|
276
|
+
return { op: "e2e", n, c: b64e(ct) };
|
|
263
277
|
}
|
|
264
278
|
async recv(msg) {
|
|
265
|
-
|
|
266
|
-
return JSON.parse(utf8d(pt));
|
|
279
|
+
return receive(this.recvCs, this.#hs.h, msg);
|
|
267
280
|
}
|
|
268
281
|
}
|
|
282
|
+
|
|
283
|
+
async function receive(cipher, handshakeHash, msg) {
|
|
284
|
+
if (!Number.isInteger(msg.n) || msg.n < 0 || msg.n >= 0xffffffff) throw new Error("e2e: invalid nonce counter");
|
|
285
|
+
const plaintext = await cipher.decrypt(handshakeHash, b64d(msg.c), msg.n);
|
|
286
|
+
return JSON.parse(utf8d(plaintext));
|
|
287
|
+
}
|