@linxin666/dsh-remote-bridge 0.4.8 → 0.4.9
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 +50 -17
- package/README.zh.md +50 -17
- package/lib/client.js +274 -253
- package/lib/client.js.map +1 -1
- package/lib/index.js +6690 -2277
- package/lib/types/client/PairEntry.d.ts +2 -2
- package/lib/types/client/PairPanel.d.ts +10 -2
- package/lib/types/client/icons.d.ts +5 -13
- package/lib/types/client/locales.d.ts +10 -14
- package/lib/types/client/semantic.d.ts +9 -5
- package/lib/types/core/commands.d.ts +59 -0
- package/lib/types/core/connection.d.ts +68 -0
- package/lib/types/core/devices.d.ts +98 -2
- package/lib/types/core/hub.d.ts +65 -15
- package/lib/types/core/liveactivity.d.ts +324 -0
- package/lib/types/core/protocol.d.ts +260 -1
- package/lib/types/core/push.d.ts +178 -0
- package/lib/types/core/questions.d.ts +61 -9
- package/lib/types/core/session-meta.d.ts +55 -1
- package/lib/types/core/squads.d.ts +130 -0
- package/lib/types/core/usage-stats.d.ts +160 -0
- package/lib/types/core/usage.d.ts +294 -0
- package/lib/types/host/apns.d.ts +151 -0
- package/lib/types/host/squads-ledger.d.ts +39 -0
- package/lib/types/host/squads.d.ts +77 -0
- package/lib/types/host/usage-stats.d.ts +75 -0
- package/lib/types/host/watchers.d.ts +88 -0
- package/lib/types/index.d.ts +18 -2
- package/package.json +13 -13
- package/src/client/PairEntry.tsx +58 -31
- package/src/client/PairPanel.tsx +181 -97
- package/src/client/icons.tsx +17 -60
- package/src/client/locales.ts +15 -23
- package/src/client/panel.module.css +239 -186
- package/src/client/semantic.ts +10 -6
- package/src/core/commands.ts +95 -0
- package/src/core/connection.ts +646 -10
- package/src/core/devices.ts +221 -3
- package/src/core/hub.ts +362 -42
- package/src/core/liveactivity.ts +833 -0
- package/src/core/protocol.ts +294 -1
- package/src/core/push.ts +354 -0
- package/src/core/questions.ts +81 -10
- package/src/core/session-meta.ts +73 -1
- package/src/core/squads.ts +345 -0
- package/src/core/usage-stats.ts +424 -0
- package/src/core/usage.ts +937 -0
- package/src/core/wire.ts +8 -0
- package/src/host/apns.ts +320 -0
- package/src/host/squads-ledger.ts +105 -0
- package/src/host/squads.ts +206 -0
- package/src/host/usage-stats.ts +155 -0
- package/src/host/watchers.ts +272 -0
- package/src/index.ts +257 -8
package/src/core/devices.ts
CHANGED
|
@@ -20,8 +20,61 @@ import { base64UrlEncode, constantTimeEqualText, fingerprintOfSpki, isP256Spki,
|
|
|
20
20
|
export const DEVICE_TOKEN_BYTES = 32
|
|
21
21
|
/** Entropy of one device id. */
|
|
22
22
|
export const DEVICE_ID_BYTES = 12
|
|
23
|
-
/**
|
|
24
|
-
|
|
23
|
+
/**
|
|
24
|
+
* On-disk schema version of the roster file.
|
|
25
|
+
*
|
|
26
|
+
* 1 carried the six identity fields only; 2 adds the optional `push` block per
|
|
27
|
+
* row (§28). The difference is additive, so a version-1 file reads as-is: no row
|
|
28
|
+
* loses a field, every paired phone stays paired, and the file is rewritten as
|
|
29
|
+
* version 2 the next time anything is written. The reader therefore accepts any
|
|
30
|
+
* version whose row table validates rather than quarantining a file it merely
|
|
31
|
+
* does not recognise — a downgrade must never un-pair every phone.
|
|
32
|
+
*/
|
|
33
|
+
export const ROSTER_VERSION = 2
|
|
34
|
+
|
|
35
|
+
/** The APNs environments a device may register for. */
|
|
36
|
+
export type PushEnvironment = 'sandbox' | 'production'
|
|
37
|
+
|
|
38
|
+
/** One Live Activity push token, with the session the card belongs to. */
|
|
39
|
+
export interface PushLiveActivityToken {
|
|
40
|
+
readonly activityId: string
|
|
41
|
+
readonly token: string
|
|
42
|
+
/**
|
|
43
|
+
* The session this card belongs to (§29.9).
|
|
44
|
+
*
|
|
45
|
+
* A card is about one session, and a token without this field cannot be
|
|
46
|
+
* addressed at all: this bridge has nothing to put on it. Absent is therefore
|
|
47
|
+
* "never pushed to", not "pushed to with whatever is at hand" — the other
|
|
48
|
+
* reading would put one session's state on another session's lock screen.
|
|
49
|
+
*/
|
|
50
|
+
readonly sessionId?: string
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* What one device registered so this bridge can notify it.
|
|
55
|
+
*
|
|
56
|
+
* Stored beside the device's identity in the same row: it is a fact about that
|
|
57
|
+
* device, so it disappears with the row when the device is revoked and it never
|
|
58
|
+
* becomes a second identity. Nothing here is a secret of the bridge's — an APNs
|
|
59
|
+
* token is the phone's, and holding it grants nothing without the signing key,
|
|
60
|
+
* which never touches this file.
|
|
61
|
+
*/
|
|
62
|
+
export interface PushRegistration {
|
|
63
|
+
/** APNs device token, as the hex string the system handed the app. */
|
|
64
|
+
readonly deviceToken: string
|
|
65
|
+
readonly environment: PushEnvironment
|
|
66
|
+
/** The app's bundle id, used verbatim as `apns-topic`. */
|
|
67
|
+
readonly bundle: string
|
|
68
|
+
readonly locale?: string
|
|
69
|
+
readonly liveActivityTokens?: readonly PushLiveActivityToken[]
|
|
70
|
+
readonly registeredAt: number
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** One device a notification can be addressed to. */
|
|
74
|
+
export interface PushTargetView {
|
|
75
|
+
readonly deviceId: string
|
|
76
|
+
readonly push: PushRegistration
|
|
77
|
+
}
|
|
25
78
|
|
|
26
79
|
/** One paired device as the bridge keeps it. */
|
|
27
80
|
export interface DeviceRecord {
|
|
@@ -32,6 +85,12 @@ export interface DeviceRecord {
|
|
|
32
85
|
readonly fingerprint: string
|
|
33
86
|
readonly createdAt: number
|
|
34
87
|
readonly lastSeenAt: number
|
|
88
|
+
/**
|
|
89
|
+
* The device's APNs registration, absent until it calls `push.register`.
|
|
90
|
+
* A row without it is a paired phone that asks for no notification, which is
|
|
91
|
+
* every phone that predates the channel.
|
|
92
|
+
*/
|
|
93
|
+
readonly push?: PushRegistration
|
|
35
94
|
}
|
|
36
95
|
|
|
37
96
|
/** A roster row plus the one field that never leaves the file. */
|
|
@@ -53,6 +112,59 @@ export interface DeviceRosterOptions {
|
|
|
53
112
|
readonly log?: (message: string) => void
|
|
54
113
|
}
|
|
55
114
|
|
|
115
|
+
/**
|
|
116
|
+
* Read one row's `push` block.
|
|
117
|
+
*
|
|
118
|
+
* A malformed block is dropped rather than quarantining the file: the roster is
|
|
119
|
+
* every paired phone, and one unreadable push token is not a reason to un-pair
|
|
120
|
+
* them all. The row keeps its identity and simply asks for no notification.
|
|
121
|
+
* @param value - the raw `push` field of a row.
|
|
122
|
+
* @param log - where a dropped block is reported.
|
|
123
|
+
* @returns the registration, or undefined when there is none to read.
|
|
124
|
+
*/
|
|
125
|
+
function pushOf(value: unknown, log: (message: string) => void): PushRegistration | undefined {
|
|
126
|
+
if (value === undefined) return undefined
|
|
127
|
+
if (typeof value !== 'object' || value === null) {
|
|
128
|
+
log('a device row carries an unreadable push block; ignoring it and keeping the row')
|
|
129
|
+
return undefined
|
|
130
|
+
}
|
|
131
|
+
const row = value as Record<string, unknown>
|
|
132
|
+
const environment = row.environment
|
|
133
|
+
if (typeof row.deviceToken !== 'string' || row.deviceToken === ''
|
|
134
|
+
|| typeof row.bundle !== 'string' || row.bundle === ''
|
|
135
|
+
|| (environment !== 'sandbox' && environment !== 'production')) {
|
|
136
|
+
log('a device row carries an unreadable push block; ignoring it and keeping the row')
|
|
137
|
+
return undefined
|
|
138
|
+
}
|
|
139
|
+
const liveActivityTokens = Array.isArray(row.liveActivityTokens)
|
|
140
|
+
? row.liveActivityTokens.filter((entry): entry is PushLiveActivityToken => {
|
|
141
|
+
if (typeof entry !== 'object' || entry === null) return false
|
|
142
|
+
const token = entry as Record<string, unknown>
|
|
143
|
+
return typeof token.activityId === 'string' && token.activityId !== ''
|
|
144
|
+
&& typeof token.token === 'string' && token.token !== ''
|
|
145
|
+
}).map((entry): PushLiveActivityToken => {
|
|
146
|
+
// A session id this reader cannot trust is dropped rather than repaired: the
|
|
147
|
+
// entry keeps its token and simply names no session, which is the state this
|
|
148
|
+
// bridge never pushes to. One unreadable card is not a reason to un-pair a
|
|
149
|
+
// phone, the same way one unreadable push block is not.
|
|
150
|
+
const raw = (entry as unknown as Record<string, unknown>).sessionId
|
|
151
|
+
return {
|
|
152
|
+
activityId: entry.activityId,
|
|
153
|
+
token: entry.token,
|
|
154
|
+
...(typeof raw === 'string' && raw !== '' ? { sessionId: raw } : {}),
|
|
155
|
+
}
|
|
156
|
+
})
|
|
157
|
+
: undefined
|
|
158
|
+
return {
|
|
159
|
+
deviceToken: row.deviceToken,
|
|
160
|
+
environment,
|
|
161
|
+
bundle: row.bundle,
|
|
162
|
+
...(typeof row.locale === 'string' && row.locale !== '' ? { locale: row.locale } : {}),
|
|
163
|
+
...(liveActivityTokens === undefined ? {} : { liveActivityTokens }),
|
|
164
|
+
registeredAt: typeof row.registeredAt === 'number' ? row.registeredAt : 0,
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
56
168
|
function isStoredDevice(value: unknown): value is StoredDevice {
|
|
57
169
|
if (typeof value !== 'object' || value === null) return false
|
|
58
170
|
const row = value as Record<string, unknown>
|
|
@@ -77,6 +189,7 @@ export class DeviceRoster {
|
|
|
77
189
|
readonly #log: (message: string) => void
|
|
78
190
|
readonly #devices = new Map<string, StoredDevice>()
|
|
79
191
|
#loaded: Promise<void> | undefined
|
|
192
|
+
#writeQueue: Promise<void> = Promise.resolve()
|
|
80
193
|
|
|
81
194
|
constructor(options: DeviceRosterOptions) {
|
|
82
195
|
this.#file = options.file
|
|
@@ -149,6 +262,89 @@ export class DeviceRoster {
|
|
|
149
262
|
if (touched !== undefined) await this.#persist()
|
|
150
263
|
}
|
|
151
264
|
|
|
265
|
+
/**
|
|
266
|
+
* Record this device's APNs registration, replacing any it had.
|
|
267
|
+
*
|
|
268
|
+
* Registering twice is what a token rotation looks like from here, so this is
|
|
269
|
+
* a plain overwrite rather than a merge: the newest complete registration is
|
|
270
|
+
* the only one worth sending to, and a half-merged pair of rows would send to
|
|
271
|
+
* a token the phone already retired.
|
|
272
|
+
* @param deviceId - the device that just registered.
|
|
273
|
+
* @param registration - what it registered.
|
|
274
|
+
* @returns false when no such device is paired.
|
|
275
|
+
*/
|
|
276
|
+
async registerPush(deviceId: string, registration: PushRegistration): Promise<boolean> {
|
|
277
|
+
for (const [hash, row] of this.#devices) {
|
|
278
|
+
if (row.deviceId !== deviceId) continue
|
|
279
|
+
this.#devices.set(hash, { ...row, push: registration })
|
|
280
|
+
await this.#persist()
|
|
281
|
+
return true
|
|
282
|
+
}
|
|
283
|
+
return false
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Forget one Live Activity token the phone no longer holds.
|
|
288
|
+
*
|
|
289
|
+
* APNs answers 410 (or 400 `BadDeviceToken`) for the token of a card the app
|
|
290
|
+
* already ended, and such a token never works again: the next card arrives as a
|
|
291
|
+
* new activity with a new token through `push.register`, which is a complete
|
|
292
|
+
* replacement of this set. Dropping it here is therefore the whole recovery —
|
|
293
|
+
* retrying would only repeat the same refusal (§29 token lifecycle).
|
|
294
|
+
* @param activityId - the card the token belonged to.
|
|
295
|
+
* @param token - the token APNs refused.
|
|
296
|
+
* @returns whether any row changed.
|
|
297
|
+
*/
|
|
298
|
+
async removeLiveActivityToken(activityId: string, token: string): Promise<boolean> {
|
|
299
|
+
for (const [hash, row] of this.#devices) {
|
|
300
|
+
const registration = row.push
|
|
301
|
+
if (registration === undefined) continue
|
|
302
|
+
const held = registration.liveActivityTokens
|
|
303
|
+
if (held === undefined) continue
|
|
304
|
+
const kept = held.filter(entry => !(entry.activityId === activityId && entry.token === token))
|
|
305
|
+
if (kept.length === held.length) continue
|
|
306
|
+
this.#devices.set(hash, { ...row, push: { ...registration, liveActivityTokens: kept } })
|
|
307
|
+
await this.#persist()
|
|
308
|
+
return true
|
|
309
|
+
}
|
|
310
|
+
return false
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Forget one device's notification registration for a token APNs will never
|
|
315
|
+
* accept again.
|
|
316
|
+
*
|
|
317
|
+
* A dead alert token is the whole app install's refusal: the phone was restored,
|
|
318
|
+
* reinstalled, or the token belongs to another environment or bundle, so the
|
|
319
|
+
* Live Activity tokens that same install registered are dead by the same fact
|
|
320
|
+
* and go with it. The next `push.register` — which a live app performs on launch
|
|
321
|
+
* — writes a complete new block. The device itself stays paired: revoking a
|
|
322
|
+
* phone is a roster edit, never something a push refusal may do.
|
|
323
|
+
* @param deviceToken - the token APNs refused.
|
|
324
|
+
* @returns whether any row changed.
|
|
325
|
+
*/
|
|
326
|
+
async removePush(deviceToken: string): Promise<boolean> {
|
|
327
|
+
for (const [hash, row] of this.#devices) {
|
|
328
|
+
const registration = row.push
|
|
329
|
+
if (registration === undefined || registration.deviceToken !== deviceToken) continue
|
|
330
|
+
const { tokenHash, deviceId, pubKey, fingerprint, createdAt, lastSeenAt } = row
|
|
331
|
+
this.#devices.set(hash, { tokenHash, deviceId, pubKey, fingerprint, createdAt, lastSeenAt })
|
|
332
|
+
await this.#persist()
|
|
333
|
+
return true
|
|
334
|
+
}
|
|
335
|
+
return false
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/** @returns every paired device that registered for notifications. */
|
|
339
|
+
pushTargets(): readonly PushTargetView[] {
|
|
340
|
+
const targets: PushTargetView[] = []
|
|
341
|
+
for (const row of this.#devices.values()) {
|
|
342
|
+
if (row.push === undefined) continue
|
|
343
|
+
targets.push({ deviceId: row.deviceId, push: row.push })
|
|
344
|
+
}
|
|
345
|
+
return targets
|
|
346
|
+
}
|
|
347
|
+
|
|
152
348
|
async #read(): Promise<void> {
|
|
153
349
|
let text: string
|
|
154
350
|
try {
|
|
@@ -170,7 +366,21 @@ export class DeviceRoster {
|
|
|
170
366
|
await this.#quarantine('has no valid device table')
|
|
171
367
|
return
|
|
172
368
|
}
|
|
173
|
-
|
|
369
|
+
const declared = (parsed as { version?: unknown }).version
|
|
370
|
+
if (typeof declared === 'number' && declared !== ROSTER_VERSION) {
|
|
371
|
+
// Not an error and not a quarantine: the row table above already validated,
|
|
372
|
+
// so an older file is read as the roster it is and written back as the
|
|
373
|
+
// current schema on the next write.
|
|
374
|
+
this.#log(`device roster ${this.#file} is schema ${declared}; reading it as ${ROSTER_VERSION}`)
|
|
375
|
+
}
|
|
376
|
+
for (const row of rows) {
|
|
377
|
+
// The raw block is re-read per row instead of trusted from the guard, so a
|
|
378
|
+
// row that carries an unreadable token keeps its identity and loses only
|
|
379
|
+
// its notification.
|
|
380
|
+
const { push: raw, ...rest } = row as StoredDevice & { readonly push?: unknown }
|
|
381
|
+
const push = pushOf(raw, this.#log)
|
|
382
|
+
this.#devices.set(row.tokenHash, push === undefined ? rest : { ...rest, push })
|
|
383
|
+
}
|
|
174
384
|
this.#log(`device roster ${this.#file}: ${rows.length} paired device(s)`)
|
|
175
385
|
}
|
|
176
386
|
|
|
@@ -188,8 +398,16 @@ export class DeviceRoster {
|
|
|
188
398
|
/**
|
|
189
399
|
* Write the roster atomically: same directory, 0600 from creation, flushed
|
|
190
400
|
* before the rename so a crash cannot leave a truncated file in place.
|
|
401
|
+
* Writes are serialized per roster so concurrent token retirement cannot race
|
|
402
|
+
* the replacement (Windows rejects overlapping renames).
|
|
191
403
|
*/
|
|
192
404
|
async #persist(): Promise<void> {
|
|
405
|
+
const pending = this.#writeQueue.then(() => this.#write())
|
|
406
|
+
this.#writeQueue = pending.catch(() => undefined)
|
|
407
|
+
return pending
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
async #write(): Promise<void> {
|
|
193
411
|
const directory = dirname(this.#file)
|
|
194
412
|
await mkdir(directory, { recursive: true, mode: 0o700 })
|
|
195
413
|
const payload = JSON.stringify({ version: ROSTER_VERSION, devices: [...this.#devices.values()] }, null, 2)
|