ae22d5dd96
An online-mode server checks players by their account's UUID, so op, whitelist, ban and pardon now resolve a name as vanilla's CachedUserNameToIdResolver does: a player on the server, then one seen since the start (in memory; vanilla also keeps usercache.json), then the services' profiles/getByName endpoint through the discovery document. A name with no account is "That player does not exist", with vanilla's "Couldn't find profile with name" warning. Offline servers keep the offline profiles. horizon-server does the same while the server is stopped (op, whitelist add, and the players init writes), and removes entries by the name already on the list. `init --offline` (or HORIZON_ONLINE=false) sets online-mode off; init's note now warns about offline servers only. The online-mode harness adds a sixth login: `whitelist add` for the player who joined earlier needs no lookup and lets them in, and an unknown name is looked up and refused, on vanilla's server and on Horizon's (43 checks). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
176 lines
6.7 KiB
Rust
176 lines
6.7 KiB
Rust
//! `horizon-server`: host a Horizon server (docs/server.md). It keeps the server's settings in `server.toml`,
|
|
//! runs the game headless as the engine, and manages the vanilla files around it.
|
|
|
|
mod api;
|
|
mod backup;
|
|
mod cli;
|
|
mod config;
|
|
mod engine;
|
|
mod http;
|
|
mod init;
|
|
mod lists;
|
|
mod panel;
|
|
mod service;
|
|
mod signals;
|
|
mod supervisor;
|
|
|
|
use std::{path::PathBuf, process::ExitCode};
|
|
|
|
const HELP: &str = "\
|
|
horizon-server: host a Horizon (Minecraft Java Edition 26.3) server
|
|
|
|
Usage: horizon-server [--dir DIR] <command>
|
|
|
|
init Set up a server here: the EULA, the world, the port, who may join (asks, or takes flags:
|
|
--eula --world NAME --seed SEED --port N --whitelist/--no-whitelist --player NAME... --op NAME
|
|
--offline --yes)
|
|
run Run the server in the foreground; type console commands, Ctrl-C stops it cleanly. It
|
|
restarts the game if it crashes.
|
|
start Run the server in the background (its output goes to logs/supervisor.log)
|
|
stop, restart
|
|
status [--json]
|
|
Up or not, TPS, MSPT, players, RAM and CPU
|
|
cmd COMMAND...
|
|
Run a console command on the running server and print what it answers
|
|
logs [-f] [-n N]
|
|
The last N lines of the log (default 50); -f follows it
|
|
backup [NAME]
|
|
Zip the world and the settings into backups/ (a running server saves first and stays up)
|
|
backups List the backups
|
|
restore BACKUP [--yes]
|
|
Put a backup's world in place (the current one is kept in backups/); a running
|
|
server restarts with it
|
|
service install|uninstall|print [--system]
|
|
Start the server at boot or login: a systemd unit on Linux (--system: for every user,
|
|
with sudo), a launchd agent on macOS, a task at logon on Windows
|
|
panel A one-time login link for the web panel ([panel] in server.toml; 127.0.0.1:8130 by
|
|
default)
|
|
import FILE Write server.toml from a vanilla server.properties
|
|
op NAME, deop NAME
|
|
whitelist on|off|list, whitelist add|remove NAME
|
|
Who may join (on the running server, or in the files when it is stopped)
|
|
|
|
--dir DIR The server folder (default: the current folder)
|
|
|
|
Settings live in server.toml; the server writes server.properties from it before every start.
|
|
";
|
|
|
|
fn main() -> ExitCode {
|
|
let mut args: Vec<String> = std::env::args().skip(1).collect();
|
|
let mut dir = PathBuf::from(".");
|
|
if let Some(i) = args.iter().position(|a| a == "--dir") {
|
|
if i + 1 >= args.len() {
|
|
eprintln!("horizon-server: --dir needs a folder");
|
|
return ExitCode::from(2);
|
|
}
|
|
dir = PathBuf::from(args.remove(i + 1));
|
|
args.remove(i);
|
|
}
|
|
let result = match args.first().map(String::as_str) {
|
|
None | Some("help" | "-h" | "--help") => {
|
|
print!("{HELP}");
|
|
Ok(())
|
|
}
|
|
Some("version" | "-V" | "--version") => {
|
|
println!("horizon-server {}", env!("CARGO_PKG_VERSION"));
|
|
Ok(())
|
|
}
|
|
Some("init") => init_answers(&args[1..]).and_then(|a| init::run(&dir, a)),
|
|
Some("run") => supervisor::run(&dir),
|
|
Some("start") => cli::start(&dir),
|
|
Some("stop") => cli::stop(&dir, false),
|
|
Some("restart") => cli::stop(&dir, true),
|
|
Some("status") => cli::status(&dir, args[1..].iter().any(|a| a == "--json")),
|
|
Some("cmd") => cli::command(&dir, &args[1..]),
|
|
Some("logs") => logs(&dir, &args[1..]),
|
|
Some("panel") => cli::panel(&dir),
|
|
Some("backup") => cli::backup(&dir, args.get(1).map(String::as_str)),
|
|
Some("backups") => cli::backups(&dir),
|
|
Some("service") => service::command(&dir, &args[1..]),
|
|
Some("restore") => match args.get(1) {
|
|
Some(name) => cli::restore(&dir, name, args[2..].iter().any(|a| a == "--yes")),
|
|
None => Err("restore needs a backup (`horizon-server backups` lists them)".into()),
|
|
},
|
|
Some("import") => import(&dir, &args[1..]),
|
|
Some(command @ ("op" | "deop" | "whitelist")) => lists::run(&dir, command, &args[1..]),
|
|
Some(other) => Err(format!(
|
|
"unknown command {other}; `horizon-server help` lists them"
|
|
)),
|
|
};
|
|
match result {
|
|
Ok(()) => ExitCode::SUCCESS,
|
|
Err(e) => {
|
|
eprintln!("horizon-server: {e}");
|
|
ExitCode::FAILURE
|
|
}
|
|
}
|
|
}
|
|
|
|
fn logs(dir: &std::path::Path, args: &[String]) -> Result<(), String> {
|
|
let mut follow = false;
|
|
let mut count = 50;
|
|
let mut it = args.iter();
|
|
while let Some(arg) = it.next() {
|
|
match arg.as_str() {
|
|
"-f" | "--follow" => follow = true,
|
|
"-n" => {
|
|
count = it
|
|
.next()
|
|
.and_then(|n| n.parse().ok())
|
|
.ok_or("-n needs a number of lines")?;
|
|
}
|
|
other => return Err(format!("logs: unknown option {other}")),
|
|
}
|
|
}
|
|
cli::logs(dir, follow, count)
|
|
}
|
|
|
|
fn init_answers(args: &[String]) -> Result<init::Answers, String> {
|
|
let mut answers = init::Answers::from_env()?;
|
|
let mut it = args.iter();
|
|
while let Some(arg) = it.next() {
|
|
let mut value = |flag: &str| {
|
|
it.next()
|
|
.cloned()
|
|
.ok_or_else(|| format!("{flag} needs a value"))
|
|
};
|
|
match arg.as_str() {
|
|
"--eula" => answers.eula = true,
|
|
"--yes" | "-y" => answers.yes = true,
|
|
"--world" => answers.world = Some(value(arg)?),
|
|
"--seed" => answers.seed = Some(value(arg)?),
|
|
"--port" => {
|
|
answers.port = Some(
|
|
value(arg)?
|
|
.parse()
|
|
.map_err(|_| "--port needs a number from 1 to 65535")?,
|
|
);
|
|
}
|
|
"--whitelist" => answers.whitelist = Some(true),
|
|
"--no-whitelist" => answers.whitelist = Some(false),
|
|
"--player" => answers.players.push(value(arg)?),
|
|
"--op" => answers.op = Some(value(arg)?),
|
|
"--offline" => answers.online = Some(false),
|
|
other => return Err(format!("init: unknown option {other}")),
|
|
}
|
|
}
|
|
Ok(answers)
|
|
}
|
|
|
|
fn import(dir: &std::path::Path, args: &[String]) -> Result<(), String> {
|
|
let [source] = args else {
|
|
return Err("import needs the server.properties to read".into());
|
|
};
|
|
let target = dir.join(config::FILE);
|
|
if target.exists() {
|
|
return Err(format!(
|
|
"{} already exists; move it away first",
|
|
target.display()
|
|
));
|
|
}
|
|
let cfg = config::Config::import(&config::read_properties(std::path::Path::new(source))?)?;
|
|
std::fs::write(&target, config::render(&cfg)).map_err(|e| e.to_string())?;
|
|
println!("Wrote {}", target.display());
|
|
Ok(())
|
|
}
|