|
|
@@ -9,7 +9,7 @@ import { DESKTOP_HOST_PROTOCOL, type DesktopHostExchange, type DesktopHostRecord
|
|
|
import { readComputerNames, saveComputerName } from './computerNames.js'
|
|
|
|
|
|
const hash = (value: string) => createHash('sha256').update(value).digest('hex')
|
|
|
-const enrollInput = z.object({ name: z.string().trim().min(1).max(100) }).strict()
|
|
|
+const enrollInput = z.object({ name: z.string().trim().min(1).max(100), installId: z.string().uuid().optional() }).strict()
|
|
|
const exchangeInput = z.object({
|
|
|
host: z.object({ id: z.string().uuid(), name: z.string().max(128), platform: z.string().max(16), screen: z.boolean(), accessibility: z.boolean(), approved: z.array(z.string().regex(/^[a-f0-9]{64}$/)).max(100), full: z.array(z.string().regex(/^[a-f0-9]{64}$/)).max(100).optional() }).strict(),
|
|
|
results: z.array(z.object({ id: z.string().uuid(), value: z.unknown().optional(), error: z.string().max(500).optional() }).strict()).max(20),
|
|
|
@@ -29,9 +29,29 @@ export class DesktopHostHub {
|
|
|
enroll(value: unknown) {
|
|
|
const body = parse(enrollInput, value)
|
|
|
const token = randomBytes(32).toString('base64url')
|
|
|
- const record: DesktopHostRecord = { ...body, id: randomUUID(), tokenHash: hash(token), enabled: true, createdAt: Date.now() }
|
|
|
- this.store.put('_system', 'desktop-host', record.id, record)
|
|
|
- return { host: this.summary(record), token }
|
|
|
+ const records = this.records()
|
|
|
+ let record = body.installId ? records.find(item => item.installId === body.installId) : undefined
|
|
|
+ if (!record && body.installId) {
|
|
|
+ const unnamed = records.filter(item => item.enabled && item.name === body.name && !item.installId).sort((a, b) => a.createdAt - b.createdAt)
|
|
|
+ record = unnamed[0]
|
|
|
+ for (const extra of unnamed.slice(1)) {
|
|
|
+ extra.enabled = false
|
|
|
+ this.store.put('_system', 'desktop-host', extra.id, extra)
|
|
|
+ this.online.delete(extra.id)
|
|
|
+ this.drop(extra.id)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (record) {
|
|
|
+ record.name = body.name
|
|
|
+ record.installId = body.installId
|
|
|
+ record.tokenHash = hash(token)
|
|
|
+ record.enabled = true
|
|
|
+ this.store.put('_system', 'desktop-host', record.id, record)
|
|
|
+ return { host: this.summary(record), token }
|
|
|
+ }
|
|
|
+ const created: DesktopHostRecord = { name: body.name, installId: body.installId, id: randomUUID(), tokenHash: hash(token), enabled: true, createdAt: Date.now() }
|
|
|
+ this.store.put('_system', 'desktop-host', created.id, created)
|
|
|
+ return { host: this.summary(created), token }
|
|
|
}
|
|
|
summary(record: DesktopHostRecord): DesktopHostSummary {
|
|
|
const { tokenHash: _secret, ...publicRecord } = record
|