.docs/architecture.md
Docs/architecture.md

Architecture

Monarch uses a hub-and-spoke topology centered on Monarch.panel. Managed hosts never expose control ports to the network; every management channel is multiplexed over an outbound WebSocket initiated by monarch-daemon.

Browser ──HTTPS/WSS──▶ Monarch.panel (Nuxt 4 SPA + Nitro + SQLite, :4382)
                         ├── /api/*        REST API (sessions, nodes, VMs, logs, settings)
                         ├── /ws/live      2s telemetry, 5s VM lists, node status events
                         ├── /ws/stream    Relayed PTY, VNC, serial, journal, uploads
                         └── /api/mobile/* QR-paired phone API (cert-pinned)
                                ▲
                                │ Outbound WSS /ws/daemon
                                │ (ECDSA P-256 challenge auth + SHA-256 TLS pin)
                                │
                       monarch-daemon (.NET 10 root agent on each host)
                         ├── Libvirt / QEMU / KVM (virsh, virt-install, loopbacked VNC)
                         ├── Storage & Cache (lsblk, lvs, zfs, smartctl, cache-health.json)
                         ├── Network & Proxy (ip, wg, Caddyfile, acme.sh, firewalld/ufw)
                         └── Host (systemctl, journalctl, docker/podman, pacman/apt/dnf/zypper)

Daemon wire protocol (/ws/daemon)

Tunnel.cs in Monarch.daemon and server/routes/ws/daemon.ts / hub.ts / rpc.ts in Monarch.panel share a single multiplexed WebSocket per node:

  • Handshake: hello → auth (signed challenge controlcenter-daemon-auth:v2:<nonce>:<machineId>) → ready.
  • Text frames (JSON):
    • req / res: Request/response RPC calls across allowlisted modules (libvirt.*, storage.*, lvmcache.*, caddy.*, certs.*, containers.*, firewall.*, services.*, accounts.*, updates.*, monarch.*, clyocloud.*).
    • open / opened / close: Lifecycle for bidirectional streams, plus control frames such as resize, finish, and progress.
    • Periodic pushes: telemetry every 2 seconds, vms every 5 seconds, host info, and ping/pong.
  • Binary frames: A 4-byte big-endian stream ID followed by raw payload bytes. Stream IDs below 0x80000000 are panel-initiated (pty, exec, serial, vnc, journal.follow, ctr.logs.follow, ctr.pull, updates.apply, vol.upload, vol.download, certs.acme); IDs starting at 0x80000000 are daemon-initiated (logfile shipping).

Data persistence & retention

  • Panel state (node:sqlite): Nodes, ECDSA public keys, hashed join tokens, mobile device records, alert rules, audit logs, and time-series metrics. Live metrics arrive every 2 seconds; minute-resolution telemetry history is kept for 90 days (network interfaces and VMs for 30 days).
  • Host state:
    • /etc/monarch-daemon/daemon.json and daemon.key (0600) for node identity and panel pin.
    • /var/lib/monarch/cache-health.json updated every 10 minutes by monarch-cache-guard.
    • /etc/clyocloud/clyocloud.env and secret.env when ClyoCloud is enabled on Monarch OS.