@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,88 @@
|
|
|
1
|
+
/** Cloud CLI entry. Configure by environment (DEPLOY.md has the full story):
|
|
2
|
+
*
|
|
3
|
+
* HERDGATE_PORT listen port (default 8787)
|
|
4
|
+
* HERDGATE_BIND bind address (default 127.0.0.1)
|
|
5
|
+
* HERDGATE_STATE state JSON path (default: none, in-memory)
|
|
6
|
+
* HERDGATE_ACCOUNT_TOKEN required, account secret (mint: `openssl rand -base64 32`)
|
|
7
|
+
* HERDGATE_ACCOUNT_NAME display name (default "owner")
|
|
8
|
+
* HERDGATE_PUBLIC_HOST enforce this Host header (the cloudflared hostname)
|
|
9
|
+
* HERDGATE_PUBLIC_URL shown in pairing URLs (e.g. https://herd.example.com)
|
|
10
|
+
* HERDGATE_WEB_ROOT the PWA directory (default: src/web beside this file)
|
|
11
|
+
* HERDGATE_NO_WEB "1" disables the PWA (API only)
|
|
12
|
+
* HERDGATE_SSH_PORT the SSH leg for phones (default 2222; 0 disables)
|
|
13
|
+
* HERDGATE_SSH_BIND its bind address (default 127.0.0.1; 0.0.0.0 for LAN)
|
|
14
|
+
* HERDGATE_SSH_KEY host key file (default: beside the state file)
|
|
15
|
+
* HERDGATE_NO_PUSH "1" disables the push-wake endpoints and VAPID keys
|
|
16
|
+
* HERDGATE_SSH_HOST the hostname phones dial for SSH (a DNS-only name)
|
|
17
|
+
* HERDGATE_HC_BIN herdr-core binary on hosts (hosts resolve hc themselves)
|
|
18
|
+
*/
|
|
19
|
+
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
20
|
+
import { dirname, join } from "node:path";
|
|
21
|
+
import { fileURLToPath } from "node:url";
|
|
22
|
+
import { startCloud } from "./gateway.ts";
|
|
23
|
+
import { generateVapidKeys, loadVapidKeys, type VapidKeys } from "./vapid.ts";
|
|
24
|
+
|
|
25
|
+
const env = process.env;
|
|
26
|
+
const token = env.HERDGATE_ACCOUNT_TOKEN;
|
|
27
|
+
if (!token) {
|
|
28
|
+
console.error("herdgate: HERDGATE_ACCOUNT_TOKEN is required (openssl rand -base64 32)");
|
|
29
|
+
process.exit(2);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Default web root: the `src/web` directory beside this file, so `npm start` serves the PWA. */
|
|
33
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
34
|
+
const defaultWebRoot = join(here, "..", "web");
|
|
35
|
+
const webRoot = env.HERDGATE_NO_WEB === "1" ? undefined : (env.HERDGATE_WEB_ROOT ?? defaultWebRoot);
|
|
36
|
+
|
|
37
|
+
const sshPort = Number(env.HERDGATE_SSH_PORT ?? 2222);
|
|
38
|
+
const sshKeyPath =
|
|
39
|
+
env.HERDGATE_SSH_KEY ?? (env.HERDGATE_STATE ? join(dirname(env.HERDGATE_STATE), "ssh_host_ed25519.json") : undefined);
|
|
40
|
+
|
|
41
|
+
// Push wake (VAPID): keys live beside the state file so the public key browsers
|
|
42
|
+
// subscribed to never changes behind their backs. HERDGATE_NO_PUSH=1 disables.
|
|
43
|
+
let push: { keys: VapidKeys; sub: string } | null = null;
|
|
44
|
+
if (env.HERDGATE_NO_PUSH !== "1") {
|
|
45
|
+
const vapidPath = env.HERDGATE_STATE ? join(dirname(env.HERDGATE_STATE), "vapid.json") : undefined;
|
|
46
|
+
let keys: VapidKeys | undefined;
|
|
47
|
+
if (vapidPath) {
|
|
48
|
+
try {
|
|
49
|
+
keys = loadVapidKeys(JSON.parse(readFileSync(vapidPath, "utf8")).jwk);
|
|
50
|
+
} catch {
|
|
51
|
+
keys = generateVapidKeys();
|
|
52
|
+
mkdirSync(dirname(vapidPath), { recursive: true });
|
|
53
|
+
writeFileSync(vapidPath, JSON.stringify({ jwk: keys.privateJwk }), { mode: 0o600 });
|
|
54
|
+
}
|
|
55
|
+
} else {
|
|
56
|
+
keys = generateVapidKeys();
|
|
57
|
+
}
|
|
58
|
+
push = { keys, sub: env.HERDGATE_PUSH_SUB ?? "mailto:owner@herdgate.invalid" };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const cloud = await startCloud({
|
|
62
|
+
bind: env.HERDGATE_BIND ?? "127.0.0.1",
|
|
63
|
+
port: Number(env.HERDGATE_PORT ?? 8787),
|
|
64
|
+
statePath: env.HERDGATE_STATE,
|
|
65
|
+
expectedHost: env.HERDGATE_PUBLIC_HOST,
|
|
66
|
+
publicUrl: env.HERDGATE_PUBLIC_URL ?? null,
|
|
67
|
+
webRoot,
|
|
68
|
+
ssh: {
|
|
69
|
+
port: sshPort,
|
|
70
|
+
bind: env.HERDGATE_SSH_BIND ?? "127.0.0.1",
|
|
71
|
+
keyPath: sshKeyPath,
|
|
72
|
+
},
|
|
73
|
+
push,
|
|
74
|
+
sshHost: env.HERDGATE_SSH_HOST,
|
|
75
|
+
accounts: [{ id: "a_owner", name: env.HERDGATE_ACCOUNT_NAME ?? "owner", token }],
|
|
76
|
+
log: (l) => console.error(`herdgate: ${l}`),
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
console.error(`herdgate: listening on http://${env.HERDGATE_BIND ?? "127.0.0.1"}:${cloud.port}`);
|
|
80
|
+
console.error(`herdgate: app on ${env.HERDGATE_PUBLIC_URL ?? `http://${env.HERDGATE_BIND ?? "127.0.0.1"}:${cloud.port}`}/${webRoot ? "" : " (disabled)"}`);
|
|
81
|
+
console.error(`herdgate: pair a host: curl -XPOST ${env.HERDGATE_PUBLIC_URL ?? `http://127.0.0.1:${cloud.port}`}/v1/agent/code -d '{"name":"my-mac"}'`);
|
|
82
|
+
|
|
83
|
+
const bye = (): void => {
|
|
84
|
+
console.error("herdgate: shutting down");
|
|
85
|
+
void cloud.close().then(() => process.exit(0));
|
|
86
|
+
};
|
|
87
|
+
process.on("SIGINT", bye);
|
|
88
|
+
process.on("SIGTERM", bye);
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host-agent pairing (GitHub-style device flow): the host mints a code, a human
|
|
3
|
+
* approves it from a logged-in device, the host polls for its room token.
|
|
4
|
+
*
|
|
5
|
+
* host cloud device (logged in)
|
|
6
|
+
* POST /v1/agent/code → {code, url}
|
|
7
|
+
* print url ──────────────────────────────────────────▶ user opens url
|
|
8
|
+
* POST /v1/agent/approve ◀──── {code} (account token)
|
|
9
|
+
* GET /v1/agent/code/:code/status (poll)
|
|
10
|
+
* ◀── {status:"approved", roomToken} (returned once, then forgotten)
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { AGENT_CODE_TTL_MS } from "../protocol/envelope.ts";
|
|
14
|
+
import { randomCode } from "./util.ts";
|
|
15
|
+
|
|
16
|
+
export interface MintedCode {
|
|
17
|
+
code: string;
|
|
18
|
+
url: string;
|
|
19
|
+
expiresIn: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface Entry {
|
|
23
|
+
code: string;
|
|
24
|
+
hostName: string;
|
|
25
|
+
expires: number;
|
|
26
|
+
approved: boolean;
|
|
27
|
+
/** Plaintext room token, cleared after the first successful poll. */
|
|
28
|
+
roomToken: string | null;
|
|
29
|
+
roomId: string | null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export class AgentCodes {
|
|
33
|
+
readonly #entries: Entry[] = [];
|
|
34
|
+
readonly #now: () => number;
|
|
35
|
+
readonly #publicUrl: string | null;
|
|
36
|
+
|
|
37
|
+
constructor(now: () => number = Date.now, publicUrl: string | null = null) {
|
|
38
|
+
this.#now = now;
|
|
39
|
+
this.#publicUrl = publicUrl;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
mint(hostName: string): MintedCode {
|
|
43
|
+
this.#prune();
|
|
44
|
+
const e: Entry = { code: randomCode(8), hostName, expires: this.#now() + AGENT_CODE_TTL_MS, approved: false, roomToken: null, roomId: null };
|
|
45
|
+
this.#entries.push(e);
|
|
46
|
+
return { code: e.code, url: this.#publicUrl ? `${this.#publicUrl.replace(/\/$/, "")}/pair?code=${e.code}` : e.code, expiresIn: AGENT_CODE_TTL_MS / 1000 };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Binds an approved code to a fresh room; the token is handed out via `status`. */
|
|
50
|
+
fulfill(code: string, roomToken: string, roomId: string): boolean {
|
|
51
|
+
this.#prune();
|
|
52
|
+
const e = this.#find(code);
|
|
53
|
+
if (!e) return false;
|
|
54
|
+
e.approved = true;
|
|
55
|
+
e.roomToken = roomToken;
|
|
56
|
+
e.roomId = roomId;
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
status(code: string): { status: "pending" | "approved" | "unknown"; roomToken?: string; roomId?: string; hostName?: string } {
|
|
61
|
+
this.#prune();
|
|
62
|
+
const e = this.#find(code);
|
|
63
|
+
if (!e) return { status: "unknown" };
|
|
64
|
+
if (!e.approved) return { status: "pending", hostName: e.hostName };
|
|
65
|
+
const out = { status: "approved" as const, roomToken: e.roomToken ?? undefined, roomId: e.roomId ?? undefined, hostName: e.hostName };
|
|
66
|
+
e.roomToken = null; // single retrieval
|
|
67
|
+
return out;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
#find(code: string): Entry | undefined {
|
|
71
|
+
const up = code.trim().toUpperCase();
|
|
72
|
+
return this.#entries.find((e) => e.code === up);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
#prune(): void {
|
|
76
|
+
const now = this.#now();
|
|
77
|
+
for (let i = this.#entries.length - 1; i >= 0; i--) {
|
|
78
|
+
if (this.#entries[i].expires <= now) this.#entries.splice(i, 1);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* herdgate SSH bridge — a reachable front door for the SSH leg, on any host.
|
|
3
|
+
*
|
|
4
|
+
* The problem it solves: the Herdr phone app speaks plain SSH over TCP, which cannot
|
|
5
|
+
* cross Cloudflare. When the gate machine itself has no reachable port (NAT, no
|
|
6
|
+
* IPv6), run this on a host that does (a tiny VPS is enough): it dials the cloud over
|
|
7
|
+
* an OUTBOUND WebSocket and pipes each phone's raw SSH bytes both ways. Everything
|
|
8
|
+
* that matters — device-key auth, act-scope enforcement, the read-only allowlist,
|
|
9
|
+
* brute-force strikes — stays in the cloud's sshd, untouched; the bridge carries no
|
|
10
|
+
* secrets, so its credential is the account (owner) token.
|
|
11
|
+
*
|
|
12
|
+
* Zero dependencies beyond Node ≥ 22 (global WebSocket). One file: copy it to the
|
|
13
|
+
* VPS and run it.
|
|
14
|
+
*
|
|
15
|
+
* HERDGATE_URL the cloud (e.g. https://gate.example.com) (required)
|
|
16
|
+
* HERDGATE_BRIDGE_TOKEN the account token — owner-level (required)
|
|
17
|
+
* HERDGATE_BRIDGE_BIND local TCP bind (default 0.0.0.0)
|
|
18
|
+
* HERDGATE_BRIDGE_PORT local TCP port (default 2299)
|
|
19
|
+
* HERDGATE_BRIDGE_PUBLIC_HOST advertise this host to phones (default: none —
|
|
20
|
+
* HERDGATE_BRIDGE_PUBLIC_PORT with PUBLIC_HOST, the PWA's SSH hint shows it)
|
|
21
|
+
*
|
|
22
|
+
* npm run ssh-bridge
|
|
23
|
+
* node ssh-bridge.ts (standalone; no repo imports)
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import { createServer as tcpServer, type Socket } from "node:net";
|
|
27
|
+
import { hostname } from "node:os";
|
|
28
|
+
|
|
29
|
+
export interface BridgeOptions {
|
|
30
|
+
/** The cloud base URL (https://…). */
|
|
31
|
+
cloudUrl: string;
|
|
32
|
+
/** Account (owner) token — the bridge is owner infrastructure carrying no secrets. */
|
|
33
|
+
token: string;
|
|
34
|
+
/** Local TCP listener. Defaults: 0.0.0.0:2299. */
|
|
35
|
+
bind?: string;
|
|
36
|
+
port?: number;
|
|
37
|
+
/** Tell phones (via the device hello) to dial this address instead of the gate. */
|
|
38
|
+
advertise?: { host: string; port: number } | null;
|
|
39
|
+
log?: (line: string) => void;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface BridgeHandle {
|
|
43
|
+
port: number;
|
|
44
|
+
/** Resolves once the cloud accepted our hello — phones can dial from then on. */
|
|
45
|
+
whenReady: Promise<void>;
|
|
46
|
+
close(): Promise<void>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Encode a v1 envelope. Inlined (no repo imports) so the file ships alone. */
|
|
50
|
+
const enc = (msg: Record<string, unknown>): string => JSON.stringify({ v: 1, ...msg });
|
|
51
|
+
|
|
52
|
+
export function startSshBridge(o: BridgeOptions): Promise<BridgeHandle> {
|
|
53
|
+
const log = o.log ?? (() => {});
|
|
54
|
+
const wsUrl = o.cloudUrl.replace(/\/$/, "").replace(/^http/, "ws") + "/v1/bridge/ws";
|
|
55
|
+
const bind = o.bind ?? "0.0.0.0";
|
|
56
|
+
const port = o.port ?? 2299;
|
|
57
|
+
|
|
58
|
+
let ws: WebSocket | null = null;
|
|
59
|
+
let backoff = 1_000;
|
|
60
|
+
let closing = false;
|
|
61
|
+
let sidSeq = 0;
|
|
62
|
+
const sessions = new Map<string, Socket>();
|
|
63
|
+
|
|
64
|
+
const server = tcpServer((sock) => {
|
|
65
|
+
const sid = `bs_${++sidSeq}`;
|
|
66
|
+
if (!ws || ws.readyState !== WebSocket.OPEN) {
|
|
67
|
+
sock.destroy(); // no cloud behind us: fail fast, the phone retries
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
sessions.set(sid, sock);
|
|
71
|
+
ws.send(enc({ t: "bridge.open", sid }));
|
|
72
|
+
sock.on("data", (d: Buffer) => {
|
|
73
|
+
if (ws?.readyState === WebSocket.OPEN) ws.send(enc({ t: "bridge.data", sid, b64: d.toString("base64") }));
|
|
74
|
+
});
|
|
75
|
+
const bye = (): void => {
|
|
76
|
+
if (sessions.delete(sid) && ws?.readyState === WebSocket.OPEN) ws.send(enc({ t: "bridge.close", sid }));
|
|
77
|
+
};
|
|
78
|
+
sock.on("close", bye);
|
|
79
|
+
sock.on("error", bye);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
/** Declared before `dial` so the catch can reference it (hoisting by hand). */
|
|
83
|
+
let retry: () => void = () => {};
|
|
84
|
+
let readyOk: () => void;
|
|
85
|
+
/** Resolves on the first accepted cloud hello (rejects never; retries forever). */
|
|
86
|
+
const whenReady = new Promise<void>((res) => {
|
|
87
|
+
readyOk = res;
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
const dial = (): void => {
|
|
91
|
+
if (closing) return;
|
|
92
|
+
let sock2: WebSocket;
|
|
93
|
+
try {
|
|
94
|
+
sock2 = new WebSocket(wsUrl);
|
|
95
|
+
} catch {
|
|
96
|
+
return retry();
|
|
97
|
+
}
|
|
98
|
+
ws = sock2;
|
|
99
|
+
sock2.onopen = () => {
|
|
100
|
+
sock2.send(enc({
|
|
101
|
+
t: "hello",
|
|
102
|
+
bridgeToken: o.token,
|
|
103
|
+
...(o.advertise ? { advertise: o.advertise } : {}),
|
|
104
|
+
}));
|
|
105
|
+
};
|
|
106
|
+
sock2.onmessage = (ev) => {
|
|
107
|
+
let m: { t?: string; sid?: string; b64?: string; ok?: boolean };
|
|
108
|
+
try {
|
|
109
|
+
m = JSON.parse(typeof ev.data === "string" ? ev.data : String(ev.data));
|
|
110
|
+
} catch {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
if (m.t === "hello" && m.ok === true) {
|
|
114
|
+
backoff = 1_000;
|
|
115
|
+
readyOk();
|
|
116
|
+
log(`connected to ${o.cloudUrl}${o.advertise ? ` (advertising ${o.advertise.host}:${o.advertise.port})` : ""}`);
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
if (m.t === "bridge.data" && m.sid && typeof m.b64 === "string") {
|
|
120
|
+
const s = sessions.get(m.sid);
|
|
121
|
+
if (s && !s.destroyed) s.write(Buffer.from(m.b64, "base64"));
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
if (m.t === "bridge.close" && m.sid) {
|
|
125
|
+
const s = sessions.get(m.sid);
|
|
126
|
+
if (s) {
|
|
127
|
+
sessions.delete(m.sid);
|
|
128
|
+
s.end();
|
|
129
|
+
s.destroy();
|
|
130
|
+
}
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
retry = (): void => {
|
|
135
|
+
const wait = Math.min(30_000, backoff) + Math.random() * 500;
|
|
136
|
+
backoff *= 2;
|
|
137
|
+
log(`disconnected from the cloud; retrying in ${Math.round(wait)}ms`);
|
|
138
|
+
setTimeout(dial, wait).unref?.();
|
|
139
|
+
};
|
|
140
|
+
sock2.onclose = () => {
|
|
141
|
+
// The cloud went away: end every phone session (they redial on their own).
|
|
142
|
+
for (const s of sessions.values()) s.destroy();
|
|
143
|
+
sessions.clear();
|
|
144
|
+
ws = null;
|
|
145
|
+
retry();
|
|
146
|
+
};
|
|
147
|
+
sock2.onerror = () => {
|
|
148
|
+
/* onclose carries the retry */
|
|
149
|
+
};
|
|
150
|
+
};
|
|
151
|
+
dial();
|
|
152
|
+
|
|
153
|
+
return new Promise((resolve, reject) => {
|
|
154
|
+
server.once("error", reject);
|
|
155
|
+
server.listen(port, bind, () => {
|
|
156
|
+
const addr = server.address();
|
|
157
|
+
const realPort = typeof addr === "object" && addr ? addr.port : port;
|
|
158
|
+
log(`ssh bridge listening on ${bind}:${realPort} → ${o.cloudUrl} (${hostname()})`);
|
|
159
|
+
resolve({
|
|
160
|
+
port: realPort,
|
|
161
|
+
whenReady,
|
|
162
|
+
close: () =>
|
|
163
|
+
new Promise<void>((done) => {
|
|
164
|
+
closing = true;
|
|
165
|
+
for (const s of sessions.values()) s.destroy();
|
|
166
|
+
sessions.clear();
|
|
167
|
+
try {
|
|
168
|
+
// Plain close(): the WHATWG client API rejects close(1001) — only 1000
|
|
169
|
+
// and 3000-4999 are valid client codes — and a throw here would leave
|
|
170
|
+
// the socket (and the cloud's bridge registry) hanging open.
|
|
171
|
+
ws?.close();
|
|
172
|
+
} catch {
|
|
173
|
+
/* already gone */
|
|
174
|
+
}
|
|
175
|
+
server.close(() => done());
|
|
176
|
+
}),
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// ---- CLI (run directly) ------------------------------------------------------
|
|
183
|
+
|
|
184
|
+
const ranDirectly = process.argv[1]?.endsWith("ssh-bridge.ts");
|
|
185
|
+
if (ranDirectly) {
|
|
186
|
+
const env = process.env;
|
|
187
|
+
if (!env.HERDGATE_URL || !env.HERDGATE_BRIDGE_TOKEN) {
|
|
188
|
+
console.error("ssh-bridge: HERDGATE_URL and HERDGATE_BRIDGE_TOKEN are required");
|
|
189
|
+
process.exit(2);
|
|
190
|
+
}
|
|
191
|
+
const adv =
|
|
192
|
+
env.HERDGATE_BRIDGE_PUBLIC_HOST
|
|
193
|
+
? {
|
|
194
|
+
host: env.HERDGATE_BRIDGE_PUBLIC_HOST,
|
|
195
|
+
port: Number(env.HERDGATE_BRIDGE_PUBLIC_PORT ?? env.HERDGATE_BRIDGE_PORT ?? 2299),
|
|
196
|
+
}
|
|
197
|
+
: null;
|
|
198
|
+
const bridge = await startSshBridge({
|
|
199
|
+
cloudUrl: env.HERDGATE_URL,
|
|
200
|
+
token: env.HERDGATE_BRIDGE_TOKEN,
|
|
201
|
+
bind: env.HERDGATE_BRIDGE_BIND,
|
|
202
|
+
port: Number(env.HERDGATE_BRIDGE_PORT ?? 2299),
|
|
203
|
+
advertise: adv,
|
|
204
|
+
log: (l) => console.error(`herdgate-bridge: ${l}`),
|
|
205
|
+
});
|
|
206
|
+
console.error(`herdgate-bridge: phones dial ${adv ? `${adv.host}:${adv.port}` : `this host:${bridge.port}`}`);
|
|
207
|
+
const bye = (): void => {
|
|
208
|
+
void bridge.close().then(() => process.exit(0));
|
|
209
|
+
};
|
|
210
|
+
process.on("SIGINT", bye);
|
|
211
|
+
process.on("SIGTERM", bye);
|
|
212
|
+
}
|