diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 38b3af3d0..27636b0eb 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -109,18 +109,18 @@ jobs: - name: ALSA and udev headers if: runner.os == 'Linux' run: sudo apt-get update && sudo apt-get install -y --no-install-recommends libasound2-dev libudev-dev - - name: Build the client, horizon-ctl and horizon-prepare - run: cargo build --release --locked -p horizon-client --bin horizon-client --bin horizon-ctl -p horizon-prepare --bin horizon-prepare + - name: Build the client, horizon-ctl, horizon-prepare and horizon-server + run: cargo build --release --locked -p horizon-client --bin horizon-client --bin horizon-ctl -p horizon-prepare --bin horizon-prepare -p horizon-server --bin horizon-server - name: Every program the release folder takes was built run: | - for binary in horizon-client horizon-ctl horizon-prepare; do + for binary in horizon-client horizon-ctl horizon-prepare horizon-server; do test -f "target/release/$binary${{ matrix.exe }}" || { echo "::error::target/release/$binary${{ matrix.exe }} is missing"; exit 1; } done - name: Only system libraries # Windows: no non-system DLL (a "DLL not found" dialog on some players' machines). Linux: the standard # loader and libraries every desktop distribution has. macOS: only the system's own libraries. run: | - for binary in horizon-client horizon-ctl horizon-prepare; do + for binary in horizon-client horizon-ctl horizon-prepare horizon-server; do file="target/release/$binary${{ matrix.exe }}" if [[ "$RUNNER_OS" == macOS ]]; then others=$(otool -L "$file" | tail -n +2 | awk '{print $1}' | grep -v -E '^(/usr/lib/|/System/Library/)' || true) @@ -137,6 +137,7 @@ jobs: "$bin/horizon-ctl${{ matrix.exe }}" --help > /dev/null "$bin/horizon-prepare${{ matrix.exe }}" --version "$bin/horizon-prepare${{ matrix.exe }}" --help | grep -q -- '--minecraft-dir' + "$bin/horizon-server${{ matrix.exe }}" --version - name: Tools from the runner if: runner.environment == 'self-hosted' run: bash tools/ci/runner-env.sh diff --git a/Cargo.lock b/Cargo.lock index 6ebc69964..1d5897d11 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1398,6 +1398,14 @@ dependencies = [ "serde_json", ] +[[package]] +name = "horizon-bots" +version = "0.1.0" +dependencies = [ + "horizon-protocol", + "serde_json", +] + [[package]] name = "horizon-client" version = "0.1.0" @@ -1417,6 +1425,7 @@ dependencies = [ "horizon-friends", "horizon-mods", "horizon-protocol", + "horizon-server-files", "horizon-shaderpack", "horizon-telemetry", "horizon-theme", @@ -1541,6 +1550,31 @@ dependencies = [ "serde_json", ] +[[package]] +name = "horizon-server" +version = "0.1.0" +dependencies = [ + "horizon-bench", + "horizon-server-files", + "libc", + "serde", + "serde_json", + "toml", + "toml_edit", + "windows-sys 0.61.2", + "zip", +] + +[[package]] +name = "horizon-server-files" +version = "0.1.0" +dependencies = [ + "horizon-core", + "horizon-protocol", + "horizon-world", + "log", +] + [[package]] name = "horizon-shaderpack" version = "0.1.0" @@ -3739,6 +3773,7 @@ dependencies = [ "indexmap", "toml_datetime", "toml_parser", + "toml_writer", "winnow", ] diff --git a/Cargo.toml b/Cargo.toml index 1b4cb92d4..30a30386d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] exclude = ["launcher", "crates/horizon-telemetry-server"] -members = ["crates/horizon-core", "crates/horizon-dualsense", "crates/horizon-world", "crates/voxel-kernels", "crates/horizon-client", "crates/horizon-assets", "crates/horizon-protocol", "crates/horizon-theme", "crates/horizon-shaderpack", "crates/horizon-prepare", "crates/horizon-telemetry", "crates/horizon-bench", "crates/horizon-friends", "crates/horizon-relay", "crates/horizon-mod-sdk", "crates/horizon-mods", "crates/horizon-mods/tests/guest", "crates/cargo-horizon-mod", "crates/horizon-port-pack", "crates/horizon-mod-sdk/examples/coords-hud", "xtask"] +members = ["crates/horizon-core", "crates/horizon-dualsense", "crates/horizon-world", "crates/voxel-kernels", "crates/horizon-client", "crates/horizon-server-files", "crates/horizon-server", "crates/horizon-bots", "crates/horizon-assets", "crates/horizon-protocol", "crates/horizon-theme", "crates/horizon-shaderpack", "crates/horizon-prepare", "crates/horizon-telemetry", "crates/horizon-bench", "crates/horizon-friends", "crates/horizon-relay", "crates/horizon-mod-sdk", "crates/horizon-mods", "crates/horizon-mods/tests/guest", "crates/cargo-horizon-mod", "crates/horizon-port-pack", "crates/horizon-mod-sdk/examples/coords-hud", "xtask"] resolver = "2" [workspace.package] diff --git a/crates/horizon-bots/Cargo.toml b/crates/horizon-bots/Cargo.toml new file mode 100644 index 000000000..5a3e5e5c7 --- /dev/null +++ b/crates/horizon-bots/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "horizon-bots" +version = "0.1.0" +edition.workspace = true +license.workspace = true +rust-version.workspace = true +description = "Load-test bots for Minecraft Java Edition 26.3 servers (docs/server.md, section 9)" + +[dependencies] +horizon-protocol = { path = "../horizon-protocol" } +serde_json = "1.0.151" + +[lints] +workspace = true diff --git a/crates/horizon-bots/src/main.rs b/crates/horizon-bots/src/main.rs new file mode 100644 index 000000000..d76da5e84 --- /dev/null +++ b/crates/horizon-bots/src/main.rs @@ -0,0 +1,589 @@ +//! `horizon-bots`: load-test bots for any Minecraft Java Edition 26.3 server, vanilla, Paper or Horizon +//! (docs/server.md, section 9). Each bot logs in offline, goes through configuration as vanilla's client +//! does, answers keep-alives and pings, accepts teleports, acknowledges chunk batches, and moves at 20 Hz. +//! +//! Bots fly a little above the ground, so the server must give them creative mode (`gamemode=creative` +//! and `force-gamemode=true`): they never touch blocks, so they need no world of their own, and no server +//! can refuse their moves for walking into terrain. Every behaviour is deterministic from `--seed`. +//! +//! horizon-bots --server 127.0.0.1:25565 --bots 25 --behaviour mixed --seconds 360 --report bots.json +//! +//! It prints one JSON line a second (bots online, chunks received) and writes a summary with `--report`. + +use horizon_protocol::{ + PROTOCOL_VERSION, + buf::{Reader, Writer}, + connection::{ConnWriter, Connection}, + crypto::offline_uuid, + ids, + packets::{ + Empty, configuration, + handshake::{self, Intention}, + login::{self, ServerHello}, + play, + }, +}; +use serde_json::json; +use std::{ + sync::{ + Arc, Mutex, + atomic::{AtomicBool, AtomicU64, Ordering}, + }, + thread, + time::{Duration, Instant}, +}; + +const HELP: &str = "\ +horizon-bots: load-test bots for a Minecraft Java Edition 26.3 server (offline mode, creative) + + --server HOST:PORT default 127.0.0.1:25565 + --bots N default 10 + --behaviour B idle, walk, explore or mixed (60 % walk, 20 % explore, 20 % idle); default mixed + --join-every SECONDS default 1 + --seconds N how long to run after the first bot joins; default 60 + --view-distance N what the bots ask for; default 10 + --seed N default 12345 + --name PREFIX bot names: PREFIX0, PREFIX1...; default Bot + --report FILE a JSON summary at the end +"; + +#[derive(Clone, Copy, Debug, PartialEq)] +enum Behaviour { + Idle, + Walk, + Explore, +} + +impl Behaviour { + fn name(self) -> &'static str { + match self { + Self::Idle => "idle", + Self::Walk => "walk", + Self::Explore => "explore", + } + } +} + +struct Options { + host: String, + port: u16, + bots: usize, + behaviour: String, + join_every: f64, + seconds: u64, + view_distance: i8, + seed: u64, + name: String, + report: Option, +} + +fn options() -> Result { + let mut o = Options { + host: "127.0.0.1".into(), + port: 25565, + bots: 10, + behaviour: "mixed".into(), + join_every: 1.0, + seconds: 60, + view_distance: 10, + seed: 12345, + name: "Bot".into(), + report: None, + }; + let mut args = std::env::args().skip(1); + while let Some(arg) = args.next() { + if arg == "-h" || arg == "--help" { + print!("{HELP}"); + std::process::exit(0); + } + let value = args.next().ok_or(format!("{arg} needs a value"))?; + let number = |what: &str| format!("{arg} needs {what}, not {value:?}"); + match arg.as_str() { + "--server" => { + let (host, port) = value.rsplit_once(':').unwrap_or((&value, "25565")); + o.host = host.into(); + o.port = port.parse().map_err(|_| number("a port"))?; + } + "--bots" => o.bots = value.parse().map_err(|_| number("a number"))?, + "--behaviour" => o.behaviour = value, + "--join-every" => o.join_every = value.parse().map_err(|_| number("seconds"))?, + "--seconds" => o.seconds = value.parse().map_err(|_| number("seconds"))?, + "--view-distance" => o.view_distance = value.parse().map_err(|_| number("2 to 32"))?, + "--seed" => o.seed = value.parse().map_err(|_| number("a number"))?, + "--name" => o.name = value, + "--report" => o.report = Some(value), + _ => return Err(format!("unknown option {arg}; --help lists them")), + } + } + if !["idle", "walk", "explore", "mixed"].contains(&o.behaviour.as_str()) { + return Err(format!( + "--behaviour {}: use idle, walk, explore or mixed", + o.behaviour + )); + } + Ok(o) +} + +/// What one bot is doing, shared between its reader and its ticks. +#[derive(Default)] +struct Bot { + online: AtomicBool, + chunks: AtomicU64, + /// Where the server last put the bot (a teleport), to start from. + teleport: Mutex>, + error: Mutex>, + joined_after_ms: AtomicU64, +} + +/// A small deterministic generator (SplitMix64), so every run moves the bots the same way. +struct Random(u64); +impl Random { + fn next(&mut self) -> u64 { + self.0 = self.0.wrapping_add(0x9E37_79B9_7F4A_7C15); + let mut z = self.0; + z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9); + z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB); + z ^ (z >> 31) + } + fn unit(&mut self) -> f64 { + (self.next() >> 11) as f64 / (1u64 << 53) as f64 + } +} + +fn raw(id: i32, write: impl FnOnce(&mut Writer)) -> Vec { + let mut w = Writer::new(); + w.varint(id); + write(&mut w); + w.data +} + +fn err(e: impl std::fmt::Display) -> String { + e.to_string() +} + +/// Handshake, offline login and configuration; returns the connection in the play state. +fn join(o: &Options, name: &str) -> Result { + let mut c = Connection::connect(&o.host, o.port, Duration::from_secs(10)).map_err(err)?; + c.send( + &handshake::Serverbound::Intention(Intention { + protocol: PROTOCOL_VERSION, + host: o.host.clone(), + port: o.port, + intent: handshake::LOGIN, + }) + .encode(), + ) + .map_err(err)?; + c.send( + &login::Serverbound::Hello(ServerHello { + name: name.into(), + uuid: offline_uuid(name), + }) + .encode(), + ) + .map_err(err)?; + loop { + let (id, body) = c.recv().map_err(err)?; + match login::Clientbound::decode(id, &body).map_err(err)? { + login::Clientbound::Compression(p) => c.set_compression(p.threshold), + login::Clientbound::LoginFinished(_) => { + c.send(&login::Serverbound::LoginAcknowledged(Empty).encode()) + .map_err(err)?; + break; + } + login::Clientbound::Disconnect(p) => return Err(format!("refused: {}", p.reason_json)), + login::Clientbound::Hello(_) => { + return Err("the server is in online mode; bots need online-mode=false".into()); + } + login::Clientbound::CustomQuery(p) => c + .send( + &login::Serverbound::CustomQueryAnswer(login::CustomQueryAnswer { + transaction: p.transaction, + payload: None, + }) + .encode(), + ) + .map_err(err)?, + login::Clientbound::CookieRequest(p) => c + .send( + &login::Serverbound::CookieResponse(login::CookieResponse { + key: p.key, + payload: None, + }) + .encode(), + ) + .map_err(err)?, + login::Clientbound::Unknown(..) => {} + } + } + use ids::configuration::clientbound as cb; + c.send( + &configuration::Serverbound::ClientInformation(configuration::ClientInformation::new( + o.view_distance, + )) + .encode(), + ) + .map_err(err)?; + loop { + let (id, body) = c.recv().map_err(err)?; + let reply = match id { + cb::SELECT_KNOWN_PACKS + | cb::KEEP_ALIVE + | cb::PING + | cb::CODE_OF_CONDUCT + | cb::COOKIE_REQUEST + | cb::DISCONNECT + | cb::FINISH_CONFIGURATION => { + match configuration::Clientbound::decode(id, &body).map_err(err)? { + configuration::Clientbound::SelectKnownPacks(p) => { + configuration::Serverbound::SelectKnownPacks(p) + } + configuration::Clientbound::KeepAlive(p) => { + configuration::Serverbound::KeepAlive(p) + } + configuration::Clientbound::Ping(p) => configuration::Serverbound::Pong(p), + configuration::Clientbound::CodeOfConduct(_) => { + configuration::Serverbound::AcceptCodeOfConduct(Empty) + } + configuration::Clientbound::CookieRequest(p) => { + configuration::Serverbound::CookieResponse(configuration::CookieResponse { + key: p.key, + payload: None, + }) + } + configuration::Clientbound::Disconnect(_) => { + return Err("disconnected during configuration".into()); + } + configuration::Clientbound::FinishConfiguration(_) => { + c.send(&configuration::Serverbound::FinishConfiguration(Empty).encode()) + .map_err(err)?; + return Ok(c); + } + _ => continue, + } + } + _ => continue, + }; + c.send(&reply.encode()).map_err(err)?; + } +} + +/// Reads the play state: answers what a client must answer, counts chunks, records teleports. +fn read( + mut reader: horizon_protocol::connection::ConnReader, + writer: Arc>, + bot: Arc, +) { + use ids::play::clientbound as cb; + use ids::play::serverbound as sb; + let send = |body: Vec| writer.lock().unwrap().send(&body); + loop { + let (id, body) = match reader.recv() { + Ok(packet) => packet, + Err(e) => { + if bot.online.swap(false, Ordering::SeqCst) { + *bot.error.lock().unwrap() = Some(e.to_string()); + } + return; + } + }; + let mut r = Reader::new(&body); + let sent = match id { + cb::KEEP_ALIVE => match r.i64() { + Ok(v) => send(raw(sb::KEEP_ALIVE, |w| w.i64(v))), + Err(_) => Ok(()), + }, + cb::PING => match r.i32() { + Ok(v) => send(raw(sb::PONG, |w| w.i32(v))), + Err(_) => Ok(()), + }, + cb::LEVEL_CHUNK_WITH_LIGHT => { + bot.chunks.fetch_add(1, Ordering::Relaxed); + Ok(()) + } + cb::CHUNK_BATCH_FINISHED => send( + play::Serverbound::ChunkBatchReceived(play::ChunkBatchReceived { + chunks_per_tick: 25.0, + }) + .encode(), + ), + cb::PLAYER_POSITION => match play::Clientbound::decode(id, &body) { + Ok(play::Clientbound::PlayerPosition(p)) => { + let mut at = bot.teleport.lock().unwrap(); + let previous = at.unwrap_or([0.0; 3]); + // Relative flags: X 1, Y 2, Z 4. + let pos: [f64; 3] = std::array::from_fn(|i| { + if p.relatives & (1 << i) != 0 { + previous[i] + p.position[i] + } else { + p.position[i] + } + }); + *at = Some(pos); + send( + play::Serverbound::AcceptTeleportation(play::AcceptTeleportation { + id: p.teleport, + x: pos[0], + y: pos[1], + z: pos[2], + yaw: p.yaw, + pitch: p.pitch, + }) + .encode(), + ) + } + _ => Ok(()), + }, + cb::DISCONNECT | cb::START_CONFIGURATION => { + bot.online.store(false, Ordering::SeqCst); + *bot.error.lock().unwrap() = Some(if id == cb::DISCONNECT { + format!( + "disconnected: {}", + String::from_utf8_lossy(&body) + .chars() + .filter(|c| !c.is_control()) + .collect::() + ) + } else { + "sent back to configuration".into() + }); + return; + } + _ => Ok(()), + }; + if sent.is_err() { + bot.online.store(false, Ordering::SeqCst); + return; + } + } +} + +/// One bot: join, then move every 50 ms until `until`. +fn run_bot( + o: Arc, + index: usize, + behaviour: Behaviour, + bot: Arc, + until: Arc>>, +) { + let name = format!("{}{index}", o.name); + let asked = Instant::now(); + let connection = match join(&o, &name) { + Ok(c) => c, + Err(e) => { + *bot.error.lock().unwrap() = Some(e); + return; + } + }; + let (reader, writer) = connection.split(); + let writer = Arc::new(Mutex::new(writer)); + bot.online.store(true, Ordering::SeqCst); + { + let (writer, bot) = (writer.clone(), bot.clone()); + thread::spawn(move || read(reader, writer, bot)); + } + use ids::play::serverbound as sb; + // Wait for the spawn position. + let spawn = loop { + if let Some(at) = *bot.teleport.lock().unwrap() { + break at; + } + if !bot.online.load(Ordering::SeqCst) || asked.elapsed() > Duration::from_secs(60) { + if bot.error.lock().unwrap().is_none() { + *bot.error.lock().unwrap() = Some("no spawn position within 60 s".into()); + } + return; + } + thread::sleep(Duration::from_millis(20)); + }; + bot.joined_after_ms + .store(asked.elapsed().as_millis() as u64, Ordering::SeqCst); + let send = |body: Vec| writer.lock().unwrap().send(&body).is_ok(); + send(raw(sb::PLAYER_LOADED, |_| {})); + let mut random = Random(o.seed ^ (index as u64).wrapping_mul(0x2545_F491_4F6C_DD1D)); + let mut pos = spawn; + let cruise = spawn[1] + 24.0; + let heading = random.unit() * std::f64::consts::TAU; + let mut target = pos; + let mut next = Instant::now(); + loop { + if until.lock().unwrap().is_some_and(|u| Instant::now() >= u) + || !bot.online.load(Ordering::SeqCst) + { + break; + } + // A teleport from the server wins over where the bot thought it was. + if let Some(at) = bot.teleport.lock().unwrap().take() { + pos = at; + } + if pos[1] < cruise { + pos[1] = (pos[1] + 0.5).min(cruise); + } else { + match behaviour { + Behaviour::Idle => {} + Behaviour::Walk => { + let (dx, dz) = (target[0] - pos[0], target[2] - pos[2]); + let distance = dx.hypot(dz); + if distance < 1.0 { + let angle = random.unit() * std::f64::consts::TAU; + let reach = 8.0 + random.unit() * 40.0; + target = [ + spawn[0] + angle.cos() * reach, + cruise, + spawn[2] + angle.sin() * reach, + ]; + } else { + // Sprinting: 5.6 blocks a second. + let step = 0.28_f64.min(distance); + pos[0] += dx / distance * step; + pos[2] += dz / distance * step; + } + } + Behaviour::Explore => { + // Flying fast in creative: about 22 blocks a second, which keeps new chunks coming. + pos[0] += heading.cos() * 1.1; + pos[2] += heading.sin() * 1.1; + } + } + } + let ok = send( + play::Serverbound::MovePlayerPos(play::MovePlayerPos { + x: pos[0], + y: pos[1], + z: pos[2], + flags: 0, + }) + .encode(), + ) && send(raw(sb::CLIENT_TICK_END, |_| {})); + if !ok { + bot.online.store(false, Ordering::SeqCst); + break; + } + next += Duration::from_millis(50); + let now = Instant::now(); + if next > now { + thread::sleep(next - now); + } else if now - next > Duration::from_secs(1) { + next = now; + } + } + // Offline first, so the reader doesn't count the closed connection as a failure. + bot.online.store(false, Ordering::SeqCst); + writer.lock().unwrap().shutdown(); +} + +fn behaviour_of(mode: &str, index: usize) -> Behaviour { + match mode { + "idle" => Behaviour::Idle, + "walk" => Behaviour::Walk, + "explore" => Behaviour::Explore, + // mixed: of every five bots, three walk, one explores, one stands. + _ => match index % 5 { + 0 | 2 | 4 => Behaviour::Walk, + 1 => Behaviour::Explore, + _ => Behaviour::Idle, + }, + } +} + +fn main() { + let o = match options() { + Ok(o) => Arc::new(o), + Err(e) => { + eprintln!("horizon-bots: {e}"); + std::process::exit(2); + } + }; + let start = Instant::now(); + let until: Arc>> = Arc::new(Mutex::new(None)); + let bots: Vec> = (0..o.bots).map(|_| Arc::new(Bot::default())).collect(); + let launcher = { + let (o, bots, until) = (o.clone(), bots.clone(), until.clone()); + thread::spawn(move || { + let mut handles = Vec::new(); + for (i, bot) in bots.iter().enumerate() { + let (o2, bot, until) = (o.clone(), bot.clone(), until.clone()); + let behaviour = behaviour_of(&o.behaviour, i); + handles.push(thread::spawn(move || run_bot(o2, i, behaviour, bot, until))); + thread::sleep(Duration::from_secs_f64(o.join_every)); + } + handles + }) + }; + // The run lasts --seconds after the first bot is in. + let mut previous_chunks = 0; + let mut first = None; + loop { + thread::sleep(Duration::from_secs(1)); + let online = bots + .iter() + .filter(|b| b.online.load(Ordering::SeqCst)) + .count(); + let chunks: u64 = bots.iter().map(|b| b.chunks.load(Ordering::Relaxed)).sum(); + let failed = bots + .iter() + .filter(|b| b.error.lock().unwrap().is_some()) + .count(); + if first.is_none() && online > 0 { + first = Some(Instant::now()); + *until.lock().unwrap() = Some(Instant::now() + Duration::from_secs(o.seconds)); + } + println!( + "{}", + json!({ + "t": start.elapsed().as_secs(), + "online": online, + "failed": failed, + "chunks": chunks, + "chunks_per_second": chunks - previous_chunks, + }) + ); + previous_chunks = chunks; + let over = until.lock().unwrap().is_some_and(|u| Instant::now() >= u); + let all_failed = failed == o.bots; + if over || all_failed || (first.is_none() && start.elapsed() > Duration::from_secs(120)) { + *until.lock().unwrap() = Some(Instant::now()); + break; + } + } + for handle in launcher.join().unwrap_or_default() { + let _ = handle.join(); + } + let summary = json!({ + "server": format!("{}:{}", o.host, o.port), + "bots": o.bots, + "behaviour": o.behaviour, + "seed": o.seed, + "view_distance": o.view_distance, + "seconds": o.seconds, + "joined": bots.iter().filter(|b| b.joined_after_ms.load(Ordering::SeqCst) > 0).count(), + "chunks": bots.iter().map(|b| b.chunks.load(Ordering::Relaxed)).sum::(), + "per_bot": bots.iter().enumerate().map(|(i, b)| json!({ + "name": format!("{}{i}", o.name), + "behaviour": behaviour_of(&o.behaviour, i).name(), + "joined_after_ms": b.joined_after_ms.load(Ordering::SeqCst), + "chunks": b.chunks.load(Ordering::Relaxed), + "error": *b.error.lock().unwrap(), + })).collect::>(), + }); + if let Some(path) = &o.report { + if let Err(e) = std::fs::write(path, format!("{summary:#}\n")) { + eprintln!("horizon-bots: {path}: {e}"); + } + } + let errors: Vec = bots + .iter() + .enumerate() + .filter_map(|(i, b)| { + b.error + .lock() + .unwrap() + .clone() + .map(|e| format!("{}{i}: {e}", o.name)) + }) + .collect(); + for e in errors.iter().take(10) { + eprintln!("horizon-bots: {e}"); + } + std::process::exit(if errors.is_empty() { 0 } else { 1 }); +} diff --git a/crates/horizon-client/Cargo.toml b/crates/horizon-client/Cargo.toml index 19b4c81db..29a02553f 100644 --- a/crates/horizon-client/Cargo.toml +++ b/crates/horizon-client/Cargo.toml @@ -12,6 +12,8 @@ horizon-assets = { path = "../horizon-assets" } horizon-core = { path = "../horizon-core" } horizon-world = { path = "../horizon-world" } horizon-protocol = { path = "../horizon-protocol" } +# server.properties, eula.txt and the user lists, shared with horizon-server. +horizon-server-files = { path = "../horizon-server-files" } horizon-friends = { path = "../horizon-friends" } # Horizon mods: sandboxed WebAssembly components, off unless installed and turned on (docs/mods.md). horizon-mods = { path = "../horizon-mods" } diff --git a/crates/horizon-client/src/game/dedicated.rs b/crates/horizon-client/src/game/dedicated.rs index 01728b351..79aa85204 100644 --- a/crates/horizon-client/src/game/dedicated.rs +++ b/crates/horizon-client/src/game/dedicated.rs @@ -29,6 +29,8 @@ use std::{ time::{Duration, Instant}, }; +mod status; + /// Double.toString for the magnitudes positions have (no exponent form /// between 1e-3 and 1e7). pub(super) fn java_double(v: f64) -> String { @@ -64,6 +66,11 @@ pub(super) fn log(level: &str, message: &str) { _ => ::log::Level::Info, }; horizon_client::logging::write_only(file_level, message); + CAPTURE.with_borrow_mut(|lines| { + if let Some(lines) = lines { + lines.push(message.to_owned()); + } + }); let secs = now_millis().div_euclid(1000).rem_euclid(86_400); println!( "[{:02}:{:02}:{:02}] [Server thread/{level}]: {message}", @@ -73,6 +80,18 @@ pub(super) fn log(level: &str, message: &str) { ); } +thread_local! { + /// The lines logged while a control request runs a console command. + static CAPTURE: std::cell::RefCell>> = const { std::cell::RefCell::new(None) }; +} + +/// Runs `f` and returns the lines it logged. +fn capture_log(f: impl FnOnce()) -> Vec { + CAPTURE.set(Some(Vec::new())); + f(); + CAPTURE.take().unwrap_or_default() +} + /// Main.main up to the server start: arguments, server.properties (stored /// back at once), eula.txt. False when the process should stop here. pub(super) fn preflight(options: &mut Options) -> Result { @@ -289,6 +308,9 @@ struct ServerState { stop: bool, autosave: bool, empty_ticks: i32, + /// When the last passes of the loop ended, for the tick rate horizon-server shows. + ticks: std::collections::VecDeque, + paused: bool, } impl Game<'_> { @@ -470,14 +492,18 @@ impl Game<'_> { stop: false, autosave: true, empty_ticks: 0, + ticks: Default::default(), + paused: false, }; let pause_after = props.pause_when_empty_seconds * 20; let idle_timeout = props.player_idle_timeout; let mut next = Instant::now(); while !state.stop { + let tick_start = Instant::now(); while let Ok(line) = state.console.try_recv() { self.console_command(&mut state, &line); } + self.poll_server_control(&mut state); let players = self.lan.as_ref().map_or(0, |h| h.hub.guests.len()); if pause_after > 0 { if players == 0 { @@ -487,6 +513,7 @@ impl Game<'_> { } } let paused = pause_after > 0 && state.empty_ticks >= pause_after; + state.paused = paused; if paused && state.empty_ticks == pause_after { log( "INFO", @@ -512,6 +539,9 @@ impl Game<'_> { if idle_timeout > 0 { self.kick_idle(idle_timeout); } + // MinecraftServer.tickServer records each tick's time, for /tick query (and the status). + self.clock.record(tick_start.elapsed()); + state.ticked(Instant::now()); let rate = self.clock.rate().max(1.0); next += Duration::from_secs_f64(1.0 / f64::from(rate)); let now = Instant::now(); diff --git a/crates/horizon-client/src/game/dedicated/status.rs b/crates/horizon-client/src/game/dedicated/status.rs new file mode 100644 index 000000000..a2ec4c1ae --- /dev/null +++ b/crates/horizon-client/src/game/dedicated/status.rs @@ -0,0 +1,100 @@ +//! What `horizon-server` asks the dedicated server on its control endpoint (`--control-socket`, docs/server.md +//! section 2): `server-status` answers with the tick rate, the tick times `/tick query` reports, the players +//! and the loaded chunks; `console ` runs a console command and answers with the lines it logged. +use super::{ServerState, capture_log}; +use crate::game::Game; +use serde_json::{Value, json}; +use std::{ + sync::atomic::Ordering, + time::{Duration, Instant}, +}; + +/// The window the tick rate is measured over. +const TPS_WINDOW: Duration = Duration::from_secs(5); + +impl ServerState { + /// One pass of the server loop ended. + pub(super) fn ticked(&mut self, at: Instant) { + self.ticks.push_back(at); + while self + .ticks + .front() + .is_some_and(|t| at.duration_since(*t) > TPS_WINDOW) + { + self.ticks.pop_front(); + } + } + /// Ticks per second over the last few seconds. + fn tps(&self) -> f64 { + match (self.ticks.front(), self.ticks.back()) { + (Some(first), Some(last)) if self.ticks.len() > 1 => { + (self.ticks.len() - 1) as f64 / last.duration_since(*first).as_secs_f64().max(1e-9) + } + _ => 0.0, + } + } +} + +impl Game<'_> { + /// Answers the requests waiting on the control endpoint. + pub(super) fn poll_server_control(&mut self, state: &mut ServerState) { + while let Some(request) = self.control.as_ref().and_then(|c| c.try_recv()) { + let reply = match request.line.trim().split_once(' ') { + _ if request.line.trim() == "server-status" => self.server_status(state), + Some(("console", line)) => { + // Command feedback is logged as system chat; the reply carries the text alone. + let output: Vec = capture_log(|| self.console_command(state, line)) + .into_iter() + .map(|l| { + l.strip_prefix("System chat: ") + .map_or(l.clone(), str::to_owned) + }) + .collect(); + json!({ "output": output }) + } + _ => json!({ "error": format!("unknown request {:?}", request.line) }), + }; + let _ = request.reply.send(reply.to_string()); + } + } + + fn server_status(&self, state: &ServerState) -> Value { + let millis = |nanos: u64| nanos as f64 / 1e6; + let samples = self.clock.sorted_samples(); + // The percentiles `/tick query` prints (TickCommand.percentileToString). + let at = |q: f64| { + let i = ((samples.len() as f64 * q) as usize).min(samples.len().saturating_sub(1)); + samples.get(i).copied().map_or(0.0, millis) + }; + let (players, max_players) = self.lan.as_ref().map_or((Vec::new(), 0), |h| { + let players = h + .hub + .guests + .iter() + .map(|g| { + json!({ + "name": g.name, + "uuid": horizon_protocol::crypto::uuid_string(g.uuid), + "address": g.address.ip().to_string(), + "ping_ms": g.latency.load(Ordering::Relaxed), + }) + }) + .collect(); + (players, h.hub.lan.shared.max_players()) + }); + json!({ + "tps": state.tps(), + "target_tps": self.clock.rate(), + "mspt": { + "average": millis(self.clock.average_nanos()), + "p50": at(0.5), + "p95": at(0.95), + "p99": at(0.99), + }, + "players": players, + "max_players": max_players, + "chunks": self.world.chunks.len(), + "paused": state.paused, + }) + } +} diff --git a/crates/horizon-client/src/game/falling_damage.rs b/crates/horizon-client/src/game/falling_damage.rs index 313ddbaac..16a003c04 100644 --- a/crates/horizon-client/src/game/falling_damage.rs +++ b/crates/horizon-client/src/game/falling_damage.rs @@ -43,11 +43,11 @@ impl Game<'_> { if anvil && impact.wear_chance > 0.0 && self.world.random.next_float() < impact.wear_chance - && let Some(mut state) = self.res.assets.registry.state(block.state).cloned() + && let Some(mut state) = self.pack.assets.registry.state(block.state).cloned() { if falling_block::wear_anvil(&mut state) { block.state = self - .res + .pack .assets .registry .resolve(&state) diff --git a/crates/horizon-client/src/lib.rs b/crates/horizon-client/src/lib.rs index 6148dc4aa..79a8ddd86 100644 --- a/crates/horizon-client/src/lib.rs +++ b/crates/horizon-client/src/lib.rs @@ -148,7 +148,7 @@ pub mod screen_draw; pub mod screen_effects; pub mod screen_layout; pub mod sculk; -pub mod server; +pub use horizon_server_files as server; pub mod settings; pub mod shader_pack; pub mod shadows; diff --git a/crates/horizon-server-files/Cargo.toml b/crates/horizon-server-files/Cargo.toml new file mode 100644 index 000000000..52114718d --- /dev/null +++ b/crates/horizon-server-files/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "horizon-server-files" +version = "0.1.0" +edition.workspace = true +license.workspace = true +rust-version = "1.90" +description = "The dedicated server's vanilla files: server.properties, eula.txt, ops, whitelist and bans" + +[dependencies] +horizon-core = { path = "../horizon-core" } +horizon-protocol = { path = "../horizon-protocol" } +horizon-world = { path = "../horizon-world" } +log = "0.4" + +[lints] +workspace = true diff --git a/crates/horizon-client/src/server/eula.rs b/crates/horizon-server-files/src/eula.rs similarity index 86% rename from crates/horizon-client/src/server/eula.rs rename to crates/horizon-server-files/src/eula.rs index b5896cfd7..769af9401 100644 --- a/crates/horizon-client/src/server/eula.rs +++ b/crates/horizon-server-files/src/eula.rs @@ -9,8 +9,13 @@ pub const EULA_LINK: &str = "https://aka.ms/MinecraftEULA"; /// The default file, as Properties.store(OutputStream) writes it. pub fn default_file(millis: i64) -> String { + file(millis, false) +} + +/// The file with `eula` set: what the owner's agreement writes (horizon-server init). +pub fn file(millis: i64, agreed: bool) -> String { let mut p = Properties::default(); - p.put("eula", "false".into()); + p.put("eula", agreed.to_string()); p.store( Some(&format!( "By changing the setting below to TRUE you are indicating your agreement to our EULA ({EULA_LINK})." diff --git a/crates/horizon-client/src/server/java_properties.rs b/crates/horizon-server-files/src/java_properties.rs similarity index 100% rename from crates/horizon-client/src/server/java_properties.rs rename to crates/horizon-server-files/src/java_properties.rs diff --git a/crates/horizon-client/src/server/json.rs b/crates/horizon-server-files/src/json.rs similarity index 100% rename from crates/horizon-client/src/server/json.rs rename to crates/horizon-server-files/src/json.rs diff --git a/crates/horizon-client/src/server/mod.rs b/crates/horizon-server-files/src/lib.rs similarity index 50% rename from crates/horizon-client/src/server/mod.rs rename to crates/horizon-server-files/src/lib.rs index 1bb0d80c1..30c75c869 100644 --- a/crates/horizon-client/src/server/mod.rs +++ b/crates/horizon-server-files/src/lib.rs @@ -1,8 +1,8 @@ //! The dedicated server's files and settings, ported from //! net.minecraft.server.dedicated and net.minecraft.server.players: -//! server.properties, eula.txt and the four user lists. The server itself -//! runs the game's own world, simulation and LAN hub headless -//! (`horizon-client --dedicated`, see the game's `dedicated` module). +//! server.properties, eula.txt and the four user lists. The game's dedicated +//! server (`horizon-client --dedicated`) reads them, and `horizon-server` +//! manages them; this crate needs neither a window nor the game. pub mod eula; pub mod java_properties; pub mod json; diff --git a/crates/horizon-client/src/server/properties.rs b/crates/horizon-server-files/src/properties.rs similarity index 100% rename from crates/horizon-client/src/server/properties.rs rename to crates/horizon-server-files/src/properties.rs diff --git a/crates/horizon-client/src/server/user_lists.rs b/crates/horizon-server-files/src/user_lists.rs similarity index 100% rename from crates/horizon-client/src/server/user_lists.rs rename to crates/horizon-server-files/src/user_lists.rs diff --git a/crates/horizon-server/Cargo.toml b/crates/horizon-server/Cargo.toml new file mode 100644 index 000000000..c3800b80c --- /dev/null +++ b/crates/horizon-server/Cargo.toml @@ -0,0 +1,29 @@ +[package] +name = "horizon-server" +version = "0.1.0" +edition.workspace = true +license.workspace = true +rust-version = "1.90" +description = "Host a Horizon server: server.toml, the CLI and the supervisor that runs the game headless (docs/server.md)" + +[dependencies] +horizon-server-files = { path = "../horizon-server-files" } +# The game process's RAM and CPU (the same probes the benchmark reports use). +horizon-bench = { path = "../horizon-bench" } +serde = { version = "1", features = ["derive"] } +serde_json = "1" +toml = "1.1" +# Edits server.toml in place, keeping the owner's comments and order. +toml_edit = "0.25" +# Backups: one zip anyone can open, Windows included. +zip = { version = "2", default-features = false, features = ["deflate"] } + +[target.'cfg(unix)'.dependencies] +# SIGINT/SIGTERM/SIGHUP: stop the game cleanly when the terminal or the service manager asks. +libc = "0.2" + +[target.'cfg(windows)'.dependencies] +windows-sys = { version = "0.61", features = ["Win32_Foundation", "Win32_System_Console", "Win32_System_Threading"] } + +[lints] +workspace = true 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

+ + + +
NamePing
+

Nobody is online.

+
+ +
+

Backups

+
+ + +
+ + + +
BackupSize
+

No backups yet.

+
+
+ +
+

Console

+

+    
+ + +
+
+
+ +
+

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 new file mode 100644 index 000000000..85bc0e2b5 --- /dev/null +++ b/crates/horizon-server/src/api.rs @@ -0,0 +1,497 @@ +//! The control API (docs/server.md, section 8): HTTP and JSON on 127.0.0.1, with a random token in +//! `.horizon/api.json` that only the server's own user can read. The CLI manages a running server through +//! it; the web panel and the MCP server will too. +//! +//! - `GET /api/v1/status`: the supervisor's state, the game's status (TPS, MSPT, players), RAM and CPU. +//! - `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::{ + collections::VecDeque, + io::Write, + net::{SocketAddr, TcpListener}, + path::{Path, PathBuf}, + sync::{ + Arc, Mutex, + atomic::{AtomicBool, Ordering}, + }, + time::{Duration, Instant}, +}; + +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)] +pub enum Phase { + Starting, + Running, + Stopping, + /// Stopping the game to start it again (`restart`), or waiting to after a crash. + Restarting, +} + +impl Phase { + fn name(self) -> &'static str { + match self { + Self::Starting => "starting", + Self::Running => "running", + Self::Stopping => "stopping", + Self::Restarting => "restarting", + } + } +} + +pub struct State { + pub phase: Phase, + pub engine_pid: Option, + pub engine_started: Option, + pub crashes: u32, + pub last_crash: Option, + /// The latest sample (`sample`): the game's status, RAM and CPU. + pub sample: Value, +} + +/// The log, numbered from the first line since the supervisor started. +#[derive(Default)] +pub struct Log { + lines: VecDeque, + first: u64, +} + +impl Log { + pub fn push(&mut self, line: String) { + if self.lines.len() == LOG_LINES { + self.lines.pop_front(); + self.first += 1; + } + self.lines.push_back(line); + } + /// The lines after the `since`th, and the number to ask from next. + pub fn since(&self, since: u64) -> (u64, Vec) { + let skip = since.saturating_sub(self.first) as usize; + let next = self.first + self.lines.len() as u64; + (next, self.lines.iter().skip(skip).cloned().collect()) + } +} + +/// What the supervisor and the API threads share. +pub struct Shared { + pub dir: PathBuf, + pub state: Mutex, + pub log: Mutex, + pub stop: AtomicBool, + pub restart: AtomicBool, + /// The backup to put in place before the game starts again (`restore`). + pub restore: Mutex>, + /// 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 { + pub fn new(dir: &Path) -> Arc { + Arc::new(Self { + dir: dir.to_owned(), + state: Mutex::new(State { + phase: Phase::Starting, + engine_pid: None, + engine_started: None, + crashes: 0, + last_crash: None, + sample: Value::Null, + }), + log: Mutex::new(Log::default()), + stop: AtomicBool::new(false), + restart: AtomicBool::new(false), + 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; + } + fn phase(&self) -> Phase { + self.state.lock().unwrap().phase + } + + /// A backup while the server runs: the game saves everything and stops writing while the zip is made. + pub fn back_up(&self, file_name: &str) -> Result { + let _one = self.backing_up.lock().unwrap(); + let config = Config::load(&self.dir)?; + let mut game = match self.phase() { + Phase::Running => Some( + engine::Control::connect(&self.dir) + .map_err(|e| format!("the game didn't answer: {e}"))?, + ), + // Between a crash and the restart nothing writes the world. + Phase::Restarting => None, + Phase::Starting | Phase::Stopping => { + return Err("the game is starting or stopping: try again in a moment".into()); + } + }; + if let Some(game) = game.as_mut() { + for command in ["console save-off", "console save-all flush"] { + game.ask(command) + .map_err(|e| format!("the game didn't save: {e}"))?; + } + } + let result = backup::create(&self.dir, &config, file_name); + if let Some(game) = game.as_mut() { + let _ = game.ask("console save-on"); + } + result + } +} + +fn status(shared: &Shared) -> Value { + let state = shared.state.lock().unwrap(); + json!({ + "phase": state.phase.name(), + "version": env!("CARGO_PKG_VERSION"), + "pid": std::process::id(), + "uptime_seconds": shared.started.elapsed().as_secs(), + "engine_pid": state.engine_pid, + "engine_uptime_seconds": state.engine_started.map(|t| t.elapsed().as_secs()), + "crashes": state.crashes, + "last_crash": state.last_crash, + "sample": state.sample, + }) +} + +/// A random token: two 64-bit hashes keyed by the OS's random hasher seeds, the time and the process. +pub fn random_token() -> String { + use std::hash::{BuildHasher, Hasher}; + (0..2u64) + .map(|i| { + let mut h = std::collections::hash_map::RandomState::new().build_hasher(); + h.write_u64(i); + h.write_u128( + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map_or(0, |d| d.as_nanos()), + ); + h.write_u32(std::process::id()); + format!("{:016x}", h.finish()) + }) + .collect() +} + +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 +} + +/// Writes a file only its owner can read. +pub fn write_private(path: &Path, text: &str) -> std::io::Result<()> { + let mut options = std::fs::OpenOptions::new(); + options.write(true).create(true).truncate(true); + #[cfg(unix)] + std::os::unix::fs::OpenOptionsExt::mode(&mut options, 0o600); + options.open(path)?.write_all(text.as_bytes()) +} + +/// Removes the endpoint file when the supervisor ends. +pub struct Endpoint(PathBuf); +impl Drop for Endpoint { + fn drop(&mut self) { + let _ = std::fs::remove_file(&self.0); + } +} + +/// Starts the API on a free port of 127.0.0.1 and writes where it is and its token. +pub fn start(shared: Arc) -> Result { + let listener = TcpListener::bind("127.0.0.1:0").map_err(|e| format!("control API: {e}"))?; + let address = listener.local_addr().map_err(|e| e.to_string())?; + let token = random_token(); + let path = shared.dir.join(ENDPOINT); + write_private( + &path, + &json!({ "address": address.to_string(), "token": token, "pid": std::process::id() }) + .to_string(), + ) + .map_err(|e| format!("{}: {e}", path.display()))?; + let hosts = [address.to_string(), format!("localhost:{}", address.port())]; + http::serve(listener, move |request| { + // A page in a browser can't send the token, and a rebound DNS name can't pass as this address. + let authorised = request + .header("authorization") + .and_then(|h| h.strip_prefix("Bearer ")) + .is_some_and(|t| same_token(t.trim(), &token)); + if !authorised { + return Response::error(401, "missing or wrong token (see .horizon/api.json)"); + } + if !request + .header("host") + .is_some_and(|h| hosts.iter().any(|a| a == h)) + { + 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)) +} + +/// 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") + .and_then(|s| s.parse().ok()) + .unwrap_or(0); + let (next, lines) = shared.log.lock().unwrap().since(since); + Response::json(200, &json!({ "next": next, "lines": lines })) + } + ("POST", "/api/v1/command") => { + let line = serde_json::from_slice::(&request.body) + .ok() + .and_then(|v| v["line"].as_str().map(str::to_owned)); + let Some(line) = line.filter(|l| !l.trim().is_empty() && !l.contains('\n')) else { + return Response::error(400, "the body must be {\"line\": \"\"}"); + }; + if shared.state.lock().unwrap().phase != Phase::Running { + return Response::error(409, "the game isn't running right now"); + } + match engine::Control::connect(&shared.dir) + .and_then(|mut c| c.ask(&format!("console {}", line.trim_start_matches('/')))) + { + Ok(reply) => Response::json(200, &reply), + Err(e) => Response::error(409, &format!("the game didn't answer: {e}")), + } + } + ("POST", "/api/v1/stop") => { + shared.stop.store(true, Ordering::SeqCst); + Response::json(200, &json!({ "ok": true })) + } + ("POST", "/api/v1/restart") => { + shared.restart.store(true, Ordering::SeqCst); + Response::json(200, &json!({ "ok": true })) + } + ("GET", "/api/v1/backups") => { + let list: Vec = backup::list(&shared.dir) + .into_iter() + .map(|b| json!({ "name": b.name, "bytes": b.bytes })) + .collect(); + Response::json(200, &json!({ "backups": list })) + } + ("POST", "/api/v1/backups") => { + let body: Value = serde_json::from_slice(&request.body).unwrap_or(Value::Null); + let made = backup::file_name(std::time::SystemTime::now(), body["name"].as_str()) + .and_then(|name| shared.back_up(&name)); + match made { + Ok(path) => Response::json( + 200, + &json!({ "name": path.file_name().map(|n| n.to_string_lossy()) }), + ), + Err(e) => Response::error(409, &e), + } + } + ("POST", "/api/v1/restore") => { + let body: Value = serde_json::from_slice(&request.body).unwrap_or(Value::Null); + match backup::find(&shared.dir, body["name"].as_str().unwrap_or_default()) { + Ok(name) => { + *shared.restore.lock().unwrap() = Some(name.clone()); + shared.restart.store(true, Ordering::SeqCst); + Response::json(200, &json!({ "name": name })) + } + Err(e) => Response::error(404, &e), + } + } + ( + _, + "/api/v1/status" | "/api/v1/logs" | "/api/v1/command" | "/api/v1/stop" + | "/api/v1/restart" | "/api/v1/backups" | "/api/v1/restore" | "/api/v1/metrics", + ) => Response::error(405, "wrong method"), + _ => Response::error(404, "no such endpoint"), + } +} + +/// The API of the server running in a folder, for the CLI. +pub struct Client { + address: SocketAddr, + token: String, +} + +impl Client { + /// The running server's API; `None` when no server runs in `dir`. + pub fn open(dir: &Path) -> Result, String> { + if crate::supervisor::lock(dir).is_ok() { + return Ok(None); + } + // The supervisor writes the endpoint just after taking the lock: give it a moment. + let path = dir.join(ENDPOINT); + let deadline = Instant::now() + Duration::from_secs(5); + let text = loop { + match std::fs::read_to_string(&path) { + Ok(text) => break text, + Err(_) if Instant::now() < deadline => { + std::thread::sleep(Duration::from_millis(100)) + } + Err(e) => return Err(format!("{}: {e}", path.display())), + } + }; + let v: Value = + serde_json::from_str(&text).map_err(|e| format!("{}: {e}", path.display()))?; + let address = v["address"] + .as_str() + .and_then(|a| a.parse().ok()) + .ok_or_else(|| format!("{}: no address", path.display()))?; + let token = v["token"].as_str().unwrap_or_default().to_owned(); + Ok(Some(Self { address, token })) + } + + pub fn call(&self, method: &str, path: &str, body: Option<&Value>) -> Result { + self.call_within(method, path, body, Duration::from_secs(60)) + } + + /// A call that may take long (a backup of a big world). + pub fn call_within( + &self, + method: &str, + path: &str, + body: Option<&Value>, + timeout: Duration, + ) -> Result { + let (status, text) = http::request(self.address, &self.token, method, path, body, timeout) + .map_err(|e| format!("the server didn't answer: {e}"))?; + let value: Value = serde_json::from_str(&text).unwrap_or(Value::Null); + if status == 200 { + Ok(value) + } else { + Err(value["error"].as_str().unwrap_or(&text).to_owned()) + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn log_keeps_the_last_lines_numbered() { + let mut log = Log::default(); + for i in 0..LOG_LINES + 10 { + log.push(i.to_string()); + } + let (next, lines) = log.since(0); + assert_eq!(next, (LOG_LINES + 10) as u64); + assert_eq!(lines.len(), LOG_LINES); + assert_eq!(lines[0], "10"); + let (next2, tail) = log.since(next - 2); + assert_eq!( + (next2, tail), + ( + next, + vec![(LOG_LINES + 8).to_string(), (LOG_LINES + 9).to_string()] + ) + ); + } + + #[test] + fn the_api_wants_its_token() { + let dir = std::env::temp_dir().join(format!("horizon-server-api-{}", std::process::id())); + std::fs::create_dir_all(dir.join(".horizon")).unwrap(); + let shared = Shared::new(&dir); + let _endpoint = start(shared.clone()).unwrap(); + let v: Value = + serde_json::from_str(&std::fs::read_to_string(dir.join(ENDPOINT)).unwrap()).unwrap(); + let address: SocketAddr = v["address"].as_str().unwrap().parse().unwrap(); + let token = v["token"].as_str().unwrap(); + let get = |token: &str| { + http::request( + address, + token, + "GET", + "/api/v1/status", + None, + Duration::from_secs(5), + ) + .unwrap() + }; + assert_eq!(get("nope").0, 401); + let (status, body) = get(token); + assert_eq!(status, 200); + let status: Value = serde_json::from_str(&body).unwrap(); + assert_eq!(status["phase"], "starting"); + let (code, _) = http::request( + address, + token, + "POST", + "/api/v1/stop", + None, + Duration::from_secs(5), + ) + .unwrap(); + assert_eq!(code, 200); + assert!(shared.stop.load(Ordering::SeqCst)); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + let mode = std::fs::metadata(dir.join(ENDPOINT)) + .unwrap() + .permissions() + .mode(); + assert_eq!(mode & 0o777, 0o600); + } + drop(_endpoint); + assert!(!dir.join(ENDPOINT).exists()); + std::fs::remove_dir_all(&dir).unwrap(); + } +} diff --git a/crates/horizon-server/src/backup.rs b/crates/horizon-server/src/backup.rs new file mode 100644 index 000000000..c446d56ca --- /dev/null +++ b/crates/horizon-server/src/backup.rs @@ -0,0 +1,332 @@ +//! Backups and restore (docs/server.md, section 7). A backup is one zip in `backups/`: the world folder and +//! the server's settings and lists. On a running server the game first saves everything and stops writing +//! (`save-off`, `save-all`), and writes again once the zip is done (`save-on`), so players stay online. +//! A restore puts the backup's world in place of the current one, which is moved aside, never deleted. + +use crate::config::Config; +use std::{ + fs::File, + io::{BufWriter, Write}, + path::{Path, PathBuf}, + time::SystemTime, +}; +use zip::{CompressionMethod, ZipArchive, ZipWriter, write::SimpleFileOptions}; + +pub const DIR: &str = "backups"; +/// The settings and lists a backup holds beside the world. +const FILES: [&str; 6] = [ + "server.toml", + "server.properties", + "ops.json", + "whitelist.json", + "banned-players.json", + "banned-ips.json", +]; +/// Automatic backups are named `