/** * Pairing a phone with a venue server (native app). * * Input comes from the pairing QR code shown in the manager * (`http:///?pair=`) or from an address typed by hand * (`192.168.1.50`, `192.168.1.50:8080`, `http://…`). Either way the server must * answer GET /api/system/identity as a Xenia server with an API version this app * understands, and — when the QR carried a site id — the SAME site id, so a code * from one venue can't silently pair with a different machine that took its IP. * * Contract: docs/reference/lan-access.md */ import { deliveryMode, saveVenue } from '../config/server' import { syncTrustedKeys, tlsBaseUrl } from './tls' import { SUPPORTED_API_VERSIONS } from './apiVersion' export { SUPPORTED_API_VERSIONS } const PROBE_TIMEOUT_MS = 5000 export class PairingError extends Error {} /** * Turn scanned or typed text into { baseUrl, expectedSiteId }. * Only the origin of a URL is kept — paths and other params are ignored. */ export function parseServerInput(raw) { const text = String(raw || '').trim() if (!text) throw new PairingError('Συμπληρώστε τη διεύθυνση του server.') const withScheme = /^https?:\/\//i.test(text) ? text : `http://${text}` let url try { url = new URL(withScheme) } catch { throw new PairingError('Μη έγκυρη διεύθυνση. Παράδειγμα: 192.168.1.50') } if (!url.hostname) throw new PairingError('Μη έγκυρη διεύθυνση. Παράδειγμα: 192.168.1.50') return { baseUrl: url.origin, expectedSiteId: url.searchParams.get('pair') || null, expectedKey: url.searchParams.get('k') || null, // TLS public-key pin from the manager's QR } } /** Ask a server who it is. Resolves to the identity object or throws PairingError. */ export async function probeServer(baseUrl) { const controller = new AbortController() const timer = setTimeout(() => controller.abort(), PROBE_TIMEOUT_MS) let res try { res = await fetch(`${baseUrl}/api/system/identity`, { signal: controller.signal }) } catch { throw new PairingError( 'Δεν βρέθηκε server σε αυτή τη διεύθυνση. Ελέγξτε ότι το κινητό είναι στο ίδιο WiFi με το κατάστημα.' ) } finally { clearTimeout(timer) } if (res.status === 403) { throw new PairingError('Ο server αρνήθηκε τη σύνδεση. Το κινητό πρέπει να είναι στο WiFi του καταστήματος.') } let identity = null try { identity = await res.json() } catch { // not JSON → not a Xenia server } if (!res.ok || identity?.app !== 'xenia-pos') { throw new PairingError('Η διεύθυνση δεν ανήκει σε server Xenia.') } if (!SUPPORTED_API_VERSIONS.includes(identity.api_version)) { throw new PairingError( `Ο server (έκδοση ${identity.version}) δεν είναι συμβατός με αυτή την εφαρμογή. Ενημερώστε την εφαρμογή ή τον server.` ) } return identity } /** Storage-safe venue id for servers without a SITE_ID (dev / unregistered). */ function fallbackSiteId(baseUrl) { return 'local-' + new URL(baseUrl).host.replace(/[^a-z0-9]+/gi, '-') } /** * Validate a scanned/typed address and save it as a paired venue. * Does NOT switch to it — the caller decides (switchVenue reloads the app). */ export async function pairWith(raw) { const { baseUrl, expectedSiteId, expectedKey } = parseServerInput(raw) const identity = await probeServer(baseUrl) if (expectedSiteId && identity.site_id && identity.site_id !== expectedSiteId) { throw new PairingError( 'Σε αυτή τη διεύθυνση απαντά διαφορετικό κατάστημα από αυτό του QR. Ζητήστε νέο QR από τον διαχειριστή.' ) } const spki = identity.tls?.spki_sha256 || null if (expectedKey && spki !== expectedKey) { // The QR carries the real server's key: anything else is a different machine throw new PairingError( 'Το κλειδί ασφαλείας του server δεν ταιριάζει με το QR. Ζητήστε νέο QR από τον διαχειριστή.' ) } const venue = { siteId: identity.site_id || fallbackSiteId(baseUrl), name: identity.venue_name || new URL(baseUrl).host, baseUrl, spki, } // Native app + server offers TLS: talk to it encrypted from now on. The pin // comes from the QR (verified above) or, for a typed address, from the server // itself (trust on first use). If :tls.port isn't reachable yet (proxy not // updated), stay on plain HTTP — upgradeToTls() retries on every start. if (spki && identity.site_id && deliveryMode() === 'native') { const secureUrl = tlsBaseUrl(baseUrl, identity.tls) await syncTrustedKeys([spki]) try { const secure = await probeServer(secureUrl) if (secure.site_id === identity.site_id) venue.baseUrl = secureUrl } catch { // keep plain HTTP for now } } return saveVenue(venue) }