pi-pocket 0.27.0 → 0.28.0-beta.2
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 +11 -8
- package/docs/architecture.md +9 -1
- package/docs/customizing.md +16 -2
- package/docs/docker.md +2 -0
- package/docs/examples/quiet-note.js +21 -0
- package/docs/examples/quiet-note.ts +73 -0
- package/docs/extensions/app.md +1 -0
- package/docs/extensions/policies/api-stability.md +3 -0
- package/docs/extensions/reference/limits.md +11 -0
- package/docs/extensions/reference/pocket.md +22 -1
- package/docs/extensions/reference/theme-surface.md +12 -0
- package/docs/extensions/reference/web.md +36 -2
- package/docs/extensions.md +2 -0
- package/docs/features.md +20 -16
- package/docs/map.md +24 -12
- package/docs/systemd.md +1 -1
- package/npm-shrinkwrap.json +3163 -0
- package/package.json +3 -2
- package/src/launcher/access.ts +75 -4
- package/src/launcher/main.ts +303 -60
- package/src/launcher/options.ts +8 -5
- package/src/launcher/serve.ts +677 -0
- package/src/server/app.ts +223 -14
- package/src/server/avatars.ts +54 -0
- package/src/server/background.ts +214 -42
- package/src/server/build.ts +53 -0
- package/src/server/commands.ts +28 -33
- package/src/server/config.ts +9 -1
- package/src/server/docs.ts +8 -1
- package/src/server/entry-presentation.ts +275 -0
- package/src/server/export.ts +26 -3
- package/src/server/extension-web.ts +113 -8
- package/src/server/extensions/pip.ts +57 -2
- package/src/server/host.ts +21 -3
- package/src/server/http/api.ts +106 -6
- package/src/server/http/assets.ts +9 -1
- package/src/server/http/conversation-routes.ts +61 -0
- package/src/server/http/events.ts +19 -0
- package/src/server/http/icon-route.ts +196 -0
- package/src/server/http.ts +41 -2
- package/src/server/icon-mask.ts +33 -0
- package/src/server/icon-render.ts +149 -0
- package/src/server/icon-store.ts +57 -0
- package/src/server/model-images.ts +124 -0
- package/src/server/pip.ts +30 -6
- package/src/server/projection.ts +127 -32
- package/src/server/room.ts +171 -8
- package/src/server/running.ts +307 -2
- package/src/server/schedules.ts +5 -0
- package/src/server/skills.ts +99 -8
- package/src/server/spend.ts +5 -0
- package/src/server/transcripts.ts +28 -3
- package/src/server/when.ts +25 -11
- package/types/pocket.d.ts +53 -2
- package/web/app-icon.js +149 -0
- package/web/app.js +12 -8
- package/web/avatar.js +46 -2
- package/web/composer.js +107 -15
- package/web/entry-presentation.js +264 -0
- package/web/extension-ui.js +18 -0
- package/web/fingerprint.d.ts +2 -0
- package/web/fingerprint.js +21 -0
- package/web/launcher.js +15 -3
- package/web/model-favorites.js +27 -0
- package/web/peeks.js +11 -3
- package/web/pip.js +7 -1
- package/web/running-board.js +1197 -0
- package/web/session-cache.js +144 -0
- package/web/session-tabs.js +58 -0
- package/web/sessions.js +51 -12
- package/web/sheets/all-artifacts.js +104 -0
- package/web/sheets/appearance.js +45 -0
- package/web/sheets/background.js +228 -0
- package/web/sheets/find.js +61 -7
- package/web/sheets/folder.js +43 -4
- package/web/sheets/menu.js +3 -1
- package/web/sheets/message.js +80 -15
- package/web/sheets/model.js +39 -3
- package/web/sheets/people.js +35 -1
- package/web/sheets/providers.js +80 -26
- package/web/sheets/schedules.js +4 -1
- package/web/sheets/shortcuts.js +1 -0
- package/web/sheets/text.js +2 -0
- package/web/sheets.js +11 -4
- package/web/store.js +837 -27
- package/web/style.css +1928 -166
- package/web/subagents-board.js +3 -2
- package/web/tab-order.js +34 -0
- package/web/theme-surface.js +7 -0
- package/web/theme.js +15 -0
- package/web/transcript.js +588 -57
- package/web/user-search.js +6 -0
- package/web/sheets/running.js +0 -130
|
@@ -0,0 +1,677 @@
|
|
|
1
|
+
// Publishing Pi Pocket on the tailnet: tailscaled terminates HTTPS on 443 and proxies to the server on loopback, so the
|
|
2
|
+
// server keeps speaking plain http and every request arrives with X-Forwarded-Proto: https and the original Host.
|
|
3
|
+
import { spawnSync } from "node:child_process";
|
|
4
|
+
|
|
5
|
+
/** The first Tailscale with this `serve` CLI; before 1.52.0 the positional form was the one there, and errors now. */
|
|
6
|
+
export const TAILSCALE_MIN_VERSION: readonly [number, number, number] = [1, 52, 0];
|
|
7
|
+
/** Tailscale serves a node on 443, the port every device expects https on. */
|
|
8
|
+
export const SERVE_PORT = 443;
|
|
9
|
+
|
|
10
|
+
const MINIMUM = `${TAILSCALE_MIN_VERSION[0]}.${TAILSCALE_MIN_VERSION[1]}`;
|
|
11
|
+
/** The Tailscale CLI has no timeout of its own and waits forever on a hung daemon, so every command gets this long. */
|
|
12
|
+
const TIMEOUT_MS = 10_000;
|
|
13
|
+
/** Reading the status of the daemon is local and quick, so a probe that hangs is abandoned sooner than a mount is. */
|
|
14
|
+
const PROBE_TIMEOUT_MS = 3_000;
|
|
15
|
+
/**
|
|
16
|
+
* The mount waits on the control plane, and enabling HTTPS certificates is a step an admin may be doing right then, so
|
|
17
|
+
* it gets longer than a probe. It is still bounded: the launcher blocks on the CLI until it answers.
|
|
18
|
+
*/
|
|
19
|
+
const MOUNT_TIMEOUT_MS = 30_000;
|
|
20
|
+
|
|
21
|
+
const NOT_RUNNING = "Tailscale is not running or not signed in. Run: tailscale up";
|
|
22
|
+
const NOT_AUTHORIZED = "This machine is waiting for approval in the tailnet's admin console.";
|
|
23
|
+
const STARTING = "Tailscale is still starting up. Try again in a moment.";
|
|
24
|
+
const NOT_NAMED =
|
|
25
|
+
'Tailscale has not given this machine a name in the tailnet yet. Check "tailscale status", then try again.';
|
|
26
|
+
// An empty CertDomains is also what a netmap that has not arrived yet looks like, so this asks rather than asserts.
|
|
27
|
+
const NO_CERTIFICATES =
|
|
28
|
+
"Pi Pocket could not confirm an HTTPS name for this machine yet. If HTTPS certificates are off in this tailnet, open DNS in the admin console, enable MagicDNS and HTTPS certificates, then try again.";
|
|
29
|
+
const UNREADABLE =
|
|
30
|
+
'Pi Pocket could not read the "tailscale serve status" output. Update Tailscale, then try again.';
|
|
31
|
+
|
|
32
|
+
/** What one run of the CLI said; `message` is set instead of the output when it failed. */
|
|
33
|
+
type Run = { ok: true; out: string; err: string } | { ok: false; message: string };
|
|
34
|
+
/** The serve config, as `tailscale serve status --json` printed it. */
|
|
35
|
+
type ConfigRun = { ok: true; config: unknown } | { ok: false; message: string };
|
|
36
|
+
|
|
37
|
+
export type PortState =
|
|
38
|
+
| { kind: "free" }
|
|
39
|
+
| { kind: "ours"; url: string }
|
|
40
|
+
| { kind: "taken"; detail: string; command: string }
|
|
41
|
+
| { kind: "funnel"; detail: string };
|
|
42
|
+
|
|
43
|
+
export interface ServeEvents {
|
|
44
|
+
problem(line: string): void;
|
|
45
|
+
/** Persist ownership before a new mount can outlive this process. */
|
|
46
|
+
mountAttempt?(): void;
|
|
47
|
+
/** Forget ownership only after the mount is absent or its removal succeeds. */
|
|
48
|
+
mountRemoved?(): void;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export type ServeResult =
|
|
52
|
+
{ ok: true; url: string; adopted: boolean } | { ok: false; message: string };
|
|
53
|
+
|
|
54
|
+
/** The first `1.2.3` line of `tailscale version`, or of anything else that starts with one. */
|
|
55
|
+
export function parseTailscaleVersion(text: string): number[] | undefined {
|
|
56
|
+
const match = /^(\d+)\.(\d+)\.(\d+)/m.exec(text);
|
|
57
|
+
|
|
58
|
+
if (match === null) {
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return [Number(match[1]), Number(match[2]), Number(match[3])];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function serveSupported(version: number[] | undefined): boolean {
|
|
66
|
+
if (version === undefined) {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
for (let i = 0; i < TAILSCALE_MIN_VERSION.length; i += 1) {
|
|
71
|
+
const found = version[i] ?? 0;
|
|
72
|
+
const needed = TAILSCALE_MIN_VERSION[i] ?? 0;
|
|
73
|
+
|
|
74
|
+
if (found !== needed) {
|
|
75
|
+
return found > needed;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Why this node cannot serve anything yet, in terms someone can act on; undefined when it can. */
|
|
83
|
+
export function preflightProblem(status: unknown): string | undefined {
|
|
84
|
+
const settings = recordOf(status);
|
|
85
|
+
|
|
86
|
+
if (settings === undefined) {
|
|
87
|
+
return NOT_RUNNING;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const backend = settings.BackendState;
|
|
91
|
+
|
|
92
|
+
// A node the tailnet has not admitted yet is signed in, so it waits on the admin console rather than on `tailscale up`.
|
|
93
|
+
if (backend === "NeedsMachineAuth") {
|
|
94
|
+
return NOT_AUTHORIZED;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// A daemon on its way up is a moment, not a fault, and `tailscale up` is not the advice for it.
|
|
98
|
+
if (backend === "Starting") {
|
|
99
|
+
return STARTING;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// NeedsLogin, Stopped and NoState, like a missing backend state, all mean there is nothing to serve into.
|
|
103
|
+
if (backend !== "Running") {
|
|
104
|
+
return NOT_RUNNING;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Self is null while the node is logged out, and a node that is not in the tailnet cannot be served into it.
|
|
108
|
+
if (recordOf(settings.Self) === undefined) {
|
|
109
|
+
return NOT_RUNNING;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// A node with no name of its own has no address to serve at, and mounting under an empty key fails after the fact.
|
|
113
|
+
if (tailnetHost(dnsNameOf(settings) ?? "") === undefined) {
|
|
114
|
+
return NOT_NAMED;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return undefined;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Why this node has no HTTPS certificate for its name yet, in terms someone can act on; undefined when it has one.
|
|
122
|
+
* `CertDomains` comes from the control plane and is empty unless HTTPS certificates are enabled in the admin console.
|
|
123
|
+
* An empty list is also what a netmap that has not arrived yet looks like, so this asks rather than asserts. It is a
|
|
124
|
+
* check of its own rather than part of preflightProblem() because a mount already in place needs no new certificate.
|
|
125
|
+
*/
|
|
126
|
+
export function certificateProblem(status: unknown): string | undefined {
|
|
127
|
+
const certificates = recordOf(status)?.CertDomains;
|
|
128
|
+
|
|
129
|
+
if (!Array.isArray(certificates) || certificates.length === 0) {
|
|
130
|
+
return NO_CERTIFICATES;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return undefined;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Who holds `port` on this node, and whether that is this Pi Pocket's own mount at `/`. `localPort` is the server's own
|
|
138
|
+
* port on loopback, which is what a mount of ours proxies to. A `taken` state carries the command that frees the port:
|
|
139
|
+
* what frees a web mount does not free a TCP forward, and tailscaled refuses the web form while one is there.
|
|
140
|
+
*/
|
|
141
|
+
export function portState(
|
|
142
|
+
config: unknown,
|
|
143
|
+
dnsName: string,
|
|
144
|
+
port: number,
|
|
145
|
+
localPort: number,
|
|
146
|
+
): PortState {
|
|
147
|
+
const host = tailnetHost(dnsName);
|
|
148
|
+
const settings = recordOf(config);
|
|
149
|
+
const allowFunnel = recordOf(settings?.AllowFunnel);
|
|
150
|
+
const funnelKey = host === undefined ? undefined : entryKey(allowFunnel, host, port);
|
|
151
|
+
|
|
152
|
+
// Funnel comes before the mounts: it publishes the port to the whole internet, which matters more than who else, or
|
|
153
|
+
// we ourselves, holds a path on it. `AllowFunnel` is keyed "<dnsname>:<port>" in every version 1.52 and later.
|
|
154
|
+
if (funnelKey !== undefined && allowFunnel?.[funnelKey] === true) {
|
|
155
|
+
return { kind: "funnel", detail: webKey(host ?? dnsName, port) };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const mount = host === undefined ? undefined : mountAt(config, dnsName, port);
|
|
159
|
+
const tcp = recordOf(recordOf(settings?.TCP)?.[String(port)]);
|
|
160
|
+
|
|
161
|
+
if (host !== undefined && mount !== undefined) {
|
|
162
|
+
// Our own mount can be spelled several ways, so the target is compared rather than the exact string: it is ours
|
|
163
|
+
// while it points at this server's own loopback port, whoever wrote it down. A plain-http serve at our port is
|
|
164
|
+
// not: calling it https would hand out an address that answers with a TLS error.
|
|
165
|
+
if (sameLocalTarget(mount.Proxy, localPort) && tcp?.HTTP !== true) {
|
|
166
|
+
return { kind: "ours", url: serveUrl(host, port) };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return {
|
|
170
|
+
kind: "taken",
|
|
171
|
+
detail: typeof mount.Proxy === "string" ? mount.Proxy : 'another mount at "/"',
|
|
172
|
+
command:
|
|
173
|
+
tcp?.HTTP === true
|
|
174
|
+
? `tailscale serve --http=${port} --set-path=/ off`
|
|
175
|
+
: `tailscale serve --https=${port} --set-path=/ off`,
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// HTTPS on the port is how tailscaled terminates TLS for the mounts above, ours included, so on its own it holds
|
|
180
|
+
// nothing. A TCP forward does: it hands the whole port to something that is not http.
|
|
181
|
+
const forward = tcp?.TCPForward;
|
|
182
|
+
|
|
183
|
+
if (typeof forward === "string" && forward !== "") {
|
|
184
|
+
return {
|
|
185
|
+
kind: "taken",
|
|
186
|
+
detail: `a TCP forward to ${forward}`,
|
|
187
|
+
command: `tailscale serve --tcp=${port} off`,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return { kind: "free" };
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Whether `proxy` is a proxy target on this machine's own loopback at exactly `port`. Serve records a target as written,
|
|
196
|
+
* so `localhost:8787`, `127.0.0.1:8787` and `http://[::1]:8787` all name the same local server as `http://127.0.0.1:8787`.
|
|
197
|
+
*/
|
|
198
|
+
export function sameLocalTarget(proxy: unknown, port: number): boolean {
|
|
199
|
+
if (typeof proxy !== "string") {
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const text = proxy.trim();
|
|
204
|
+
|
|
205
|
+
if (text === "") {
|
|
206
|
+
return false;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const withScheme = /^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//.test(text) ? text : `http://${text}`;
|
|
210
|
+
let target: URL;
|
|
211
|
+
|
|
212
|
+
try {
|
|
213
|
+
target = new URL(withScheme);
|
|
214
|
+
} catch {
|
|
215
|
+
return false;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (!LOOPBACK_HOSTS.has(target.hostname.toLowerCase())) {
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const targetPort =
|
|
223
|
+
target.port === "" ? (target.protocol === "https:" ? 443 : 80) : Number(target.port);
|
|
224
|
+
|
|
225
|
+
// The root of the port over plain http, which is what a mount of this server is: `/app` is a different address, and
|
|
226
|
+
// https at a server that speaks http is a mount that only half works. Neither is ours to adopt or to report.
|
|
227
|
+
return target.protocol === "http:" && target.pathname === "/" && targetPort === port;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Pi Pocket's address on the tailnet, served by tailscaled itself: one CLI command mounts it, and no process of ours
|
|
232
|
+
* keeps running afterwards. A mount that is already ours is adopted rather than mounted again, so restarting Pi Pocket
|
|
233
|
+
* leaves the address someone is using where it is.
|
|
234
|
+
*/
|
|
235
|
+
export class TailscaleServe {
|
|
236
|
+
readonly #binary: string;
|
|
237
|
+
readonly #port: number;
|
|
238
|
+
readonly #events: ServeEvents;
|
|
239
|
+
/**
|
|
240
|
+
* Whether this run tried to put a mount at `/`, so a later stop removes only what this run is responsible for. An
|
|
241
|
+
* attempt counts even when the CLI did not answer: only the scoped `off` can tell.
|
|
242
|
+
*/
|
|
243
|
+
#published = false;
|
|
244
|
+
/** The node's DNS name as `start` found it, where the mount at `/` is looked for. */
|
|
245
|
+
#dnsName = "";
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* `binary` is the tailscale CLI to run, and `port` the local server it should proxy to: the tailnet port is always
|
|
249
|
+
* SERVE_PORT.
|
|
250
|
+
*/
|
|
251
|
+
constructor(binary: string, port: number, events: ServeEvents) {
|
|
252
|
+
this.#binary = binary;
|
|
253
|
+
this.#port = port;
|
|
254
|
+
this.#events = events;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
start(): ServeResult {
|
|
258
|
+
const versionRun = this.#run(["version"]);
|
|
259
|
+
|
|
260
|
+
if (!versionRun.ok) {
|
|
261
|
+
return { ok: false, message: versionRun.message };
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const version = parseTailscaleVersion(`${versionRun.out}\n${versionRun.err}`);
|
|
265
|
+
|
|
266
|
+
if (version === undefined) {
|
|
267
|
+
return {
|
|
268
|
+
ok: false,
|
|
269
|
+
message: `Pi Pocket could not tell which Tailscale is installed. Version ${MINIMUM} or newer is needed; update Tailscale.`,
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
if (!serveSupported(version)) {
|
|
274
|
+
return {
|
|
275
|
+
ok: false,
|
|
276
|
+
message: `Tailscale ${version.join(".")} is too old for serve. Version ${MINIMUM} or newer is needed; update Tailscale.`,
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const statusRun = this.#run(["status", "--json"]);
|
|
281
|
+
|
|
282
|
+
if (!statusRun.ok) {
|
|
283
|
+
return { ok: false, message: statusRun.message };
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const status = parseJson(statusRun.out);
|
|
287
|
+
const problem = preflightProblem(status);
|
|
288
|
+
|
|
289
|
+
if (problem !== undefined) {
|
|
290
|
+
return { ok: false, message: problem };
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const dnsName = dnsNameOf(status) ?? "";
|
|
294
|
+
|
|
295
|
+
this.#dnsName = dnsName;
|
|
296
|
+
// The node's own name is what every message about it should use; a node with none cannot be served by name.
|
|
297
|
+
const servedName = tailnetHost(dnsName) ?? "this node";
|
|
298
|
+
const proxy = loopback(this.#port);
|
|
299
|
+
const before = this.#serveConfig();
|
|
300
|
+
|
|
301
|
+
if (!before.ok) {
|
|
302
|
+
return { ok: false, message: before.message };
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const state = portState(before.config, dnsName, SERVE_PORT, this.#port);
|
|
306
|
+
const adopted = state.kind === "ours";
|
|
307
|
+
|
|
308
|
+
// A mount of ours needs no certificate check: it is up, so its certificate already is. Reading the mount before
|
|
309
|
+
// the check is what lets an existing address survive a netmap that has not reported CertDomains yet.
|
|
310
|
+
if (!adopted) {
|
|
311
|
+
const certificates = certificateProblem(status);
|
|
312
|
+
|
|
313
|
+
if (certificates !== undefined) {
|
|
314
|
+
return { ok: false, message: certificates };
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
if (state.kind === "funnel") {
|
|
319
|
+
// Mounting `/` under Funnel would put Pi Pocket on the internet, not just on the tailnet the menu promises.
|
|
320
|
+
return {
|
|
321
|
+
ok: false,
|
|
322
|
+
message: `Tailscale Funnel is on for ${state.detail ?? servedName}, which would publish Pi Pocket to the internet. Turn it off with "tailscale funnel --https=${SERVE_PORT} off", which takes every handler on ${SERVE_PORT} with it, then try again.`,
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
if (state.kind === "taken") {
|
|
327
|
+
return {
|
|
328
|
+
ok: false,
|
|
329
|
+
message: `Port ${SERVE_PORT} is already served by ${state.detail}. Pi Pocket will not replace it. Free it with "${state.command}", or pick another access mode.`,
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/** What the mount command said, for the case where it exits without mounting anything. */
|
|
334
|
+
let said = "";
|
|
335
|
+
|
|
336
|
+
// `#published` follows what this run is responsible for. A mount command that timed out may still have been
|
|
337
|
+
// applied by tailscaled, and nothing else can tell us whether it was, so an attempt counts before it runs. An
|
|
338
|
+
// adopted mount is not this run's to remove: quitting leaves it where it is.
|
|
339
|
+
if (!adopted) {
|
|
340
|
+
this.#events.mountAttempt?.();
|
|
341
|
+
this.#published = true;
|
|
342
|
+
|
|
343
|
+
// --set-path=/ keeps the mount to this one path, so removing it later cannot take another mount with it.
|
|
344
|
+
const mounted = this.#run(
|
|
345
|
+
["serve", "--bg", `--https=${SERVE_PORT}`, "--set-path=/", proxy],
|
|
346
|
+
MOUNT_TIMEOUT_MS,
|
|
347
|
+
"If you were enabling HTTPS certificates for this tailnet, finish that in the admin console, then try again.",
|
|
348
|
+
);
|
|
349
|
+
|
|
350
|
+
if (!mounted.ok) {
|
|
351
|
+
return { ok: false, message: mounted.message };
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// Tailscale can exit 0 without mounting anything: with HTTPS certificates off it prints the address that
|
|
355
|
+
// turns them on and stops. That address is the only useful thing to say next, so keep it.
|
|
356
|
+
said = lastSaid(`${mounted.out}\n${mounted.err}`);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
const after = this.#serveConfig();
|
|
360
|
+
const served = after.ok
|
|
361
|
+
? portState(after.config, dnsName, SERVE_PORT, this.#port)
|
|
362
|
+
: undefined;
|
|
363
|
+
const url = served?.kind === "ours" ? served.url : undefined;
|
|
364
|
+
|
|
365
|
+
if (url === undefined) {
|
|
366
|
+
// Only tailscaled's own report is worth opening: Self.DNSName would name an address nothing serves when the
|
|
367
|
+
// mount did not take, or when someone removed it in between. main removes the mount after a failed start.
|
|
368
|
+
return {
|
|
369
|
+
ok: false,
|
|
370
|
+
message: after.ok
|
|
371
|
+
? `Tailscale Serve reports no mount at "/" for ${servedName}:${SERVE_PORT}.${said === "" ? "" : ` Tailscale said: ${said}`} Check "tailscale serve status".`
|
|
372
|
+
: after.message,
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
return { ok: true, url, adopted };
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
stop(): void {
|
|
380
|
+
if (!this.#published) {
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
this.#published = false;
|
|
385
|
+
|
|
386
|
+
// Someone may have put another service at `/` since: that one is theirs, and stays.
|
|
387
|
+
const now = this.#serveConfig();
|
|
388
|
+
const mount = now.ok ? mountAt(now.config, this.#dnsName, SERVE_PORT) : undefined;
|
|
389
|
+
|
|
390
|
+
if (mount !== undefined && !sameLocalTarget(mount.Proxy, this.#port)) {
|
|
391
|
+
this.#events.mountRemoved?.();
|
|
392
|
+
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
const removed = this.#run(["serve", `--https=${SERVE_PORT}`, "--set-path=/", "off"]);
|
|
397
|
+
|
|
398
|
+
// An `off` with no handler under it, or with no serve config at all, has nothing to remove: that is what a mount
|
|
399
|
+
// that never took leaves behind, and it is not a problem to report. Any other failure leaves the mount publishing
|
|
400
|
+
// this server, so say what that means: the start after this one takes it down, or reuses it in serve mode.
|
|
401
|
+
if (!removed.ok && !missingMount(removed.message)) {
|
|
402
|
+
this.#events.problem(
|
|
403
|
+
`${removed.message} The tailnet may still point here; the next start takes the mount down or reuses it.`,
|
|
404
|
+
);
|
|
405
|
+
} else {
|
|
406
|
+
this.#events.mountRemoved?.();
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
#serveConfig(): ConfigRun {
|
|
411
|
+
return readServeConfig(this.#binary);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
#run(args: string[], timeout = TIMEOUT_MS, timedOutHint?: string): Run {
|
|
415
|
+
return runTailscale(this.#binary, args, timeout, timedOutHint);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Remove a mount a crashed launcher left at `/` for one of `ports`, so the next start finds it free or adopts it
|
|
421
|
+
* instead of refusing its own remains. Any port the launcher used before counts, because a mount left by a killed run
|
|
422
|
+
* still points at the port that run listened on, not at this one. This never throws: a problem is said through
|
|
423
|
+
* `events?.problem`, and the answer is whether a mount of ours was there to remove.
|
|
424
|
+
*/
|
|
425
|
+
export function removeStaleServe(
|
|
426
|
+
binary: string,
|
|
427
|
+
ports: readonly number[],
|
|
428
|
+
events?: ServeEvents,
|
|
429
|
+
): boolean {
|
|
430
|
+
try {
|
|
431
|
+
const statusRun = runTailscale(binary, ["status", "--json"], PROBE_TIMEOUT_MS);
|
|
432
|
+
|
|
433
|
+
if (!statusRun.ok) {
|
|
434
|
+
events?.problem(statusRun.message);
|
|
435
|
+
|
|
436
|
+
return false;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
const dnsName = dnsNameOf(parseJson(statusRun.out));
|
|
440
|
+
|
|
441
|
+
if (dnsName === undefined) {
|
|
442
|
+
return false;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
const before = readServeConfig(binary, PROBE_TIMEOUT_MS);
|
|
446
|
+
|
|
447
|
+
if (!before.ok) {
|
|
448
|
+
events?.problem(before.message);
|
|
449
|
+
|
|
450
|
+
return false;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
const mount = mountAt(before.config, dnsName, SERVE_PORT);
|
|
454
|
+
|
|
455
|
+
// Only a `/` of ours is ours to remove: the scoped `off` cannot take another mount or another path with it.
|
|
456
|
+
if (mount === undefined || !ports.some((port) => sameLocalTarget(mount.Proxy, port))) {
|
|
457
|
+
events?.mountRemoved?.();
|
|
458
|
+
|
|
459
|
+
return false;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
const removed = runTailscale(binary, [
|
|
463
|
+
"serve",
|
|
464
|
+
`--https=${SERVE_PORT}`,
|
|
465
|
+
"--set-path=/",
|
|
466
|
+
"off",
|
|
467
|
+
]);
|
|
468
|
+
|
|
469
|
+
if (!removed.ok) {
|
|
470
|
+
events?.problem(removed.message);
|
|
471
|
+
|
|
472
|
+
return false;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
events?.mountRemoved?.();
|
|
476
|
+
|
|
477
|
+
return true;
|
|
478
|
+
} catch (error) {
|
|
479
|
+
// A stale mount is tidying up before the real start, so nothing in here is worth failing the start over.
|
|
480
|
+
events?.problem(error instanceof Error ? error.message : String(error));
|
|
481
|
+
|
|
482
|
+
return false;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
function loopback(port: number): string {
|
|
487
|
+
return `http://127.0.0.1:${port}`;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* One CLI command, bounded by `timeout` so a hung daemon cannot hang Pi Pocket with it. `timedOutHint` replaces the
|
|
492
|
+
* generic "it may be hung" advice for a command that legitimately waits on something outside this machine.
|
|
493
|
+
*/
|
|
494
|
+
function runTailscale(
|
|
495
|
+
binary: string,
|
|
496
|
+
args: string[],
|
|
497
|
+
timeout = TIMEOUT_MS,
|
|
498
|
+
timedOutHint?: string,
|
|
499
|
+
): Run {
|
|
500
|
+
const result = spawnSync(binary, args, {
|
|
501
|
+
timeout,
|
|
502
|
+
// `status --json` lists every peer: a large tailnet passes the 1 MiB default.
|
|
503
|
+
maxBuffer: 64 * 1024 * 1024,
|
|
504
|
+
encoding: "utf8",
|
|
505
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
506
|
+
});
|
|
507
|
+
const error = result.error;
|
|
508
|
+
|
|
509
|
+
if (error !== undefined) {
|
|
510
|
+
const timedOut = "code" in error && error.code === "ETIMEDOUT";
|
|
511
|
+
|
|
512
|
+
return {
|
|
513
|
+
ok: false,
|
|
514
|
+
message: timedOut
|
|
515
|
+
? `"tailscale ${args.join(" ")}" did not answer within ${timeout / 1000} seconds. ${timedOutHint ?? "Tailscale may be hung: restart it, then try again."}`
|
|
516
|
+
: `Could not run ${binary}: ${error.message}`,
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
const out = result.stdout;
|
|
521
|
+
const err = result.stderr;
|
|
522
|
+
|
|
523
|
+
if (result.status !== 0) {
|
|
524
|
+
return { ok: false, message: complaint(err, out, args) };
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
return { ok: true, out, err };
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
function readServeConfig(binary: string, timeout = TIMEOUT_MS): ConfigRun {
|
|
531
|
+
const run = runTailscale(binary, ["serve", "status", "--json"], timeout);
|
|
532
|
+
|
|
533
|
+
if (!run.ok) {
|
|
534
|
+
return run;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
// `{}`, a missing Web, and `null` all mean nothing is served; unparsable output is not the same thing.
|
|
538
|
+
const config = parseJson(run.out);
|
|
539
|
+
|
|
540
|
+
if (config === undefined) {
|
|
541
|
+
return { ok: false, message: UNREADABLE };
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
return { ok: true, config };
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
/** Whether a failed `off` is tailscaled saying there was nothing mounted under that path. */
|
|
548
|
+
/** Whether a failed `off` is tailscaled saying there was nothing mounted under that path. */
|
|
549
|
+
function missingMount(message: string): boolean {
|
|
550
|
+
// "handler does not exist" is what the web `off` prints for a path with no handler. The `--tcp` form says "serve
|
|
551
|
+
// config does not exist" instead, and this never runs that one.
|
|
552
|
+
return message.toLowerCase().includes("handler does not exist");
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
/** The last thing a command said, for the message about why it exited without doing what it was asked. */
|
|
556
|
+
function lastSaid(text: string): string {
|
|
557
|
+
const said = text
|
|
558
|
+
.split(/\r?\n/)
|
|
559
|
+
.map((line) => line.trim())
|
|
560
|
+
.filter((line) => line !== "")
|
|
561
|
+
.slice(-3)
|
|
562
|
+
.join(" ");
|
|
563
|
+
|
|
564
|
+
return said.length > 300 ? `${said.slice(0, 297)}…` : said;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
/** Why the CLI refused, in terms someone can act on, or its own last word. */
|
|
568
|
+
function complaint(err: string, out: string, args: string[]): string {
|
|
569
|
+
if (err.includes("serve config denied")) {
|
|
570
|
+
return 'Run "sudo tailscale set --operator=$USER" once, so Pi Pocket may configure serve.';
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
// Tailscale has reworded this more than once, and says it either way when nothing is listening on the socket.
|
|
574
|
+
const said = `${err}\n${out}`.toLowerCase();
|
|
575
|
+
|
|
576
|
+
if (
|
|
577
|
+
said.includes("failed to connect to local tailscale daemon") ||
|
|
578
|
+
said.includes("failed to connect to local tailscale service")
|
|
579
|
+
) {
|
|
580
|
+
return "Tailscale is not running on this machine. Start it (or run: tailscale up), then try again.";
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
return (
|
|
584
|
+
lastLine(err) ?? lastLine(out) ?? `"tailscale ${args.join(" ")}" failed without saying why.`
|
|
585
|
+
);
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
function lastLine(text: string): string | undefined {
|
|
589
|
+
const lines = text
|
|
590
|
+
.split(/\r?\n/)
|
|
591
|
+
.map((line) => line.trim())
|
|
592
|
+
.filter((line) => line !== "");
|
|
593
|
+
|
|
594
|
+
return lines.at(-1);
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
function parseJson(text: string): unknown {
|
|
598
|
+
try {
|
|
599
|
+
return JSON.parse(text);
|
|
600
|
+
} catch {
|
|
601
|
+
return undefined;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
function recordOf(value: unknown): Record<string, unknown> | undefined {
|
|
606
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
607
|
+
return undefined;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
return value as Record<string, unknown>;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
/** The hosts that name this machine itself; `URL` keeps the brackets on an IPv6 literal. */
|
|
614
|
+
const LOOPBACK_HOSTS: ReadonlySet<string> = new Set(["127.0.0.1", "localhost", "[::1]", "::1"]);
|
|
615
|
+
|
|
616
|
+
/** The node's name without Tailscale's trailing dot. */
|
|
617
|
+
function tailnetHost(dnsName: string): string | undefined {
|
|
618
|
+
const host = dnsName.trim().replace(/\.+$/, "");
|
|
619
|
+
|
|
620
|
+
return host === "" ? undefined : host;
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
/** ServeConfig keys its mounts by "<dnsname>:<port>". */
|
|
624
|
+
function webKey(host: string, port: number): string {
|
|
625
|
+
return `${host}:${port}`;
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
/**
|
|
629
|
+
* The key `record` holds for `host` and `port`, in the case it spells the name itself: Tailscale prints the DNS name the
|
|
630
|
+
* way the control plane gave it, which need not match the case of the one read from `tailscale status`.
|
|
631
|
+
*/
|
|
632
|
+
function entryKey(
|
|
633
|
+
record: Record<string, unknown> | undefined,
|
|
634
|
+
host: string,
|
|
635
|
+
port: number,
|
|
636
|
+
): string | undefined {
|
|
637
|
+
if (record === undefined) {
|
|
638
|
+
return undefined;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
const wanted = webKey(host, port).toLowerCase();
|
|
642
|
+
|
|
643
|
+
return Object.keys(record).find((key) => key.toLowerCase() === wanted);
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
/** The `/` mount this node holds on `port`, when `tailscale serve status --json` reports one. */
|
|
647
|
+
function mountAt(
|
|
648
|
+
config: unknown,
|
|
649
|
+
dnsName: string,
|
|
650
|
+
port: number,
|
|
651
|
+
): Record<string, unknown> | undefined {
|
|
652
|
+
const host = tailnetHost(dnsName);
|
|
653
|
+
|
|
654
|
+
if (host === undefined) {
|
|
655
|
+
return undefined;
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
const web = recordOf(recordOf(config)?.Web);
|
|
659
|
+
const key = entryKey(web, host, port);
|
|
660
|
+
const entry = key === undefined ? undefined : recordOf(web?.[key]);
|
|
661
|
+
|
|
662
|
+
return recordOf(recordOf(entry?.Handlers)?.["/"]);
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
/**
|
|
666
|
+
* The https address of a mount on `port`, with no trailing slash: it is a base a path gets appended to (the panel's
|
|
667
|
+
* sign-in link), and tailscaled answers `/` itself when a browser opens it.
|
|
668
|
+
*/
|
|
669
|
+
function serveUrl(host: string, port: number): string {
|
|
670
|
+
return port === SERVE_PORT ? `https://${host}` : `https://${host}:${port}`;
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
function dnsNameOf(status: unknown): string | undefined {
|
|
674
|
+
const name = recordOf(recordOf(status)?.Self)?.DNSName;
|
|
675
|
+
|
|
676
|
+
return typeof name === "string" ? name : undefined;
|
|
677
|
+
}
|