@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,294 @@
|
|
|
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
|
+
|
|
29
|
+
import type { LiveActivityAddress, LiveActivityNotification, LiveActivitySender } from '../core/liveactivity.ts'
|
|
30
|
+
import type { PushAddress, PushNotification, PushRoute, PushSendResult, PushSender } from '../core/push.ts'
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Where the hosted relay lives by default.
|
|
34
|
+
*
|
|
35
|
+
* The account service the App already signs into: one deployment, one hostname, so
|
|
36
|
+
* an operator with no relay of their own configures nothing at all.
|
|
37
|
+
*/
|
|
38
|
+
export const RELAY_DEFAULT_URL = 'https://account.chatwithai.icu'
|
|
39
|
+
/** The environment variable that moves the relay to another deployment. */
|
|
40
|
+
export const RELAY_ENV = { url: 'DSH_REMOTE_BRIDGE_RELAY_URL' } as const
|
|
41
|
+
/** The one path a delivery is posted to. */
|
|
42
|
+
export const RELAY_PATH = '/v1/push/relay'
|
|
43
|
+
/** How long one delivery may take before it is abandoned. */
|
|
44
|
+
export const RELAY_TIMEOUT_MS = 10_000
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Read the relay's base address.
|
|
48
|
+
* @param env - the environment to read; defaults to this process's.
|
|
49
|
+
* @returns the trimmed base with no trailing slash, or {@link RELAY_DEFAULT_URL}.
|
|
50
|
+
*/
|
|
51
|
+
export function relayUrlFromEnv(env: NodeJS.ProcessEnv = process.env): string {
|
|
52
|
+
const configured = env[RELAY_ENV.url]?.trim()
|
|
53
|
+
if (configured === undefined || configured === '') return RELAY_DEFAULT_URL
|
|
54
|
+
return configured.endsWith('/') ? configured.slice(0, -1) : configured
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The notification body the relay accepts (contract v1, frozen). */
|
|
58
|
+
export interface RelayNotificationRequest {
|
|
59
|
+
readonly event: PushNotification['st']
|
|
60
|
+
readonly sessionId: string
|
|
61
|
+
/** The session's own name; the service substitutes its fixed line when absent. */
|
|
62
|
+
readonly title?: string
|
|
63
|
+
/** The workspace's short name, which is a directory's basename and not a path. */
|
|
64
|
+
readonly workspace?: string
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** The Live Activity body the relay accepts (contract v1, frozen). */
|
|
68
|
+
export interface RelayLiveActivityRequest {
|
|
69
|
+
readonly event: 'live-activity'
|
|
70
|
+
readonly sessionId: string
|
|
71
|
+
readonly activityId: string
|
|
72
|
+
readonly activityEvent: LiveActivityNotification['aps']['event']
|
|
73
|
+
readonly contentState: LiveActivityNotification['aps']['content-state']
|
|
74
|
+
/**
|
|
75
|
+
* Whether the card must go the moment this delivery lands, rather than at its
|
|
76
|
+
* `stale-date`. Only ever `true`: the key is absent when the card stays, because
|
|
77
|
+
* a `false` would be a second way to say the same thing and an older service
|
|
78
|
+
* would have to learn it. The dismissal date itself is the service's to write —
|
|
79
|
+
* it is the one that knows when the delivery landed (§29.3).
|
|
80
|
+
*/
|
|
81
|
+
readonly dismiss?: boolean
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The notification one delivery becomes.
|
|
86
|
+
*
|
|
87
|
+
* Pure, and exported so the body can be asserted without a network. What crosses is
|
|
88
|
+
* exactly what §28.4 already allows on a lock screen — the event, the session id,
|
|
89
|
+
* the session's name and the workspace's short name — and nothing else: no message,
|
|
90
|
+
* no tool output, no path, no question body.
|
|
91
|
+
* @param notification - the payload the notifier built for the direct path.
|
|
92
|
+
* @returns the body to POST.
|
|
93
|
+
*/
|
|
94
|
+
export function buildRelayNotification(notification: PushNotification): RelayNotificationRequest {
|
|
95
|
+
return {
|
|
96
|
+
event: notification.st,
|
|
97
|
+
sessionId: notification.s,
|
|
98
|
+
// The notifier already decided what may name this event — the session's own
|
|
99
|
+
// title, or the fixed status line when it holds none — so passing it through
|
|
100
|
+
// keeps one answer to "what may a notification say" for both routes.
|
|
101
|
+
title: notification.aps.alert.title,
|
|
102
|
+
...(notification.w === undefined || notification.w === '' ? {} : { workspace: notification.w }),
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The Live Activity body one delivery becomes.
|
|
108
|
+
*
|
|
109
|
+
* The card's content state crosses verbatim: it is the App's own
|
|
110
|
+
* `SessionActivityState` (§29.3), already built by `core/liveactivity.ts` with its
|
|
111
|
+
* frozen key set and Apple-reference timestamps, and rebuilding it here would be a
|
|
112
|
+
* second answer to the same encoding question.
|
|
113
|
+
* @param target - the card being pushed to.
|
|
114
|
+
* @param notification - the payload the notifier built for the direct path.
|
|
115
|
+
* @returns the body to POST, or undefined when the card names no session.
|
|
116
|
+
*/
|
|
117
|
+
export function buildRelayLiveActivity(
|
|
118
|
+
target: LiveActivityAddress,
|
|
119
|
+
notification: LiveActivityNotification,
|
|
120
|
+
): RelayLiveActivityRequest | undefined {
|
|
121
|
+
if (target.sessionId === undefined) return undefined
|
|
122
|
+
return {
|
|
123
|
+
event: 'live-activity',
|
|
124
|
+
sessionId: target.sessionId,
|
|
125
|
+
activityId: target.activityId,
|
|
126
|
+
activityEvent: notification.aps.event,
|
|
127
|
+
contentState: notification.aps['content-state'],
|
|
128
|
+
// The direct payload already decided this: it writes `dismissal-date` only for an
|
|
129
|
+
// `end` whose session left the roster. Reading that decision back off the payload
|
|
130
|
+
// keeps one answer to "does this card go now" for both routes, and the date is
|
|
131
|
+
// left to the service, which stamps its own delivery moment.
|
|
132
|
+
...(notification.aps['dismissal-date'] === undefined ? {} : { dismiss: true }),
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** The 200 body the relay answers with (contract v1, frozen). */
|
|
137
|
+
export interface RelayAnswer {
|
|
138
|
+
readonly ok?: boolean
|
|
139
|
+
/** How many devices took the delivery. */
|
|
140
|
+
readonly delivered?: number
|
|
141
|
+
/** How many registrations the relay judged dead and cleaned up. */
|
|
142
|
+
readonly dead?: number
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** The one request shape this file makes, with the answer narrowed to what it reads. */
|
|
146
|
+
export type RelayFetch = (
|
|
147
|
+
url: string,
|
|
148
|
+
init: {
|
|
149
|
+
readonly method: string
|
|
150
|
+
readonly headers: Readonly<Record<string, string>>
|
|
151
|
+
readonly body: string
|
|
152
|
+
readonly signal: AbortSignal
|
|
153
|
+
},
|
|
154
|
+
) => Promise<{ readonly status: number; text(): Promise<string> }>
|
|
155
|
+
|
|
156
|
+
export interface RelaySenderOptions {
|
|
157
|
+
/** The relay's base address, from {@link relayUrlFromEnv}. */
|
|
158
|
+
readonly url: string
|
|
159
|
+
/** The HTTP client; the global `fetch` by default, injectable so failures need no network. */
|
|
160
|
+
readonly fetch?: RelayFetch
|
|
161
|
+
readonly timeoutMs?: number
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* The relay half of the push channel.
|
|
166
|
+
*
|
|
167
|
+
* Every method resolves: a delivery that failed is a {@link PushSendResult} with
|
|
168
|
+
* `ok: false`, never a rejection, because both callers sit on frame paths that must
|
|
169
|
+
* not learn about the network at all. Nothing is retried — an alert that could not
|
|
170
|
+
* be delivered is one line in the log, and the next event is the next attempt.
|
|
171
|
+
*/
|
|
172
|
+
export class RelaySender implements PushSender, LiveActivitySender {
|
|
173
|
+
readonly #url: string
|
|
174
|
+
readonly #fetch: RelayFetch
|
|
175
|
+
readonly #timeoutMs: number
|
|
176
|
+
|
|
177
|
+
constructor(options: RelaySenderOptions) {
|
|
178
|
+
this.#url = options.url
|
|
179
|
+
this.#fetch = options.fetch ?? ((url, init) => globalThis.fetch(url, init))
|
|
180
|
+
this.#timeoutMs = options.timeoutMs ?? RELAY_TIMEOUT_MS
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** Deliver one notification, or report why it did not arrive. */
|
|
184
|
+
async send(target: PushAddress, notification: PushNotification): Promise<PushSendResult> {
|
|
185
|
+
if (target.relayToken === undefined || target.relayToken === '') {
|
|
186
|
+
return { ok: false, reason: 'this device row holds no relay token' }
|
|
187
|
+
}
|
|
188
|
+
return await this.#post(target.relayToken, buildRelayNotification(notification))
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** Deliver one Live Activity push, or report why it did not arrive. */
|
|
192
|
+
async sendLiveActivity(target: LiveActivityAddress, notification: LiveActivityNotification): Promise<PushSendResult> {
|
|
193
|
+
if (target.relayToken === undefined || target.relayToken === '') {
|
|
194
|
+
return { ok: false, reason: 'this device row holds no relay token' }
|
|
195
|
+
}
|
|
196
|
+
const body = buildRelayLiveActivity(target, notification)
|
|
197
|
+
if (body === undefined) return { ok: false, reason: 'this card names no session, so the relay has nothing to address' }
|
|
198
|
+
return await this.#post(target.relayToken, body)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* POST one already-built body.
|
|
203
|
+
*
|
|
204
|
+
* The relay answers 200 for every outcome, including a delivery Apple refused:
|
|
205
|
+
* `delivered` counts what landed and `dead` counts the tokens it retired. A
|
|
206
|
+
* delivery that landed on nothing is therefore reported as the failure it is,
|
|
207
|
+
* with `dead` telling the roster to forget a registration that can never work
|
|
208
|
+
* again.
|
|
209
|
+
* @param token - the relay credential of the device this is addressed to.
|
|
210
|
+
* @param body - the JSON body to send.
|
|
211
|
+
* @returns whether anything took it, and what the relay said when nothing did.
|
|
212
|
+
*/
|
|
213
|
+
async #post(token: string, body: RelayNotificationRequest | RelayLiveActivityRequest): Promise<PushSendResult> {
|
|
214
|
+
let status: number
|
|
215
|
+
let text: string
|
|
216
|
+
try {
|
|
217
|
+
const response = await this.#fetch(`${this.#url}${RELAY_PATH}`, {
|
|
218
|
+
method: 'POST',
|
|
219
|
+
headers: {
|
|
220
|
+
authorization: `Bearer ${token}`,
|
|
221
|
+
'content-type': 'application/json',
|
|
222
|
+
accept: 'application/json',
|
|
223
|
+
},
|
|
224
|
+
body: JSON.stringify(body),
|
|
225
|
+
signal: AbortSignal.timeout(this.#timeoutMs),
|
|
226
|
+
})
|
|
227
|
+
status = response.status
|
|
228
|
+
text = await response.text()
|
|
229
|
+
} catch (error) {
|
|
230
|
+
// A connection that never answered, a timeout, a DNS failure and a body the
|
|
231
|
+
// proxy rewrote all end here, as the same one line.
|
|
232
|
+
return { ok: false, reason: `relay request failed: ${String(error)}` }
|
|
233
|
+
}
|
|
234
|
+
if (status === 401) return { ok: false, status, reason: 'unauthorized' }
|
|
235
|
+
if (status !== 200) return { ok: false, status, reason: `relay answered ${String(status)}` }
|
|
236
|
+
const answer = parseAnswer(text)
|
|
237
|
+
if (answer === undefined || answer.ok !== true) return { ok: false, status, reason: 'the relay answered no usable result' }
|
|
238
|
+
if ((answer.delivered ?? 0) > 0) return { ok: true, status }
|
|
239
|
+
if ((answer.dead ?? 0) > 0) return { ok: false, status, dead: true, reason: 'the relay retired this registration' }
|
|
240
|
+
return { ok: false, status, reason: 'the relay delivered to no device' }
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** @returns the parsed answer, or undefined when the body is not one. */
|
|
245
|
+
function parseAnswer(text: string): RelayAnswer | undefined {
|
|
246
|
+
try {
|
|
247
|
+
const parsed: unknown = JSON.parse(text)
|
|
248
|
+
if (typeof parsed !== 'object' || parsed === null) return undefined
|
|
249
|
+
return parsed as RelayAnswer
|
|
250
|
+
} catch {
|
|
251
|
+
return undefined
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Which road this bridge stands on, given the two credentials it could hold.
|
|
257
|
+
*
|
|
258
|
+
* A local key wins over a relay token, deliberately: the machine that can sign for
|
|
259
|
+
* itself must not route a developer's events through the hosted service, and the
|
|
260
|
+
* two routes never run side by side for one event.
|
|
261
|
+
* @param localKey - whether this machine holds a complete APNs credential.
|
|
262
|
+
* @param relayArmed - whether some paired row holds a relay token.
|
|
263
|
+
* @returns the route the channel is on.
|
|
264
|
+
*/
|
|
265
|
+
export function pushRouteOf(localKey: boolean, relayArmed: boolean): PushRoute {
|
|
266
|
+
if (localKey) return 'direct'
|
|
267
|
+
return relayArmed ? 'relay' : 'off'
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** What the mount log needs to know to say where this bridge stands. */
|
|
271
|
+
export interface PushChannelDescription {
|
|
272
|
+
readonly route: PushRoute
|
|
273
|
+
/** The key id of the local credential, when the route is `direct`. */
|
|
274
|
+
readonly keyId?: string
|
|
275
|
+
/** The team that key belongs to, when the route is `direct`. */
|
|
276
|
+
readonly teamId?: string
|
|
277
|
+
/** The relay's base address, when the route is `relay`. */
|
|
278
|
+
readonly relayUrl?: string
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* The one line the mount log says about the push channel.
|
|
283
|
+
*
|
|
284
|
+
* Three routes, three lines, and the line names the credential each one reads: an
|
|
285
|
+
* operator reading a log has to be able to tell a bridge signing for itself from
|
|
286
|
+
* one asking a service to, without opening any file.
|
|
287
|
+
* @param description - the route and the parts of it worth naming.
|
|
288
|
+
* @returns the line to log.
|
|
289
|
+
*/
|
|
290
|
+
export function pushChannelLine(description: PushChannelDescription): string {
|
|
291
|
+
if (description.route === 'direct') return `push: direct (APNs key ${description.keyId ?? '?'} of team ${description.teamId ?? '?'})`
|
|
292
|
+
if (description.route === 'relay') return `push: relay (${description.relayUrl ?? RELAY_DEFAULT_URL})`
|
|
293
|
+
return `push: off (no APNs key on this machine and no paired device holds a relay token)`
|
|
294
|
+
}
|
package/src/host/routes.ts
CHANGED
|
@@ -19,8 +19,20 @@ import { discoverPairOrigin, type PairOriginFacts } from './pair-origin.ts'
|
|
|
19
19
|
export interface PairTokenRouteDeps {
|
|
20
20
|
readonly pairing: PairingService
|
|
21
21
|
readonly log: (message: string) => void
|
|
22
|
-
/**
|
|
22
|
+
/**
|
|
23
|
+
* 桥自己此刻的公网入口(relay 租约的 hostname 或隧道的 running URL),由接线方
|
|
24
|
+
* 注入:本模块不认识隧道与租约,只按顺序问地址。返回实时快照,不缓存。
|
|
25
|
+
*/
|
|
26
|
+
readonly realtime?: () => string | undefined
|
|
27
|
+
/** 无公网时的局域网兜底:本机地址与 webServer 端口。 */
|
|
28
|
+
readonly lan?: () => { address: string; port: number } | undefined
|
|
29
|
+
/** Origin discovery seam; defaults to {@link discoverPairOrigin} over the two seams above. */
|
|
23
30
|
readonly pairOrigin?: () => PairOriginFacts | undefined
|
|
31
|
+
/**
|
|
32
|
+
* 桌面端打开配对面板(或本机请求一次性令牌)时的钩子:尚未配对任何设备且未开隧道的
|
|
33
|
+
* 宿主在此时按需触发一次租约申领,避免无人使用远程功能时后台空转。
|
|
34
|
+
*/
|
|
35
|
+
readonly onPairTokenIssued?: () => void
|
|
24
36
|
}
|
|
25
37
|
|
|
26
38
|
/** Write one JSON response. The family's shared reader/writer is not copied into this package. */
|
|
@@ -35,6 +47,33 @@ function writeJson(res: ServerResponse, status: number, body: unknown): void {
|
|
|
35
47
|
res.end(text)
|
|
36
48
|
}
|
|
37
49
|
|
|
50
|
+
/** The schemes one discovered origin can carry, and the socket scheme it dials. */
|
|
51
|
+
const DIAL_SCHEME: Readonly<Record<string, string>> = { 'https://': 'wss://', 'http://': 'ws://' }
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The WebSocket URL a phone dials for one one-shot ticket.
|
|
55
|
+
*
|
|
56
|
+
* `origin` names where this host is reachable (`https://<id>.dsh-market.com`,
|
|
57
|
+
* `http://192.168.1.5:3080`), but the handshake this URL opens is a WebSocket
|
|
58
|
+
* one: the scheme follows the transport, so https becomes wss and http becomes
|
|
59
|
+
* ws. Keeping the origin's own scheme would hand the client an HTTP URL and ask
|
|
60
|
+
* it to dial a socket with it.
|
|
61
|
+
* @param origin - the discovered origin, with or without the scheme.
|
|
62
|
+
* @param token - the one-shot ticket to carry in the query.
|
|
63
|
+
* @returns the URL the pairing QR encodes.
|
|
64
|
+
*/
|
|
65
|
+
export function pairingDialUrlOf(origin: string, token: string): string {
|
|
66
|
+
const base = origin.replace(/\/+$/, '')
|
|
67
|
+
let dial = base
|
|
68
|
+
for (const [scheme, socket] of Object.entries(DIAL_SCHEME)) {
|
|
69
|
+
if (base.startsWith(scheme)) {
|
|
70
|
+
dial = socket + base.slice(scheme.length)
|
|
71
|
+
break
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return dial + WS_PATH + '?token=' + token
|
|
75
|
+
}
|
|
76
|
+
|
|
38
77
|
/**
|
|
39
78
|
* Build the handler for `GET /remote-bridge/pair/token`.
|
|
40
79
|
* @param deps - the pairing service that mints the token, plus the Host log.
|
|
@@ -52,6 +91,11 @@ export function makePairTokenHandler(deps: PairTokenRouteDeps): WebRoute['handle
|
|
|
52
91
|
res.end()
|
|
53
92
|
return
|
|
54
93
|
}
|
|
94
|
+
try {
|
|
95
|
+
deps.onPairTokenIssued?.()
|
|
96
|
+
} catch {
|
|
97
|
+
// 触发后台租约申领失败绝不能影响本机取配对码。
|
|
98
|
+
}
|
|
55
99
|
const ticket = deps.pairing.issueToken()
|
|
56
100
|
// The link a client ON THIS MACHINE can open directly. The token rides the
|
|
57
101
|
// query because a WebSocket handshake started from a share sheet cannot set
|
|
@@ -62,8 +106,18 @@ export function makePairTokenHandler(deps: PairTokenRouteDeps): WebRoute['handle
|
|
|
62
106
|
// The link the PHONE should dial, when this machine knows its own public
|
|
63
107
|
// origin. `pairingUrl` is what a QR should carry: the iOS client parses
|
|
64
108
|
// `{token, url}` and adopts the origin from `url`, so one scan sets both.
|
|
65
|
-
|
|
66
|
-
|
|
109
|
+
//
|
|
110
|
+
// The origin comes from the live-first discovery order (pair-origin.ts): this
|
|
111
|
+
// bridge's own relay lease or running tunnel first, then the configured base,
|
|
112
|
+
// then the LAN forwarder while it is listening. The response shape and the
|
|
113
|
+
// field names are unchanged.
|
|
114
|
+
const discovered = (deps.pairOrigin ?? (() => discoverPairOrigin({
|
|
115
|
+
...(deps.realtime === undefined ? {} : { realtime: deps.realtime }),
|
|
116
|
+
...(deps.lan === undefined ? {} : { lan: deps.lan }),
|
|
117
|
+
})))()
|
|
118
|
+
const pairingUrl = discovered === undefined
|
|
119
|
+
? undefined
|
|
120
|
+
: pairingDialUrlOf(discovered.origin, ticket.value)
|
|
67
121
|
deps.log(
|
|
68
122
|
pairingUrl === undefined
|
|
69
123
|
? `pair a device with ${url}`
|