feat(waiter): encrypted, key-pinned connection to the venue server (native app)

- Android: MainActivity installs a WebViewClient whose onReceivedSslError
  proceeds only when the server's SPKI SHA-256 is in TrustStore (pins of all
  paired venues, SharedPreferences); XeniaTlsPlugin lets JS set that list.
  Spike showed this one callback covers fetch/XHR, images AND WebSockets.
  Pins are per key, not per address: IP changes, renewals and expiry never
  need action; a different key is always refused.
- Pairing: QR key (k=) must equal the server's advertised pin, else refused;
  typed addresses trust the advertised key on first use. Then the venue moves
  to https://<ip>:<tls.port> (stays on HTTP if that port isn't reachable yet).
- upgradeToTls(): on every start, a plain-HTTP venue moves onto TLS once the
  server offers it (never accepting a key different from the stored one).
- main.jsx pushes the venues' pins to native before the first request.
- Rediscovery made proactive: checks the saved address at start and every
  minute while the live connection is down, instead of waiting for requests
  to an unanswered IP to time out (minutes). HTTPS probes get 5s: the first
  TLS connection in a fresh process takes ~2s (measured).

E2E on the emulator vs an isolated stack: wrong-key QR refused; pairing on
TLS; tables + wss live; impostor server with another key refused natively;
HTTP venue upgraded on start; server cert renewed (same key) - app keeps
working without re-pairing; rediscovery over TLS (11s). Step 5 HTTP
rediscovery (now 2.7s/4.8s) and cold-start login tests, and web modes, pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-09-28 17:41:25 +03:00
co-authored by Claude Opus 5.5
parent 7811bf5dcb
commit 18f13f12dd
10 changed files with 263 additions and 26 deletions
+26 -16
View File
@@ -1,21 +1,25 @@
import { useEffect, useState } from 'react'
import useConnectionStore from '../store/connectionStore'
import { canRediscover, rediscover } from '../native/autoRediscover'
import { canRediscover, rediscover, upgradeToTls } from '../native/autoRediscover'
const RETRY_WHILE_OFFLINE_MS = 2 * 60_000
const WATCHDOG_MS = 60_000
const RELOAD_DELAY_MS = 1500
/**
* Native app: when the venue server stops answering, look for it at a new
* address on the same network and reconnect automatically (plan step 5).
* Native app: keep the connection to the venue server healthy on its own
* (plan steps 5 and 7).
*
* Triggers: the connection is confirmed offline (logged in), or any request
* fails with a network error (login / offline screens). Retries every 2 minutes
* while offline. On success it shows a short notice and reloads into the new
* address — the offline queue lives in IndexedDB and syncs after the reload.
* - On start: move a plain-HTTP venue onto the encrypted endpoint, and check the
* saved address (1.5s probe) — a server that moved is found right away instead
* of waiting for requests to an unanswered IP to time out, which can take minutes.
* - Every minute while the live connection is down (or the app is offline), and
* whenever a request fails with a network error: look for the server at a new
* address on the same network.
* A server that answers at its saved address costs one small request; a found
* new address shows a short notice and reloads — the offline queue lives in
* IndexedDB and syncs after the reload.
*/
export default function ServerRediscovery() {
const status = useConnectionStore(s => s.status)
const [movedTo, setMovedTo] = useState(null)
const enabled = canRediscover()
@@ -33,18 +37,24 @@ export default function ServerRediscovery() {
setTimeout(() => window.location.replace(target), RELOAD_DELAY_MS)
}
upgradeToTls().then(url => {
if (cancelled) return
if (url) window.location.reload()
else attempt()
})
const watchdog = setInterval(() => {
const { status, sseAlive } = useConnectionStore.getState()
if (status !== 'online' || !sseAlive) attempt()
}, WATCHDOG_MS)
window.addEventListener('backend-offline', attempt)
let timer = null
if (status === 'offline') {
attempt()
timer = setInterval(attempt, RETRY_WHILE_OFFLINE_MS)
}
return () => {
cancelled = true
clearInterval(watchdog)
window.removeEventListener('backend-offline', attempt)
clearInterval(timer)
}
}, [enabled, status])
}, [enabled])
if (!movedTo) return null
return (