@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,152 @@
|
|
|
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
|
+
/** Observe the lease state (logs only — the card still renders the registry). */
|
|
98
|
+
onState?: (state: RelayPairState) => void;
|
|
99
|
+
baseDelayMs?: number;
|
|
100
|
+
maxDelayMs?: number;
|
|
101
|
+
/** Schedule one retry; defaults to an unref'd node timer. */
|
|
102
|
+
timerFn?: (fn: () => void, ms: number) => {
|
|
103
|
+
unref?: () => void;
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Own the lease half of the relay: claim it on every tunnel start, bring the
|
|
108
|
+
* named connector up beside the quick one, and retry a failed claim with a
|
|
109
|
+
* capped backoff while the quick connector keeps serving.
|
|
110
|
+
*/
|
|
111
|
+
export declare class RelayConnectorPair {
|
|
112
|
+
private readonly identity;
|
|
113
|
+
private readonly options;
|
|
114
|
+
private stateValue;
|
|
115
|
+
private leaseValue;
|
|
116
|
+
private attempts;
|
|
117
|
+
private timer;
|
|
118
|
+
private disposed;
|
|
119
|
+
constructor(identity: RelayIdentity, options?: RelayConnectorPairOptions);
|
|
120
|
+
/** The lease half's current state. */
|
|
121
|
+
get state(): RelayPairState;
|
|
122
|
+
/** The connector the phone is served from, for the last accepted lease. */
|
|
123
|
+
get served(): RelayConnectorMode | undefined;
|
|
124
|
+
/** The last accepted lease (undefined until one lands). */
|
|
125
|
+
get lease(): RelayClaim | undefined;
|
|
126
|
+
/**
|
|
127
|
+
* Claim one lease and start (or restart) the named connector for it. A
|
|
128
|
+
* refusal is graded into a retry and never tears down the quick connector:
|
|
129
|
+
* the instance keeps the origin the registry already gave it.
|
|
130
|
+
* @param input - the local origin port and the reporting plugin version.
|
|
131
|
+
* @returns the accepted lease, or undefined when the attempt did not land.
|
|
132
|
+
*/
|
|
133
|
+
start(input: {
|
|
134
|
+
port: number;
|
|
135
|
+
pluginVersion?: string;
|
|
136
|
+
}): Promise<RelayClaim | undefined>;
|
|
137
|
+
/**
|
|
138
|
+
* The plugin's offline action (toggle off, profile unmount): release the
|
|
139
|
+
* lease and stop the idle connector. Never retires — the hostname is the
|
|
140
|
+
* phone's bookmark, and the tunnel behind it survives.
|
|
141
|
+
*/
|
|
142
|
+
release(): Promise<void>;
|
|
143
|
+
/** Stop retrying and drop the connector (the relay half was disposed). */
|
|
144
|
+
dispose(): void;
|
|
145
|
+
private startNamed;
|
|
146
|
+
private stopNamed;
|
|
147
|
+
/** Bring the bridge connector up for one accepted lease, if it carries one. */
|
|
148
|
+
private startBridge;
|
|
149
|
+
private scheduleRetry;
|
|
150
|
+
private clearTimer;
|
|
151
|
+
private setState;
|
|
152
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
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
|
+
/** The control-plane endpoints (market apex, never a relay hostname). */
|
|
15
|
+
export declare const RELAY_CLAIM_URL = "https://dsh-market.com/api/relay/claim";
|
|
16
|
+
export declare const RELAY_RELEASE_URL = "https://dsh-market.com/api/relay/release";
|
|
17
|
+
/** Which connector serves the relay hostname. `bridge` is the Durable Object
|
|
18
|
+
* meeting point (`relay-bridge-connector.ts`), `named` the instance's own cloud
|
|
19
|
+
* tunnel, `quick` today's Worker proxy path. */
|
|
20
|
+
export type RelayConnectorMode = 'named' | 'quick' | 'bridge';
|
|
21
|
+
/** What a bridge connector needs to dial: an endpoint and a credential to
|
|
22
|
+
* present. The credential is the lease's own long-lived id and secret -- the
|
|
23
|
+
* reply names it rather than repeating a secret in a body. */
|
|
24
|
+
export interface RelayBridgeGrant {
|
|
25
|
+
endpoint: string;
|
|
26
|
+
instance: string;
|
|
27
|
+
credential: 'lease-secret';
|
|
28
|
+
}
|
|
29
|
+
/** One accepted lease, as the control plane answers it. */
|
|
30
|
+
export interface RelayClaim {
|
|
31
|
+
mode: RelayConnectorMode;
|
|
32
|
+
/** The instance's own tunnel. Absent for a bridged lease: there is nothing
|
|
33
|
+
* to run and no token to run it with. */
|
|
34
|
+
tunnel?: {
|
|
35
|
+
id: string;
|
|
36
|
+
token: string;
|
|
37
|
+
};
|
|
38
|
+
hostname: string;
|
|
39
|
+
/** The CNAME target behind the hostname; absent for a bridged lease too. */
|
|
40
|
+
dns?: {
|
|
41
|
+
target: string;
|
|
42
|
+
};
|
|
43
|
+
/** Present when the control plane can route this instance over the bridge. */
|
|
44
|
+
bridge?: RelayBridgeGrant;
|
|
45
|
+
}
|
|
46
|
+
/** What one claim attempt resolved to. */
|
|
47
|
+
export type RelayClaimOutcome = {
|
|
48
|
+
ok: true;
|
|
49
|
+
claim: RelayClaim;
|
|
50
|
+
} | {
|
|
51
|
+
ok: false;
|
|
52
|
+
status: number;
|
|
53
|
+
error: string;
|
|
54
|
+
retryAfterMs?: number;
|
|
55
|
+
};
|
|
56
|
+
/** One claim request (the lease the caller wants refreshed). */
|
|
57
|
+
export interface RelayClaimRequest {
|
|
58
|
+
id: string;
|
|
59
|
+
secret: string;
|
|
60
|
+
/** Carried only by a fresh identity: the first claim creates the row. */
|
|
61
|
+
newSecret?: string;
|
|
62
|
+
/** Local origin port the lease ingress points at (`ctx.webServer.port`). */
|
|
63
|
+
port: number;
|
|
64
|
+
/** The plugin version, for the adoption readout of the migration. */
|
|
65
|
+
pluginVersion?: string;
|
|
66
|
+
}
|
|
67
|
+
/** The capped backoff a failed claim retries on. */
|
|
68
|
+
export declare const CLAIM_RETRY_BASE_MS = 5000;
|
|
69
|
+
export declare const CLAIM_RETRY_MAX_MS = 60000;
|
|
70
|
+
/** Build the claim body (an absent value is omitted, never sent as null). */
|
|
71
|
+
export declare function relayClaimBody(request: RelayClaimRequest): string;
|
|
72
|
+
/** One lease read back from an accepted claim, or undefined for a bad shape. */
|
|
73
|
+
export declare function relayClaimOf(value: unknown): RelayClaim | undefined;
|
|
74
|
+
/** The bridge grant of one accepted lease, or undefined when it carries none. */
|
|
75
|
+
export declare function relayBridgeGrantOf(value: unknown): RelayBridgeGrant | undefined;
|
|
76
|
+
/**
|
|
77
|
+
* Claim one lease. A transport failure is graded like `502 cf-api-unavailable`
|
|
78
|
+
* (status 0) so the caller retries on the same path as a refused upstream.
|
|
79
|
+
* @param request - the lease to refresh.
|
|
80
|
+
* @param options - endpoint and fetch seams (defaults are the real ones).
|
|
81
|
+
*/
|
|
82
|
+
export declare function claimRelayLease(request: RelayClaimRequest, options?: {
|
|
83
|
+
url?: string;
|
|
84
|
+
fetchFn?: typeof fetch;
|
|
85
|
+
}): Promise<RelayClaimOutcome>;
|
|
86
|
+
/**
|
|
87
|
+
* Release one lease (the plugin's offline action: toggle off, profile
|
|
88
|
+
* unmount). Best-effort — a release that never lands only leaves the row
|
|
89
|
+
* marked active, which the 90-day GC recovers.
|
|
90
|
+
*/
|
|
91
|
+
export declare function releaseRelayLease(identity: RelayIdentity, options?: {
|
|
92
|
+
url?: string;
|
|
93
|
+
fetchFn?: typeof fetch;
|
|
94
|
+
}): Promise<void>;
|
|
95
|
+
/** How one failed claim should be retried. */
|
|
96
|
+
export interface RelayClaimRetry {
|
|
97
|
+
/** Whether another attempt is worth making at all. */
|
|
98
|
+
retry: boolean;
|
|
99
|
+
/** How long to wait before that attempt. */
|
|
100
|
+
delayMs: number;
|
|
101
|
+
/**
|
|
102
|
+
* Whether the identity must be re-claimed with `new_secret`: the control
|
|
103
|
+
* plane lost the row (a D1 restore or a manual wipe), which is the one
|
|
104
|
+
* condition a fresh claim fixes.
|
|
105
|
+
*/
|
|
106
|
+
refreshIdentity: boolean;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Grade one failed claim into a retry decision.
|
|
110
|
+
*
|
|
111
|
+
* `403 auth-failed` is terminal: the id belongs to another secret and no
|
|
112
|
+
* number of retries can take it back. Everything else retries on a capped
|
|
113
|
+
* exponential backoff, with the control plane's own `retryAfterMs` (a 502
|
|
114
|
+
* names it) raising the floor so a retry cannot land inside a known outage.
|
|
115
|
+
* @param outcome - the refused attempt.
|
|
116
|
+
* @param attempt - how many attempts already failed (0 for the first).
|
|
117
|
+
* @param options - backoff bounds (defaults are the shipped ones).
|
|
118
|
+
*/
|
|
119
|
+
export declare function relayClaimRetryOf(outcome: Extract<RelayClaimOutcome, {
|
|
120
|
+
ok: false;
|
|
121
|
+
}>, attempt: number, options?: {
|
|
122
|
+
baseDelayMs?: number;
|
|
123
|
+
maxDelayMs?: number;
|
|
124
|
+
}): RelayClaimRetry;
|
|
@@ -0,0 +1,114 @@
|
|
|
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 = "https://dsh-market.com/api/relay/register";
|
|
19
|
+
export declare const RELAY_UNREGISTER_URL = "https://dsh-market.com/api/relay/unregister";
|
|
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
|
+
export declare const RELAY_BASE_SUFFIX = ".dsh-market.com";
|
|
26
|
+
export declare const RELAY_ID_RE: RegExp;
|
|
27
|
+
export declare const RELAY_SECRET_RE: RegExp;
|
|
28
|
+
/** Persisted per-profile relay identity. */
|
|
29
|
+
export interface RelayIdentity {
|
|
30
|
+
id: string;
|
|
31
|
+
secret: string;
|
|
32
|
+
/**
|
|
33
|
+
* True only for an identity minted in this process that has never been
|
|
34
|
+
* accepted by the registry: the first register carries `new_secret` so
|
|
35
|
+
* the endpoint may create the row; refreshes authenticate with `secret`
|
|
36
|
+
* alone and must not re-claim an id that already exists.
|
|
37
|
+
*/
|
|
38
|
+
fresh?: boolean;
|
|
39
|
+
}
|
|
40
|
+
/** The observable relay lifecycle the settings card renders. */
|
|
41
|
+
export type RelayState = {
|
|
42
|
+
state: 'off';
|
|
43
|
+
} | {
|
|
44
|
+
state: 'registering';
|
|
45
|
+
} | {
|
|
46
|
+
state: 'running';
|
|
47
|
+
url: string;
|
|
48
|
+
} | {
|
|
49
|
+
state: 'failed';
|
|
50
|
+
error: string;
|
|
51
|
+
};
|
|
52
|
+
/** Registry storage layout: one file per profile under the relay directory. */
|
|
53
|
+
export declare function relayIdentityFile(profile: string, home?: string): string;
|
|
54
|
+
/** Generate one identity pair (id: 8-byte hex slug; secret: 32-byte base64url). */
|
|
55
|
+
export declare function generateRelayIdentity(random?: (size: number) => Buffer): RelayIdentity;
|
|
56
|
+
/**
|
|
57
|
+
* Load (or lazily mint) the per-profile relay identity. The file is written
|
|
58
|
+
* owner-only on first mint; a corrupt file is reminted (the phone then
|
|
59
|
+
* re-pairs once — the same cost as losing the devices file).
|
|
60
|
+
*/
|
|
61
|
+
export declare function loadRelayIdentity(profile: string, home?: string, random?: (size: number) => Buffer): RelayIdentity;
|
|
62
|
+
/**
|
|
63
|
+
* 读一份**已经存在**的身份,绝不生成。
|
|
64
|
+
*
|
|
65
|
+
* 与 {@link loadRelayIdentity} 的唯一区别就在这里:文件不在、或读不出合法的
|
|
66
|
+
* id/secret 时返回 undefined,既不 mint 也不写盘,目录也不会被创建。关掉 relay 的
|
|
67
|
+
* 挂载路径靠它收掉上一次挂载留下的注册行与租约——挂载本身不是「申请一份新身份」的
|
|
68
|
+
* 理由,一个从没配过 relay 的宿主不该因为挂载就在 $DSH_HOME 里落下一份密钥文件。
|
|
69
|
+
* 坏文件同样原样留在盘上:等 relay 真的打开时由 loadRelayIdentity 按既有语义重新生成。
|
|
70
|
+
* @param profile - 受管 profile。
|
|
71
|
+
* @param home - DSH home(测试注入)。
|
|
72
|
+
* @returns 已存在的身份,或 undefined(没有可用身份,且什么都没写)。
|
|
73
|
+
*/
|
|
74
|
+
export declare function readExistingRelayIdentity(profile: string, home?: string): RelayIdentity | undefined;
|
|
75
|
+
/** Best-effort removal of the mapping (used on disable; errors are swallowed). */
|
|
76
|
+
export declare function unregisterRelay(identity: RelayIdentity, endpoint?: string, fetchFn?: typeof fetch): Promise<void>;
|
|
77
|
+
/**
|
|
78
|
+
* The stable relay base for one identity — the origin the QR and the phone
|
|
79
|
+
* bookmark use while (or after) the registration is accepted.
|
|
80
|
+
*/
|
|
81
|
+
export declare function relayBaseOf(identity: RelayIdentity): string;
|
|
82
|
+
/**
|
|
83
|
+
* Registration loop with capped exponential backoff. Re-registration happens
|
|
84
|
+
* on every tunnel start (the quick URL can change after a crash-restart);
|
|
85
|
+
* failures retry with backoff and surface the last error to the card.
|
|
86
|
+
*/
|
|
87
|
+
export declare class RelayRegistrar {
|
|
88
|
+
private readonly identity;
|
|
89
|
+
private readonly setState;
|
|
90
|
+
private readonly options;
|
|
91
|
+
private timer;
|
|
92
|
+
private attempts;
|
|
93
|
+
private lastError;
|
|
94
|
+
private disposed;
|
|
95
|
+
constructor(identity: RelayIdentity, setState: (state: RelayState) => void, options?: {
|
|
96
|
+
fetchFn?: typeof fetch;
|
|
97
|
+
registerUrl?: string;
|
|
98
|
+
unregisterUrl?: string;
|
|
99
|
+
baseDelayMs?: number;
|
|
100
|
+
maxDelayMs?: number;
|
|
101
|
+
timerFn?: (fn: () => void, ms: number) => NodeJS.Timeout;
|
|
102
|
+
});
|
|
103
|
+
/** The stable relay base for the QR while registered. */
|
|
104
|
+
get baseUrl(): string;
|
|
105
|
+
/** The last registration failure detail (undefined while healthy). */
|
|
106
|
+
get error(): string | undefined;
|
|
107
|
+
/** Push one tunnel target; resolves the stable base once accepted. */
|
|
108
|
+
announce(target: string): Promise<string | undefined>;
|
|
109
|
+
/** Best-effort removal of the registry row (the relay toggle turned off). */
|
|
110
|
+
unregister(): Promise<void>;
|
|
111
|
+
private scheduleRetry;
|
|
112
|
+
/** Stop retrying and clear the card state (tunnel stopped or disabled). */
|
|
113
|
+
dispose(): void;
|
|
114
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The hosted relay this bridge hands a notification to when it holds no Apple key.
|
|
3
|
+
*
|
|
4
|
+
* §28's channel needs an Apple signing key, and a distributed App cannot get one
|
|
5
|
+
* onto every user's machine: the key would have to be shipped with the App's
|
|
6
|
+
* operator, which is the one thing a store release may not do. So the same event
|
|
7
|
+
* can be delivered two ways, and this file is the second one — one HTTPS POST to
|
|
8
|
+
* a service that signs with a key no deployment holds.
|
|
9
|
+
*
|
|
10
|
+
* Four rules, each of them borrowed from the direct path beside it:
|
|
11
|
+
*
|
|
12
|
+
* - the bridge sends the EVENT, not the state: `{event, sessionId, title, workspace}`
|
|
13
|
+
* for a notification and `{event: 'live-activity', sessionId, activityId,
|
|
14
|
+
* activityEvent, contentState, dismiss?}` for a card. It no longer decides the APNs
|
|
15
|
+
* headers, the bundle, the environment or the alert bodies — the service does,
|
|
16
|
+
* from the registration the App made directly with it;
|
|
17
|
+
* - a failure is a return value, never a rejection and never a retry: both callers
|
|
18
|
+
* sit on frame paths that must not learn about the network at all (§28.5);
|
|
19
|
+
* - the relay token is the phone's credential, scoped to this pairing's
|
|
20
|
+
* fingerprint. It is read off the roster row and put in one header; it is never
|
|
21
|
+
* logged, echoed in a frame, or written anywhere but that row;
|
|
22
|
+
* - the route is exclusive with the local key rather than a fallback chain. A
|
|
23
|
+
* machine that holds a key keeps signing for itself, so a developer's delivery
|
|
24
|
+
* never travels through the service and a hosted deployment never silently
|
|
25
|
+
* switches routes halfway through a session.
|
|
26
|
+
* @module dsh-remote-bridge/host/relay
|
|
27
|
+
*/
|
|
28
|
+
import type { LiveActivityAddress, LiveActivityNotification, LiveActivitySender } from '../core/liveactivity.ts';
|
|
29
|
+
import type { PushAddress, PushNotification, PushRoute, PushSendResult, PushSender } from '../core/push.ts';
|
|
30
|
+
/**
|
|
31
|
+
* Where the hosted relay lives by default.
|
|
32
|
+
*
|
|
33
|
+
* The account service the App already signs into: one deployment, one hostname, so
|
|
34
|
+
* an operator with no relay of their own configures nothing at all.
|
|
35
|
+
*/
|
|
36
|
+
export declare const RELAY_DEFAULT_URL = "https://account.dsh-market.com";
|
|
37
|
+
/** The environment variable that moves the relay to another deployment. */
|
|
38
|
+
export declare const RELAY_ENV: {
|
|
39
|
+
readonly url: "DSH_REMOTE_BRIDGE_RELAY_URL";
|
|
40
|
+
};
|
|
41
|
+
/** The one path a delivery is posted to. */
|
|
42
|
+
export declare const RELAY_PATH = "/v1/push/relay";
|
|
43
|
+
/** How long one delivery may take before it is abandoned. */
|
|
44
|
+
export declare const RELAY_TIMEOUT_MS = 10000;
|
|
45
|
+
/**
|
|
46
|
+
* Read the relay's base address.
|
|
47
|
+
* @param env - the environment to read; defaults to this process's.
|
|
48
|
+
* @returns the trimmed base with no trailing slash, or {@link RELAY_DEFAULT_URL}.
|
|
49
|
+
*/
|
|
50
|
+
export declare function relayUrlFromEnv(env?: NodeJS.ProcessEnv): string;
|
|
51
|
+
/** The notification body the relay accepts (contract v1, frozen). */
|
|
52
|
+
export interface RelayNotificationRequest {
|
|
53
|
+
readonly event: PushNotification['st'];
|
|
54
|
+
readonly sessionId: string;
|
|
55
|
+
/** The session's own name; the service substitutes its fixed line when absent. */
|
|
56
|
+
readonly title?: string;
|
|
57
|
+
/** The workspace's short name, which is a directory's basename and not a path. */
|
|
58
|
+
readonly workspace?: string;
|
|
59
|
+
}
|
|
60
|
+
/** The Live Activity body the relay accepts (contract v1, frozen). */
|
|
61
|
+
export interface RelayLiveActivityRequest {
|
|
62
|
+
readonly event: 'live-activity';
|
|
63
|
+
readonly sessionId: string;
|
|
64
|
+
readonly activityId: string;
|
|
65
|
+
readonly activityEvent: LiveActivityNotification['aps']['event'];
|
|
66
|
+
readonly contentState: LiveActivityNotification['aps']['content-state'];
|
|
67
|
+
/**
|
|
68
|
+
* Whether the card must go the moment this delivery lands, rather than at its
|
|
69
|
+
* `stale-date`. Only ever `true`: the key is absent when the card stays, because
|
|
70
|
+
* a `false` would be a second way to say the same thing and an older service
|
|
71
|
+
* would have to learn it. The dismissal date itself is the service's to write —
|
|
72
|
+
* it is the one that knows when the delivery landed (§29.3).
|
|
73
|
+
*/
|
|
74
|
+
readonly dismiss?: boolean;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* The notification one delivery becomes.
|
|
78
|
+
*
|
|
79
|
+
* Pure, and exported so the body can be asserted without a network. What crosses is
|
|
80
|
+
* exactly what §28.4 already allows on a lock screen — the event, the session id,
|
|
81
|
+
* the session's name and the workspace's short name — and nothing else: no message,
|
|
82
|
+
* no tool output, no path, no question body.
|
|
83
|
+
* @param notification - the payload the notifier built for the direct path.
|
|
84
|
+
* @returns the body to POST.
|
|
85
|
+
*/
|
|
86
|
+
export declare function buildRelayNotification(notification: PushNotification): RelayNotificationRequest;
|
|
87
|
+
/**
|
|
88
|
+
* The Live Activity body one delivery becomes.
|
|
89
|
+
*
|
|
90
|
+
* The card's content state crosses verbatim: it is the App's own
|
|
91
|
+
* `SessionActivityState` (§29.3), already built by `core/liveactivity.ts` with its
|
|
92
|
+
* frozen key set and Apple-reference timestamps, and rebuilding it here would be a
|
|
93
|
+
* second answer to the same encoding question.
|
|
94
|
+
* @param target - the card being pushed to.
|
|
95
|
+
* @param notification - the payload the notifier built for the direct path.
|
|
96
|
+
* @returns the body to POST, or undefined when the card names no session.
|
|
97
|
+
*/
|
|
98
|
+
export declare function buildRelayLiveActivity(target: LiveActivityAddress, notification: LiveActivityNotification): RelayLiveActivityRequest | undefined;
|
|
99
|
+
/** The 200 body the relay answers with (contract v1, frozen). */
|
|
100
|
+
export interface RelayAnswer {
|
|
101
|
+
readonly ok?: boolean;
|
|
102
|
+
/** How many devices took the delivery. */
|
|
103
|
+
readonly delivered?: number;
|
|
104
|
+
/** How many registrations the relay judged dead and cleaned up. */
|
|
105
|
+
readonly dead?: number;
|
|
106
|
+
}
|
|
107
|
+
/** The one request shape this file makes, with the answer narrowed to what it reads. */
|
|
108
|
+
export type RelayFetch = (url: string, init: {
|
|
109
|
+
readonly method: string;
|
|
110
|
+
readonly headers: Readonly<Record<string, string>>;
|
|
111
|
+
readonly body: string;
|
|
112
|
+
readonly signal: AbortSignal;
|
|
113
|
+
}) => Promise<{
|
|
114
|
+
readonly status: number;
|
|
115
|
+
text(): Promise<string>;
|
|
116
|
+
}>;
|
|
117
|
+
export interface RelaySenderOptions {
|
|
118
|
+
/** The relay's base address, from {@link relayUrlFromEnv}. */
|
|
119
|
+
readonly url: string;
|
|
120
|
+
/** The HTTP client; the global `fetch` by default, injectable so failures need no network. */
|
|
121
|
+
readonly fetch?: RelayFetch;
|
|
122
|
+
readonly timeoutMs?: number;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* The relay half of the push channel.
|
|
126
|
+
*
|
|
127
|
+
* Every method resolves: a delivery that failed is a {@link PushSendResult} with
|
|
128
|
+
* `ok: false`, never a rejection, because both callers sit on frame paths that must
|
|
129
|
+
* not learn about the network at all. Nothing is retried — an alert that could not
|
|
130
|
+
* be delivered is one line in the log, and the next event is the next attempt.
|
|
131
|
+
*/
|
|
132
|
+
export declare class RelaySender implements PushSender, LiveActivitySender {
|
|
133
|
+
#private;
|
|
134
|
+
constructor(options: RelaySenderOptions);
|
|
135
|
+
/** Deliver one notification, or report why it did not arrive. */
|
|
136
|
+
send(target: PushAddress, notification: PushNotification): Promise<PushSendResult>;
|
|
137
|
+
/** Deliver one Live Activity push, or report why it did not arrive. */
|
|
138
|
+
sendLiveActivity(target: LiveActivityAddress, notification: LiveActivityNotification): Promise<PushSendResult>;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Which road this bridge stands on, given the two credentials it could hold.
|
|
142
|
+
*
|
|
143
|
+
* A local key wins over a relay token, deliberately: the machine that can sign for
|
|
144
|
+
* itself must not route a developer's events through the hosted service, and the
|
|
145
|
+
* two routes never run side by side for one event.
|
|
146
|
+
* @param localKey - whether this machine holds a complete APNs credential.
|
|
147
|
+
* @param relayArmed - whether some paired row holds a relay token.
|
|
148
|
+
* @returns the route the channel is on.
|
|
149
|
+
*/
|
|
150
|
+
export declare function pushRouteOf(localKey: boolean, relayArmed: boolean): PushRoute;
|
|
151
|
+
/** What the mount log needs to know to say where this bridge stands. */
|
|
152
|
+
export interface PushChannelDescription {
|
|
153
|
+
readonly route: PushRoute;
|
|
154
|
+
/** The key id of the local credential, when the route is `direct`. */
|
|
155
|
+
readonly keyId?: string;
|
|
156
|
+
/** The team that key belongs to, when the route is `direct`. */
|
|
157
|
+
readonly teamId?: string;
|
|
158
|
+
/** The relay's base address, when the route is `relay`. */
|
|
159
|
+
readonly relayUrl?: string;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* The one line the mount log says about the push channel.
|
|
163
|
+
*
|
|
164
|
+
* Three routes, three lines, and the line names the credential each one reads: an
|
|
165
|
+
* operator reading a log has to be able to tell a bridge signing for itself from
|
|
166
|
+
* one asking a service to, without opening any file.
|
|
167
|
+
* @param description - the route and the parts of it worth naming.
|
|
168
|
+
* @returns the line to log.
|
|
169
|
+
*/
|
|
170
|
+
export declare function pushChannelLine(description: PushChannelDescription): string;
|
|
@@ -14,9 +14,32 @@ import { type PairOriginFacts } from './pair-origin.ts';
|
|
|
14
14
|
export interface PairTokenRouteDeps {
|
|
15
15
|
readonly pairing: PairingService;
|
|
16
16
|
readonly log: (message: string) => void;
|
|
17
|
-
/**
|
|
17
|
+
/**
|
|
18
|
+
* 桥自己此刻的公网入口(relay 租约的 hostname 或隧道的 running URL),由接线方
|
|
19
|
+
* 注入:本模块不认识隧道与租约,只按顺序问地址。返回实时快照,不缓存。
|
|
20
|
+
*/
|
|
21
|
+
readonly realtime?: () => string | undefined;
|
|
22
|
+
/** 无公网时的局域网兜底:本机地址与 webServer 端口。 */
|
|
23
|
+
readonly lan?: () => {
|
|
24
|
+
address: string;
|
|
25
|
+
port: number;
|
|
26
|
+
} | undefined;
|
|
27
|
+
/** Origin discovery seam; defaults to {@link discoverPairOrigin} over the two seams above. */
|
|
18
28
|
readonly pairOrigin?: () => PairOriginFacts | undefined;
|
|
19
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* The WebSocket URL a phone dials for one one-shot ticket.
|
|
32
|
+
*
|
|
33
|
+
* `origin` names where this host is reachable (`https://<id>.dsh-market.com`,
|
|
34
|
+
* `http://192.168.1.5:3080`), but the handshake this URL opens is a WebSocket
|
|
35
|
+
* one: the scheme follows the transport, so https becomes wss and http becomes
|
|
36
|
+
* ws. Keeping the origin's own scheme would hand the client an HTTP URL and ask
|
|
37
|
+
* it to dial a socket with it.
|
|
38
|
+
* @param origin - the discovered origin, with or without the scheme.
|
|
39
|
+
* @param token - the one-shot ticket to carry in the query.
|
|
40
|
+
* @returns the URL the pairing QR encodes.
|
|
41
|
+
*/
|
|
42
|
+
export declare function pairingDialUrlOf(origin: string, token: string): string;
|
|
20
43
|
/**
|
|
21
44
|
* Build the handler for `GET /remote-bridge/pair/token`.
|
|
22
45
|
* @param deps - the pairing service that mints the token, plus the Host log.
|