@linxin666/dsh-remote-bridge 0.4.12 → 0.4.14
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 +53 -11
- package/README.zh.md +53 -11
- package/lib/client.js +112 -90
- package/lib/client.js.map +1 -1
- package/lib/index.js +18800 -334
- package/lib/types/client/PairPanel.d.ts +7 -0
- package/lib/types/client/locales.d.ts +13 -10
- package/lib/types/core/connection.d.ts +50 -0
- package/lib/types/core/devices.d.ts +80 -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 +24 -0
- package/lib/types/core/push.d.ts +70 -3
- 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 +234 -0
- package/lib/types/host/relay-connector.d.ts +156 -0
- package/lib/types/host/relay-control.d.ts +195 -0
- package/lib/types/host/relay-registry.d.ts +123 -0
- package/lib/types/host/relay-secondary.d.ts +209 -0
- package/lib/types/host/relay.d.ts +170 -0
- package/lib/types/host/routes.d.ts +29 -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 +488 -1
- package/package.json +4 -2
- package/src/client/PairPanel.tsx +21 -0
- package/src/client/locales.ts +19 -15
- package/src/client/panel.module.css +48 -1
- package/src/core/connection.ts +316 -11
- package/src/core/devices.ts +165 -16
- 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 +27 -0
- package/src/core/push.ts +90 -5
- 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 +849 -0
- package/src/host/relay-connector.ts +324 -0
- package/src/host/relay-control.ts +315 -0
- package/src/host/relay-registry.ts +275 -0
- package/src/host/relay-secondary.ts +478 -0
- package/src/host/relay.ts +294 -0
- package/src/host/routes.ts +57 -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 +1516 -23
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The relay's two connectors. The quick tunnel keeps serving every instance
|
|
3
|
+
* that has not migrated, and the named connector is established beside it on
|
|
4
|
+
* the same relay hostname and left idle while the wildcard Worker route still
|
|
5
|
+
* shadows the CNAME. The control plane's `mode` names the connector the phone
|
|
6
|
+
* is meant to be served from; nothing here switches the edge — it only keeps
|
|
7
|
+
* both sides up, so a server-side flip needs no plugin restart and a rollback
|
|
8
|
+
* is the same flip back.
|
|
9
|
+
*/
|
|
10
|
+
import { type RelayClaim, type RelayClaimOutcome, type RelayClaimRequest, type RelayConnectorMode } from './relay-control.ts';
|
|
11
|
+
import type { RelayIdentity } from './relay-registry.ts';
|
|
12
|
+
import { TunnelManager, type TunnelInfo } from './tunnel.ts';
|
|
13
|
+
/**
|
|
14
|
+
* The crash-recovery record slot the idle named connector owns. Kept apart
|
|
15
|
+
* from the quick connector's slot so each connector's leftover child is reaped
|
|
16
|
+
* by the start that belongs to it instead of overwriting its sibling's record.
|
|
17
|
+
*/
|
|
18
|
+
export declare function namedTunnelChildRecordFile(home?: string): string;
|
|
19
|
+
/**
|
|
20
|
+
* Read this plugin's own version from its package manifest (the claim reports
|
|
21
|
+
* it so the migration can measure adoption by released version).
|
|
22
|
+
*
|
|
23
|
+
* This module sits one level deeper than the source root (`src/host/`) while
|
|
24
|
+
* the bundle stays one level under the package root (`lib/index.js`), so the
|
|
25
|
+
* two depths are tried in order with the bundle's own first. A manifest that
|
|
26
|
+
* cannot be read at either depth is a miss, and the claim then carries no
|
|
27
|
+
* version at all.
|
|
28
|
+
* @returns the version, or undefined when the manifest cannot be read.
|
|
29
|
+
*/
|
|
30
|
+
export declare function readPluginVersion(): string | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* The dedupe key of one claim trigger: the local origin port plus the quick
|
|
33
|
+
* tunnel URL the start reported. A settings sync re-runs the tunnel plan with
|
|
34
|
+
* the same key; a tunnel restart mints a new quick URL and therefore claims
|
|
35
|
+
* again (which is also what keeps the lease's `last_seen_at` inside the cron
|
|
36
|
+
* GC's TTL).
|
|
37
|
+
* @param port - the bound web server port, or undefined before it settles.
|
|
38
|
+
* @param target - the quick tunnel URL, or undefined while none is minted.
|
|
39
|
+
* @returns the key, or undefined when there is nothing to claim yet.
|
|
40
|
+
*/
|
|
41
|
+
export declare function relayClaimKeyOf(port: number | undefined, target: string | undefined): string | undefined;
|
|
42
|
+
/** The idle named connector: start, stop and report, nothing else. */
|
|
43
|
+
export interface NamedRelayConnector {
|
|
44
|
+
/** Run one named connector for the lease (token via the environment). */
|
|
45
|
+
start(token: string, hostname: string): void;
|
|
46
|
+
/** Stop the connector (the relay half was disposed). */
|
|
47
|
+
stop(): void;
|
|
48
|
+
readonly info: TunnelInfo;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Build the named connector the relay keeps beside the quick tunnel.
|
|
52
|
+
*
|
|
53
|
+
* Health probing is off on purpose: while the wildcard Worker route answers
|
|
54
|
+
* the relay hostname, a probe of the idle connector's public URL sees the
|
|
55
|
+
* Worker's offline page and would declare a healthy connector dead, restarting
|
|
56
|
+
* it forever (the failure mode issue #1723 fixed for the quick tunnel).
|
|
57
|
+
* @param options - an existing manager (tests); defaults spawn the real one.
|
|
58
|
+
*/
|
|
59
|
+
export declare function createNamedRelayConnector(options?: {
|
|
60
|
+
manager?: TunnelManager;
|
|
61
|
+
}): NamedRelayConnector;
|
|
62
|
+
/** The lease half's observable state (logged, and read by the tests). */
|
|
63
|
+
export type RelayPairState = {
|
|
64
|
+
state: 'idle';
|
|
65
|
+
} | {
|
|
66
|
+
state: 'claiming';
|
|
67
|
+
} | {
|
|
68
|
+
state: 'claimed';
|
|
69
|
+
mode: RelayConnectorMode;
|
|
70
|
+
hostname: string;
|
|
71
|
+
} | {
|
|
72
|
+
state: 'failed';
|
|
73
|
+
error: string;
|
|
74
|
+
};
|
|
75
|
+
/** Everything the bridge connector needs to run for one accepted lease. */
|
|
76
|
+
export interface RelayBridgeStart {
|
|
77
|
+
claim: RelayClaim;
|
|
78
|
+
identity: RelayIdentity;
|
|
79
|
+
port: number;
|
|
80
|
+
}
|
|
81
|
+
/** Injectable seams of the lease half (defaults are the real control plane). */
|
|
82
|
+
export interface RelayConnectorPairOptions {
|
|
83
|
+
/** One claim attempt. */
|
|
84
|
+
claim?: (request: RelayClaimRequest) => Promise<RelayClaimOutcome>;
|
|
85
|
+
/** Start the named connector for one accepted lease. */
|
|
86
|
+
startNamed?: (token: string, hostname: string) => void;
|
|
87
|
+
/** Stop the named connector. */
|
|
88
|
+
stopNamed?: () => void;
|
|
89
|
+
/** Start (or restart) the bridge connector for one accepted lease. It runs
|
|
90
|
+
* beside the quick tunnel and idles while the instance's mode is quick, so a
|
|
91
|
+
* server-side flip needs no plugin restart. */
|
|
92
|
+
startBridge?: (start: RelayBridgeStart) => void;
|
|
93
|
+
/** Stop the bridge connector. */
|
|
94
|
+
stopBridge?: () => void;
|
|
95
|
+
/** Release the lease (the offline action, never a retire). */
|
|
96
|
+
release?: (identity: RelayIdentity) => Promise<void>;
|
|
97
|
+
/** The control-plane base the two lease endpoints hang off. The defaults
|
|
98
|
+
* below derive `claim` and `release` from it, so a host that configures
|
|
99
|
+
* nothing dials exactly the shipped market URLs. */
|
|
100
|
+
controlUrl?: string;
|
|
101
|
+
/** Observe the lease state (logs only — the card still renders the registry). */
|
|
102
|
+
onState?: (state: RelayPairState) => void;
|
|
103
|
+
baseDelayMs?: number;
|
|
104
|
+
maxDelayMs?: number;
|
|
105
|
+
/** Schedule one retry; defaults to an unref'd node timer. */
|
|
106
|
+
timerFn?: (fn: () => void, ms: number) => {
|
|
107
|
+
unref?: () => void;
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Own the lease half of the relay: claim it on every tunnel start, bring the
|
|
112
|
+
* named connector up beside the quick one, and retry a failed claim with a
|
|
113
|
+
* capped backoff while the quick connector keeps serving.
|
|
114
|
+
*/
|
|
115
|
+
export declare class RelayConnectorPair {
|
|
116
|
+
private readonly identity;
|
|
117
|
+
private readonly options;
|
|
118
|
+
private stateValue;
|
|
119
|
+
private leaseValue;
|
|
120
|
+
private attempts;
|
|
121
|
+
private timer;
|
|
122
|
+
private disposed;
|
|
123
|
+
constructor(identity: RelayIdentity, options?: RelayConnectorPairOptions);
|
|
124
|
+
/** The lease half's current state. */
|
|
125
|
+
get state(): RelayPairState;
|
|
126
|
+
/** The connector the phone is served from, for the last accepted lease. */
|
|
127
|
+
get served(): RelayConnectorMode | undefined;
|
|
128
|
+
/** The last accepted lease (undefined until one lands). */
|
|
129
|
+
get lease(): RelayClaim | undefined;
|
|
130
|
+
/**
|
|
131
|
+
* Claim one lease and start (or restart) the named connector for it. A
|
|
132
|
+
* refusal is graded into a retry and never tears down the quick connector:
|
|
133
|
+
* the instance keeps the origin the registry already gave it.
|
|
134
|
+
* @param input - the local origin port and the reporting plugin version.
|
|
135
|
+
* @returns the accepted lease, or undefined when the attempt did not land.
|
|
136
|
+
*/
|
|
137
|
+
start(input: {
|
|
138
|
+
port: number;
|
|
139
|
+
pluginVersion?: string;
|
|
140
|
+
}): Promise<RelayClaim | undefined>;
|
|
141
|
+
/**
|
|
142
|
+
* The plugin's offline action (toggle off, profile unmount): release the
|
|
143
|
+
* lease and stop the idle connector. Never retires — the hostname is the
|
|
144
|
+
* phone's bookmark, and the tunnel behind it survives.
|
|
145
|
+
*/
|
|
146
|
+
release(): Promise<void>;
|
|
147
|
+
/** Stop retrying and drop the connector (the relay half was disposed). */
|
|
148
|
+
dispose(): void;
|
|
149
|
+
private startNamed;
|
|
150
|
+
private stopNamed;
|
|
151
|
+
/** Bring the bridge connector up for one accepted lease, if it carries one. */
|
|
152
|
+
private startBridge;
|
|
153
|
+
private scheduleRetry;
|
|
154
|
+
private clearTimer;
|
|
155
|
+
private setState;
|
|
156
|
+
}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Relay control-plane client: the lease claim that turns one relay identity
|
|
3
|
+
* into a lease row (the tunnel transports get a tunnel, its ingress, a CNAME
|
|
4
|
+
* and the per-instance connector token; the bridge transport gets neither
|
|
5
|
+
* block, because its data plane owns no Cloudflare object) plus the release
|
|
6
|
+
* bookkeeping.
|
|
7
|
+
*
|
|
8
|
+
* Claiming is best-effort and additive. Every failure is graded into a retry
|
|
9
|
+
* decision instead of breaking the quick tunnel that already serves the phone:
|
|
10
|
+
* the control plane answers `mode`, so an instance whose claim cannot land
|
|
11
|
+
* keeps the origin the registry gave it.
|
|
12
|
+
*/
|
|
13
|
+
import type { RelayIdentity } from './relay-registry.ts';
|
|
14
|
+
/**
|
|
15
|
+
* The control-plane base every lease and registry endpoint hangs off: the
|
|
16
|
+
* market apex, never a relay hostname.
|
|
17
|
+
*
|
|
18
|
+
* This constant is the one home of the shipped default. A self-hosted meeting
|
|
19
|
+
* point sets the `relayControlUrl` config field, and its `claim`, `release`,
|
|
20
|
+
* `register` and `unregister` paths all move with that one base.
|
|
21
|
+
*/
|
|
22
|
+
export declare const RELAY_CONTROL_URL = "https://dsh-market.com/api/relay";
|
|
23
|
+
/** The paths one control-plane base serves. */
|
|
24
|
+
export type RelayControlEndpoint = 'claim' | 'release' | 'register' | 'unregister';
|
|
25
|
+
/**
|
|
26
|
+
* One endpoint of a control-plane base.
|
|
27
|
+
*
|
|
28
|
+
* The base arrives from a hand-written profile YAML, so its trailing slashes
|
|
29
|
+
* are trimmed instead of doubling the separator this adds.
|
|
30
|
+
* @param endpoint - the path below the base.
|
|
31
|
+
* @param controlUrl - the base; undefined means the shipped market apex.
|
|
32
|
+
* @returns the absolute endpoint URL.
|
|
33
|
+
*/
|
|
34
|
+
export declare function relayControlEndpointOf(endpoint: RelayControlEndpoint, controlUrl?: string): string;
|
|
35
|
+
/** The default lease endpoints (market apex, never a relay hostname). */
|
|
36
|
+
export declare const RELAY_CLAIM_URL: string;
|
|
37
|
+
export declare const RELAY_RELEASE_URL: string;
|
|
38
|
+
/** Which connector serves the relay hostname. `bridge` is the Durable Object
|
|
39
|
+
* meeting point (`relay-bridge-connector.ts`), `named` the instance's own cloud
|
|
40
|
+
* tunnel, `quick` today's Worker proxy path. */
|
|
41
|
+
export type RelayConnectorMode = 'named' | 'quick' | 'bridge';
|
|
42
|
+
/** What a bridge connector needs to dial: an endpoint and a credential to
|
|
43
|
+
* present. The credential is the lease's own long-lived id and secret -- the
|
|
44
|
+
* reply names it rather than repeating a secret in a body. */
|
|
45
|
+
export interface RelayBridgeGrant {
|
|
46
|
+
endpoint: string;
|
|
47
|
+
instance: string;
|
|
48
|
+
credential: 'lease-secret';
|
|
49
|
+
}
|
|
50
|
+
/** One accepted lease, as the control plane answers it. */
|
|
51
|
+
export interface RelayClaim {
|
|
52
|
+
mode: RelayConnectorMode;
|
|
53
|
+
/** The instance's own tunnel. Absent for a bridged lease: there is nothing
|
|
54
|
+
* to run and no token to run it with. */
|
|
55
|
+
tunnel?: {
|
|
56
|
+
id: string;
|
|
57
|
+
token: string;
|
|
58
|
+
};
|
|
59
|
+
hostname: string;
|
|
60
|
+
/** The CNAME target behind the hostname; absent for a bridged lease too. */
|
|
61
|
+
dns?: {
|
|
62
|
+
target: string;
|
|
63
|
+
};
|
|
64
|
+
/** Present when the control plane can route this instance over the bridge. */
|
|
65
|
+
bridge?: RelayBridgeGrant;
|
|
66
|
+
}
|
|
67
|
+
/** What one claim attempt resolved to. */
|
|
68
|
+
export type RelayClaimOutcome = {
|
|
69
|
+
ok: true;
|
|
70
|
+
claim: RelayClaim;
|
|
71
|
+
} | {
|
|
72
|
+
ok: false;
|
|
73
|
+
status: number;
|
|
74
|
+
error: string;
|
|
75
|
+
retryAfterMs?: number;
|
|
76
|
+
};
|
|
77
|
+
/** One claim request (the lease the caller wants refreshed). */
|
|
78
|
+
export interface RelayClaimRequest {
|
|
79
|
+
id: string;
|
|
80
|
+
secret: string;
|
|
81
|
+
/** Carried only by a fresh identity: the first claim creates the row. */
|
|
82
|
+
newSecret?: string;
|
|
83
|
+
/** Local origin port the lease ingress points at (`ctx.webServer.port`). */
|
|
84
|
+
port: number;
|
|
85
|
+
/** The plugin version, for the adoption readout of the migration. */
|
|
86
|
+
pluginVersion?: string;
|
|
87
|
+
/**
|
|
88
|
+
* The account service's relay grant, when this bridge holds one.
|
|
89
|
+
*
|
|
90
|
+
* Absent is the claim a host makes when no phone has handed a grant over yet,
|
|
91
|
+
* and the key is then left out of the body entirely: the second meeting point
|
|
92
|
+
* grades "no grant" as `tier-required`, which is a different answer from "this
|
|
93
|
+
* grant does not verify" (`grant-invalid`), and `null` would be read as the
|
|
94
|
+
* latter.
|
|
95
|
+
*/
|
|
96
|
+
grant?: string;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* The capped backoff a failed claim retries on. The cap is the whole Cloudflare
|
|
100
|
+
* rate-limit window: a spent account request budget answers `retryAfterMs:
|
|
101
|
+
* 300000` because that budget is counted over 300 seconds, so a shorter cap
|
|
102
|
+
* only brings the instance back inside the same spent window.
|
|
103
|
+
*/
|
|
104
|
+
export declare const CLAIM_RETRY_BASE_MS = 5000;
|
|
105
|
+
export declare const CLAIM_RETRY_MAX_MS = 300000;
|
|
106
|
+
/**
|
|
107
|
+
* The control plane's refusal code for a claim whose grant does not reach the
|
|
108
|
+
* tier this meeting point admits. It is not an authentication answer — the id
|
|
109
|
+
* and secret verified — so it is graded apart from `auth-failed`.
|
|
110
|
+
*/
|
|
111
|
+
export declare const RELAY_TIER_REQUIRED = "tier-required";
|
|
112
|
+
/**
|
|
113
|
+
* The control plane's refusal code for a grant it will not vouch for: a bad
|
|
114
|
+
* signature, the wrong scope, or — the case that is not an error at all — a grant
|
|
115
|
+
* that has simply reached the end of its 30 days.
|
|
116
|
+
*
|
|
117
|
+
* It shares {@link RELAY_TIER_REQUIRED}'s branch because it is the same kind of
|
|
118
|
+
* fact: the id and the secret are fine, the admission this host was given is not,
|
|
119
|
+
* and the repair is a fresh grant (which the phone hands over) or another
|
|
120
|
+
* evaluation after the plan or the clock moved. `auth-failed` stays the one
|
|
121
|
+
* terminal 403.
|
|
122
|
+
*/
|
|
123
|
+
export declare const RELAY_GRANT_INVALID = "grant-invalid";
|
|
124
|
+
/**
|
|
125
|
+
* How long a claim refused for its admission (`tier-required` or
|
|
126
|
+
* `grant-invalid`) waits before the next attempt: six hours.
|
|
127
|
+
*
|
|
128
|
+
* Deliberately outside {@link CLAIM_RETRY_MAX_MS}. That cap exists because a
|
|
129
|
+
* spent rate-limit budget is counted over one 300-second window, so retrying
|
|
130
|
+
* faster lands inside the same spent window. An admission refusal is not a
|
|
131
|
+
* window: it is a fact about the plan or about the grant's own 30-day lifetime.
|
|
132
|
+
* Six hours is the interval the lease refresh itself runs on, so a downgrade is
|
|
133
|
+
* noticed at the next refresh, while an upgrade needs no wait at all: the phone
|
|
134
|
+
* hands the new grant over (`relay.tier.v1`) and that re-claims immediately.
|
|
135
|
+
*/
|
|
136
|
+
export declare const CLAIM_TIER_REQUIRED_DELAY_MS = 21600000;
|
|
137
|
+
/** Build the claim body (an absent value is omitted, never sent as null). */
|
|
138
|
+
export declare function relayClaimBody(request: RelayClaimRequest): string;
|
|
139
|
+
/** One lease read back from an accepted claim, or undefined for a bad shape. */
|
|
140
|
+
export declare function relayClaimOf(value: unknown): RelayClaim | undefined;
|
|
141
|
+
/** The bridge grant of one accepted lease, or undefined when it carries none. */
|
|
142
|
+
export declare function relayBridgeGrantOf(value: unknown): RelayBridgeGrant | undefined;
|
|
143
|
+
/**
|
|
144
|
+
* Claim one lease. A transport failure is graded like `502 cf-api-unavailable`
|
|
145
|
+
* (status 0) so the caller retries on the same path as a refused upstream.
|
|
146
|
+
* @param request - the lease to refresh.
|
|
147
|
+
* @param options - endpoint and fetch seams (defaults are the real ones).
|
|
148
|
+
*/
|
|
149
|
+
export declare function claimRelayLease(request: RelayClaimRequest, options?: {
|
|
150
|
+
url?: string;
|
|
151
|
+
fetchFn?: typeof fetch;
|
|
152
|
+
}): Promise<RelayClaimOutcome>;
|
|
153
|
+
/**
|
|
154
|
+
* Release one lease (the plugin's offline action: toggle off, profile
|
|
155
|
+
* unmount). Best-effort — a release that never lands only leaves the row
|
|
156
|
+
* marked active, which the 90-day GC recovers.
|
|
157
|
+
*/
|
|
158
|
+
export declare function releaseRelayLease(identity: RelayIdentity, options?: {
|
|
159
|
+
url?: string;
|
|
160
|
+
fetchFn?: typeof fetch;
|
|
161
|
+
}): Promise<void>;
|
|
162
|
+
/** How one failed claim should be retried. */
|
|
163
|
+
export interface RelayClaimRetry {
|
|
164
|
+
/** Whether another attempt is worth making at all. */
|
|
165
|
+
retry: boolean;
|
|
166
|
+
/** How long to wait before that attempt. */
|
|
167
|
+
delayMs: number;
|
|
168
|
+
/**
|
|
169
|
+
* Whether the identity must be re-claimed with `new_secret`: the control
|
|
170
|
+
* plane lost the row (a D1 restore or a manual wipe), which is the one
|
|
171
|
+
* condition a fresh claim fixes.
|
|
172
|
+
*/
|
|
173
|
+
refreshIdentity: boolean;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Grade one failed claim into a retry decision.
|
|
177
|
+
*
|
|
178
|
+
* `403 auth-failed` is terminal: the id belongs to another secret and no
|
|
179
|
+
* number of retries can take it back. The two admission 403s —
|
|
180
|
+
* `tier-required` and `grant-invalid` — are the ones that are not: the id and
|
|
181
|
+
* the secret verified and only the admission is missing, so both retry on the
|
|
182
|
+
* same fixed wait ({@link CLAIM_TIER_REQUIRED_DELAY_MS}) that the cap below
|
|
183
|
+
* deliberately does not clamp. Everything else retries on a capped
|
|
184
|
+
* exponential backoff, with the control plane's own `retryAfterMs` (a 502
|
|
185
|
+
* names it) raising the floor so a retry cannot land inside a known outage.
|
|
186
|
+
* @param outcome - the refused attempt.
|
|
187
|
+
* @param attempt - how many attempts already failed (0 for the first).
|
|
188
|
+
* @param options - backoff bounds (defaults are the shipped ones).
|
|
189
|
+
*/
|
|
190
|
+
export declare function relayClaimRetryOf(outcome: Extract<RelayClaimOutcome, {
|
|
191
|
+
ok: false;
|
|
192
|
+
}>, attempt: number, options?: {
|
|
193
|
+
baseDelayMs?: number;
|
|
194
|
+
maxDelayMs?: number;
|
|
195
|
+
}): RelayClaimRetry;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Relay registry client: a stable `https://<id>.dsh-market.com` origin in
|
|
3
|
+
* front of the plugin-managed quick tunnel, so a paired phone keeps one
|
|
4
|
+
* bookmark and one cookie context across `dsh web` restarts.
|
|
5
|
+
*
|
|
6
|
+
* The plugin mints an id + secret on first run and persists them per profile
|
|
7
|
+
* under `$DSH_HOME/remote-web-ui-registry/`; every tunnel (re)start
|
|
8
|
+
* re-registers the current quick URL with the market worker. Registration is
|
|
9
|
+
* best-effort: a registry outage never blocks the tunnel — the QR just falls
|
|
10
|
+
* back to the raw quick URL for that session (the pre-relay behavior).
|
|
11
|
+
*
|
|
12
|
+
* Not to be confused with `./relay.ts`, the bridge's own APNs push sender: the
|
|
13
|
+
* relay in this file is the Cloudflare-side one that fronts the tunnel. The
|
|
14
|
+
* persisted directory and file names below keep their historical spelling on
|
|
15
|
+
* purpose, so an existing profile keeps the identity it already paired with.
|
|
16
|
+
*/
|
|
17
|
+
/** The market worker's registration endpoints (dsh-market.com edge API). */
|
|
18
|
+
export declare const RELAY_REGISTER_URL: string;
|
|
19
|
+
export declare const RELAY_UNREGISTER_URL: string;
|
|
20
|
+
/**
|
|
21
|
+
* The public origin template: one stable single-label subdomain per
|
|
22
|
+
* registration id. Single label on purpose — Universal SSL covers exactly
|
|
23
|
+
* one subdomain level, so a two-level hostname would get no certificate.
|
|
24
|
+
*
|
|
25
|
+
* This constant is the one home of the shipped default suffix; a self-hosted
|
|
26
|
+
* meeting point sets `relayPublicSuffix` and the hostname template follows.
|
|
27
|
+
*/
|
|
28
|
+
export declare const RELAY_BASE_SUFFIX = ".dsh-market.com";
|
|
29
|
+
export declare const RELAY_ID_RE: RegExp;
|
|
30
|
+
export declare const RELAY_SECRET_RE: RegExp;
|
|
31
|
+
/** Persisted per-profile relay identity. */
|
|
32
|
+
export interface RelayIdentity {
|
|
33
|
+
id: string;
|
|
34
|
+
secret: string;
|
|
35
|
+
/**
|
|
36
|
+
* True only for an identity minted in this process that has never been
|
|
37
|
+
* accepted by the registry: the first register carries `new_secret` so
|
|
38
|
+
* the endpoint may create the row; refreshes authenticate with `secret`
|
|
39
|
+
* alone and must not re-claim an id that already exists.
|
|
40
|
+
*/
|
|
41
|
+
fresh?: boolean;
|
|
42
|
+
}
|
|
43
|
+
/** The observable relay lifecycle the settings card renders. */
|
|
44
|
+
export type RelayState = {
|
|
45
|
+
state: 'off';
|
|
46
|
+
} | {
|
|
47
|
+
state: 'registering';
|
|
48
|
+
} | {
|
|
49
|
+
state: 'running';
|
|
50
|
+
url: string;
|
|
51
|
+
} | {
|
|
52
|
+
state: 'failed';
|
|
53
|
+
error: string;
|
|
54
|
+
};
|
|
55
|
+
/** Registry storage layout: one file per profile under the relay directory. */
|
|
56
|
+
export declare function relayIdentityFile(profile: string, home?: string): string;
|
|
57
|
+
/** Generate one identity pair (id: 8-byte hex slug; secret: 32-byte base64url). */
|
|
58
|
+
export declare function generateRelayIdentity(random?: (size: number) => Buffer): RelayIdentity;
|
|
59
|
+
/**
|
|
60
|
+
* Load (or lazily mint) the per-profile relay identity. The file is written
|
|
61
|
+
* owner-only on first mint; a corrupt file is reminted (the phone then
|
|
62
|
+
* re-pairs once — the same cost as losing the devices file).
|
|
63
|
+
*/
|
|
64
|
+
export declare function loadRelayIdentity(profile: string, home?: string, random?: (size: number) => Buffer): RelayIdentity;
|
|
65
|
+
/**
|
|
66
|
+
* 读一份**已经存在**的身份,绝不生成。
|
|
67
|
+
*
|
|
68
|
+
* 与 {@link loadRelayIdentity} 的唯一区别就在这里:文件不在、或读不出合法的
|
|
69
|
+
* id/secret 时返回 undefined,既不 mint 也不写盘,目录也不会被创建。关掉 relay 的
|
|
70
|
+
* 挂载路径靠它收掉上一次挂载留下的注册行与租约——挂载本身不是「申请一份新身份」的
|
|
71
|
+
* 理由,一个从没配过 relay 的宿主不该因为挂载就在 $DSH_HOME 里落下一份密钥文件。
|
|
72
|
+
* 坏文件同样原样留在盘上:等 relay 真的打开时由 loadRelayIdentity 按既有语义重新生成。
|
|
73
|
+
* @param profile - 受管 profile。
|
|
74
|
+
* @param home - DSH home(测试注入)。
|
|
75
|
+
* @returns 已存在的身份,或 undefined(没有可用身份,且什么都没写)。
|
|
76
|
+
*/
|
|
77
|
+
export declare function readExistingRelayIdentity(profile: string, home?: string): RelayIdentity | undefined;
|
|
78
|
+
/** Best-effort removal of the mapping (used on disable; errors are swallowed). */
|
|
79
|
+
export declare function unregisterRelay(identity: RelayIdentity, endpoint?: string, fetchFn?: typeof fetch): Promise<void>;
|
|
80
|
+
/**
|
|
81
|
+
* The stable relay base for one identity — the origin the QR and the phone
|
|
82
|
+
* bookmark use while (or after) the registration is accepted.
|
|
83
|
+
* @param identity - the registration identity.
|
|
84
|
+
* @param publicSuffix - the configured suffix; undefined means the shipped one.
|
|
85
|
+
* @returns the origin, with no path of its own.
|
|
86
|
+
*/
|
|
87
|
+
export declare function relayBaseOf(identity: RelayIdentity, publicSuffix?: string): string;
|
|
88
|
+
/**
|
|
89
|
+
* Registration loop with capped exponential backoff. Re-registration happens
|
|
90
|
+
* on every tunnel start (the quick URL can change after a crash-restart);
|
|
91
|
+
* failures retry with backoff and surface the last error to the card.
|
|
92
|
+
*/
|
|
93
|
+
export declare class RelayRegistrar {
|
|
94
|
+
private readonly identity;
|
|
95
|
+
private readonly setState;
|
|
96
|
+
private readonly options;
|
|
97
|
+
private timer;
|
|
98
|
+
private attempts;
|
|
99
|
+
private lastError;
|
|
100
|
+
private disposed;
|
|
101
|
+
constructor(identity: RelayIdentity, setState: (state: RelayState) => void, options?: {
|
|
102
|
+
fetchFn?: typeof fetch;
|
|
103
|
+
registerUrl?: string;
|
|
104
|
+
unregisterUrl?: string;
|
|
105
|
+
/** The public suffix the hostname template uses (default
|
|
106
|
+
* {@link RELAY_BASE_SUFFIX}). */
|
|
107
|
+
publicSuffix?: string;
|
|
108
|
+
baseDelayMs?: number;
|
|
109
|
+
maxDelayMs?: number;
|
|
110
|
+
timerFn?: (fn: () => void, ms: number) => NodeJS.Timeout;
|
|
111
|
+
});
|
|
112
|
+
/** The stable relay base for the QR while registered. */
|
|
113
|
+
get baseUrl(): string;
|
|
114
|
+
/** The last registration failure detail (undefined while healthy). */
|
|
115
|
+
get error(): string | undefined;
|
|
116
|
+
/** Push one tunnel target; resolves the stable base once accepted. */
|
|
117
|
+
announce(target: string): Promise<string | undefined>;
|
|
118
|
+
/** Best-effort removal of the registry row (the relay toggle turned off). */
|
|
119
|
+
unregister(): Promise<void>;
|
|
120
|
+
private scheduleRetry;
|
|
121
|
+
/** Stop retrying and clear the card state (tunnel stopped or disabled). */
|
|
122
|
+
dispose(): void;
|
|
123
|
+
}
|