Merge pull request #1017 from ludovic111/feat/launcher-servers-backend

🤖 Agent: merged after verify passed (launcher-only; no parity harness depends on it). The launcher's server backend and `horizon-launcher servers …` CLI.
This commit is contained in:
Ludovic Marie
2026-10-19 16:22:02 +02:00
committed by GitHub
6 changed files with 1352 additions and 1 deletions
+6
View File
@@ -105,6 +105,12 @@ The same binary is a command-line tool; without a subcommand it opens the window
horizon-launcher modpack port <pack> [--from <dir>] [--version <tag>] get the jars and carry them over
horizon-launcher modpack play <pack> [--version <tag>] play it as its own profile
horizon-launcher modpack remove <pack>
horizon-launcher servers [list] the servers hosted from the launcher, and whether they run
horizon-launcher servers create [--name N] [--seed S | --copy-world DIR] [--mode M] [--difficulty D]
[--max-players N] [--open] [--check-accounts] [--player NAME]... --accept-eula
horizon-launcher servers set-up [--version <tag>] copy a prepared version's programs and game files for servers
horizon-launcher servers run <server> -- <horizon-server command> for example: servers run "My server" -- start
horizon-launcher servers forget <server> take it off the list (its folder stays)
horizon-launcher benchmark [--repeats N] measure the launcher (see Performance log)
horizon-launcher data [status] what data collection is allowed, the install ID, what waits
horizon-launcher data allow|deny <kinds|all> performance, crashes, bugs, benchmarks
+2 -1
View File
@@ -1313,7 +1313,8 @@ pub fn server_dir(requested: Option<PathBuf>) -> PathBuf {
.unwrap_or_else(|| data_dir().join("server"))
}
fn need_prepared(tag: &str) -> Result<(), String> {
/// Whether `tag` is prepared, with the run-time reports it needs completed when another copy has them.
pub fn need_prepared(tag: &str) -> Result<(), String> {
if is_prepared(tag) {
complete_reports(&versions_dir(), tag);
Ok(())
+164
View File
@@ -21,6 +21,58 @@ struct Cli {
command: Option<Cmd>,
}
#[derive(Subcommand)]
enum ServersCmd {
/// The servers, their folder and whether they run
List,
/// Create a server; with no options it is a new world, survival, normal, 10 players, friends only
Create {
#[arg(long, default_value = "My server")]
name: String,
/// The new world's seed (default: random)
#[arg(long, conflicts_with = "copy_world")]
seed: Option<String>,
/// Start from a copy of this world folder (one of your saves)
#[arg(long)]
copy_world: Option<PathBuf>,
#[arg(long, default_value = "survival", value_parser = ["survival", "creative", "adventure", "spectator"])]
mode: String,
#[arg(long, default_value = "normal", value_parser = ["peaceful", "easy", "normal", "hard"])]
difficulty: String,
#[arg(long, default_value_t = 10)]
max_players: u32,
/// Let anyone in (no whitelist)
#[arg(long)]
open: bool,
/// Check players' Minecraft accounts (they must sign in with Microsoft)
#[arg(long)]
check_accounts: bool,
/// Who may join (repeatable); the first becomes an operator
#[arg(long = "player")]
players: Vec<String>,
/// The installed version whose programs and game files servers use, if none are set up yet
#[arg(long)]
version: Option<String>,
/// Agree to the Minecraft EULA (https://aka.ms/MinecraftEULA)
#[arg(long)]
accept_eula: bool,
},
/// Copy an installed, prepared version's programs and game files for the servers (again)
SetUp {
#[arg(long)]
version: Option<String>,
},
/// Run a horizon-server command on a server, for example: servers run "My server" -- status
Run {
/// The server's name or folder
server: String,
#[arg(last = true, required = true)]
args: Vec<String>,
},
/// Take a server off the list (its folder stays)
Forget { server: String },
}
#[derive(Subcommand)]
enum AccountCmd {
/// The signed-in accounts and which one plays
@@ -205,6 +257,12 @@ enum Cmd {
#[command(subcommand)]
action: Option<ModpackCmd>,
},
/// Servers hosted with horizon-server (the Servers page): list them, create one, or run any
/// horizon-server command on one
Servers {
#[command(subcommand)]
action: Option<ServersCmd>,
},
/// Serve the Model Context Protocol on stdin and stdout, for AI agents
Mcp,
/// Measure the launcher: starts (the first is the cold one), page switching, idle CPU and
@@ -465,6 +523,7 @@ fn execute(command: Cmd, as_json: bool) -> Result<(), String> {
}
}
Cmd::Modpack { action } => modpack(action.unwrap_or(ModpackCmd::List), as_json)?,
Cmd::Servers { action } => servers(action.unwrap_or(ServersCmd::List), as_json)?,
Cmd::Mcp => unreachable!(),
}
Ok(())
@@ -657,6 +716,111 @@ fn data(action: DataCmd, as_json: bool) -> Result<(), String> {
Ok(())
}
fn servers(action: ServersCmd, as_json: bool) -> Result<(), String> {
use horizon_launcher::servers::{self, NewServer, World};
let find = |which: &str| {
servers::list()
.into_iter()
.find(|e| e.name == which || e.dir == std::path::Path::new(which))
.ok_or_else(|| {
format!("No server called {which}: `horizon-launcher servers` lists them")
})
};
match action {
ServersCmd::List => {
let list = servers::list();
if as_json {
json(serde_json::Value::Array(
list.iter()
.map(|e| {
serde_json::json!({
"name": e.name, "dir": e.dir,
"phase": servers::status(&e.dir).phase,
})
})
.collect(),
));
} else if list.is_empty() {
println!("No server yet: horizon-launcher servers create --accept-eula");
}
for e in list.iter().filter(|_| !as_json) {
println!(
"{} ({}): {}",
e.name,
e.dir.display(),
servers::status(&e.dir).phase
);
}
}
ServersCmd::Create {
name,
seed,
copy_world,
mode,
difficulty,
max_players,
open,
check_accounts,
players,
version,
accept_eula,
} => {
let spec = NewServer {
name,
world: match copy_world {
Some(dir) => World::Copy(dir),
None => World::New {
seed: seed.unwrap_or_default(),
},
},
game_mode: mode,
difficulty,
max_players,
friends_only: !open,
check_accounts,
players,
eula: accept_eula,
};
let tag = backend::resolve_version(version.as_deref()).ok();
let entry = servers::create(&spec, tag.as_deref(), &|l| eprintln!("{l}"))
.map_err(|p| p.message)?;
if as_json {
json(serde_json::json!({ "name": entry.name, "dir": entry.dir }));
} else {
println!("Created {} in {}", entry.name, entry.dir.display());
}
}
ServersCmd::SetUp { version } => {
let tag = backend::resolve_version(version.as_deref())?;
servers::set_up(&tag)?;
println!(
"Servers run Horizon {}",
servers::runtime_release().unwrap_or(tag)
);
}
ServersCmd::Run { server, args } => {
let entry = find(&server)?;
let args: Vec<&str> = args.iter().map(String::as_str).collect();
let status = servers::command(&entry.dir, &args)?
.status()
.map_err(|e| e.to_string())?;
if !status.success() {
return Err(format!("horizon-server exited with {status}"));
}
}
ServersCmd::Forget { server } => {
let entry = find(&server)?;
servers::forget(&entry.dir)?;
println!(
"{} is off the list; its folder stays in {}",
entry.name,
entry.dir.display()
);
}
}
Ok(())
}
fn modpack(action: ModpackCmd, as_json: bool) -> Result<(), String> {
use horizon_launcher::modpacks::{self, Imported};
use horizon_port_pack::report::Status;
+1
View File
@@ -4,5 +4,6 @@ pub mod backend;
pub mod desktop;
pub mod modpacks;
pub mod mojang;
pub mod servers;
pub mod sources;
pub mod telemetry;
+954
View File
@@ -0,0 +1,954 @@
//! Servers hosted from the launcher. Every server is a `horizon-server` folder and the launcher drives
//! `horizon-server` (docs/server.md in the repository) rather than its own copy of the logic: `init`,
//! `start`, `stop`, `config set`, `backup`, `restore`, `update`, `service`, and the control API the running
//! supervisor serves on 127.0.0.1 for the live status, the log and console commands.
//!
//! The programs come from an installed Horizon version, laid out as `install.sh` lays them out
//! (`server-runtime/releases/<tag>`, `current`, `game-files/`), so `horizon-server update` and its
//! rollback work on them unchanged. The game files are the player's own, copied from the version the
//! launcher prepared: nothing of Mojang's is downloaded or shipped.
use crate::backend;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::time::Duration;
const PROGRAM: &str = if cfg!(windows) {
"horizon-server.exe"
} else {
"horizon-server"
};
/// The Minecraft EULA a server owner agrees to.
pub const EULA_URL: &str = "https://aka.ms/MinecraftEULA";
// ----- The list of servers -----------------------------------------------------------------------
/// One server the launcher knows: its name and its folder.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Entry {
pub name: String,
pub dir: PathBuf,
}
fn list_file() -> PathBuf {
backend::data_dir().join("servers.json")
}
/// The servers, in the order they were added; folders that are gone are left out.
pub fn list() -> Vec<Entry> {
let entries: Vec<Entry> = fs::read_to_string(list_file())
.ok()
.and_then(|t| serde_json::from_str(&t).ok())
.unwrap_or_default();
entries.into_iter().filter(|e| e.dir.is_dir()).collect()
}
fn save_list(entries: &[Entry]) -> Result<(), String> {
fs::create_dir_all(backend::data_dir()).map_err(|e| e.to_string())?;
let text = serde_json::to_string_pretty(entries).map_err(|e| e.to_string())?;
fs::write(list_file(), text).map_err(|e| e.to_string())
}
/// Adds a server folder to the list (once).
pub fn add(entry: Entry) -> Result<(), String> {
let mut all = list();
all.retain(|e| e.dir != entry.dir);
all.push(entry);
save_list(&all)
}
/// Takes a server off the list; its folder stays.
pub fn forget(dir: &Path) -> Result<(), String> {
let mut all = list();
all.retain(|e| e.dir != dir);
save_list(&all)
}
/// A folder name for a server name: lower case letters, digits and dashes.
pub fn slug(name: &str) -> String {
let mut out = String::new();
for c in name.trim().chars() {
if c.is_ascii_alphanumeric() {
out.push(c.to_ascii_lowercase());
} else if !out.ends_with('-') {
out.push('-');
}
}
let out = out.trim_matches('-').to_string();
if out.is_empty() {
"server".into()
} else {
out
}
}
/// A new folder for a server called `name`, under `servers/` in the data folder.
pub fn new_folder(name: &str) -> PathBuf {
let root = backend::data_dir().join("servers");
let base = slug(name);
let mut dir = root.join(&base);
let mut n = 2;
while dir.exists() {
dir = root.join(format!("{base}-{n}"));
n += 1;
}
dir
}
// ----- The programs: an installed Horizon version, laid out as install.sh does ---------------------
pub fn runtime_root() -> PathBuf {
backend::data_dir().join("server-runtime")
}
/// `horizon-server` of the release in use, once one is set up.
pub fn program() -> Option<PathBuf> {
Some(runtime_root().join("current").join(PROGRAM)).filter(|p| p.is_file())
}
/// The release the servers run.
pub fn runtime_release() -> Option<String> {
let target = fs::canonicalize(runtime_root().join("current")).ok()?;
Some(target.file_name()?.to_string_lossy().into_owned())
}
/// Where `horizon-server` finds the game files: the folder install.sh keeps across updates.
pub fn game_files(root: &Path, game_version: &str) -> PathBuf {
root.join("game-files/horizon-client").join(game_version)
}
/// Whether the servers' game files are there.
pub fn game_files_ready() -> bool {
game_files(&runtime_root(), "26.3")
.join("horizon-resources.json")
.is_file()
}
/// Whether an installed version can host: it ships `horizon-server` and is prepared.
pub fn can_host(tag: &str) -> Result<(), String> {
if !backend::version_dir(tag).join(PROGRAM).is_file() {
return Err(format!(
"Horizon {tag} has no server program. Install a newer version on the Versions page."
));
}
if !backend::is_prepared(tag) {
return Err(format!(
"Horizon {tag} isn't prepared yet: press Prepare on the Play page (it uses your own Minecraft {})",
backend::game_version(tag)
));
}
Ok(())
}
/// Sets up the servers' programs from the installed version `tag` (if none are yet) and copies its
/// prepared game files for them. Safe to run again: it only refreshes the game files.
pub fn set_up(tag: &str) -> Result<(), String> {
can_host(tag)?;
// The data generator's reports the game reads at run time, as Play checks them.
backend::need_prepared(tag)?;
set_up_in(
&runtime_root(),
&backend::version_dir(tag),
tag,
&backend::game_version(tag),
)
}
/// [`set_up`] with its folders given (for tests).
pub fn set_up_in(root: &Path, version: &Path, tag: &str, game_version: &str) -> Result<(), String> {
let releases = root.join("releases");
fs::create_dir_all(&releases).map_err(|e| format!("{}: {e}", releases.display()))?;
let release = releases.join(tag);
if !root.join("current").exists() {
if !release.join(PROGRAM).is_file() {
let scratch = releases.join(format!(".{tag}.copying"));
let _ = fs::remove_dir_all(&scratch);
copy_release(version, &scratch).map_err(|e| format!("copying {tag}: {e}"))?;
let _ = fs::remove_dir_all(&release);
fs::rename(&scratch, &release).map_err(|e| e.to_string())?;
}
let local = release.join(".local");
fs::create_dir_all(&local).map_err(|e| e.to_string())?;
let link = local.join("horizon-client");
if link.symlink_metadata().is_err() {
fs::create_dir_all(root.join("game-files/horizon-client"))
.map_err(|e| e.to_string())?;
point(&link, &root.join("game-files/horizon-client")).map_err(|e| e.to_string())?;
}
point(&root.join("current"), &release).map_err(|e| e.to_string())?;
}
// The game files: a copy (preparing again rewrites files in place), swapped in whole.
let from = version.join(".local/horizon-client").join(game_version);
let to = game_files(root, game_version);
let fresh = to.with_extension("copying");
let _ = fs::remove_dir_all(&fresh);
copy_dir(&from, &fresh).map_err(|e| format!("copying the game files: {e}"))?;
let old = to.with_extension("old");
let _ = fs::remove_dir_all(&old);
if to.exists() {
fs::rename(&to, &old).map_err(|e| e.to_string())?;
}
fs::rename(&fresh, &to).map_err(|e| e.to_string())?;
let _ = fs::remove_dir_all(&old);
Ok(())
}
/// A release folder without the launcher's own files (its prepared copy and install marker).
fn copy_release(from: &Path, to: &Path) -> std::io::Result<()> {
fs::create_dir_all(to)?;
for entry in fs::read_dir(from)? {
let entry = entry?;
let name = entry.file_name();
if name == ".local" || name == ".installed" {
continue;
}
let target = to.join(&name);
if entry.file_type()?.is_dir() {
copy_dir(&entry.path(), &target)?;
} else {
fs::copy(entry.path(), &target)?;
}
}
Ok(())
}
fn copy_dir(from: &Path, to: &Path) -> std::io::Result<()> {
fs::create_dir_all(to)?;
for entry in fs::read_dir(from)? {
let entry = entry?;
let target = to.join(entry.file_name());
let kind = entry.file_type()?;
if kind.is_dir() {
copy_dir(&entry.path(), &target)?;
} else if kind.is_file() {
fs::copy(entry.path(), target)?;
}
}
Ok(())
}
/// Makes `link` point at the folder `target`, as install.sh (a symbolic link) and install.ps1 (a
/// junction, which needs no admin rights) do.
fn point(link: &Path, target: &Path) -> std::io::Result<()> {
let fresh = link.with_extension("switching");
let _ = fs::remove_file(&fresh);
#[cfg(unix)]
{
std::os::unix::fs::symlink(target, &fresh)?;
fs::rename(&fresh, link)
}
#[cfg(windows)]
{
let _ = fs::remove_dir(link);
let status = Command::new("cmd")
.args(["/C", "mklink", "/J"])
.arg(link)
.arg(target)
.stdout(Stdio::null())
.status()?;
if status.success() {
Ok(())
} else {
Err(std::io::Error::other("mklink /J failed"))
}
}
}
// ----- Running horizon-server -------------------------------------------------------------------
/// `horizon-server --dir <dir> <args>`.
pub fn command(dir: &Path, args: &[&str]) -> Result<Command, String> {
let program = program().ok_or(
"The server programs aren't set up yet: create a server, or press Prepare on its page.",
)?;
let mut cmd = Command::new(program);
cmd.arg("--dir").arg(dir).args(args);
#[cfg(windows)]
std::os::windows::process::CommandExt::creation_flags(&mut cmd, 0x0800_0000); // CREATE_NO_WINDOW
Ok(cmd)
}
fn supervisor_log(dir: &Path) -> PathBuf {
dir.join("logs/supervisor.log")
}
fn log_length(dir: &Path) -> u64 {
fs::metadata(supervisor_log(dir)).map_or(0, |m| m.len())
}
/// Runs one `horizon-server` command to its end: what it printed, or the problem in plain words.
pub fn run(dir: &Path, args: &[&str]) -> Result<String, Problem> {
let since = log_length(dir);
let output = command(dir, args)
.map_err(Problem::plain)?
.stdin(Stdio::null())
.output()
.map_err(|e| Problem::plain(format!("Couldn't run horizon-server: {e}")))?;
let out = String::from_utf8_lossy(&output.stdout).into_owned();
if output.status.success() {
Ok(out)
} else {
let err = String::from_utf8_lossy(&output.stderr);
Err(Problem::explain(&format!("{out}\n{err}"), dir, since))
}
}
/// Runs a long `horizon-server` command, passing each line it prints to `line`.
pub fn run_streamed(dir: &Path, args: &[&str], line: &dyn Fn(String)) -> Result<(), Problem> {
use std::io::{BufRead, BufReader, Read};
let since = log_length(dir);
let mut child = command(dir, args)
.map_err(Problem::plain)?
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.map_err(|e| Problem::plain(format!("Couldn't run horizon-server: {e}")))?;
let mut err = child.stderr.take();
let errors = std::thread::spawn(move || {
let mut text = String::new();
if let Some(e) = &mut err {
let _ = e.read_to_string(&mut text);
}
text
});
let mut out = String::new();
if let Some(stdout) = child.stdout.take() {
// `start` prints a dot every half second on one line: those aren't lines worth showing.
for l in BufReader::new(stdout).lines().map_while(Result::ok) {
let shown = l
.trim_start_matches(['.', ' '])
.trim_start_matches("Starting");
let shown = shown.trim_start_matches('.').trim();
if !shown.is_empty() {
line(shown.to_string());
}
out.push_str(&l);
out.push('\n');
}
}
let status = child.wait().map_err(|e| Problem::plain(e.to_string()))?;
let err = errors.join().unwrap_or_default();
if status.success() {
Ok(())
} else {
Err(Problem::explain(&format!("{out}\n{err}"), dir, since))
}
}
/// What can be done about a problem, with one button.
#[derive(Clone, Debug, PartialEq)]
pub enum Fix {
/// The port is taken: use this free one.
UsePort(u16),
/// The game files are missing: prepare them (Play page) and copy them for the servers.
Prepare,
/// The EULA isn't agreed to.
AgreeEula,
/// The game's own log says more.
ShowLog,
}
/// A problem, in plain words.
#[derive(Clone, Debug, PartialEq)]
pub struct Problem {
pub message: String,
pub fix: Option<Fix>,
}
impl Problem {
pub fn plain(message: impl Into<String>) -> Self {
Self {
message: message.into(),
fix: None,
}
}
/// Reads what `horizon-server` (and the game log it points at) said.
/// `since` is the background server's log length before the command: only what it logged after counts.
pub fn explain(output: &str, dir: &Path, since: u64) -> Self {
let log = fs::read(supervisor_log(dir)).unwrap_or_default();
let new = log.get(since as usize..).unwrap_or_default();
Self::explain_with(output, &String::from_utf8_lossy(new), dir)
}
fn explain_with(output: &str, log: &str, dir: &Path) -> Self {
let both = format!("{output}\n{log}");
let has = |s: &str| both.contains(s);
// Vanilla says FAILED TO BIND TO PORT for any failure while starting; only these are a busy port.
let busy = [
"already in use",
"Address in use",
"Only one usage of each socket address",
];
if busy.iter().any(|b| both.contains(b)) {
let port = config(dir)
.and_then(|c| c["server"]["port"].as_u64())
.unwrap_or(25565) as u16;
return Self {
message: format!(
"Port {port} is already used by another program (maybe another server). Use a free \
port instead: friends then add it to the address."
),
fix: Some(Fix::UsePort(free_port(port + 1, &[]))),
};
}
if has("no prepared game files") || has("not found next to the resources") {
return Self {
message:
"The server needs the game files from your own Minecraft. Prepare them: it \
takes a few minutes, once."
.into(),
fix: Some(Fix::Prepare),
};
}
if has("EULA") && !has("eula=true") {
return Self {
message: "Running a server needs your agreement to the Minecraft EULA.".into(),
fix: Some(Fix::AgreeEula),
};
}
if has("isn't up after") || has("the server stopped") {
return Self {
message: "The game didn't start. Its log below says why.".into(),
fix: Some(Fix::ShowLog),
};
}
let said = both
.lines()
.rev()
.find_map(|l| l.strip_prefix("horizon-server: "))
.map(str::to_string)
.or_else(|| {
output
.lines()
.rev()
.find(|l| !l.trim().is_empty())
.map(str::to_string)
})
.unwrap_or_else(|| "horizon-server failed without saying why".into());
Self::plain(capitalised(&said))
}
}
fn capitalised(s: &str) -> String {
let mut c = s.chars();
match c.next() {
Some(f) => f.to_uppercase().chain(c).collect(),
None => String::new(),
}
}
// ----- Creating a server ------------------------------------------------------------------------
/// The world a new server starts with.
#[derive(Clone, Debug, PartialEq)]
pub enum World {
/// A new world; an empty seed is a random one.
New { seed: String },
/// A copy of one of the player's worlds (its folder).
Copy(PathBuf),
}
/// What the Create dialog asks; [`NewServer::default`] alone makes a working server.
#[derive(Clone, Debug, PartialEq)]
pub struct NewServer {
pub name: String,
pub world: World,
pub game_mode: String,
pub difficulty: String,
pub max_players: u32,
/// Only listed players (and operators) may join.
pub friends_only: bool,
/// Check players' Minecraft accounts: they must sign in with Microsoft.
pub check_accounts: bool,
/// Who may join first (whitelisted), and the first of them becomes an operator.
pub players: Vec<String>,
pub eula: bool,
}
impl Default for NewServer {
fn default() -> Self {
Self {
name: "My server".into(),
world: World::New {
seed: String::new(),
},
game_mode: "survival".into(),
difficulty: "normal".into(),
max_players: 10,
friends_only: true,
check_accounts: false,
players: Vec::new(),
eula: false,
}
}
}
/// A port nobody listens on, from `from` up, and not one of `taken`.
pub fn free_port(from: u16, taken: &[u16]) -> u16 {
(from..u16::MAX)
.find(|p| !taken.contains(p) && std::net::TcpListener::bind(("0.0.0.0", *p)).is_ok())
.unwrap_or(from)
}
/// The ports the launcher's other servers use: their game port and their web panel.
fn ports_taken() -> Vec<u16> {
let mut out = Vec::new();
for e in list() {
if let Some(c) = config(&e.dir) {
out.extend(c["server"]["port"].as_u64().map(|p| p as u16));
out.extend(
c["panel"]["bind"]
.as_str()
.and_then(|b| b.rsplit_once(':'))
.and_then(|(_, p)| p.parse::<u16>().ok()),
);
}
}
out
}
/// The player's worlds (the game home's `saves/`), newest first: their name and folder.
pub fn player_worlds() -> Vec<(String, PathBuf)> {
let saves = backend::game_home().join("saves");
let mut worlds: Vec<(std::time::SystemTime, String, PathBuf)> = fs::read_dir(saves)
.into_iter()
.flatten()
.flatten()
.filter(|e| e.path().join("level.dat").is_file())
.map(|e| {
let when = e
.path()
.join("level.dat")
.metadata()
.and_then(|m| m.modified())
.unwrap_or(std::time::UNIX_EPOCH);
(when, e.file_name().to_string_lossy().into_owned(), e.path())
})
.collect();
worlds.sort_by_key(|w| std::cmp::Reverse(w.0));
worlds.into_iter().map(|(_, n, p)| (n, p)).collect()
}
/// The `horizon-server init` arguments for a new server.
pub fn init_args(spec: &NewServer, world: &str, port: u16) -> Vec<String> {
let mut args: Vec<String> = vec!["init".into(), "--yes".into()];
if spec.eula {
args.push("--eula".into());
}
args.extend(["--world".into(), world.to_string()]);
if let World::New { seed } = &spec.world {
if !seed.trim().is_empty() {
args.extend(["--seed".into(), seed.trim().to_string()]);
}
}
args.extend(["--port".into(), port.to_string()]);
args.push(if spec.friends_only {
"--whitelist".into()
} else {
"--no-whitelist".into()
});
if !spec.check_accounts {
args.push("--offline".into());
}
for p in &spec.players {
args.extend(["--player".into(), p.clone()]);
}
if let Some(first) = spec.players.first() {
args.extend(["--op".into(), first.clone()]);
}
args
}
/// The `horizon-server config set` arguments that finish a new server.
pub fn settings_args(spec: &NewServer, panel_port: u16) -> Vec<String> {
vec![
"config".into(),
"set".into(),
format!("world.game_mode={}", spec.game_mode),
format!("world.difficulty={}", spec.difficulty),
format!("server.max_players={}", spec.max_players.clamp(1, 1000)),
format!("server.motd={}", spec.name.trim()),
format!("panel.bind=127.0.0.1:{panel_port}"),
]
}
/// Creates a server: its folder, the world (a copy when asked), `init` and the settings, then adds it to
/// the list. `tag` is the installed version whose programs and game files it uses when none are set up.
pub fn create(spec: &NewServer, tag: Option<&str>, log: &dyn Fn(String)) -> Result<Entry, Problem> {
if !spec.eula {
return Err(Problem {
message: "Running a server needs your agreement to the Minecraft EULA.".into(),
fix: Some(Fix::AgreeEula),
});
}
if program().is_none() || !game_files_ready() {
let tag = tag.ok_or_else(|| {
Problem::plain("Install a Horizon version first (Versions page), then prepare it.")
})?;
log(format!("Setting up the server programs from Horizon {tag}"));
set_up(tag).map_err(|e| {
if e.contains("prepared") {
Problem {
message: e,
fix: Some(Fix::Prepare),
}
} else {
Problem::plain(e)
}
})?;
}
let name = if spec.name.trim().is_empty() {
"My server".to_string()
} else {
spec.name.trim().to_string()
};
let dir = new_folder(&name);
fs::create_dir_all(&dir).map_err(|e| Problem::plain(e.to_string()))?;
let world = match &spec.world {
World::New { .. } => "world".to_string(),
World::Copy(from) => {
let world = from
.file_name()
.map(|n| slug(&n.to_string_lossy()))
.unwrap_or_else(|| "world".into());
log(format!("Copying the world {}", from.display()));
copy_world(from, &dir.join(&world)).map_err(|e| {
let _ = fs::remove_dir_all(&dir);
Problem::plain(format!("Couldn't copy the world: {e}"))
})?;
world
}
};
let taken = ports_taken();
let port = free_port(25565, &taken);
let panel = free_port(8130, &taken);
let undo = |p: Problem| {
let _ = fs::remove_dir_all(&dir);
p
};
let init = init_args(spec, &world, port);
log("Writing the server's settings".into());
run(&dir, &init.iter().map(String::as_str).collect::<Vec<_>>()).map_err(undo)?;
let settings = settings_args(spec, panel);
run(
&dir,
&settings.iter().map(String::as_str).collect::<Vec<_>>(),
)
.map_err(undo)?;
let entry = Entry { name, dir };
add(entry.clone()).map_err(Problem::plain)?;
Ok(entry)
}
/// A copy of a world folder, without the lock a running game holds.
fn copy_world(from: &Path, to: &Path) -> std::io::Result<()> {
fs::create_dir_all(to)?;
for entry in fs::read_dir(from)? {
let entry = entry?;
let name = entry.file_name();
if name == "session.lock" {
continue;
}
let target = to.join(&name);
if entry.file_type()?.is_dir() {
copy_world(&entry.path(), &target)?;
} else {
fs::copy(entry.path(), target)?;
}
}
Ok(())
}
// ----- A server's files --------------------------------------------------------------------------
/// The settings, as `horizon-server config --json` prints them (`None` before `init`, or from a
/// `horizon-server` older than the command).
pub fn config(dir: &Path) -> Option<Value> {
if !dir.join("server.toml").is_file() {
return None;
}
let out = run(dir, &["config", "--json"]).ok()?;
serde_json::from_str(out.trim()).ok()
}
/// What `config set` did: the settings applied now, and those waiting for a restart.
#[derive(Clone, Debug, Default, PartialEq, Deserialize)]
pub struct Applied {
#[serde(default)]
pub applied: Vec<String>,
#[serde(default)]
pub restart: Vec<String>,
#[serde(default)]
pub running: bool,
}
/// Changes settings through `horizon-server config set` (checked there; nothing is written if one is
/// wrong).
pub fn set_config(dir: &Path, changes: &[(&str, String)]) -> Result<Applied, Problem> {
let pairs: Vec<String> = changes.iter().map(|(k, v)| format!("{k}={v}")).collect();
let mut args = vec!["config", "set"];
args.extend(pairs.iter().map(String::as_str));
args.push("--json");
let out = run(dir, &args)?;
serde_json::from_str(out.trim()).map_err(|e| Problem::plain(e.to_string()))
}
/// A list file's names (`whitelist.json`, `ops.json`, `banned-players.json`), as vanilla writes them.
pub fn names(dir: &Path, file: &str) -> Vec<String> {
fs::read_to_string(dir.join(file))
.ok()
.and_then(|t| serde_json::from_str::<Vec<Value>>(&t).ok())
.unwrap_or_default()
.iter()
.filter_map(|e| e["name"].as_str().map(str::to_string))
.collect()
}
/// A backup, as `horizon-server backups --json` lists it.
#[derive(Clone, Debug, PartialEq, Deserialize)]
pub struct Backup {
pub name: String,
pub bytes: u64,
#[serde(default)]
pub modified: Option<u64>,
}
pub fn backups(dir: &Path) -> Result<Vec<Backup>, Problem> {
let out = run(dir, &["backups", "--json"])?;
serde_json::from_str(out.trim()).map_err(|e| Problem::plain(e.to_string()))
}
/// The address friends on the same network type: this computer's LAN address. No packet is sent: the
/// system only picks the interface it would use.
pub fn lan_address() -> Option<std::net::IpAddr> {
let socket = std::net::UdpSocket::bind("0.0.0.0:0").ok()?;
socket.connect("192.0.2.1:9").ok()?;
let ip = socket.local_addr().ok()?.ip();
(!ip.is_unspecified() && !ip.is_loopback()).then_some(ip)
}
/// Whether the server starts at login: the service definition `horizon-server service print` names exists.
pub fn service_installed(dir: &Path) -> bool {
if cfg!(windows) {
let name = format!("horizon-server-{}", service_suffix(dir));
return Command::new("schtasks")
.args(["/Query", "/TN", &name])
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.is_ok_and(|s| s.success());
}
run(dir, &["service", "print"])
.ok()
.and_then(|out| {
out.lines()
.next()
.and_then(|l| l.strip_prefix("# "))
.map(PathBuf::from)
})
.is_some_and(|p| p.is_file())
}
/// horizon-server's service name for a folder, after `horizon-server-`.
fn service_suffix(dir: &Path) -> String {
let folder = dir
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_default();
let clean: String = folder
.chars()
.map(|c| {
if c.is_ascii_alphanumeric() {
c.to_ascii_lowercase()
} else {
'-'
}
})
.collect();
let clean = clean.trim_matches('-');
if clean.is_empty() {
"server".into()
} else {
clean.into()
}
}
// ----- The running server: its control API -------------------------------------------------------
/// The running supervisor's control API (`.horizon/api.json`: its address and token).
#[derive(Clone, Debug)]
pub struct Api {
address: String,
token: String,
}
impl Api {
/// The API of the server running in `dir`, if one runs.
pub fn open(dir: &Path) -> Option<Self> {
let text = fs::read_to_string(dir.join(".horizon/api.json")).ok()?;
let v: Value = serde_json::from_str(&text).ok()?;
Some(Self {
address: v["address"].as_str()?.to_string(),
token: v["token"].as_str()?.to_string(),
})
}
pub fn call(&self, method: &str, path: &str, body: Option<Value>) -> Result<Value, String> {
let agent = ureq::AgentBuilder::new()
.timeout(Duration::from_secs(10))
.build();
let request = agent
.request(method, &format!("http://{}{path}", self.address))
.set("Authorization", &format!("Bearer {}", self.token));
let response = match body {
Some(b) => request.send_json(b),
None => request.call(),
};
match response {
Ok(r) => r.into_json().map_err(|e| e.to_string()),
Err(ureq::Error::Status(_, r)) => {
let v: Value = r.into_json().unwrap_or(Value::Null);
Err(v["error"]
.as_str()
.unwrap_or("the server refused")
.to_string())
}
Err(e) => Err(e.to_string()),
}
}
pub fn status(&self) -> Option<Status> {
self.call("GET", "/api/v1/status", None)
.ok()
.map(|v| Status::parse(&v))
}
/// The log lines after `since`, and the number to ask from next time.
pub fn logs(&self, since: u64) -> Option<(u64, Vec<String>)> {
let v = self
.call("GET", &format!("/api/v1/logs?since={since}"), None)
.ok()?;
let lines = v["lines"]
.as_array()?
.iter()
.filter_map(|l| l.as_str().map(str::to_string))
.collect();
Some((v["next"].as_u64().unwrap_or(since), lines))
}
/// One console command; what the game answered.
pub fn command(&self, line: &str) -> Result<Vec<String>, String> {
let v = self.call("POST", "/api/v1/command", Some(json!({ "line": line })))?;
Ok(v["output"]
.as_array()
.into_iter()
.flatten()
.filter_map(|l| l.as_str().map(str::to_string))
.collect())
}
/// A one-time login link for the web panel.
pub fn panel_link(&self) -> Result<String, String> {
let v = self.call("POST", "/api/v1/panel/login", None)?;
v["url"]
.as_str()
.map(str::to_string)
.ok_or_else(|| "the panel gave no link".into())
}
}
/// A player online.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Player {
pub name: String,
pub uuid: String,
pub ping: Option<u64>,
}
/// What the supervisor reports.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Status {
/// starting, running, stopping or restarting (stopped when nothing answers).
pub phase: String,
pub uptime: Option<u64>,
pub crashes: u64,
pub last_crash: Option<String>,
pub tps: Option<f64>,
pub mspt: Option<f64>,
pub players: Vec<Player>,
pub max_players: Option<u64>,
pub ram_bytes: Option<u64>,
pub cpu_percent: Option<f64>,
/// Paused because nobody is online (vanilla's pause-when-empty).
pub paused: bool,
/// The release in use and a newer one, when the supervisor found one.
pub release: Option<String>,
pub update_available: Option<String>,
/// An update under way: what it is doing now.
pub update_busy: Option<String>,
/// How the last update went (its message, and whether it worked).
pub update_last: Option<(String, bool)>,
}
impl Status {
pub fn stopped() -> Self {
Self {
phase: "stopped".into(),
..Self::default()
}
}
pub fn parse(v: &Value) -> Self {
let game = &v["sample"]["game"];
let update = &v["update"];
Self {
phase: v["phase"].as_str().unwrap_or("stopped").to_string(),
uptime: v["engine_uptime_seconds"].as_u64(),
crashes: v["crashes"].as_u64().unwrap_or(0),
last_crash: v["last_crash"].as_str().map(str::to_string),
tps: game["tps"].as_f64(),
mspt: game["mspt"]["average"].as_f64(),
players: game["players"]
.as_array()
.into_iter()
.flatten()
.map(|p| Player {
name: p["name"].as_str().unwrap_or_default().to_string(),
uuid: p["uuid"].as_str().unwrap_or_default().to_string(),
ping: p["ping"].as_u64(),
})
.collect(),
max_players: game["max_players"].as_u64(),
ram_bytes: v["sample"]["rss_bytes"].as_u64(),
cpu_percent: v["sample"]["cpu_percent"].as_f64(),
paused: game["paused"] == true,
release: update["installed"].as_str().map(str::to_string),
update_available: update["available"].as_str().map(str::to_string),
update_busy: update["busy"].as_str().map(str::to_string),
update_last: update["last"]
.as_str()
.map(|m| (m.to_string(), update["ok"] != false)),
}
}
pub fn running(&self) -> bool {
self.phase != "stopped"
}
}
/// The status of the server in `dir`: what its API says, or stopped.
pub fn status(dir: &Path) -> Status {
Api::open(dir)
.and_then(|api| api.status())
.unwrap_or_else(Status::stopped)
}
#[cfg(test)]
#[path = "servers/tests.rs"]
mod tests;
+225
View File
@@ -0,0 +1,225 @@
use super::*;
use std::io::{BufRead, BufReader, Write};
fn scratch(name: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!("horizon-servers-{name}-{}", std::process::id()));
let _ = fs::remove_dir_all(&dir);
fs::create_dir_all(&dir).unwrap();
dir
}
#[test]
fn folder_names_are_plain() {
assert_eq!(slug("Ludo's Survival!"), "ludo-s-survival");
assert_eq!(slug(" "), "server");
assert_eq!(slug("Été 2026"), "t-2026");
assert_eq!(service_suffix(Path::new("/x/My World")), "my-world");
}
#[test]
fn create_asks_init_and_config_set() {
let spec = NewServer {
name: "Friends".into(),
world: World::New {
seed: " 12345 ".into(),
},
players: vec!["Alex".into(), "Steve".into()],
eula: true,
..NewServer::default()
};
assert_eq!(
init_args(&spec, "world", 25566).join(" "),
"init --yes --eula --world world --seed 12345 --port 25566 --whitelist --offline \
--player Alex --player Steve --op Alex"
);
let open = NewServer {
friends_only: false,
check_accounts: true,
world: World::Copy("/saves/Old".into()),
..NewServer::default()
};
assert_eq!(
init_args(&open, "old", 25565).join(" "),
"init --yes --world old --port 25565 --no-whitelist"
);
assert_eq!(
settings_args(&spec, 8131).join(" "),
"config set world.game_mode=survival world.difficulty=normal server.max_players=10 \
server.motd=Friends panel.bind=127.0.0.1:8131"
);
}
#[cfg(unix)]
#[test]
fn programs_are_laid_out_as_install_sh_does() {
let root = scratch("runtime");
let version = root.join("versions/v1");
fs::create_dir_all(version.join(".local/horizon-client/26.3")).unwrap();
fs::write(version.join(PROGRAM), "server").unwrap();
fs::write(version.join("horizon-client"), "game").unwrap();
fs::write(version.join(".installed"), "").unwrap();
fs::write(
version.join(".local/horizon-client/26.3/horizon-resources.json"),
"{}",
)
.unwrap();
let rt = root.join("runtime");
set_up_in(&rt, &version, "v1", "26.3").unwrap();
assert_eq!(
fs::read_to_string(rt.join("current").join(PROGRAM)).unwrap(),
"server"
);
assert!(!rt.join("releases/v1/.installed").exists());
// The release finds the game files where horizon-server looks: .local beside it.
assert!(rt
.join("current/.local/horizon-client/26.3/horizon-resources.json")
.is_file());
// Again with newer game files: only they change, swapped in whole.
fs::write(
version.join(".local/horizon-client/26.3/horizon-resources.json"),
"{\"new\":1}",
)
.unwrap();
set_up_in(&rt, &version, "v1", "26.3").unwrap();
assert_eq!(
fs::read_to_string(game_files(&rt, "26.3").join("horizon-resources.json")).unwrap(),
"{\"new\":1}"
);
assert!(!game_files(&rt, "26.3").with_extension("old").exists());
fs::remove_dir_all(&root).unwrap();
}
#[test]
fn problems_say_what_to_do() {
let dir = scratch("problems");
let port = Problem::explain_with(
"",
"[Server thread/WARN]: **** FAILED TO BIND TO PORT!\nThe exception was: Address already in use (os error 48)",
&dir,
);
assert!(port.message.contains("already used"), "{}", port.message);
assert!(matches!(port.fix, Some(Fix::UsePort(p)) if p > 25565));
let files = Problem::explain_with(
"horizon-server: no prepared game files. Horizon reads them from",
"",
&dir,
);
assert_eq!(files.fix, Some(Fix::Prepare));
let reports = Problem::explain_with(
"",
"**** FAILED TO BIND TO PORT!\nThe exception was: registries.json not found next to the resources",
&dir,
);
assert_eq!(reports.fix, Some(Fix::Prepare));
let other = Problem::explain_with("horizon-server: a server is already running in x", "", &dir);
assert_eq!(other, Problem::plain("A server is already running in x"));
fs::remove_dir_all(&dir).unwrap();
}
#[test]
fn lists_and_status_read_as_vanilla_and_the_supervisor_write_them() {
let dir = scratch("lists");
fs::write(
dir.join("whitelist.json"),
r#"[{"uuid": "a", "name": "Alex"}, {"uuid": "b", "name": "Steve"}]"#,
)
.unwrap();
assert_eq!(names(&dir, "whitelist.json"), ["Alex", "Steve"]);
assert!(names(&dir, "ops.json").is_empty());
fs::remove_dir_all(&dir).unwrap();
let s = Status::parse(&json!({
"phase": "running", "engine_uptime_seconds": 61, "crashes": 1, "last_crash": "signal: 9",
"sample": { "rss_bytes": 800_000_000u64, "cpu_percent": 3.5, "game": {
"tps": 20.0, "mspt": { "average": 4.2 }, "max_players": 10, "paused": false,
"players": [{ "name": "Alex", "uuid": "u1", "ping": 12 }] } },
"update": { "installed": "v1", "available": "v2", "busy": null, "last": "Not updated: x", "ok": false }
}));
assert_eq!(s.phase, "running");
assert_eq!(s.players[0].name, "Alex");
assert_eq!(s.players[0].ping, Some(12));
assert_eq!(
(s.tps, s.mspt, s.max_players),
(Some(20.0), Some(4.2), Some(10))
);
assert_eq!(s.update_available.as_deref(), Some("v2"));
assert_eq!(s.update_last, Some(("Not updated: x".into(), false)));
assert!(!Status::stopped().running());
}
/// The control API: the token goes with every call, and errors come back as the server words them.
#[test]
fn the_api_sends_the_token() {
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let address = listener.local_addr().unwrap().to_string();
let server = std::thread::spawn(move || {
let mut seen = Vec::new();
for reply in [
r#"{"phase":"running","sample":{"game":{"players":[]}}}"#,
r#"{"error":"the game didn't answer"}"#,
] {
let (stream, _) = listener.accept().unwrap();
let mut reader = BufReader::new(stream.try_clone().unwrap());
let mut head = String::new();
loop {
let mut line = String::new();
reader.read_line(&mut line).unwrap();
if line == "\r\n" || line.is_empty() {
break;
}
head.push_str(&line);
}
// Read the body too: closing with it unread would reset the connection.
let length = head
.lines()
.find_map(|l| {
l.to_ascii_lowercase()
.strip_prefix("content-length:")
.map(|n| n.trim().parse::<usize>().unwrap())
})
.unwrap_or(0);
let mut body = vec![0; length];
std::io::Read::read_exact(&mut reader, &mut body).unwrap();
seen.push(head);
let code = if reply.contains("error") {
"409 Conflict"
} else {
"200 OK"
};
let mut stream = stream;
write!(
stream,
"HTTP/1.1 {code}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{reply}",
reply.len()
)
.unwrap();
}
seen
});
let dir = scratch("api");
fs::create_dir_all(dir.join(".horizon")).unwrap();
fs::write(
dir.join(".horizon/api.json"),
json!({ "address": address, "token": "t0k", "pid": 1 }).to_string(),
)
.unwrap();
assert_eq!(status(&dir).phase, "running");
let api = Api::open(&dir).unwrap();
assert_eq!(
api.command("list").unwrap_err(),
"the game didn't answer".to_string()
);
let seen = server.join().unwrap();
assert!(
seen[0].starts_with("GET /api/v1/status HTTP/1.1"),
"{}",
seen[0]
);
assert!(seen
.iter()
.all(|h| h.to_ascii_lowercase().contains("authorization: bearer t0k")));
assert!(seen[1].starts_with("POST /api/v1/command"));
fs::remove_dir_all(&dir).unwrap();
// Nothing answers: stopped.
assert_eq!(status(Path::new("/nonexistent")).phase, "stopped");
}