diff --git a/crates/horizon-server/panel/index.html b/crates/horizon-server/panel/index.html
new file mode 100644
index 000000000..051b7283e
--- /dev/null
+++ b/crates/horizon-server/panel/index.html
@@ -0,0 +1,78 @@
+
+
+
+
+
+
+Horizon server
+
+
+
+
+
+
+
Horizon server
+ …
+
+
+
+
+
+
+
+
+
+
+ TPS –
+ MSPT –
+ RAM –
+ CPU –
+
+
+
+
+ Players
+
+ Nobody is online.
+
+
+
+ Backups
+
+
+ No backups yet.
+
+
+
+
+
+
+
+ Sign in with a link from the server: run horizon-server panel in the server's folder and open
+ the link it prints. Links work once, for 10 minutes.
+
+
+
+ The server isn't answering. It may have stopped: start it again with horizon-server start,
+ then reload this page.
+
+
+
+
+
diff --git a/crates/horizon-server/panel/panel.css b/crates/horizon-server/panel/panel.css
new file mode 100644
index 000000000..5d635d509
--- /dev/null
+++ b/crates/horizon-server/panel/panel.css
@@ -0,0 +1,128 @@
+/* The Horizon server panel. Colours and spacing are horizon-theme's tokens (website/src/tokens.css). */
+:root {
+ --page: #fafafc;
+ --plate-soft: #f3f3f8;
+ --plate: #ebecf4;
+ --surface: #ffffff;
+ --rule: #e2e3ea;
+ --ink: #16181f;
+ --ink-soft: #3b3e49;
+ --muted: #646875;
+ --accent: #5b3fd6;
+ --on-accent: #ffffff;
+ --danger: #b4372a;
+ --success: #1d7a48;
+ --font: "Geist", ui-sans-serif, system-ui, sans-serif;
+ --mono: "Geist Mono", ui-monospace, monospace;
+ --radius: 8px;
+ --radius-plate: 12px;
+ color-scheme: light;
+}
+@media (prefers-color-scheme: dark) {
+ :root {
+ --page: #0d0e13;
+ --plate-soft: #181a22;
+ --plate: #20222c;
+ --surface: #16181f;
+ --rule: #24262f;
+ --ink: #ecedf3;
+ --ink-soft: #c5c7d1;
+ --muted: #8b8f9c;
+ --accent: #9a86ff;
+ --on-accent: #0d0e13;
+ --danger: #f07a68;
+ --success: #45c27a;
+ color-scheme: dark;
+ }
+}
+
+* { box-sizing: border-box; }
+[hidden] { display: none !important; }
+body {
+ margin: 0;
+ background: var(--page);
+ color: var(--ink);
+ font: 14px/1.5 var(--font);
+}
+header {
+ display: flex;
+ flex-wrap: wrap;
+ gap: 12px;
+ align-items: center;
+ justify-content: space-between;
+ padding: 16px 24px;
+ border-bottom: 1px solid var(--rule);
+ background: var(--surface);
+}
+.title { display: flex; flex-wrap: wrap; gap: 12px; align-items: baseline; }
+h1 { font-size: 18px; font-weight: 500; margin: 0; letter-spacing: -0.01em; }
+h2 { font-size: 13px; font-weight: 500; margin: 0 0 8px; color: var(--ink-soft); }
+main { padding: 24px; max-width: 1200px; margin: 0 auto; display: grid; gap: 24px; }
+section { background: var(--surface); border: 1px solid var(--rule); border-radius: var(--radius-plate); padding: 16px; }
+.muted { color: var(--muted); }
+.pill {
+ font-size: 12px;
+ padding: 2px 10px;
+ border-radius: 999px;
+ background: var(--plate);
+ color: var(--ink-soft);
+}
+.pill.running { background: color-mix(in srgb, var(--success) 18%, transparent); color: var(--success); }
+.pill.stopping, .pill.restarting, .pill.starting { background: color-mix(in srgb, var(--accent) 16%, transparent); color: var(--accent); }
+.pill.down { background: color-mix(in srgb, var(--danger) 16%, transparent); color: var(--danger); }
+
+.charts { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 16px; }
+figure { margin: 0; }
+figcaption { font-size: 12px; color: var(--muted); display: flex; justify-content: space-between; }
+figcaption b { color: var(--ink); font-weight: 500; font-variant-numeric: tabular-nums; }
+canvas { width: 100%; height: 96px; display: block; }
+
+.columns { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 24px; }
+table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
+th { text-align: left; font-weight: 400; font-size: 12px; color: var(--muted); padding: 4px 0; }
+td { padding: 6px 0; border-top: 1px solid var(--rule); }
+td:last-child { text-align: right; white-space: nowrap; }
+td button { margin-left: 4px; }
+
+button, input {
+ font: inherit;
+ height: 32px;
+ border-radius: var(--radius);
+ border: 1px solid var(--rule);
+ background: var(--plate-soft);
+ color: var(--ink);
+ padding: 0 12px;
+}
+button { cursor: pointer; }
+button:hover { background: var(--plate); }
+button:disabled { opacity: 0.5; cursor: default; }
+button[type="submit"] { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
+button.danger { color: var(--danger); }
+input { background: var(--surface); flex: 1; min-width: 0; }
+input:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
+.row { display: flex; gap: 8px; margin: 8px 0; }
+
+pre {
+ font: 12px/1.5 var(--mono);
+ background: var(--plate-soft);
+ border-radius: var(--radius);
+ padding: 12px;
+ margin: 0;
+ height: 320px;
+ overflow: auto;
+ white-space: pre-wrap;
+ word-break: break-word;
+}
+.message { max-width: 560px; }
+code { font-family: var(--mono); font-size: 13px; }
+#toast {
+ position: fixed;
+ bottom: 24px;
+ left: 50%;
+ transform: translateX(-50%);
+ background: var(--ink);
+ color: var(--page);
+ padding: 8px 16px;
+ border-radius: var(--radius);
+ max-width: 90vw;
+}
diff --git a/crates/horizon-server/panel/panel.js b/crates/horizon-server/panel/panel.js
new file mode 100644
index 000000000..f74c3e1c2
--- /dev/null
+++ b/crates/horizon-server/panel/panel.js
@@ -0,0 +1,270 @@
+// The Horizon server panel (docs/server.md, section 6). It polls the control API once a second through
+// the panel's session; every change is a POST with the X-Horizon header. Text from the server is only
+// ever set with textContent.
+"use strict";
+
+const $ = (id) => document.getElementById(id);
+let logNext = 0;
+let metricsSince = 0;
+const samples = [];
+let lastStatus = null;
+let backupsShown = "";
+
+class SignedOut extends Error {}
+
+async function api(method, path, body) {
+ let response;
+ try {
+ response = await fetch(path, {
+ method,
+ credentials: "same-origin",
+ headers: { "X-Horizon": "1", "Content-Type": "application/json" },
+ body: body === undefined ? undefined : JSON.stringify(body),
+ });
+ } catch (e) {
+ throw new Error("the server isn't answering");
+ }
+ if (response.status === 401) throw new SignedOut();
+ const value = await response.json().catch(() => ({}));
+ if (!response.ok) throw new Error(value.error || `error ${response.status}`);
+ return value;
+}
+
+function show(view) {
+ for (const id of ["app", "signed-out", "gone"]) $(id).hidden = id !== view;
+ for (const id of ["restart", "stop"]) $(id).disabled = view !== "app";
+}
+
+let toastTimer;
+function toast(text) {
+ $("toast").textContent = text;
+ $("toast").hidden = false;
+ clearTimeout(toastTimer);
+ toastTimer = setTimeout(() => ($("toast").hidden = true), 5000);
+}
+
+function duration(seconds) {
+ if (seconds == null) return "";
+ const d = Math.floor(seconds / 86400), h = Math.floor(seconds / 3600) % 24, m = Math.floor(seconds / 60) % 60;
+ if (d) return `${d} d ${h} h`;
+ if (h) return `${h} h ${m} min`;
+ return m ? `${m} min` : `${seconds} s`;
+}
+
+function bytes(n) {
+ if (n == null) return "–";
+ if (n >= 1e9) return `${(n / 1e9).toFixed(2)} GB`;
+ if (n >= 1e6) return `${(n / 1e6).toFixed(1)} MB`;
+ return `${Math.ceil(n / 1e3)} kB`;
+}
+
+function cell(text) {
+ const td = document.createElement("td");
+ td.textContent = text;
+ return td;
+}
+
+function button(label, action, danger) {
+ const b = document.createElement("button");
+ b.type = "button";
+ b.textContent = label;
+ if (danger) b.className = "danger";
+ b.addEventListener("click", action);
+ return b;
+}
+
+async function run(line) {
+ const reply = await api("POST", "/api/v1/command", { line });
+ return (reply.output || []).join("\n");
+}
+
+function renderStatus(status) {
+ lastStatus = status;
+ const phase = $("phase");
+ phase.textContent = status.phase;
+ phase.className = `pill ${status.phase}`;
+ const game = status.sample && status.sample.game;
+ const parts = [`up ${duration(status.engine_uptime_seconds)}`, `horizon-server ${status.version}`];
+ if (status.crashes) parts.push(`${status.crashes} crash${status.crashes > 1 ? "es" : ""}`);
+ $("summary").textContent = parts.join(" · ");
+
+ const players = (game && game.players) || [];
+ $("player-count").textContent = game ? `${players.length} of ${game.max_players}` : "";
+ $("no-players").hidden = players.length > 0;
+ const body = $("players");
+ body.replaceChildren(
+ ...players.map((p) => {
+ const tr = document.createElement("tr");
+ const actions = cell("");
+ actions.append(
+ button("Op", async () => toast(await run(`op ${p.name}`).catch((e) => e.message))),
+ button("Kick", async () => {
+ if (confirm(`Kick ${p.name}?`)) toast(await run(`kick ${p.name}`).catch((e) => e.message));
+ }, true),
+ );
+ tr.append(cell(p.name), cell(p.ping_ms == null ? "–" : `${p.ping_ms} ms`), actions);
+ return tr;
+ }),
+ );
+}
+
+function renderBackups(list) {
+ const key = JSON.stringify(list);
+ if (key === backupsShown) return;
+ backupsShown = key;
+ $("no-backups").hidden = list.length > 0;
+ $("backups").replaceChildren(
+ ...list.map((b) => {
+ const tr = document.createElement("tr");
+ const actions = cell("");
+ actions.append(
+ button("Restore", async () => {
+ if (!confirm(`Restore ${b.name}? The server restarts with that world; the current one is kept in backups/.`)) return;
+ try {
+ await api("POST", "/api/v1/restore", { name: b.name });
+ toast(`Restoring ${b.name}: the server is restarting.`);
+ } catch (e) {
+ toast(e.message);
+ }
+ }, true),
+ );
+ tr.append(cell(b.name), cell(bytes(b.bytes)), actions);
+ return tr;
+ }),
+ );
+}
+
+function appendLog(lines) {
+ if (!lines.length) return;
+ const log = $("log");
+ const atBottom = log.scrollTop + log.clientHeight >= log.scrollHeight - 8;
+ log.append(document.createTextNode(lines.join("\n") + "\n"));
+ // Keep the page light: the last 2000 lines, as the server does.
+ const text = log.textContent;
+ const all = text.split("\n");
+ if (all.length > 2100) log.textContent = all.slice(-2000).join("\n");
+ if (atBottom) log.scrollTop = log.scrollHeight;
+}
+
+function chart(id, values, { max, unit, line, decimals = 1 }) {
+ const canvas = $(id);
+ const ratio = window.devicePixelRatio || 1;
+ const width = canvas.clientWidth, height = canvas.clientHeight;
+ if (canvas.width !== width * ratio) {
+ canvas.width = width * ratio;
+ canvas.height = height * ratio;
+ }
+ const g = canvas.getContext("2d");
+ g.setTransform(ratio, 0, 0, ratio, 0, 0);
+ g.clearRect(0, 0, width, height);
+ const style = getComputedStyle(document.documentElement);
+ const known = values.filter((v) => v != null);
+ const top = Math.max(max || 0, ...known, 1e-9) * 1.1;
+ if (line != null) {
+ g.strokeStyle = style.getPropertyValue("--rule");
+ g.setLineDash([4, 4]);
+ g.beginPath();
+ const y = height - (line / top) * height;
+ g.moveTo(0, y);
+ g.lineTo(width, y);
+ g.stroke();
+ g.setLineDash([]);
+ }
+ g.strokeStyle = style.getPropertyValue("--accent");
+ g.lineWidth = 1.5;
+ g.beginPath();
+ const span = 600;
+ let drawing = false;
+ values.forEach((v, i) => {
+ if (v == null) {
+ drawing = false;
+ return;
+ }
+ const x = width - ((values.length - 1 - i) / (span - 1)) * width;
+ const y = height - (v / top) * height;
+ if (drawing) g.lineTo(x, y);
+ else g.moveTo(x, y);
+ drawing = true;
+ });
+ g.stroke();
+ const last = values[values.length - 1];
+ $(`${id}-now`).textContent = last == null ? "–" : `${last.toFixed(decimals)}${unit}`;
+}
+
+function renderCharts() {
+ chart("tps", samples.map((s) => s.tps), { max: 20, unit: "", line: 20 });
+ chart("mspt", samples.map((s) => s.mspt), { max: 50, unit: " ms", line: 50 });
+ chart("ram", samples.map((s) => (s.rss_bytes == null ? null : s.rss_bytes / 1e9)), { unit: " GB", decimals: 2 });
+ chart("cpu", samples.map((s) => s.cpu_percent), { max: 100, unit: " %", decimals: 0 });
+ $("ram-now").textContent = bytes(samples.length ? samples[samples.length - 1].rss_bytes : null);
+}
+
+let ticks = 0;
+async function poll() {
+ try {
+ const [status, metrics, logs] = await Promise.all([
+ api("GET", "/api/v1/status"),
+ api("GET", `/api/v1/metrics?since=${metricsSince}`),
+ api("GET", `/api/v1/logs?since=${logNext}`),
+ ]);
+ show("app");
+ renderStatus(status);
+ for (const s of metrics.samples) {
+ samples.push(s);
+ metricsSince = s.t;
+ }
+ samples.splice(0, Math.max(0, samples.length - 600));
+ renderCharts();
+ logNext = logs.next;
+ appendLog(logs.lines);
+ if (ticks++ % 10 === 0) renderBackups((await api("GET", "/api/v1/backups")).backups);
+ } catch (e) {
+ if (e instanceof SignedOut) show("signed-out");
+ else if (lastStatus) {
+ show("gone");
+ $("phase").textContent = "down";
+ $("phase").className = "pill down";
+ } else show("gone");
+ }
+}
+
+function setUp() {
+ $("command-form").addEventListener("submit", async (event) => {
+ event.preventDefault();
+ const line = $("command").value.trim();
+ if (!line) return;
+ $("command").value = "";
+ try {
+ const output = await run(line);
+ appendLog([`> ${line}`]);
+ if (!output) toast("Done.");
+ } catch (e) {
+ toast(e.message);
+ }
+ });
+ $("backup-form").addEventListener("submit", async (event) => {
+ event.preventDefault();
+ const name = $("backup-name").value.trim();
+ toast("Backing up…");
+ try {
+ const made = await api("POST", "/api/v1/backups", name ? { name } : {});
+ toast(`Backed up to backups/${made.name}`);
+ $("backup-name").value = "";
+ renderBackups((await api("GET", "/api/v1/backups")).backups);
+ } catch (e) {
+ toast(e.message);
+ }
+ });
+ $("restart").addEventListener("click", async () => {
+ if (!confirm("Restart the game? Players are disconnected for a moment.")) return;
+ await api("POST", "/api/v1/restart").then(() => toast("Restarting…"), (e) => toast(e.message));
+ });
+ $("stop").addEventListener("click", async () => {
+ if (!confirm("Stop the server? The panel stops with it; start it again on the server.")) return;
+ await api("POST", "/api/v1/stop").then(() => toast("Stopping…"), (e) => toast(e.message));
+ });
+ poll();
+ setInterval(poll, 1000);
+}
+
+document.addEventListener("DOMContentLoaded", setUp);
diff --git a/crates/horizon-server/src/api.rs b/crates/horizon-server/src/api.rs
index 4fe166c4b..85bc0e2b5 100644
--- a/crates/horizon-server/src/api.rs
+++ b/crates/horizon-server/src/api.rs
@@ -6,14 +6,17 @@
//! - `GET /api/v1/logs?since=N`: the log lines after the Nth.
//! - `POST /api/v1/command {"line": "..."}`: a console command; the lines it logged.
//! - `POST /api/v1/stop`, `POST /api/v1/restart`.
+//! - `GET /api/v1/metrics?since=T`: the samples taken after T (seconds since 1970), up to 10 minutes.
//! - `GET /api/v1/backups`; `POST /api/v1/backups {"name": "..."}` backs up now; `POST /api/v1/restore
//! {"name": "..."}` restarts the game with that backup's world.
+//! - `POST /api/v1/panel/login`: a one-time login link for the web panel (with the token only).
use crate::{
backup,
config::Config,
engine,
http::{self, Request, Response},
+ panel,
};
use serde_json::{Value, json};
use std::{
@@ -31,6 +34,8 @@ use std::{
const ENDPOINT: &str = ".horizon/api.json";
/// The log lines kept for `logs` and the panel's console.
const LOG_LINES: usize = 2000;
+/// The samples kept for the panel's graphs: 10 minutes, one a second.
+const METRICS: usize = 600;
/// What the supervisor is doing.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -98,6 +103,10 @@ pub struct Shared {
/// Held while a backup is written: one at a time.
pub backing_up: Mutex<()>,
pub started: Instant,
+ /// The last samples, oldest first, for the panel's graphs.
+ pub metrics: Mutex>,
+ /// The panel's address, login links and sessions.
+ pub panel: Mutex,
}
impl Shared {
@@ -118,8 +127,18 @@ impl Shared {
restore: Mutex::new(None),
backing_up: Mutex::new(()),
started: Instant::now(),
+ metrics: Mutex::new(VecDeque::new()),
+ panel: Mutex::new(panel::Auth::default()),
})
}
+ /// Keeps one sample for the graphs.
+ pub fn record(&self, sample: Value) {
+ let mut metrics = self.metrics.lock().unwrap();
+ if metrics.len() == METRICS {
+ metrics.pop_front();
+ }
+ metrics.push_back(sample);
+ }
pub fn set_phase(&self, phase: Phase) {
self.state.lock().unwrap().phase = phase;
}
@@ -172,7 +191,7 @@ fn status(shared: &Shared) -> Value {
}
/// A random token: two 64-bit hashes keyed by the OS's random hasher seeds, the time and the process.
-fn random_token() -> String {
+pub fn random_token() -> String {
use std::hash::{BuildHasher, Hasher};
(0..2u64)
.map(|i| {
@@ -189,7 +208,7 @@ fn random_token() -> String {
.collect()
}
-fn same_token(a: &str, b: &str) -> bool {
+pub fn same_token(a: &str, b: &str) -> bool {
a.len() == b.len() && a.bytes().zip(b.bytes()).fold(0u8, |d, (x, y)| d | (x ^ y)) == 0
}
@@ -238,15 +257,40 @@ pub fn start(shared: Arc) -> Result {
{
return Response::error(403, "wrong Host");
}
+ if (request.method.as_str(), request.path.as_str()) == ("POST", "/api/v1/panel/login") {
+ return match shared.panel.lock().unwrap().login_link() {
+ Some(url) => Response::json(200, &json!({ "url": url })),
+ None => Response::error(
+ 409,
+ "the panel is off: set [panel] bind in server.toml and restart",
+ ),
+ };
+ }
route(&shared, request)
})
.map_err(|e| e.to_string())?;
Ok(Endpoint(path))
}
-fn route(shared: &Shared, request: &Request) -> Response {
+/// The API's routes, for the CLI (with the token) and the panel (with a session).
+pub fn route(shared: &Shared, request: &Request) -> Response {
match (request.method.as_str(), request.path.as_str()) {
("GET", "/api/v1/status") => Response::json(200, &status(shared)),
+ ("GET", "/api/v1/metrics") => {
+ let since: u64 = request
+ .param("since")
+ .and_then(|s| s.parse().ok())
+ .unwrap_or(0);
+ let samples: Vec = shared
+ .metrics
+ .lock()
+ .unwrap()
+ .iter()
+ .filter(|m| m["t"].as_u64().is_some_and(|t| t > since))
+ .cloned()
+ .collect();
+ Response::json(200, &json!({ "samples": samples }))
+ }
("GET", "/api/v1/logs") => {
let since = request
.param("since")
@@ -313,7 +357,7 @@ fn route(shared: &Shared, request: &Request) -> Response {
(
_,
"/api/v1/status" | "/api/v1/logs" | "/api/v1/command" | "/api/v1/stop"
- | "/api/v1/restart" | "/api/v1/backups" | "/api/v1/restore",
+ | "/api/v1/restart" | "/api/v1/backups" | "/api/v1/restore" | "/api/v1/metrics",
) => Response::error(405, "wrong method"),
_ => Response::error(404, "no such endpoint"),
}
diff --git a/crates/horizon-server/src/cli.rs b/crates/horizon-server/src/cli.rs
index acbe211b9..11d9fb6b6 100644
--- a/crates/horizon-server/src/cli.rs
+++ b/crates/horizon-server/src/cli.rs
@@ -158,6 +158,24 @@ pub fn command(dir: &Path, words: &[String]) -> Result<(), String> {
Ok(())
}
+/// `panel`: a one-time login link for the web panel.
+pub fn panel(dir: &Path) -> Result<(), String> {
+ let reply = running(dir)?.call("POST", "/api/v1/panel/login", None)?;
+ let url = reply["url"].as_str().unwrap_or_default();
+ println!(
+ "Open this link in your browser (it works once, for 10 minutes):\n {url}\n\
+ From another computer: put a tailnet address in [panel] bind, or forward the port first:\n \
+ ssh -L {port}:{target} ",
+ port = url
+ .split('/')
+ .nth(2)
+ .and_then(|h| h.rsplit_once(':'))
+ .map_or("8130", |(_, p)| p),
+ target = url.split('/').nth(2).unwrap_or("127.0.0.1:8130"),
+ );
+ Ok(())
+}
+
/// `status [--json]`.
pub fn status(dir: &Path, as_json: bool) -> Result<(), String> {
let Some(client) = Client::open(dir)? else {
diff --git a/crates/horizon-server/src/config.rs b/crates/horizon-server/src/config.rs
index cbeb29310..c62869e27 100644
--- a/crates/horizon-server/src/config.rs
+++ b/crates/horizon-server/src/config.rs
@@ -48,6 +48,10 @@ restart_on_crash = true
[backups]
every = "6h" # automatic backups while the server runs: 30m, 6h, 1d...; "0" turns them off
keep = 12 # automatic backups kept (backups made by hand are never deleted)
+
+[panel]
+bind = "127.0.0.1:8130" # the web panel: this computer only, or a tailnet address (100.x); "" turns it off
+allow_public = false # true lets bind be any address; never put the panel on the open internet
"##;
/// Each typed setting: its section, its name, and the vanilla key it writes.
@@ -81,6 +85,8 @@ pub struct Config {
pub engine: Engine,
#[serde(default)]
pub backups: Backups,
+ #[serde(default)]
+ pub panel: Panel,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
@@ -117,6 +123,57 @@ pub struct Engine {
pub restart_on_crash: bool,
}
+#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
+#[serde(default, deny_unknown_fields)]
+pub struct Panel {
+ pub bind: String,
+ pub allow_public: bool,
+}
+
+impl Default for Panel {
+ fn default() -> Self {
+ Self {
+ bind: "127.0.0.1:8130".into(),
+ allow_public: false,
+ }
+ }
+}
+
+impl Panel {
+ /// Where the panel listens; `None` when it is off.
+ pub fn address(&self) -> Result