@linxin666/dsh-remote-bridge 0.4.12 → 0.4.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.i18n.yaml +2 -2
- package/README.md +45 -8
- package/README.zh.md +45 -8
- package/lib/client.js +1 -1
- package/lib/client.js.map +1 -1
- package/lib/index.js +18031 -373
- package/lib/types/core/connection.d.ts +9 -0
- package/lib/types/core/devices.d.ts +27 -6
- package/lib/types/core/liveactivity.d.ts +7 -0
- package/lib/types/core/model-efforts.d.ts +83 -0
- package/lib/types/core/protocol.d.ts +2 -0
- package/lib/types/core/push.d.ts +51 -2
- package/lib/types/host/firewall.d.ts +74 -0
- package/lib/types/host/host-bind-capability.d.ts +90 -0
- package/lib/types/host/ingress-gate.d.ts +59 -0
- package/lib/types/host/ingress-sync.d.ts +429 -0
- package/lib/types/host/lan-bind-plan.d.ts +96 -0
- package/lib/types/host/lan-bind.d.ts +106 -0
- package/lib/types/host/lan-forward.d.ts +112 -0
- package/lib/types/host/lan.d.ts +61 -0
- package/lib/types/host/pair-origin.d.ts +34 -19
- package/lib/types/host/relay-bridge-connector.d.ts +215 -0
- package/lib/types/host/relay-connector.d.ts +152 -0
- package/lib/types/host/relay-control.d.ts +124 -0
- package/lib/types/host/relay-registry.d.ts +114 -0
- package/lib/types/host/relay.d.ts +170 -0
- package/lib/types/host/routes.d.ts +24 -1
- package/lib/types/host/tunnel-orphan.d.ts +145 -0
- package/lib/types/host/tunnel-plan.d.ts +41 -0
- package/lib/types/host/tunnel.d.ts +289 -0
- package/lib/types/index.d.ts +426 -1
- package/package.json +4 -2
- package/src/client/panel.module.css +16 -1
- package/src/core/connection.ts +206 -11
- package/src/core/devices.ts +31 -6
- package/src/core/hub.ts +17 -5
- package/src/core/liveactivity.ts +16 -2
- package/src/core/model-efforts.ts +223 -0
- package/src/core/protocol.ts +3 -0
- package/src/core/push.ts +70 -4
- package/src/host/firewall.ts +230 -0
- package/src/host/host-bind-capability.ts +249 -0
- package/src/host/ingress-gate.ts +203 -0
- package/src/host/ingress-sync.ts +1074 -0
- package/src/host/lan-bind-plan.ts +134 -0
- package/src/host/lan-bind.ts +250 -0
- package/src/host/lan-forward.ts +321 -0
- package/src/host/lan.ts +142 -0
- package/src/host/pair-origin.ts +44 -38
- package/src/host/relay-bridge-connector.ts +783 -0
- package/src/host/relay-connector.ts +317 -0
- package/src/host/relay-control.ts +224 -0
- package/src/host/relay-registry.ts +263 -0
- package/src/host/relay.ts +294 -0
- package/src/host/routes.ts +47 -3
- package/src/host/tunnel-orphan.ts +325 -0
- package/src/host/tunnel-plan.ts +58 -0
- package/src/host/tunnel.ts +688 -0
- package/src/index.ts +1280 -22
|
@@ -0,0 +1,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.
|
|
@@ -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;
|