Files
horizon-airdef/crates/horizon-server/src/lists.rs
T
Ludovic Marie ae22d5dd96 Online mode: players named in commands are their accounts
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>
2026-10-17 01:31:19 +02:00

173 lines
6.9 KiB
Rust

//! `op`, `deop` and `whitelist`. While the server runs, the game owns the lists: the command goes to its
//! console. While it is stopped, they edit vanilla's files with the code the game reads them with
//! (horizon-server-files) and answer with vanilla's console messages.
use crate::{config, supervisor};
use horizon_server_files::user_lists::{NameAndId, OpEntry, OpList, WhiteEntry, WhiteList};
use std::path::Path;
pub fn run(dir: &Path, command: &str, args: &[String]) -> Result<(), String> {
let config = config::Config::load(dir)?;
// Holding the folder's lock keeps the server from starting halfway through.
let Ok(_lock) = supervisor::lock(dir) else {
let mut words = vec![command.to_owned()];
words.extend_from_slice(args);
return crate::cli::command(dir, &words);
};
let args: Vec<&str> = args.iter().map(String::as_str).collect();
let ops_path = dir.join("ops.json");
let white_path = dir.join("whitelist.json");
let message = match (command, args.as_slice()) {
("op", [name]) => {
let mut ops = load(OpList::new(ops_path))?;
let user = account(name, config.server.online_mode)?;
if ops.contains(&user) {
"Nothing changed. The player already is an operator".to_owned()
} else {
ops.add(OpEntry {
user,
level: 4,
bypasses_player_limit: false,
});
format!("Made {name} a server operator")
}
}
("deop", [name]) => {
let mut ops = load(OpList::new(ops_path))?;
let user = listed(ops.entries().iter().map(|e| &e.user), name);
if user.is_some_and(|user| ops.remove(&user)) {
format!("Made {name} no longer a server operator")
} else {
"Nothing changed. The player is not an operator".to_owned()
}
}
("whitelist", ["add", name]) => {
let mut white = load(WhiteList::new(white_path))?;
let user = account(name, config.server.online_mode)?;
if white.contains(&user) {
"Player is already whitelisted".to_owned()
} else {
white.add(WhiteEntry { user });
format!("Added {name} to the whitelist")
}
}
("whitelist", ["remove", name]) => {
let mut white = load(WhiteList::new(white_path))?;
let user = listed(white.entries().iter().map(|e| &e.user), name);
if user.is_some_and(|user| white.remove(&user)) {
format!("Removed {name} from the whitelist")
} else {
"Player is not whitelisted".to_owned()
}
}
("whitelist", [state @ ("on" | "off")]) => {
let on = *state == "on";
match (on, config.server.whitelist) {
(true, true) => "Whitelist is already turned on".to_owned(),
(false, false) => "Whitelist is already turned off".to_owned(),
_ => {
config::update(dir, "server", "whitelist", toml::Value::Boolean(on))?;
format!("Whitelist is now turned {state}")
}
}
}
("whitelist", ["list"]) => {
let white = load(WhiteList::new(white_path))?;
let names: Vec<String> = white
.entries()
.iter()
.map(|e| e.user.name.clone())
.collect();
let state = if config.server.whitelist { "on" } else { "off" };
if names.is_empty() {
format!("There are no whitelisted players (the whitelist is {state})")
} else {
format!(
"There are {} whitelisted player(s): {} (the whitelist is {state})",
names.len(),
names.join(", ")
)
}
}
_ => {
return Err(
"use: op NAME, deop NAME, whitelist on|off|list, whitelist add|remove NAME".into(),
);
}
};
println!("{message}");
Ok(())
}
/// The profile a running server would store for `name`: the offline one, or in online mode the account the
/// services know by that name.
pub fn account(name: &str, online: bool) -> Result<NameAndId, String> {
horizon_server_files::profiles::resolve(name, online).ok_or_else(|| {
if online {
format!("That player does not exist (no Minecraft account is named {name}, or the services didn't answer)")
} else {
"That player does not exist".to_owned()
}
})
}
/// The entry already on a list under `name` (any case), so removing needs no lookup.
fn listed<'a>(mut users: impl Iterator<Item = &'a NameAndId>, name: &str) -> Option<NameAndId> {
users.find(|u| u.name.eq_ignore_ascii_case(name)).cloned()
}
/// A list from its file; a missing file is an empty list, a broken one an error (vanilla would replace it).
fn load<E: horizon_server_files::user_lists::Entry>(
mut list: horizon_server_files::user_lists::UserList<E>,
) -> Result<horizon_server_files::user_lists::UserList<E>, String> {
if list.file.exists() {
list.load()
.map_err(|e| format!("{}: {e}", list.file.display()))?;
}
Ok(list)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn edits_the_lists_while_stopped() {
let dir = std::env::temp_dir().join(format!("horizon-server-lists-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join(config::FILE), config::TEMPLATE).unwrap();
let s = |v: &[&str]| v.iter().map(|a| (*a).to_owned()).collect::<Vec<_>>();
run(&dir, "op", &s(&["Alex"])).unwrap();
run(&dir, "whitelist", &s(&["add", "Steve"])).unwrap();
run(&dir, "whitelist", &s(&["off"])).unwrap();
assert!(
std::fs::read_to_string(dir.join("ops.json"))
.unwrap()
.contains("Alex")
);
assert!(
std::fs::read_to_string(dir.join("whitelist.json"))
.unwrap()
.contains("Steve")
);
assert!(!config::Config::load(&dir).unwrap().server.whitelist);
run(&dir, "deop", &s(&["Alex"])).unwrap();
assert!(
!std::fs::read_to_string(dir.join("ops.json"))
.unwrap()
.contains("Alex")
);
// A running server owns the lists: without its API, nothing changes.
let lock = supervisor::lock(&dir).unwrap();
assert!(run(&dir, "op", &s(&["Alex"])).is_err());
drop(lock);
assert!(
!std::fs::read_to_string(dir.join("ops.json"))
.unwrap()
.contains("Alex")
);
std::fs::remove_dir_all(&dir).unwrap();
}
}