Files
KlipperAnywhere/docs/architecture.md
T
2026-05-14 02:27:26 +01:00

4.3 KiB

Architecture

Problem

Klipper printers are usually controlled through Moonraker, Mainsail, Fluidd, and one or more local camera streams. These services are normally safe on a trusted LAN, but they should not be exposed directly to the internet.

KlipperAnywhere provides a self-hosted remote access layer where the printer-side agent dials out to a server the user controls. Remote clients connect to that server, and the server brokers access back to the printer through the agent connection.

High-Level Design

Browser / mobile app
        |
        | HTTPS
        v
KlipperAnywhere server
        |
        | outbound tunnel from printer network
        v
KlipperAnywhere agent
        |
        | LAN HTTP/WebSocket
        v
Moonraker, Mainsail, Fluidd, webcam services

Server Responsibilities

  • Terminate HTTPS.
  • Authenticate users.
  • Register printers and issue agent credentials.
  • Accept outbound agent tunnel connections.
  • Authorize requests to printers.
  • Proxy HTTP and WebSocket traffic to Moonraker/UI services.
  • Relay webcam streams or snapshots.
  • Store audit events.

Agent Responsibilities

  • Run on the printer host or another device on the same LAN.
  • Connect outbound to the server and keep the tunnel alive.
  • Verify server identity.
  • Discover or configure local Moonraker, UI, and camera endpoints.
  • Forward allowed traffic between the server and local services.
  • Report printer health, tunnel health, and local endpoint availability.

MVP Protocol

The simplest reliable MVP is a persistent WebSocket tunnel:

  • Agent opens wss://server.example.com/agent/tunnel.
  • Agent authenticates using a one-time enrollment token, then receives a long-lived printer credential.
  • Server multiplexes proxied HTTP/WebSocket requests over the tunnel.
  • Agent performs local requests to Moonraker or webcam endpoints and streams responses back to the server.

This is easier to deploy than WireGuard/Tailscale-style networking and keeps the initial surface area small. A later version can add native QUIC/WebTransport or WireGuard for better performance.

Data Model

  • User: login identity and server-wide permissions, persisted in a local JSON file for the MVP.
  • Printer: display name, owner, status, and registered agent identity.
  • AgentCredential: revocable secret or certificate used by the agent.
  • AccessGrant: maps users or API tokens to printers and permission scopes.
  • AuditEvent: append-only JSONL records for logins, account changes, printer UI opens, and control actions.

Permission Scopes

  • printer:read: view status, temperatures, job progress, and files.
  • printer:control: pause, resume, cancel, home, move axes, and run macros.
  • printer:upload: upload G-code.
  • printer:admin: edit printer connection settings and agent credentials.
  • camera:view: view snapshots or streams.

Deployment Shape

For MVP:

  • server container
  • local users file for account persistence
  • optional redis container for transient tunnel/session state
  • agent installed as a service or init script on the printer host

On rooted printer appliances, such as Creality/Nebula-style Klipper devices, the agent should not assume a full Debian userspace. It should be installable as a single Go binary with CGO_ENABLED=0, and startup should support systemd, OpenRC, SysV init, BusyBox init scripts, or a documented manual launch path.

For early development, SQLite is acceptable, but Postgres should be the production target because multi-printer deployments and audit logs benefit from stronger operational behavior.

Key Risks

  • Remote control of printers is physically risky. Authorization and audit logs are not optional.
  • Moonraker APIs include powerful endpoints. The proxy should enforce scope checks before forwarding dangerous calls.
  • Camera streaming can dominate bandwidth. Snapshot polling should be available as a low-bandwidth fallback.
  • A compromised server can control connected printers. Agent-side allowlists and read-only modes reduce blast radius.

Suggested First Milestone

Build a minimal vertical slice:

  1. Server exposes HTTPS routes for one printer.
  2. Agent connects to server over WebSocket.
  3. Browser can access Moonraker /printer/info through the server.
  4. Server logs every proxied request with user, printer, method, and path.
  5. Browser can open a proxied WebSocket to Moonraker.