@linxin666/dsh-remote-bridge 0.4.11 → 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 +47 -10
- package/README.zh.md +47 -10
- package/lib/client.js +1 -1
- package/lib/client.js.map +1 -1
- package/lib/index.js +18238 -416
- 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 +3 -1
- package/lib/types/core/push.d.ts +51 -2
- package/lib/types/core/turn-end-quiet.d.ts +104 -40
- 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 +440 -11
- package/package.json +4 -2
- package/src/client/panel.module.css +16 -1
- package/src/core/connection.ts +244 -13
- 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 +4 -1
- package/src/core/push.ts +70 -4
- package/src/core/turn-end-quiet.ts +281 -98
- 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 +1303 -39
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Crash recovery for the tunnel's cloudflared child. A host that dies without
|
|
3
|
+
* tearing its tunnel down hands the child over to launchd/systemd, where it
|
|
4
|
+
* keeps its edge registration forever: the next host then runs a second
|
|
5
|
+
* connector beside an orphan nobody serves, and the old process holds a
|
|
6
|
+
* hostname no tunnel answers on any more.
|
|
7
|
+
*
|
|
8
|
+
* The manager records the spawned child under the DSH home right after the
|
|
9
|
+
* spawn (pid plus the exact command line ps/proc reports) and drops the record
|
|
10
|
+
* when that child is stopped, so a record only outlives the process that wrote
|
|
11
|
+
* it when that process crashed. The next start reaps exactly that pid, and
|
|
12
|
+
* only while it is still alive AND its command line still equals the recorded
|
|
13
|
+
* one: a recycled pid or a rewritten record can therefore never make the
|
|
14
|
+
* plugin signal a process it did not spawn. Nothing here matches by pattern,
|
|
15
|
+
* by process name, or by any pid this plugin did not record itself.
|
|
16
|
+
*
|
|
17
|
+
* A record also carries the pid of the host that spawned the child. Two live
|
|
18
|
+
* hosts can share one DSH home (two profiles, or a plugin reload inside one
|
|
19
|
+
* host), and each owns its own child; only a host that is gone leaves a child
|
|
20
|
+
* behind, so a record whose owner is still running is left untouched.
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* The spawned child as its tunnel handle exposes it (node's `ChildProcess`
|
|
24
|
+
* fits: `pid` plus the argv it was spawned with).
|
|
25
|
+
*/
|
|
26
|
+
export interface TunnelChildProcess {
|
|
27
|
+
/** The child's pid. */
|
|
28
|
+
pid?: number;
|
|
29
|
+
/** The argv the child was spawned with, executable first. */
|
|
30
|
+
spawnargs?: readonly string[];
|
|
31
|
+
}
|
|
32
|
+
/** What the plugin records about one child it spawned. */
|
|
33
|
+
export interface TunnelChildRecord {
|
|
34
|
+
/** The child's pid. */
|
|
35
|
+
pid: number;
|
|
36
|
+
/**
|
|
37
|
+
* The child's full argv joined with single spaces — the same line `ps`
|
|
38
|
+
* prints. Anything else (a wrapper, a rewritten command) is a mismatch.
|
|
39
|
+
*/
|
|
40
|
+
command: string;
|
|
41
|
+
/** The host process that spawned the child; a live owner keeps it. */
|
|
42
|
+
ownerPid: number;
|
|
43
|
+
/** ISO timestamp of the recording (diagnostics only). */
|
|
44
|
+
recordedAt: string;
|
|
45
|
+
}
|
|
46
|
+
/** How one recorded leftover was settled. */
|
|
47
|
+
export type TunnelOrphanVerdict =
|
|
48
|
+
/** There was no usable record. */
|
|
49
|
+
'none'
|
|
50
|
+
/** The record did not identify a live child this plugin spawned: only the file was dropped. */
|
|
51
|
+
| 'stale'
|
|
52
|
+
/** The recorded child still belongs to a running host: it was left alone. */
|
|
53
|
+
| 'owned'
|
|
54
|
+
/** The recorded orphan was signalled away and the record dropped. */
|
|
55
|
+
| 'reaped';
|
|
56
|
+
/** Injectable seams of the reaper (defaults read the real process table). */
|
|
57
|
+
export interface TunnelOrphanSeams {
|
|
58
|
+
/** Whether a pid is still alive. */
|
|
59
|
+
alive?: (pid: number) => boolean;
|
|
60
|
+
/** Read a pid's exact command line, or undefined when it cannot be read. */
|
|
61
|
+
command?: (pid: number) => Promise<string | undefined>;
|
|
62
|
+
/** Signal one recorded pid (never a pattern). */
|
|
63
|
+
signal?: (pid: number, signal: NodeJS.Signals) => void;
|
|
64
|
+
/** Bounded wait between liveness polls. */
|
|
65
|
+
wait?: (ms: number) => Promise<void>;
|
|
66
|
+
/** How long the signalled child gets to honour SIGTERM before SIGKILL. */
|
|
67
|
+
graceMs?: number;
|
|
68
|
+
}
|
|
69
|
+
/** The crash-recovery face the tunnel manager drives. */
|
|
70
|
+
export interface TunnelChildGuard {
|
|
71
|
+
/** Reap the leftover child a crashed host recorded; resolves once settled. */
|
|
72
|
+
reap(): Promise<TunnelOrphanVerdict>;
|
|
73
|
+
/**
|
|
74
|
+
* Record a freshly spawned child so the next host start can reap it.
|
|
75
|
+
* @returns whether a record was written (a handle without its OS process, or
|
|
76
|
+
* an unwritable home, writes nothing and must not clear a sibling's record).
|
|
77
|
+
*/
|
|
78
|
+
record(child: TunnelChildProcess | undefined): boolean;
|
|
79
|
+
/** Drop the record: the child is stopped, so nothing is left to reap. */
|
|
80
|
+
clear(): void;
|
|
81
|
+
}
|
|
82
|
+
/** How long an orphan gets to honour SIGTERM before it is killed. */
|
|
83
|
+
export declare const DEFAULT_ORPHAN_GRACE_MS = 3000;
|
|
84
|
+
/** The record file this plugin owns: `$DSH_HOME/remote-web-ui-tunnel-child.json`. */
|
|
85
|
+
export declare function tunnelChildRecordFile(home?: string): string;
|
|
86
|
+
/**
|
|
87
|
+
* Read one record, ignoring a file that cannot identify a child (`pid` and
|
|
88
|
+
* `command` absent or malformed): only a well-formed record may ever lead to a
|
|
89
|
+
* signal.
|
|
90
|
+
* @param file - the record file.
|
|
91
|
+
* @returns the record, or undefined when there is none to trust.
|
|
92
|
+
*/
|
|
93
|
+
export declare function readTunnelChildRecord(file: string): TunnelChildRecord | undefined;
|
|
94
|
+
/**
|
|
95
|
+
* Write one record owner-only, creating the DSH home when it is missing.
|
|
96
|
+
* @param file - the record file.
|
|
97
|
+
* @param record - what to record about the spawned child.
|
|
98
|
+
*/
|
|
99
|
+
export declare function recordTunnelChild(file: string, record: TunnelChildRecord): void;
|
|
100
|
+
/** Drop one record; an absent file is not an error. */
|
|
101
|
+
export declare function clearTunnelChildRecord(file: string): void;
|
|
102
|
+
/**
|
|
103
|
+
* Whether a pid is still in the process table. `EPERM` means the process
|
|
104
|
+
* exists but belongs to another user, which still counts as alive.
|
|
105
|
+
* @param pid - the pid to probe.
|
|
106
|
+
* @returns whether the pid exists.
|
|
107
|
+
*/
|
|
108
|
+
export declare function processAlive(pid: number): boolean;
|
|
109
|
+
/**
|
|
110
|
+
* Read one pid's exact command line: `/proc/<pid>/cmdline` on Linux,
|
|
111
|
+
* `ps -ww -o command=` elsewhere. Arguments are joined with single spaces in
|
|
112
|
+
* both cases, which is exactly how a recorded argv is stored.
|
|
113
|
+
* @param pid - the pid to read.
|
|
114
|
+
* @returns the command line, or undefined when it cannot be read.
|
|
115
|
+
*/
|
|
116
|
+
export declare function processCommandLine(pid: number): Promise<string | undefined>;
|
|
117
|
+
/**
|
|
118
|
+
* Turn one `/proc/<pid>/cmdline` read (argv, NUL-separated, usually with a
|
|
119
|
+
* trailing NUL) into the line `ps` prints, so both platforms compare against
|
|
120
|
+
* the same recorded shape.
|
|
121
|
+
* @param raw - the raw `/proc/<pid>/cmdline` text.
|
|
122
|
+
* @returns the joined command line, or undefined for an empty file.
|
|
123
|
+
*/
|
|
124
|
+
export declare function parseProcCommandLine(raw: string): string | undefined;
|
|
125
|
+
/**
|
|
126
|
+
* Settle one recorded leftover: reap exactly the recorded child when it is
|
|
127
|
+
* still the process that record describes, and leave every other process
|
|
128
|
+
* strictly alone.
|
|
129
|
+
* @param file - the record file.
|
|
130
|
+
* @param seams - process-table and signal seams (defaults read the real table).
|
|
131
|
+
* @returns which way the record was settled.
|
|
132
|
+
*/
|
|
133
|
+
export declare function reapRecordedTunnelChild(file: string, seams?: TunnelOrphanSeams): Promise<TunnelOrphanVerdict>;
|
|
134
|
+
/** Seams of the file-backed guard (the record file defaults to the DSH home). */
|
|
135
|
+
export interface TunnelChildGuardOptions extends TunnelOrphanSeams {
|
|
136
|
+
/** The record file; resolved per call so a changed DSH home is honoured. */
|
|
137
|
+
file?: string;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Build the file-backed crash-recovery guard the tunnel manager uses: the
|
|
141
|
+
* record lives at {@link tunnelChildRecordFile} unless a file is given.
|
|
142
|
+
* @param options - record file and process seams (all optional).
|
|
143
|
+
* @returns the guard.
|
|
144
|
+
*/
|
|
145
|
+
export declare function createTunnelChildGuard(options?: TunnelChildGuardOptions): TunnelChildGuard;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure planner for the settings-sync tunnel branch: given the resolved
|
|
3
|
+
* settings slice, decide which tunnel mode runs and with what target. Kept
|
|
4
|
+
* free of cordis types so the precedence matrix is unit-testable without a
|
|
5
|
+
* host context.
|
|
6
|
+
*/
|
|
7
|
+
/** Whether a configured public base is a parseable http(s) URL with a host. */
|
|
8
|
+
export declare function isHttpUrl(value: string): boolean;
|
|
9
|
+
/** How the settings sync should drive the tunnel for one resolved value. */
|
|
10
|
+
export type TunnelPlan = {
|
|
11
|
+
mode: 'off';
|
|
12
|
+
} | {
|
|
13
|
+
mode: 'quick';
|
|
14
|
+
targetUrl: string;
|
|
15
|
+
ignored: Array<'tunnelToken' | 'publicBaseUrl'>;
|
|
16
|
+
} | {
|
|
17
|
+
mode: 'named';
|
|
18
|
+
token: string;
|
|
19
|
+
publicUrl: string;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Decide the tunnel mode and target from one resolved settings value.
|
|
23
|
+
* Precedence: the quick tunnel wins when `autoTunnel` is on (anything else
|
|
24
|
+
* configured is reported as ignored); the named tunnel runs only with a
|
|
25
|
+
* token AND a valid public hostname (the token does not carry the hostname,
|
|
26
|
+
* so the plan would have no public URL to report without it); otherwise no
|
|
27
|
+
* tunnel and the manual public base applies.
|
|
28
|
+
*
|
|
29
|
+
* The quick target URL names the local webServer port, so a plan made before
|
|
30
|
+
* the listener binds cannot be quick: it comes back `off` and the caller —
|
|
31
|
+
* which can observe the port settle — re-plans. The planner itself never
|
|
32
|
+
* emits an `http://127.0.0.1:undefined` target.
|
|
33
|
+
* @param value - the resolved settings slice that drives the tunnel.
|
|
34
|
+
* @param port - the local webServer port the tunnel forwards to (undefined
|
|
35
|
+
* before the listener binds).
|
|
36
|
+
*/
|
|
37
|
+
export declare function tunnelPlanOf(value: {
|
|
38
|
+
autoTunnel?: boolean;
|
|
39
|
+
tunnelToken?: string;
|
|
40
|
+
publicBaseUrl?: string;
|
|
41
|
+
}, port: number | undefined): TunnelPlan;
|
|
@@ -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 {};
|