Files
orca/src/main/ipc/mobile.ts
T
2026-06-01 17:30:10 -07:00

193 lines
6.3 KiB
TypeScript

import { ipcMain } from 'electron'
import { networkInterfaces } from 'os'
import QRCode from 'qrcode'
import type { RuntimeAccessGrant } from '../../shared/runtime-access-grants'
import { isTailnetIPv4Address } from '../../shared/tailnet-address'
import type { DeviceEntry } from '../runtime/device-registry'
import type { OrcaRuntimeRpcServer } from '../runtime/runtime-rpc'
export type NetworkInterface = {
name: string
address: string
}
// Why: the WebSocket transport advertises 0.0.0.0 as its endpoint, which isn't
// connectable from a mobile device. We enumerate all non-internal IPv4
// addresses so the user can choose which one to advertise in the QR code
// (e.g. LAN vs Tailscale).
function getNetworkInterfaces(): NetworkInterface[] {
const result: NetworkInterface[] = []
const interfaces = networkInterfaces()
for (const [name, addrs] of Object.entries(interfaces)) {
if (!addrs) {
continue
}
for (const addr of addrs) {
if (addr.family === 'IPv4' && !addr.internal) {
result.push({ name, address: addr.address })
}
}
}
return result.sort(
(a, b) => Number(isTailnetIPv4Address(b.address)) - Number(isTailnetIPv4Address(a.address))
)
}
function getDefaultPairingAddress(): string | null {
const ifaces = getNetworkInterfaces()
return ifaces.length > 0 ? ifaces[0]!.address : null
}
function toRuntimeAccessGrant(device: DeviceEntry): RuntimeAccessGrant {
return {
deviceId: device.deviceId,
name: device.name,
createdAt: device.pairedAt,
lastSeenAt: device.lastSeenAt > 0 ? device.lastSeenAt : null
}
}
// Why: the mobile IPC handlers provide the renderer with QR code pairing data,
// device management, and WebSocket readiness status. They depend on the
// OrcaRuntimeRpcServer because it owns the device registry and TLS state.
export function registerMobileHandlers(rpcServer: OrcaRuntimeRpcServer): void {
ipcMain.handle('mobile:listNetworkInterfaces', (): { interfaces: NetworkInterface[] } => ({
interfaces: getNetworkInterfaces()
}))
ipcMain.handle(
'mobile:getPairingQR',
async (_event, args?: { address?: string; rotate?: boolean }) => {
// Why: allow the caller to specify which network interface address to
// embed in the QR code. This supports overlay networks (Tailscale,
// ZeroTier) where the default LAN IP isn't reachable from the phone.
const ip = args?.address ?? getDefaultPairingAddress()
if (!ip) {
return { available: false as const }
}
// Why: coalesce repeated QR regenerations onto a single never-scanned
// pending token so the copy-button flow doesn't accumulate orphaned
// device credentials forever. The token graduates to a real entry when
// a phone actually connects (lastSeenAt > 0). When the caller passes
// `rotate: true` (explicit "Regenerate" intent because the prior token
// may have been exposed), we discard any pending token and mint a fresh
// one so the new QR carries a different credential.
const offer = rpcServer.createPairingOffer({
address: ip,
rotate: args?.rotate,
name: `Mobile ${new Date().toLocaleDateString()}`,
scope: 'mobile'
})
if (!offer.available) {
return { available: false as const }
}
const qrDataUrl = await QRCode.toDataURL(offer.pairingUrl, {
errorCorrectionLevel: 'M',
margin: 2,
width: 256
})
return {
available: true as const,
qrDataUrl,
pairingUrl: offer.pairingUrl,
endpoint: offer.endpoint,
deviceId: offer.deviceId
}
}
)
ipcMain.handle(
'mobile:getRuntimePairingUrl',
async (_event, args?: { address?: string; rotate?: boolean }) => {
const ip = args?.address ?? getDefaultPairingAddress()
if (!ip) {
return { available: false as const }
}
// Why: web/desktop runtime clients need full runtime access, not the
// mobile allowlist used by phone QR pairing.
const offer = rpcServer.createPairingOffer({
address: ip,
rotate: args?.rotate,
name: `Runtime ${new Date().toLocaleDateString()}`,
scope: 'runtime'
})
if (!offer.available) {
return { available: false as const }
}
return {
available: true as const,
pairingUrl: offer.pairingUrl,
webClientUrl: offer.webClientUrl,
endpoint: offer.endpoint,
deviceId: offer.deviceId
}
}
)
ipcMain.handle('mobile:listDevices', () => {
const registry = rpcServer.getDeviceRegistry()
if (!registry) {
return { devices: [] }
}
// Why: devices with lastSeenAt === 0 were created during QR generation
// but never actually scanned/connected. Showing them as "paired" is
// misleading, so we filter them out.
return {
devices: registry
.listDevices()
.filter((d) => d.scope === 'mobile' && d.lastSeenAt > 0)
.map((d) => ({
deviceId: d.deviceId,
name: d.name,
pairedAt: d.pairedAt,
lastSeenAt: d.lastSeenAt
}))
}
})
ipcMain.handle('mobile:listRuntimeAccessGrants', () => {
const registry = rpcServer.getDeviceRegistry()
if (!registry) {
return { grants: [] }
}
// Why: generated web/runtime links are bearer credentials even before a
// client first connects, so pending runtime grants must stay revocable.
return {
grants: registry
.listDevices()
.filter((d) => d.scope === 'runtime')
.sort((a, b) => b.pairedAt - a.pairedAt)
.map(toRuntimeAccessGrant)
}
})
ipcMain.handle('mobile:revokeDevice', (_event, args: { deviceId: string }) => {
const registry = rpcServer.getDeviceRegistry()
if (!registry) {
return { revoked: false }
}
return { revoked: rpcServer.revokeMobileDevice(args.deviceId) }
})
ipcMain.handle('mobile:revokeRuntimeAccess', (_event, args: { deviceId: string }) => {
const registry = rpcServer.getDeviceRegistry()
if (!registry) {
return { revoked: false }
}
return { revoked: rpcServer.revokeRuntimeAccess(args.deviceId) }
})
ipcMain.handle('mobile:isWebSocketReady', () => {
return {
ready: rpcServer.getWebSocketEndpoint() !== null,
endpoint: rpcServer.getWebSocketEndpoint()
}
})
}