@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.
- package/README.i18n.yaml +2 -2
- package/README.md +45 -8
- package/README.zh.md +45 -8
- package/lib/client.js +1 -1
- package/lib/client.js.map +1 -1
- package/lib/index.js +18031 -373
- package/lib/types/core/connection.d.ts +9 -0
- package/lib/types/core/devices.d.ts +27 -6
- package/lib/types/core/liveactivity.d.ts +7 -0
- package/lib/types/core/model-efforts.d.ts +83 -0
- package/lib/types/core/protocol.d.ts +2 -0
- package/lib/types/core/push.d.ts +51 -2
- package/lib/types/host/firewall.d.ts +74 -0
- package/lib/types/host/host-bind-capability.d.ts +90 -0
- package/lib/types/host/ingress-gate.d.ts +59 -0
- package/lib/types/host/ingress-sync.d.ts +429 -0
- package/lib/types/host/lan-bind-plan.d.ts +96 -0
- package/lib/types/host/lan-bind.d.ts +106 -0
- package/lib/types/host/lan-forward.d.ts +112 -0
- package/lib/types/host/lan.d.ts +61 -0
- package/lib/types/host/pair-origin.d.ts +34 -19
- package/lib/types/host/relay-bridge-connector.d.ts +215 -0
- package/lib/types/host/relay-connector.d.ts +152 -0
- package/lib/types/host/relay-control.d.ts +124 -0
- package/lib/types/host/relay-registry.d.ts +114 -0
- package/lib/types/host/relay.d.ts +170 -0
- package/lib/types/host/routes.d.ts +24 -1
- package/lib/types/host/tunnel-orphan.d.ts +145 -0
- package/lib/types/host/tunnel-plan.d.ts +41 -0
- package/lib/types/host/tunnel.d.ts +289 -0
- package/lib/types/index.d.ts +426 -1
- package/package.json +4 -2
- package/src/client/panel.module.css +16 -1
- package/src/core/connection.ts +206 -11
- package/src/core/devices.ts +31 -6
- package/src/core/hub.ts +17 -5
- package/src/core/liveactivity.ts +16 -2
- package/src/core/model-efforts.ts +223 -0
- package/src/core/protocol.ts +3 -0
- package/src/core/push.ts +70 -4
- package/src/host/firewall.ts +230 -0
- package/src/host/host-bind-capability.ts +249 -0
- package/src/host/ingress-gate.ts +203 -0
- package/src/host/ingress-sync.ts +1074 -0
- package/src/host/lan-bind-plan.ts +134 -0
- package/src/host/lan-bind.ts +250 -0
- package/src/host/lan-forward.ts +321 -0
- package/src/host/lan.ts +142 -0
- package/src/host/pair-origin.ts +44 -38
- package/src/host/relay-bridge-connector.ts +783 -0
- package/src/host/relay-connector.ts +317 -0
- package/src/host/relay-control.ts +224 -0
- package/src/host/relay-registry.ts +263 -0
- package/src/host/relay.ts +294 -0
- package/src/host/routes.ts +47 -3
- package/src/host/tunnel-orphan.ts +325 -0
- package/src/host/tunnel-plan.ts +58 -0
- package/src/host/tunnel.ts +688 -0
- package/src/index.ts +1280 -22
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Auto-tunnel manager: spawns a Cloudflare tunnel through the `cloudflared`
|
|
3
|
+
* npm package — its postinstall downloads the platform binary and the
|
|
4
|
+
* readiness policy below validates it and re-fetches when it cannot run (the
|
|
5
|
+
* desktop payload stages one tree for every shipped OS/arch), so no
|
|
6
|
+
* user-side tooling is involved — surfaces the public URL, and restarts the
|
|
7
|
+
* process after unexpected exits with exponential backoff. Two modes: the
|
|
8
|
+
* accountless quick tunnel (`https://xxx.trycloudflare.com`, hostname minted
|
|
9
|
+
* per start) and an account named tunnel (`cloudflared tunnel run --token`,
|
|
10
|
+
* fixed public hostname — the mode that lets a paired phone keep its
|
|
11
|
+
* bookmark and pairing cookie across restarts).
|
|
12
|
+
*
|
|
13
|
+
* The cloudflared package's Tunnel is a thin spawn wrapper; this manager
|
|
14
|
+
* owns the lifecycle policy (binary readiness, URL timeout, restart
|
|
15
|
+
* backoff, and the public-URL readiness watchdog) around it. All seams — the
|
|
16
|
+
* tunnel factory, binary readiness, the URL probe, timers — are injectable
|
|
17
|
+
* so the whole lifecycle is unit-testable without a real binary or network.
|
|
18
|
+
*
|
|
19
|
+
* 'running' is not a terminal state: the connector can lose its edge
|
|
20
|
+
* registration while the process keeps living, which leaves a hostname that
|
|
21
|
+
* no longer resolves. The watchdog below probes the public URL of a running
|
|
22
|
+
* tunnel and reuses the crash-restart path when it stops answering, so the
|
|
23
|
+
* relay is re-registered against a freshly minted URL instead of proxying a
|
|
24
|
+
* dead one (issue #1723).
|
|
25
|
+
*
|
|
26
|
+
* A host crash is the one exit the manager cannot cover in-process: the child
|
|
27
|
+
* is reparented and keeps running, holding a hostname nobody serves. Every
|
|
28
|
+
* spawn is therefore recorded under the DSH home (pid plus its exact command
|
|
29
|
+
* line) and the record is dropped when that child is stopped, so the next
|
|
30
|
+
* start reaps exactly the child a dead host left behind — see
|
|
31
|
+
* ./tunnel-orphan.ts.
|
|
32
|
+
*/
|
|
33
|
+
import { type TunnelChildGuard, type TunnelChildProcess } from './tunnel-orphan.ts';
|
|
34
|
+
/** The observable tunnel lifecycle the settings/panel surfaces render. */
|
|
35
|
+
export type TunnelPhase = 'stopped' | 'starting' | 'running' | 'failed';
|
|
36
|
+
/**
|
|
37
|
+
* What the manager should run. `quick` is the accountless trycloudflare
|
|
38
|
+
* tunnel whose hostname is minted per start (and dies with the process);
|
|
39
|
+
* `named` is an account tunnel whose public hostname is fixed — the point
|
|
40
|
+
* of the named mode is that a paired phone keeps its bookmark and its
|
|
41
|
+
* pairing cookie across restarts.
|
|
42
|
+
*/
|
|
43
|
+
export type TunnelTarget = {
|
|
44
|
+
kind: 'quick';
|
|
45
|
+
targetUrl: string;
|
|
46
|
+
originHostHeader?: string;
|
|
47
|
+
} | {
|
|
48
|
+
kind: 'named';
|
|
49
|
+
token: string;
|
|
50
|
+
publicUrl: string;
|
|
51
|
+
};
|
|
52
|
+
/** The event face the named adapter wraps (a cloudflared package Tunnel). */
|
|
53
|
+
interface NamedTunnelProcess {
|
|
54
|
+
on(event: 'connected', listener: () => void): unknown;
|
|
55
|
+
on(event: 'exit', listener: (code: number | null, signal: NodeJS.Signals | null) => void): unknown;
|
|
56
|
+
on(event: 'error', listener: (value: Error) => void): unknown;
|
|
57
|
+
off(event: string, listener: (...args: any[]) => void): unknown;
|
|
58
|
+
stop(): boolean;
|
|
59
|
+
/** The spawned OS process, when the wrapper exposes it. */
|
|
60
|
+
readonly process?: TunnelChildProcess;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Wrap a named-tunnel process so it fits the quick-tunnel handle shape: the
|
|
64
|
+
* fixed public URL is reported through the same `url` event once the first
|
|
65
|
+
* edge connection registers (cloudflared fires `connected` per connection,
|
|
66
|
+
* so only the first is taken), and exit/error pass through. The manager's
|
|
67
|
+
* URL timeout, crash-restart backoff, and stop semantics then stay fully
|
|
68
|
+
* mode-agnostic.
|
|
69
|
+
* @param inner - the running named-tunnel process.
|
|
70
|
+
* @param publicUrl - the fixed public hostname ingress maps to this server.
|
|
71
|
+
* @returns a handle emitting `url` once the tunnel is reachable.
|
|
72
|
+
*/
|
|
73
|
+
export declare function namedTunnelHandle(inner: NamedTunnelProcess, publicUrl: string): TunnelHandle;
|
|
74
|
+
/** One tunnel status frame. */
|
|
75
|
+
export interface TunnelInfo {
|
|
76
|
+
phase: TunnelPhase;
|
|
77
|
+
/** The minted public URL, once the tunnel reports it. */
|
|
78
|
+
url?: string;
|
|
79
|
+
/** Human-readable failure detail (binary install, URL timeout, spawn error). */
|
|
80
|
+
error?: string;
|
|
81
|
+
}
|
|
82
|
+
/** The tunnel handle subset this manager uses (the package's Tunnel fits). */
|
|
83
|
+
export interface TunnelHandle {
|
|
84
|
+
on(event: string, listener: (...args: any[]) => void): unknown;
|
|
85
|
+
off(event: string, listener: (...args: any[]) => void): unknown;
|
|
86
|
+
stop(): boolean;
|
|
87
|
+
/**
|
|
88
|
+
* The spawned OS process. Crash recovery records its pid and exact command
|
|
89
|
+
* line, so a host that dies without stopping it can be reaped by the next
|
|
90
|
+
* start; a handle that does not expose one is simply never recorded.
|
|
91
|
+
*/
|
|
92
|
+
process?: TunnelChildProcess;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Probe one public tunnel URL from the host. Resolves true when the URL is
|
|
96
|
+
* served (any HTTP answer below 500 — a harness without credentials answers
|
|
97
|
+
* 401/403, which is a healthy round trip), false for a DNS failure, a refused
|
|
98
|
+
* connection, a timeout, or a Cloudflare 5xx (530 "Origin DNS error" / 1033)
|
|
99
|
+
* — the shapes a tunnel that lost its edge registration produces.
|
|
100
|
+
*
|
|
101
|
+
* The probe must never reject: the watchdog reads a rejection as a dead
|
|
102
|
+
* tunnel and restarts on it, so a local programming error would otherwise
|
|
103
|
+
* recycle working tunnels forever.
|
|
104
|
+
* @param url - the tunnel's public URL.
|
|
105
|
+
* @param timeoutMs - how long one probe may take.
|
|
106
|
+
* @returns whether the URL answered as a live origin.
|
|
107
|
+
*/
|
|
108
|
+
export declare function probeTunnelUrl(url: string, timeoutMs: number): Promise<boolean>;
|
|
109
|
+
/** Injectable seams (defaults are the real cloudflared package + node timers). */
|
|
110
|
+
export interface TunnelManagerOptions {
|
|
111
|
+
/** Spawn one tunnel process for the target (quick or named). */
|
|
112
|
+
factory?: (target: TunnelTarget) => TunnelHandle;
|
|
113
|
+
/** Make sure the cloudflared binary exists, downloading it when absent. */
|
|
114
|
+
ensureBinary?: () => Promise<void>;
|
|
115
|
+
/** Wait up to this long for the tunnel URL before failing the attempt. */
|
|
116
|
+
urlTimeoutMs?: number;
|
|
117
|
+
/** First restart delay after an unexpected failure (exponential base). */
|
|
118
|
+
restartBaseMs?: number;
|
|
119
|
+
/** Cap on the exponential restart delay. */
|
|
120
|
+
restartMaxMs?: number;
|
|
121
|
+
/** Timer source (injected in tests). */
|
|
122
|
+
timer?: {
|
|
123
|
+
setTimeout(fn: () => void, ms: number): unknown;
|
|
124
|
+
clearTimeout(t: unknown): void;
|
|
125
|
+
};
|
|
126
|
+
/** Probe one public URL; false means the tunnel stopped answering. */
|
|
127
|
+
probe?: (url: string, timeoutMs: number) => Promise<boolean>;
|
|
128
|
+
/** How often to probe the public URL of a running tunnel (0 disables it). */
|
|
129
|
+
healthCheckIntervalMs?: number;
|
|
130
|
+
/** Consecutive probe failures after which the tunnel is restarted. */
|
|
131
|
+
healthCheckFailures?: number;
|
|
132
|
+
/** How long one probe may take. */
|
|
133
|
+
probeTimeoutMs?: number;
|
|
134
|
+
/**
|
|
135
|
+
* Crash recovery for the spawned child: reap the leftover a crashed host
|
|
136
|
+
* recorded, and record this one. Defaults to the file-backed guard under the
|
|
137
|
+
* DSH home (see ./tunnel-orphan.ts).
|
|
138
|
+
*/
|
|
139
|
+
orphan?: TunnelChildGuard;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Probe whether the binary at `path` actually executes on this machine. The
|
|
143
|
+
* desktop payload stages one dependency tree for every shipped OS/arch, so a
|
|
144
|
+
* binary can exist with the wrong architecture (the arm64 darwin
|
|
145
|
+
* `bin/cloudflared` on an x64 mac); existence alone must not skip the
|
|
146
|
+
* reinstall.
|
|
147
|
+
*/
|
|
148
|
+
export declare function binaryRuns(executable: string): Promise<boolean>;
|
|
149
|
+
/** Injectable seams of the default binary readiness (tests swap them all). */
|
|
150
|
+
export interface BinaryReadinessSeams {
|
|
151
|
+
exists?: (path: string) => boolean;
|
|
152
|
+
runs?: (executable: string) => Promise<boolean>;
|
|
153
|
+
install?: (path: string) => Promise<unknown>;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Build the default readiness policy over a binary path: skip when the
|
|
157
|
+
* binary has already proven runnable in this process, validate an existing
|
|
158
|
+
* file before trusting it (a stale or wrong-arch binary must not disable
|
|
159
|
+
* the reinstall), and re-fetch the current platform binary otherwise.
|
|
160
|
+
*/
|
|
161
|
+
export declare function createBinaryReadiness(executable: string, seams?: BinaryReadinessSeams): () => Promise<void>;
|
|
162
|
+
/** How many archive downloads one process may attempt before giving up. */
|
|
163
|
+
export declare const MAX_BINARY_INSTALL_ATTEMPTS = 3;
|
|
164
|
+
/** How often a running tunnel's public URL is probed (issue #1723). */
|
|
165
|
+
export declare const DEFAULT_HEALTH_CHECK_INTERVAL_MS = 60000;
|
|
166
|
+
/** Consecutive failed probes before the tunnel is declared dead. */
|
|
167
|
+
export declare const DEFAULT_HEALTH_CHECK_FAILURES = 2;
|
|
168
|
+
/** How long one public-URL probe may take. */
|
|
169
|
+
export declare const DEFAULT_PROBE_TIMEOUT_MS = 10000;
|
|
170
|
+
/**
|
|
171
|
+
* Spawn flags for one quick target. `originHostHeader` stamps the Host the
|
|
172
|
+
* local webserver sees: the Workers relay cannot control the origin-side Host
|
|
173
|
+
* at all (fetch forces it to the URL authority), so a caller that needs one
|
|
174
|
+
* exact public origin restamps it here. What any trust fence or pairing rule
|
|
175
|
+
* makes of that Host is not this module's responsibility.
|
|
176
|
+
*/
|
|
177
|
+
export declare function quickTunnelFlags(originHostHeader?: string): Record<string, string | boolean>;
|
|
178
|
+
/**
|
|
179
|
+
* Command-line arguments for a named tunnel. The account token is deliberately
|
|
180
|
+
* NOT part of the argv: `--token <t>` would expose an account credential to any
|
|
181
|
+
* local user through ps/proc for the whole tunnel lifetime, while the flag's own
|
|
182
|
+
* environment variable (TUNNEL_TOKEN, see cloudflared's tunnel run flags) is not
|
|
183
|
+
* readable that way. Note: `--no-autoupdate` and `--protocol` are flags for the
|
|
184
|
+
* `tunnel` command and must appear BEFORE the `run` subcommand. Putting them
|
|
185
|
+
* after `run` causes cloudflared to exit immediately with "flag provided but not
|
|
186
|
+
* defined: -no-autoupdate" (issues #1432, #1433).
|
|
187
|
+
*/
|
|
188
|
+
export declare function namedTunnelArgs(): string[];
|
|
189
|
+
/**
|
|
190
|
+
* Run one spawn call with TUNNEL_TOKEN set, restoring the previous value
|
|
191
|
+
* afterwards. The cloudflared package's Tunnel spawns with the inherited
|
|
192
|
+
* environment inside its constructor, so the variable only has to exist for
|
|
193
|
+
* that call; the child keeps its own copy of the environment.
|
|
194
|
+
* @param token - the named-tunnel token.
|
|
195
|
+
* @param spawnTunnel - the call that creates the child process.
|
|
196
|
+
* @returns whatever the spawn call returns.
|
|
197
|
+
*/
|
|
198
|
+
export declare function withTunnelTokenEnv<T>(token: string, spawnTunnel: () => T): T;
|
|
199
|
+
/**
|
|
200
|
+
* Own the lifecycle of one auto-tunnel: start/stop, URL surfacing, and
|
|
201
|
+
* crash-restart backoff.
|
|
202
|
+
*/
|
|
203
|
+
export declare class TunnelManager {
|
|
204
|
+
private readonly factory;
|
|
205
|
+
private readonly ensureBinary;
|
|
206
|
+
private readonly urlTimeoutMs;
|
|
207
|
+
private readonly restartBaseMs;
|
|
208
|
+
private readonly restartMaxMs;
|
|
209
|
+
private readonly timer;
|
|
210
|
+
private readonly probe;
|
|
211
|
+
private readonly healthCheckIntervalMs;
|
|
212
|
+
private readonly healthCheckFailures;
|
|
213
|
+
private readonly probeTimeoutMs;
|
|
214
|
+
private readonly orphan;
|
|
215
|
+
private phase;
|
|
216
|
+
private url;
|
|
217
|
+
private error;
|
|
218
|
+
private target;
|
|
219
|
+
private handle;
|
|
220
|
+
private urlTimer;
|
|
221
|
+
private restartTimer;
|
|
222
|
+
private healthTimer;
|
|
223
|
+
private healthFailures;
|
|
224
|
+
private attempts;
|
|
225
|
+
private recordedChild;
|
|
226
|
+
private generation;
|
|
227
|
+
private stopping;
|
|
228
|
+
private readonly urlListeners;
|
|
229
|
+
private readonly phaseListeners;
|
|
230
|
+
/**
|
|
231
|
+
* @param options - seams; defaults spawn the real quick tunnel.
|
|
232
|
+
*/
|
|
233
|
+
constructor(options?: TunnelManagerOptions);
|
|
234
|
+
/** The current status frame. */
|
|
235
|
+
get info(): TunnelInfo;
|
|
236
|
+
/**
|
|
237
|
+
* Start (or keep) a tunnel toward `target`. Restarting with a different
|
|
238
|
+
* target tears the old tunnel down first; restarting with the same target
|
|
239
|
+
* while running is a no-op. A string is the quick mode's local target URL.
|
|
240
|
+
*
|
|
241
|
+
* In quick mode the URL surfaces when cloudflared mints the ephemeral
|
|
242
|
+
* trycloudflare hostname; in named mode the fixed `publicUrl` surfaces
|
|
243
|
+
* once the first edge connection registers (see {@link namedTunnelHandle}).
|
|
244
|
+
* @param target - what to run (a string means quick toward that local URL).
|
|
245
|
+
*/
|
|
246
|
+
start(target: TunnelTarget | string): void;
|
|
247
|
+
/** Stop the tunnel for good: no restarts, no state. */
|
|
248
|
+
stop(): void;
|
|
249
|
+
/** Alias of {@link stop} for plugin-effect disposal. */
|
|
250
|
+
dispose(): void;
|
|
251
|
+
/** Subscribe to minted tunnel URLs (fire-and-forget duplicates dropped). */
|
|
252
|
+
onUrl(listener: (url: string) => void): () => void;
|
|
253
|
+
/** Subscribe to every phase change. */
|
|
254
|
+
onPhase(listener: (info: TunnelInfo) => void): () => void;
|
|
255
|
+
private attempt;
|
|
256
|
+
private handleUrl;
|
|
257
|
+
/**
|
|
258
|
+
* Watch the readiness of a running tunnel. cloudflared can keep its process
|
|
259
|
+
* (and its metrics port) alive while every edge connection is gone, leaving
|
|
260
|
+
* a minted hostname that no longer resolves: the manager used to report
|
|
261
|
+
* 'running' forever and the relay kept forwarding to that dead address, so
|
|
262
|
+
* the phone hit Cloudflare 1016 instead of the relay's offline page
|
|
263
|
+
* (issue #1723). A bounded number of consecutive probe failures ends the
|
|
264
|
+
* attempt through the ordinary fail/backoff path, which mints a new URL and
|
|
265
|
+
* re-announces the relay.
|
|
266
|
+
*/
|
|
267
|
+
private startHealthWatch;
|
|
268
|
+
/** One probe round of the running tunnel's public URL. */
|
|
269
|
+
private healthCheck;
|
|
270
|
+
/** Cancel the readiness watchdog (teardown, restart, stop). */
|
|
271
|
+
private stopHealthWatch;
|
|
272
|
+
private handleExit;
|
|
273
|
+
private fail;
|
|
274
|
+
/**
|
|
275
|
+
* Reap the child a crashed host recorded (see ./tunnel-orphan.ts). A failed
|
|
276
|
+
* recovery only loses the cleanup, so it must never fail the attempt.
|
|
277
|
+
*/
|
|
278
|
+
private reapLeftoverChild;
|
|
279
|
+
/**
|
|
280
|
+
* Stop the current child and drop its crash-recovery record: the process the
|
|
281
|
+
* record describes is gone, so a later start must not reap it again (and a
|
|
282
|
+
* healthy host never reaps its own live child).
|
|
283
|
+
*/
|
|
284
|
+
private stopHandle;
|
|
285
|
+
/** Stop the current process and cancel every pending timer (no phase change). */
|
|
286
|
+
private teardown;
|
|
287
|
+
private setPhase;
|
|
288
|
+
}
|
|
289
|
+
export {};
|