Files
openchamber/packages/web/server/lib/opencode/bootstrap-runtime.js
T
Bohdan Triapitsyn afb368e11b feat: connection candidates refresh + relay identity hardening
Candidates refresh (server + mobile + desktop clients):
- GET /api/client-auth/connection/candidates returns the server's current
  LAN URLs, relay candidate, and serverId for already-paired devices
- /health and /api/version expose serverId so clients can verify a learned
  address belongs to the expected server before sending their bearer token
- mobile: refresh saved candidates over the live transport after every
  connect/wake, hot-switch relay->LAN when a fresh address is reachable;
  serverId gate on direct probes; token no longer sent to /health
- desktop: refresh stored host apiUrl after a relay connect and hot-switch
  back to direct; electron probe verifies serverId before authenticated fetch

Fixes found while debugging a dead pairing:
- settings: strict reader that throws on corrupt/unreadable file instead of
  returning {}; relay signing/encryption key generation is now gated on it,
  so a swallowed read failure can no longer mint a new server identity and
  orphan every paired device (loud log when a keypair IS generated)
- SessionAuthGate: bounded auto-retry for transient session-check failures
  (initial request racing the relay tunnel's first WS attempt, startup 5xx)
2026-07-12 18:09:54 +03:00

159 lines
4.0 KiB
JavaScript

export const createBootstrapRuntime = (dependencies) => {
const {
createUiAuth,
registerServerStatusRoutes,
registerCommonRequestMiddleware,
registerAuthAndAccessRoutes,
registerTtsRoutes,
registerNotificationRoutes,
registerOpenChamberRoutes,
express,
} = dependencies;
const setupBaseRoutes = (app, options) => {
const {
process,
openchamberVersion,
runtimeName,
serverStartedAt,
gracefulShutdown,
getHealthSnapshot,
verboseRequestLogs,
uiPassword,
tunnelAuthController,
remoteClientAuthRuntime,
clientPairingRuntime,
getRelayPairingCandidate,
reconcileRelay,
getPairingTransports,
getDirectCandidateUrls,
getServerId,
getServerLabel,
readSettingsFromDiskMigrated,
normalizeTunnelSessionTtlMs,
sayTTSCapability,
ensurePushInitialized,
ensureGlobalWatcherStarted,
getOrCreateVapidKeys,
getUiSessionTokenFromRequest,
writeSettingsToDisk,
addOrUpdatePushSubscription,
removePushSubscription,
addOrUpdateApnsToken,
removeApnsToken,
updateUiVisibility,
clearPendingPushBadge,
isUiVisible,
getUiNotificationClients,
writeSseEvent,
sessionRuntime,
setPushInitialized,
fs,
os,
path,
server,
__dirname,
openchamberDataDir,
modelsDevApiUrl,
modelsMetadataCacheTtl,
fetchFreeZenModels,
getCachedZenModels,
setAutoAcceptSession,
} = options;
const uiAuthController = createUiAuth({
password: uiPassword,
readSettingsFromDiskMigrated,
clientAuthController: remoteClientAuthRuntime,
});
if (uiAuthController.enabled) {
console.log('UI password protection enabled for browser sessions');
}
registerServerStatusRoutes(app, {
express,
process,
openchamberVersion,
runtimeName,
serverStartedAt,
gracefulShutdown,
getHealthSnapshot,
getServerId,
tunnelAuthController,
uiAuthController,
});
registerCommonRequestMiddleware(app, { express, verboseRequestLogs });
registerAuthAndAccessRoutes(app, {
express,
tunnelAuthController,
uiAuthController,
remoteClientAuthRuntime,
clientPairingRuntime,
getRelayPairingCandidate,
reconcileRelay,
getPairingTransports,
getDirectCandidateUrls,
getServerId,
getServerLabel,
readSettingsFromDiskMigrated,
normalizeTunnelSessionTtlMs,
});
registerTtsRoutes(app, { sayTTSCapability });
registerNotificationRoutes(app, {
uiAuthController,
ensurePushInitialized,
ensureGlobalWatcherStarted,
getOrCreateVapidKeys,
getUiSessionTokenFromRequest,
readSettingsFromDiskMigrated,
writeSettingsToDisk,
addOrUpdatePushSubscription,
removePushSubscription,
addOrUpdateApnsToken,
removeApnsToken,
updateUiVisibility,
clearPendingPushBadge,
isUiVisible,
getUiNotificationClients,
writeSseEvent,
getSessionActivitySnapshot: sessionRuntime.getSessionActivitySnapshot,
getSessionStateSnapshot: sessionRuntime.getSessionStateSnapshot,
getSessionAttentionSnapshot: sessionRuntime.getSessionAttentionSnapshot,
getSessionState: sessionRuntime.getSessionState,
getSessionAttentionState: sessionRuntime.getSessionAttentionState,
markSessionViewed: sessionRuntime.markSessionViewed,
markSessionUnviewed: sessionRuntime.markSessionUnviewed,
markUserMessageSent: sessionRuntime.markUserMessageSent,
setPushInitialized,
setAutoAcceptSession,
});
registerOpenChamberRoutes(app, {
fs,
os,
path,
process,
server,
__dirname,
openchamberDataDir,
modelsDevApiUrl,
modelsMetadataCacheTtl,
readSettingsFromDiskMigrated,
fetchFreeZenModels,
getCachedZenModels,
});
return {
uiAuthController,
};
};
return {
setupBaseRoutes,
};
};