@linxin666/dsh-remote-bridge 0.4.12 → 0.4.13

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.
Files changed (59) hide show
  1. package/README.i18n.yaml +2 -2
  2. package/README.md +45 -8
  3. package/README.zh.md +45 -8
  4. package/lib/client.js +1 -1
  5. package/lib/client.js.map +1 -1
  6. package/lib/index.js +18031 -373
  7. package/lib/types/core/connection.d.ts +9 -0
  8. package/lib/types/core/devices.d.ts +27 -6
  9. package/lib/types/core/liveactivity.d.ts +7 -0
  10. package/lib/types/core/model-efforts.d.ts +83 -0
  11. package/lib/types/core/protocol.d.ts +2 -0
  12. package/lib/types/core/push.d.ts +51 -2
  13. package/lib/types/host/firewall.d.ts +74 -0
  14. package/lib/types/host/host-bind-capability.d.ts +90 -0
  15. package/lib/types/host/ingress-gate.d.ts +59 -0
  16. package/lib/types/host/ingress-sync.d.ts +429 -0
  17. package/lib/types/host/lan-bind-plan.d.ts +96 -0
  18. package/lib/types/host/lan-bind.d.ts +106 -0
  19. package/lib/types/host/lan-forward.d.ts +112 -0
  20. package/lib/types/host/lan.d.ts +61 -0
  21. package/lib/types/host/pair-origin.d.ts +34 -19
  22. package/lib/types/host/relay-bridge-connector.d.ts +215 -0
  23. package/lib/types/host/relay-connector.d.ts +152 -0
  24. package/lib/types/host/relay-control.d.ts +124 -0
  25. package/lib/types/host/relay-registry.d.ts +114 -0
  26. package/lib/types/host/relay.d.ts +170 -0
  27. package/lib/types/host/routes.d.ts +24 -1
  28. package/lib/types/host/tunnel-orphan.d.ts +145 -0
  29. package/lib/types/host/tunnel-plan.d.ts +41 -0
  30. package/lib/types/host/tunnel.d.ts +289 -0
  31. package/lib/types/index.d.ts +426 -1
  32. package/package.json +4 -2
  33. package/src/client/panel.module.css +16 -1
  34. package/src/core/connection.ts +206 -11
  35. package/src/core/devices.ts +31 -6
  36. package/src/core/hub.ts +17 -5
  37. package/src/core/liveactivity.ts +16 -2
  38. package/src/core/model-efforts.ts +223 -0
  39. package/src/core/protocol.ts +3 -0
  40. package/src/core/push.ts +70 -4
  41. package/src/host/firewall.ts +230 -0
  42. package/src/host/host-bind-capability.ts +249 -0
  43. package/src/host/ingress-gate.ts +203 -0
  44. package/src/host/ingress-sync.ts +1074 -0
  45. package/src/host/lan-bind-plan.ts +134 -0
  46. package/src/host/lan-bind.ts +250 -0
  47. package/src/host/lan-forward.ts +321 -0
  48. package/src/host/lan.ts +142 -0
  49. package/src/host/pair-origin.ts +44 -38
  50. package/src/host/relay-bridge-connector.ts +783 -0
  51. package/src/host/relay-connector.ts +317 -0
  52. package/src/host/relay-control.ts +224 -0
  53. package/src/host/relay-registry.ts +263 -0
  54. package/src/host/relay.ts +294 -0
  55. package/src/host/routes.ts +47 -3
  56. package/src/host/tunnel-orphan.ts +325 -0
  57. package/src/host/tunnel-plan.ts +58 -0
  58. package/src/host/tunnel.ts +688 -0
  59. package/src/index.ts +1280 -22
@@ -0,0 +1,429 @@
1
+ /**
2
+ * Update-only Cloudflare tunnel ingress sync (issue #1948).
3
+ *
4
+ * A named tunnel's public hostname is permanent — that is the whole point of
5
+ * the mode — but the ingress rule behind it names a fixed local port. When
6
+ * `dsh web` listens on another port (an operator changed `--port`, a profile
7
+ * carries a different value, a second instance took the old one), the hostname
8
+ * keeps resolving and answers 502 while this plugin still believes it is
9
+ * published. This module converges exactly that one number and nothing else:
10
+ *
11
+ * - a read (GET) is the only request a healthy start makes;
12
+ * - the plan proves the whole document except the matched rule's `service`
13
+ * string is unchanged, and refuses (a conflict) anything it cannot prove —
14
+ * no added or removed rule, no moved ingress entry, no touched top-level
15
+ * key, no invented catch-all;
16
+ * - the service it retargets has to be this machine's own: loopback, or one of
17
+ * this host's LAN addresses. Another host's address is a conflict, never a
18
+ * write, because the port there belongs to whatever that address serves;
19
+ * - only a proven drift is written, as one full-document PUT (the API offers
20
+ * no patch for a single rule);
21
+ * - the write is read back once. A disagreement is a conflict, never an
22
+ * automatic rewrite: one tunnel document has one writer, and a deployment
23
+ * whose dashboard or another sync owns it has to be reconciled by a human.
24
+ *
25
+ * Everything is offline-testable: the HTTP call and the timers are injected
26
+ * seams, and the default seam itself refuses to send the account token to any
27
+ * origin but `https://api.cloudflare.com`. The token never enters a log line,
28
+ * a status frame, a child process argument, or any browser-visible state — it
29
+ * exists only inside the `Authorization` header of the two requests that need
30
+ * it.
31
+ */
32
+ /** The only origin the Cloudflare API token is ever sent to. */
33
+ export declare const CLOUDFLARE_API_ORIGIN = "https://api.cloudflare.com";
34
+ /** One JSON object as the API document holds it. */
35
+ export type JsonObject = Record<string, unknown>;
36
+ /**
37
+ * The configurations endpoint of one tunnel. The account and tunnel ids are
38
+ * opaque path segments, so they are encoded rather than interpolated raw.
39
+ * @param accountId - the Cloudflare account id.
40
+ * @param tunnelId - the named tunnel id (never the tunnel's token).
41
+ * @returns the absolute API URL the sync reads and writes.
42
+ */
43
+ export declare function ingressConfigUrl(accountId: string, tunnelId: string): string;
44
+ /**
45
+ * The lower-cased hostname a configured public base names. Wildcards are not
46
+ * supported by this sync (the target rule must match one exact hostname), so a
47
+ * wildcard base is refused here rather than matched loosely later.
48
+ * @param publicBaseUrl - the configured public base URL.
49
+ * @returns the hostname to match in the ingress, or undefined when the value
50
+ * is missing, unparsable, not http(s), or a wildcard.
51
+ */
52
+ export declare function hostnameOf(publicBaseUrl: string | undefined): string | undefined;
53
+ /**
54
+ * Retarget one tunnel service string at a port, changing nothing else. The
55
+ * authority's host (including an IPv6 literal and any userinfo), the path, the
56
+ * query, and the fragment are preserved byte for byte, so the plan can prove
57
+ * the only difference in the document is the port digits.
58
+ * @param service - the rule's current service string (e.g. `http://127.0.0.1:3080`).
59
+ * @param port - the port this host's web server actually bound.
60
+ * @returns the retargeted service, or undefined when the value is not an
61
+ * http(s) origin this sync can safely rewrite.
62
+ */
63
+ export declare function serviceWithPort(service: string, port: number): string | undefined;
64
+ /**
65
+ * The authority host of one service string, with IPv6 brackets kept. The port
66
+ * and any userinfo are dropped, so the host can be judged on its own.
67
+ * @param service - the rule's service string.
68
+ * @returns the host, or undefined when the value is not an http(s) origin.
69
+ */
70
+ export declare function serviceHostOf(service: string): string | undefined;
71
+ /**
72
+ * One service string with its userinfo removed, for everything that leaves
73
+ * this process. A tunnel service may carry `user:password@`, and the
74
+ * update-only write has to preserve that byte for byte — but an audit frame, a
75
+ * log line, or the settings card has no business showing a credential the
76
+ * operator put there for the origin.
77
+ * @param service - the rule's service string.
78
+ * @returns the same service with any `userinfo@` dropped.
79
+ */
80
+ export declare function redactServiceUserinfo(service: string): string;
81
+ /**
82
+ * Whether one service authority host is a target this host may own: a loopback
83
+ * literal (any 127.0.0.0/8 address, `::1`, or `localhost`) or one of this
84
+ * machine's own LAN IPv4 addresses. A service pointing anywhere else — another
85
+ * machine, a DNS name, a NAS — is not this plugin's to retarget, and rewriting
86
+ * its port would break whatever service that address actually is.
87
+ * @param host - the authority host of the service (brackets kept for IPv6).
88
+ * @param lanHosts - this machine's LAN IPv4 addresses.
89
+ * @returns whether the service may be retargeted.
90
+ */
91
+ export declare function isRetargetableHost(host: string, lanHosts?: readonly string[]): boolean;
92
+ /**
93
+ * One document with the target rule's `service` string replaced by a marker.
94
+ * Two documents whose stripped forms are identical differ at that one key and
95
+ * nowhere else — which is the invariant the plan must prove before it writes.
96
+ * @param document - the tunnel configuration document.
97
+ * @param ruleIndex - the ingress rule the sync owns.
98
+ * @returns the canonical JSON of the stripped document.
99
+ */
100
+ export declare function withoutTargetService(document: unknown, ruleIndex: number): string;
101
+ /** Why a plan refuses to write. Every code is a zero-write verdict. */
102
+ export type IngressConflictCode = 'hostname' | 'config-shape' | 'ingress-shape' | 'rule-shape' | 'wildcard' | 'missing' | 'duplicate' | 'no-catch-all' | 'service-shape' | 'service-host';
103
+ /** What one document read asks the plan to converge on. */
104
+ export interface IngressTarget {
105
+ /** The lower-cased public hostname of the named tunnel. */
106
+ hostname: string;
107
+ /** The port this host's web server actually bound. */
108
+ port: number;
109
+ /**
110
+ * The LAN IPv4 addresses this machine owns (`lanIPv4Addresses()`). The plan
111
+ * refuses a service that points anywhere but loopback or one of these, so an
112
+ * update-only write can never retarget a service that belongs to another host.
113
+ */
114
+ allowedHosts?: readonly string[];
115
+ }
116
+ /** The verdict of one plan: read-only, one write, or a refusal. */
117
+ export type IngressPlan = {
118
+ outcome: 'unchanged';
119
+ hostname: string;
120
+ ruleIndex: number;
121
+ ruleCount: number;
122
+ service: string;
123
+ } | {
124
+ outcome: 'apply';
125
+ hostname: string;
126
+ ruleIndex: number;
127
+ ruleCount: number;
128
+ from: string;
129
+ to: string;
130
+ /** The full document to PUT: identical to the read document except one service. */
131
+ document: JsonObject;
132
+ } | {
133
+ outcome: 'conflict';
134
+ code: IngressConflictCode;
135
+ hostname: string;
136
+ detail: string;
137
+ };
138
+ /**
139
+ * Plan one update-only ingress change against a document read from the API.
140
+ *
141
+ * The supported shape is a Cloudflare tunnel configuration: an object whose
142
+ * `ingress` is an array of rule objects, exactly one of which carries the
143
+ * target hostname, and whose last rule is a catch-all (no `hostname`). A
144
+ * document outside that shape — a missing `ingress`, a wildcard anywhere, a
145
+ * duplicated hostname, no catch-all, a rule that is not an object, a service
146
+ * that is not an http(s) origin, or a service whose host is not this machine —
147
+ * is a conflict and produces no document, so the caller cannot accidentally
148
+ * write. The two service strings the verdict reports are redacted of userinfo:
149
+ * they are audit facts, and the write itself is carried by the document.
150
+ * @param config - the `result.config` document read from the API.
151
+ * @param target - the hostname, the port, and this machine's own addresses.
152
+ * @returns unchanged (one read is enough), apply (the document to PUT), or a
153
+ * conflict with the reason.
154
+ */
155
+ export declare function planIngressUpdate(config: unknown, target: IngressTarget): IngressPlan;
156
+ /**
157
+ * Read the tunnel configuration out of one API envelope. The API answers
158
+ * `{ success, result: { config: {...} }, errors }`; a body that shapes
159
+ * differently is reported, never guessed at.
160
+ * @param body - the parsed response body.
161
+ * @returns the config document, or undefined when the envelope carries none.
162
+ */
163
+ export declare function configOfEnvelope(body: unknown): JsonObject | undefined;
164
+ /**
165
+ * The API's own error messages from one envelope, joined for the audit frame.
166
+ * Cloudflare answers 4xx/5xx with a populated `errors` array; the messages are
167
+ * safe to show (they name the credential's missing permission, never its value).
168
+ * @param body - the parsed response body.
169
+ * @returns a short joined summary, or undefined when the envelope carries none.
170
+ */
171
+ export declare function errorsOfEnvelope(body: unknown): string | undefined;
172
+ /** One HTTP call the sync makes. */
173
+ export interface IngressHttpRequest {
174
+ method: 'GET' | 'PUT';
175
+ url: string;
176
+ headers: Record<string, string>;
177
+ body?: string;
178
+ }
179
+ /** One HTTP answer the sync reads. */
180
+ export interface IngressHttpResponse {
181
+ status: number;
182
+ /** The parsed JSON body (or the raw text when it was not JSON). */
183
+ body: unknown;
184
+ }
185
+ /** The injected HTTP seam: tests never reach a real API through it. */
186
+ export type IngressFetch = (request: IngressHttpRequest) => Promise<IngressHttpResponse>;
187
+ /**
188
+ * The default seam: one `fetch` round trip, and a hard refusal to send the
189
+ * account token outside the Cloudflare API even if a caller builds such a URL.
190
+ * @param request - the call to make.
191
+ * @returns the status and parsed body.
192
+ */
193
+ export declare const defaultIngressFetch: IngressFetch;
194
+ /** Timer seam (injected in tests; a synchronous double makes a run immediate). */
195
+ export interface IngressTimer {
196
+ setTimeout(fn: () => void, ms: number): unknown;
197
+ clearTimeout(handle: unknown): void;
198
+ }
199
+ /** The observable state one sync reports. */
200
+ export type IngressSyncState = 'off' | 'waiting' | 'applied' | 'failed' | 'conflict';
201
+ /**
202
+ * One audit frame. It carries the public hostname, the local port, the two
203
+ * service strings, the rule position, and a failure reason — never the token,
204
+ * never the credential's account id, and never a response body.
205
+ */
206
+ export interface IngressSyncFrame {
207
+ state: IngressSyncState;
208
+ /** The public hostname read from `publicBaseUrl`. */
209
+ hostname?: string;
210
+ /** The port this host's web server actually bound. */
211
+ port?: number;
212
+ /** The service string the remote rule holds after the last run. */
213
+ service?: string;
214
+ /** The service string an apply moved away from. */
215
+ previousService?: string;
216
+ /** How many ingress rules the read document carried. */
217
+ ruleCount?: number;
218
+ /** Index of the rule this sync owns. */
219
+ ruleIndex?: number;
220
+ /** Whether the last run actually wrote the document. */
221
+ changed?: boolean;
222
+ /** Machine-readable reason of a failed or conflict frame. */
223
+ code?: string;
224
+ /** Human-readable detail (hostname, port, status, API message) — no secret. */
225
+ detail?: string;
226
+ /** When this state was reached (ms epoch). */
227
+ at: number;
228
+ }
229
+ /** What one {@link IngressSync.schedule} call asks the runner to converge on. */
230
+ export interface IngressSyncInput {
231
+ /** The master switch AND the ingress toggle; false makes no request at all. */
232
+ enabled: boolean;
233
+ /** The lower-cased public hostname (`hostnameOf(publicBaseUrl)`). */
234
+ hostname: string | undefined;
235
+ /** The bound web server port; undefined before the listener binds. */
236
+ port: number | undefined;
237
+ /** Account-level API token (secret; only ever in the Authorization header). */
238
+ token: string | undefined;
239
+ /** Cloudflare account id. */
240
+ accountId: string | undefined;
241
+ /** Named tunnel id. */
242
+ tunnelId: string | undefined;
243
+ }
244
+ /** Injectable seams and timings. */
245
+ export interface IngressSyncOptions {
246
+ /** HTTP seam; defaults to the guarded real `fetch`. */
247
+ fetch?: IngressFetch;
248
+ /** Clock (injected in tests). */
249
+ now?: () => number;
250
+ /** Short debounce before a scheduled run starts (ms). */
251
+ debounceMs?: number;
252
+ /** First backoff step of a transient failure (ms; doubles per retry). */
253
+ retryBaseMs?: number;
254
+ /** Retries after the first attempt; bounded for the whole run. */
255
+ maxRetries?: number;
256
+ /** Timer seam (injected in tests). */
257
+ timer?: IngressTimer;
258
+ /**
259
+ * This machine's LAN IPv4 addresses (injected in tests; the live list by
260
+ * default). The plan refuses to retarget a service hosted anywhere else.
261
+ */
262
+ lanHosts?: () => readonly string[];
263
+ /** Frame sink (the host writes it into the pairing snapshot). */
264
+ onFrame?: (frame: IngressSyncFrame) => void;
265
+ }
266
+ /** Debounce before a scheduled run: a port settle and a settings save coalesce. */
267
+ export declare const DEFAULT_INGRESS_DEBOUNCE_MS = 300;
268
+ /** First backoff step of a retried request. */
269
+ export declare const DEFAULT_INGRESS_RETRY_BASE_MS = 500;
270
+ /** Retries after the first attempt of one request (so three attempts at most). */
271
+ export declare const DEFAULT_INGRESS_MAX_RETRIES = 2;
272
+ /**
273
+ * Two audit frames that describe the same state (the timestamp is not part of
274
+ * it). The pairing snapshot dedupe uses this, so a re-read that proves the same
275
+ * convergence never emits a second event.
276
+ * @param a - one frame.
277
+ * @param b - the other frame.
278
+ * @returns whether both describe the same state.
279
+ */
280
+ export declare function sameIngressFrame(a: IngressSyncFrame, b: IngressSyncFrame): boolean;
281
+ /** The resolved plugin settings this adapter reads (nothing else is consulted). */
282
+ export interface IngressSettingsSlice {
283
+ /** The plugin master switch. */
284
+ enabled: boolean;
285
+ /** The ingress-sync switch. */
286
+ ingressSync?: boolean;
287
+ /** Whether the quick tunnel runs (it owns the public base and mints its own hostname). */
288
+ autoTunnel?: boolean;
289
+ /** The fixed public hostname of the named tunnel. */
290
+ publicBaseUrl?: string;
291
+ /** Account-level API token (a settings secret). */
292
+ cfApiToken?: string;
293
+ /** Cloudflare account id. */
294
+ cfAccountId?: string;
295
+ /** Named tunnel id. */
296
+ cfTunnelId?: string;
297
+ }
298
+ /**
299
+ * Normalize one web server port into a target or into nothing. A recompose pass
300
+ * can hand the plugin a half-read webServer whose port is `NaN` or
301
+ * `Infinity`; that is not a port, and treating it as one would either mint a
302
+ * `127.0.0.1:NaN` target or leave the caller waiting forever. The host reads
303
+ * the port through this, so a non-finite value takes exactly the path an
304
+ * unbound one does: the existing port-settle retry re-plans it.
305
+ * @param port - the live `ctx.webServer.port`.
306
+ * @returns the port, or undefined when it is not a finite number.
307
+ */
308
+ export declare function livePortOf(port: number | undefined): number | undefined;
309
+ /**
310
+ * The one place resolved settings become a sync input, so "which switch turns
311
+ * this on" is testable without a cordis context. Three conditions, all
312
+ * required: the master switch, the ingress toggle, and the quick tunnel being
313
+ * off — a quick tunnel mints a new hostname per start and owns the public base,
314
+ * so that deployment has no named tunnel to update and the sync reads as off
315
+ * instead of editing an unrelated tunnel.
316
+ * @param value - the resolved settings.
317
+ * @param port - the bound web server port, undefined before the listener binds.
318
+ * @returns the input to schedule.
319
+ */
320
+ export declare function ingressInputOf(value: IngressSettingsSlice, port: number | undefined): IngressSyncInput;
321
+ /**
322
+ * Converge one named tunnel's ingress service onto this host's actual port.
323
+ *
324
+ * Scheduling is single-flight and generation-guarded: a later schedule (a
325
+ * settings save, a port settle) supersedes an in-flight run at its next
326
+ * checkpoint — before every request attempt, so a running backoff cannot
327
+ * outlive its target, and again immediately before the single PUT. Ports
328
+ * arrive late, and a half-read webServer can hand a non-finite number: neither
329
+ * one schedules anything, so no request is made and the caller's existing
330
+ * port-settle re-plan delivers the real port.
331
+ */
332
+ export declare class IngressSync {
333
+ private readonly http;
334
+ private readonly now;
335
+ private readonly debounceMs;
336
+ private readonly retryBaseMs;
337
+ private readonly maxRetries;
338
+ private readonly timer;
339
+ private readonly onFrame;
340
+ private readonly lanHosts;
341
+ private current;
342
+ private input;
343
+ private generation;
344
+ private pending;
345
+ private debounce;
346
+ /** The serialized run chain: one run at a time, queued schedules after it. */
347
+ private chain;
348
+ /** Whether the chain currently holds a live run (a later schedule only queues). */
349
+ private running;
350
+ /** Whether the sink has been told about a state yet (the first one always goes out). */
351
+ private emitted;
352
+ /** The `hostname|port|account|tunnel|token-digest` a read last proved converged. */
353
+ private settledKey;
354
+ /** Backoff waits in flight, so a dispose can cut them short. */
355
+ private readonly waits;
356
+ private disposed;
357
+ /**
358
+ * @param options - seams, timings, and the frame sink.
359
+ */
360
+ constructor(options?: IngressSyncOptions);
361
+ /** The latest audit frame. */
362
+ get frame(): IngressSyncFrame;
363
+ /**
364
+ * Ask the sync to converge on one input. Safe to call on every settings save
365
+ * and every port settle: an already-converged target is a no-op, and an
366
+ * unchanged schedule does not repeat its frame.
367
+ * @param input - the switch, the target, and the credentials to use.
368
+ */
369
+ schedule(input: IngressSyncInput): void;
370
+ /**
371
+ * Await every run scheduled so far. A test seam: the host never awaits it,
372
+ * and a synchronous timer double makes the whole chain settle immediately.
373
+ * @returns the settled run chain.
374
+ */
375
+ settled(): Promise<void>;
376
+ /** Release the timers; an in-flight run finishes its checkpoints as no-ops. */
377
+ dispose(): void;
378
+ /**
379
+ * Drop every claim this sync holds on a target: a newer generation (so an
380
+ * in-flight run cannot write, settle, or report an applied frame), no queued
381
+ * run, no converged identity, no pending debounce. Every branch of
382
+ * {@link schedule} that ends up without a target calls this, so "there is
383
+ * nothing to converge on" and "the switch is off" are one rule rather than
384
+ * four that can drift apart.
385
+ */
386
+ private invalidate;
387
+ private armDebounce;
388
+ private cancelDebounce;
389
+ /**
390
+ * Append one run to the serialized chain. The `running` flag is raised before
391
+ * the link starts, so a schedule that lands while a run is in flight only
392
+ * raises `pending` and the live loop picks it up with the newest generation
393
+ * instead of stacking a second link that would read again after it settled.
394
+ */
395
+ private pump;
396
+ /** One converge attempt toward the current target, superseded by any newer schedule. */
397
+ private run;
398
+ /** Record the converged target identity and publish the frame that says so. */
399
+ private converged;
400
+ /**
401
+ * Publish one frame, dropping a repeat that describes the same state. The
402
+ * first publish always goes out, so a sink that starts from nothing learns
403
+ * the current state even when it is the initial `off`.
404
+ */
405
+ private publish;
406
+ /**
407
+ * One request with the bounded retry policy: 429, 5xx, and a network failure
408
+ * are retried at most {@link DEFAULT_INGRESS_MAX_RETRIES} times; 401/403 and
409
+ * any other 4xx are terminal, because repeating them cannot help.
410
+ *
411
+ * The run's generation is part of the request: it is checked before every
412
+ * attempt, retries included, so a target superseded while a 429/5xx/network
413
+ * backoff was running stops there instead of writing a document for a port
414
+ * this host no longer serves. The answer is `stale` (or `disposed`) and the
415
+ * caller drops it without publishing.
416
+ */
417
+ private send;
418
+ /**
419
+ * Wait one backoff step on the injected timer. The wait is tracked so a
420
+ * dispose can release it at once: the interrupted run then wakes, sees the
421
+ * dispose at its next attempt check, and stops instead of sleeping out a
422
+ * backoff nobody is waiting for.
423
+ * @param ms - how long the step lasts.
424
+ * @returns a promise that settles when the step ends or is cancelled.
425
+ */
426
+ private wait;
427
+ /** Release every pending backoff wait (dispose); the interrupted run aborts. */
428
+ private cancelWaits;
429
+ }
@@ -0,0 +1,96 @@
1
+ /**
2
+ * Pure decision helpers shared by the lan-bind sync path (index.ts) and the
3
+ * status path (lanBindStatus). Extracted so the LAN-edge plan and the firewall
4
+ * re-apply gate have one implementation and direct unit coverage.
5
+ *
6
+ * Since issue #1917 the plugin owns no bind: the host stays on loopback and LAN
7
+ * reachability comes from the forwarder in lan-forward.ts, so the only decision
8
+ * left here is which LAN address (if any) that forwarder may listen on.
9
+ */
10
+ /** webStartup facts the CLI hands the process (absent unless a flag was passed). */
11
+ export interface StartupFacts {
12
+ host?: string;
13
+ port?: number;
14
+ }
15
+ /**
16
+ * What the running host accepts, and which concrete addresses it could pin.
17
+ * Sampled once per boot: the webserver package cannot change under a running
18
+ * process, and the interface list is frozen for the same reason the QR bases
19
+ * are (see src/lan.ts).
20
+ */
21
+ export interface LanBindCapability {
22
+ /** Whether the installed webserver schema accepts the wildcard bind (reported, never written). */
23
+ wildcardSupported: boolean;
24
+ /** Local non-loopback IPv4 literals, in preference order (`lanIPv4Addresses()`). */
25
+ candidates: string[];
26
+ }
27
+ /** Why the LAN forwarder is not listening, when it is not. */
28
+ export type ForwardStopReason =
29
+ /** The toggle is off (or was never flipped). */
30
+ 'off'
31
+ /** The host itself binds a non-loopback address (`--host`): the forwarder yields the port. */
32
+ | 'host-bind'
33
+ /** The toggle is on but this machine has no LAN IPv4 address to listen on. */
34
+ | 'no-address';
35
+ /** The LAN edge one sync should run: an address to listen on, or why not. */
36
+ export interface ForwardPlan {
37
+ /** The LAN address the forwarder listens on; undefined when it must stay off. */
38
+ host: string | undefined;
39
+ /** Why it stays off; undefined when a host is planned. */
40
+ reason: ForwardStopReason | undefined;
41
+ }
42
+ /**
43
+ * The LAN address the forwarder may listen on, or the reason it must not.
44
+ *
45
+ * The order is: the toggle off keeps LAN off; a non-loopback `--host` means the
46
+ * operator asked the HOST to bind the network itself, so the forwarder yields
47
+ * the port instead of fighting it for the same address:port (issue #1917);
48
+ * otherwise this machine's first LAN candidate. A host with no LAN IPv4 address
49
+ * has nothing to listen on, which the caller reports instead of failing.
50
+ *
51
+ * Only a concrete interface address is ever returned: the wildcard would
52
+ * collide with the loopback listener on the same port, and a forwarder bound to
53
+ * `0.0.0.0` would additionally answer the loopback port a second time.
54
+ * @param lanBind - the toggle value.
55
+ * @param startupHost - the CLI-provided bind host, when given.
56
+ * @param capability - the sampled interface candidates.
57
+ * @returns the address to listen on, or the reason there is none.
58
+ */
59
+ export declare function forwardPlanOf(lanBind: boolean | undefined, startupHost: string | undefined, capability: LanBindCapability): ForwardPlan;
60
+ /**
61
+ * The reachability decision behind the QR bases, the status `lanUrls`, the
62
+ * firewall summary and the boot's LAN warning: a bind that is not loopback is
63
+ * reachable from another device, whatever literal it names. Equality with the
64
+ * wildcard is deliberately not the test - on a host that refuses wildcard
65
+ * binds the only LAN bind is a concrete interface address.
66
+ * @param host - the block host or the live bind (absent before it settles).
67
+ * @returns true when another device could dial the listener.
68
+ */
69
+ export declare function isLanReachableBind(host: string | undefined): boolean;
70
+ /** The last firewall state this process applied. */
71
+ export interface AppliedFirewallState {
72
+ enabled: boolean;
73
+ port: number;
74
+ }
75
+ /** The managed profile when neither the config nor the runtime names one. */
76
+ export declare const DEFAULT_MANAGED_PROFILE = "web";
77
+ /**
78
+ * The profile whose `cordis.patch.yml` the LAN bind toggle manages. An
79
+ * explicit `profile` config wins; otherwise the launched profile the Host
80
+ * publishes is authoritative, because the DSH Desktop client boots the
81
+ * `desktop` profile without exporting `DSH_PROFILE` — an environment-only
82
+ * fallback edited `profiles/web` there, so the toggle looked applied while
83
+ * the running host never followed the block.
84
+ * @param configured - the explicit `profile` plugin config, when set.
85
+ * @param launched - the launched profile name the Host published, when available.
86
+ * @param env - the DSH_PROFILE environment value, when set.
87
+ */
88
+ export declare function resolveManagedProfile(configured: string | undefined, launched: string | undefined, env: string | undefined): string;
89
+ /**
90
+ * Whether the firewall rule must be (re)applied: only when the toggle or the
91
+ * bound port moved since the last application. Keeps unrelated settings
92
+ * saves from spawning netsh/firewall-cmd delete+add churn.
93
+ * @param previous - the state applied earlier by this process, if any.
94
+ * @param next - the state the settings now ask for, if derivable.
95
+ */
96
+ export declare function firewallActionNeeded(previous: AppliedFirewallState | undefined, next: AppliedFirewallState | undefined): boolean;
@@ -0,0 +1,106 @@
1
+ /**
2
+ * The lan-bind block this plugin used to write, and its removal (issue #1917).
3
+ *
4
+ * Up to 0.4.8 the toggle pinned the LAN address into the profile's
5
+ * `cordis.patch.yml` as a managed `- id: webserver` row. That approach is
6
+ * retired for two measured reasons:
7
+ *
8
+ * 1. A same-id patch row REPLACES the whole row config, so the block silently
9
+ * swallowed the official row's own bind expression; the CLI `--host` /
10
+ * `--port` never reached the running row, and the machine's port came from
11
+ * a literal this plugin guessed.
12
+ * 2. The profile patch is exactly what the Host's HMR config watcher refreshes
13
+ * from, so writing it recomposed the running process and rebound the live
14
+ * listener - the "takes effect on the next start" premise was false, and a
15
+ * write made mid-session moved the bind under the running host.
16
+ *
17
+ * LAN reachability now comes from a runtime forwarder instead (lan-forward.ts),
18
+ * so the host always listens on loopback and this module's only remaining job
19
+ * is to remove the block older versions left behind. The removal is
20
+ * strip-only: no write appends anything, so the official webserver row (and
21
+ * every other row) stays byte-identical. The block discipline (markers, atomic
22
+ * write) follows the dsh-LAN reference implementation (MIT).
23
+ */
24
+ export declare const LAN_BIND_BLOCK_BEGIN = "# --- remote-web-ui lan-bind block (managed - do not edit) ---";
25
+ export declare const LAN_BIND_BLOCK_END = "# --- end remote-web-ui lan-bind block ---";
26
+ /**
27
+ * A bind host the managed block may pin. `'0.0.0.0'` only on a host whose
28
+ * webserver schema still accepts the wildcard, `'127.0.0.1'` for the toggle
29
+ * off, and otherwise one concrete interface address of this machine (see
30
+ * lan-bind-plan.ts / host-bind-capability.ts, issue #1893).
31
+ */
32
+ export type LanBindHost = string;
33
+ /**
34
+ * The absolute path of the profile patch file this toggle manages. The value
35
+ * is config-controlled (and the runtime/environment fallbacks bypass schema
36
+ * validation entirely), so the path is guarded twice: the profile must be a
37
+ * single safe path segment, and the resolved file must stay under the
38
+ * profiles directory.
39
+ */
40
+ export declare function profilePatchFile(profile: string, home?: string): string;
41
+ /**
42
+ * Remove the managed block (its markers and the webserver row this plugin
43
+ * wrote) from patch content, keeping every other row.
44
+ *
45
+ * The markers travel with the file through writers that append rows through
46
+ * the YAML document API: the appended rows land at the end of the sequence,
47
+ * which is BEFORE a trailing END comment, so the region between the markers
48
+ * can hold rows this plugin never wrote. Deleting the whole region silently
49
+ * removed the user's model, plugin and remote-control rows on the next rewrite
50
+ * - the remote-control row carries the LAN toggle itself, so the feature reset
51
+ * to `undefined` and the phone was locked out again after a restart (DSH
52
+ * Desktop report, 2026-09-30). Only the managed item is removed here; every
53
+ * foreign row is kept in place and the fresh block is appended after them (the
54
+ * same-id row last wins).
55
+ * @param content - the patch file text.
56
+ * @returns the text with the markers and the managed row removed.
57
+ */
58
+ export declare function stripManagedBlock(content: string): string;
59
+ /** The bind state a managed block pinned (undefined when there is no block). */
60
+ export interface ManagedBindState {
61
+ host: LanBindHost | (string & {});
62
+ port: number | undefined;
63
+ }
64
+ /**
65
+ * Parse the block's pinned bind out of patch content. A block whose values
66
+ * were hand-edited reports the literals as-is so the card can surface them
67
+ * instead of silently claiming one of the two known states.
68
+ */
69
+ export declare function managedBindOf(content: string): ManagedBindState | undefined;
70
+ /** File-level state of a patch file a previous version wrote to. */
71
+ export declare function lanBindState(profile: string, home?: string): {
72
+ blockPresent: boolean;
73
+ host?: string;
74
+ port?: number;
75
+ };
76
+ /** What one legacy-block cleanup did, for the settings card and the log. */
77
+ export interface LegacyBlockCleanup {
78
+ /** Whether the file actually carried the managed block. */
79
+ removed: boolean;
80
+ /** The bind the removed block pinned, when it could be read. */
81
+ host: string | undefined;
82
+ /** The port the removed block pinned, when it could be read. */
83
+ port: number | undefined;
84
+ }
85
+ /**
86
+ * Remove the managed block a previous version wrote, and nothing else.
87
+ *
88
+ * The file is rewritten atomically (unique temp file + rename, so a concurrent
89
+ * reader never sees a half-written patch) with its original permissions
90
+ * preserved, and only when the block is actually there: a second run finds no
91
+ * markers, writes nothing, and leaves the bytes identical, which is what makes
92
+ * the plugin's once-per-boot call idempotent.
93
+ *
94
+ * The official `- id: webserver` row every profile carries is a FOREIGN row -
95
+ * the strip keeps it verbatim (see stripManagedBlock). Nothing is appended, so
96
+ * the profile keeps the row's own bind expression and the CLI flags keep
97
+ * reaching it.
98
+ *
99
+ * A failure (an unreadable file, a read-only profile) is NOT swallowed here:
100
+ * callers report it, because a silent failure leaves a block that keeps
101
+ * swallowing the bind on every later start.
102
+ * @param profile - the managed profile name.
103
+ * @param home - DSH home (test seam).
104
+ * @returns what was removed; `removed: false` when there was nothing to do.
105
+ */
106
+ export declare function removeManagedBlock(profile: string, home?: string): LegacyBlockCleanup;