diff --git a/crates/horizon-client/src/game/dedicated.rs b/crates/horizon-client/src/game/dedicated.rs index 77fd12636..33bedf696 100644 --- a/crates/horizon-client/src/game/dedicated.rs +++ b/crates/horizon-client/src/game/dedicated.rs @@ -13,7 +13,9 @@ use horizon_client::{ server::{ eula, java_properties::now_millis, + profiles, properties::{GAME_MODES, ServerProperties, SystemEntropy}, + user_cache::UserCache, user_lists::{ BanEntry, BanList, IpBanList, NameAndId, OpEntry, OpList, WhiteEntry, WhiteList, format_date, @@ -57,20 +59,17 @@ struct Setup { started: Instant, } static SETUP: OnceLock>> = OnceLock::new(); +/// usercache.json (Services.nameToIdCache): used by the server thread only. +static USER_CACHE: Mutex> = Mutex::new(None); -/// CachedUserNameToIdResolver, kept in memory only (vanilla also saves it to -/// usercache.json): players who joined, and accounts found by name, by name -/// in lower case. -static USER_CACHE: OnceLock>> = OnceLock::new(); - -/// PlayerList.placeNewPlayer adds the player to the name cache. -pub(super) fn remember(name: &str, uuid: u128) { - let user = NameAndId { - id: uuid, - name: name.to_owned(), - }; - let cache = USER_CACHE.get_or_init(Default::default); - cache.lock().unwrap().insert(name.to_lowercase(), user); +/// PlayerList.placeNewPlayer: a player who joined is cached (and the file saved). +pub(super) fn player_joined(id: u128, name: &str) { + if let Some(cache) = USER_CACHE.lock().unwrap().as_mut() { + cache.add(NameAndId { + id, + name: name.to_owned(), + }); + } } /// Log4j's console pattern: `[HH:mm:ss] [thread/LEVEL]: message`. @@ -331,8 +330,6 @@ struct ServerState { /// When the last passes of the loop ended, for the tick rate horizon-server shows. ticks: std::collections::VecDeque, paused: bool, - /// online-mode: names given to commands are looked up as accounts. - online: bool, } impl Game<'_> { @@ -365,6 +362,14 @@ impl Game<'_> { line.clear(); } })?; + // Main: Services.create loads the universe's usercache.json. + let (mut cache, warnings) = UserCache::load(universe.join("usercache.json")); + for warning in warnings { + log_from("main", "WARN", &warning); + } + // DedicatedServer.initServer: names nobody has resolve offline only without authentication. + cache.resolve_offline_users = !props.online_mode; + *USER_CACHE.lock().unwrap() = Some(cache); log("INFO", "Starting minecraft server version 26.3"); log("INFO", "Loading properties"); let game_mode = props.game_mode; @@ -530,7 +535,6 @@ impl Game<'_> { empty_ticks: 0, ticks: Default::default(), paused: false, - online: props.online_mode, }; let pause_after = props.pause_when_empty_seconds * 20; let idle_timeout = props.player_idle_timeout; @@ -672,33 +676,23 @@ impl Game<'_> { }) } - /// GameProfileArgument: a player on the server, else one seen since the - /// start, else the name resolved as the server's mode says (an online - /// server asks the services, an offline one makes the offline profile). - /// None, told to the console: no such account. - fn resolve(&self, name: &str, online: bool) -> Option { - let user = self.online_player(name).or_else(|| { - let key = name.to_lowercase(); - let cache = USER_CACHE.get_or_init(Default::default); - if let Some(user) = cache.lock().unwrap().get(&key) { - return Some(user.clone()); - } - let user = horizon_server_files::profiles::resolve(name, online); - let Some(user) = user else { - if online { - log("WARN", &format!("Couldn't find profile with name: {key}")); + /// GameProfileArgument for a name: the user cache, else the profile + /// service, else (offline) the offline player. None, told to the console: + /// no such player. + fn resolve(&self, name: &str) -> Option { + let found = USER_CACHE.lock().unwrap().as_mut().and_then(|cache| { + cache.get_by_name(name, |lower| { + let found = profiles::find_by_name(&profiles::discovery_url(), lower); + if found.is_none() { + log("WARN", &format!("Couldn't find profile with name: {lower}")); } - return None; - }; - if online { - cache.lock().unwrap().insert(key, user.clone()); - } - Some(user) + found + }) }); - if user.is_none() { + if found.is_none() { self.console_reply(Text::translate("argument.player.unknown", &[])); } - user + found } /// MinecraftServer.sendSystemMessage: what the console is told. @@ -781,7 +775,7 @@ impl Game<'_> { )); } "op" | "deop" if !rest.is_empty() => { - let Some(user) = self.resolve(rest[0], state.online) else { + let Some(user) = self.resolve(rest[0]) else { return; }; let level = 4; @@ -813,7 +807,7 @@ impl Game<'_> { } "whitelist" => self.console_whitelist(state, &rest), "ban" if !rest.is_empty() => { - let Some(user) = self.resolve(rest[0], state.online) else { + let Some(user) = self.resolve(rest[0]) else { return; }; let reason = (rest.len() > 1).then(|| rest[1..].join(" ")); @@ -905,7 +899,7 @@ impl Game<'_> { } } "pardon" if !rest.is_empty() => { - let Some(user) = self.resolve(rest[0], state.online) else { + let Some(user) = self.resolve(rest[0]) else { return; }; let removed = state.lists.lock().unwrap().bans.remove(&user); @@ -1056,7 +1050,7 @@ impl Game<'_> { self.console_reply(Text::translate("commands.whitelist.reloaded", &[])); } ["add", name] | ["remove", name] => { - let Some(user) = self.resolve(name, state.online) else { + let Some(user) = self.resolve(name) else { return; }; let mut l = state.lists.lock().unwrap(); diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index 0b624094d..b5472871c 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -799,7 +799,7 @@ impl Game<'_> { && let Some(g) = host.hub.guest(conn) { // PlayerList.placeNewPlayer, then the broadcast join message. - super::dedicated::remember(&g.name, g.uuid); + super::dedicated::player_joined(g.uuid, &g.name); let [x, y, z] = g.position; super::dedicated::log( "INFO", diff --git a/crates/horizon-server-files/src/json.rs b/crates/horizon-server-files/src/json.rs index 8e1d865b2..47fe6d5d3 100644 --- a/crates/horizon-server-files/src/json.rs +++ b/crates/horizon-server-files/src/json.rs @@ -78,6 +78,43 @@ impl Json { } } + /// `new GsonBuilder().create().toJson(element)`: compact, HTML-safe + /// strings, null members skipped. + pub fn gson(&self) -> String { + let mut out = String::new(); + self.write_gson(&mut out); + out + } + fn write_gson(&self, out: &mut String) { + match self { + Json::String(s) => quote(out, s, true), + Json::Array(items) => { + out.push('['); + for (i, v) in items.iter().enumerate() { + if i > 0 { + out.push(','); + } + v.write_gson(out); + } + out.push(']'); + } + Json::Object(m) => { + out.push('{'); + let members = m.iter().filter(|(_, v)| *v != Json::Null); + for (i, (k, v)) in members.enumerate() { + if i > 0 { + out.push(','); + } + quote(out, k, true); + out.push(':'); + v.write_gson(out); + } + out.push('}'); + } + other => other.write_compact(out), + } + } + /// `new GsonBuilder().setPrettyPrinting().create().toJson(element, writer)`: /// two-space indents, HTML-safe strings, null members skipped. pub fn pretty(&self) -> String { diff --git a/crates/horizon-server-files/src/lib.rs b/crates/horizon-server-files/src/lib.rs index babb02771..08c059905 100644 --- a/crates/horizon-server-files/src/lib.rs +++ b/crates/horizon-server-files/src/lib.rs @@ -1,7 +1,7 @@ //! The dedicated server's files and settings, ported from //! net.minecraft.server.dedicated and net.minecraft.server.players: -//! server.properties, eula.txt, the four user lists and looking players up -//! by name in online mode. The game's dedicated server +//! server.properties, eula.txt, the four user lists, usercache.json and +//! looking players up by name. 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; @@ -9,4 +9,5 @@ pub mod java_properties; pub mod json; pub mod profiles; pub mod properties; +pub mod user_cache; pub mod user_lists; diff --git a/crates/horizon-server-files/src/user_cache.rs b/crates/horizon-server-files/src/user_cache.rs new file mode 100644 index 000000000..c8ccd6cf0 --- /dev/null +++ b/crates/horizon-server-files/src/user_cache.rs @@ -0,0 +1,318 @@ +//! usercache.json (CachedUserNameToIdResolver): the players the server has +//! seen join or has looked up by name, so commands find a player by name +//! without asking the profile service again. Each entry expires a month after +//! it was added; the file keeps the 1000 most recently used. +use crate::{ + java_properties::{civil_from_days, days_from_civil, now_millis}, + json::{self, Json}, + profiles::valid_player_name, + user_lists::{NameAndId, format_date, parse_date}, +}; +use std::{collections::HashMap, path::PathBuf}; + +const MRU_LIMIT: usize = 1000; + +#[derive(Clone, Debug)] +struct Info { + user: NameAndId, + expires: i64, + last_access: u64, +} + +/// `Calendar.add(MONTH, 1)` on a UTC time: the same day and time a month +/// later, the day clamped to the end of a shorter month. +pub fn one_month_later(millis: i64) -> i64 { + let days = millis.div_euclid(86_400_000); + let time = millis.rem_euclid(86_400_000); + let (y, m, d) = civil_from_days(days); + let (y, m) = if m == 12 { (y + 1, 1) } else { (y, m + 1) }; + let last = days_from_civil( + if m == 12 { y + 1 } else { y }, + if m == 12 { 1 } else { m + 1 }, + 1, + ) - days_from_civil(y, m, 1); + let d = d.min(last as u32); + days_from_civil(y, m, d) * 86_400_000 + time +} + +pub struct UserCache { + file: PathBuf, + /// Every entry ever added; the maps point into it, as vanilla's two maps + /// share their GameProfileInfo objects. + infos: Vec, + by_name: HashMap, + by_uuid: HashMap, + operations: u64, + /// Off on an online-mode server: a name the profile service doesn't know + /// is then no player at all, rather than an offline one. + pub resolve_offline_users: bool, +} + +impl UserCache { + /// The cache in `file`, loaded; also returns the warnings vanilla logs. + pub fn load(file: impl Into) -> (Self, Vec) { + let mut cache = Self { + file: file.into(), + infos: Vec::new(), + by_name: HashMap::new(), + by_uuid: HashMap::new(), + operations: 0, + resolve_offline_users: true, + }; + let mut warnings = Vec::new(); + let entries = match std::fs::read_to_string(&cache.file) { + Ok(text) => cache.parse(&text, &mut warnings), + Err(_) => Vec::new(), + }; + for info in entries.into_iter().rev() { + cache.safe_add(info); + } + (cache, warnings) + } + + fn parse(&self, text: &str, warnings: &mut Vec) -> Vec { + let items = match json::parse(text, false) { + Ok(Json::Array(items)) => items, + // An empty file is Gson's null. + Ok(Json::Null) => return Vec::new(), + _ if text.trim().is_empty() => return Vec::new(), + _ => { + warnings.push(format!( + "Failed to load profile cache {}", + self.file.display() + )); + return Vec::new(); + } + }; + let mut out = Vec::new(); + for item in &items { + if !matches!(item, Json::Object(_)) { + continue; + } + let Some(user) = NameAndId::from_json(item) else { + continue; + }; + let Some(date) = item.get("expiresOn").and_then(Json::as_string) else { + continue; + }; + match parse_date(&date) { + Some(expires) => out.push(Info { + user, + expires, + last_access: 0, + }), + None => warnings.push(format!("Failed to parse date {date}")), + } + } + out + } + + fn safe_add(&mut self, mut info: Info) -> usize { + self.operations += 1; + info.last_access = self.operations; + let index = self.infos.len(); + self.by_name.insert(info.user.name.to_lowercase(), index); + self.by_uuid.insert(info.user.id, index); + self.infos.push(info); + index + } + + /// `add`: a player who joined, remembered for a month; saves the file. + pub fn add(&mut self, user: NameAndId) { + self.add_internal(user, now_millis()); + } + + fn add_internal(&mut self, user: NameAndId, now: i64) -> NameAndId { + let index = self.safe_add(Info { + user, + expires: one_month_later(now), + last_access: 0, + }); + self.save(); + self.infos[index].user.clone() + } + + /// `get(String)`: the player with this name (any case). An expired entry is + /// dropped; a name not cached is asked of `lookup` (the profile service, + /// given the lower-case name) and, failing that, made an offline player + /// when offline users resolve. What is found is cached. + pub fn get_by_name( + &mut self, + name: &str, + lookup: impl FnOnce(&str) -> Option, + ) -> Option { + self.get_by_name_at(name, now_millis(), lookup) + } + + fn get_by_name_at( + &mut self, + name: &str, + now: i64, + lookup: impl FnOnce(&str) -> Option, + ) -> Option { + let name = name.to_lowercase(); + let mut found = self.by_name.get(&name).copied(); + let mut needs_save = false; + if let Some(index) = found + && now >= self.infos[index].expires + { + let user = self.infos[index].user.clone(); + self.by_uuid.remove(&user.id); + self.by_name.remove(&user.name.to_lowercase()); + needs_save = true; + found = None; + } + let result = if let Some(index) = found { + self.operations += 1; + self.infos[index].last_access = self.operations; + Some(self.infos[index].user.clone()) + } else { + let profile = if valid_player_name(&name) { + lookup(&name) + } else { + None + }; + let profile = profile.or_else(|| { + self.resolve_offline_users + .then(|| NameAndId::offline(&name)) + }); + match profile { + Some(profile) => { + needs_save = false; + Some(self.add_internal(profile, now)) + } + None => None, + } + }; + if needs_save { + self.save(); + } + result + } + + /// `get(UUID)`: a cached player by id (expired or not). + pub fn get_by_id(&mut self, id: u128) -> Option { + let index = *self.by_uuid.get(&id)?; + self.operations += 1; + self.infos[index].last_access = self.operations; + Some(self.infos[index].user.clone()) + } + + /// The file's text: the 1000 most recently used entries, most recent + /// first, as Gson writes them (compact, HTML-safe). + pub fn to_json(&self) -> String { + let mut entries: Vec<&Info> = self.by_uuid.values().map(|&i| &self.infos[i]).collect(); + entries.sort_by_key(|info| std::cmp::Reverse(info.last_access)); + let items = entries + .into_iter() + .take(MRU_LIMIT) + .map(|info| { + let mut o = Json::Object(Vec::new()); + info.user.append_to(&mut o); + o.add("expiresOn", Json::String(format_date(info.expires))); + o + }) + .collect(); + Json::Array(items).gson() + } + + /// `save`: errors are ignored, as vanilla does. + pub fn save(&self) { + let _ = std::fs::write(&self.file, self.to_json()); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + const DAY: i64 = 86_400_000; + + fn cache(text: &str) -> (UserCache, Vec) { + let dir = std::env::temp_dir().join(format!( + "horizon-usercache-{}-{}", + std::process::id(), + text.len() + )); + std::fs::create_dir_all(&dir).unwrap(); + let file = dir.join("usercache.json"); + std::fs::write(&file, text).unwrap(); + UserCache::load(file) + } + + #[test] + fn months() { + let jan31 = days_from_civil(2026, 1, 31) * DAY + 3_723_000; + assert_eq!( + format_date(one_month_later(jan31)), + "2026-02-28 01:02:03 +0000" + ); + let dec15 = days_from_civil(2026, 12, 15) * DAY; + assert_eq!( + format_date(one_month_later(dec15)), + "2027-01-15 00:00:00 +0000" + ); + let jan31_leap = days_from_civil(2028, 1, 31) * DAY; + assert_eq!( + format_date(one_month_later(jan31_leap)), + "2028-02-29 00:00:00 +0000" + ); + } + + #[test] + fn keeps_order_and_skips_bad_entries() { + let (c, warnings) = cache( + r#"[{"uuid":"069a79f4-44e9-4726-a5be-fca90e38aaf5","name":"Notch","expiresOn":"2099-01-01 00:00:00 +0000"}, + 1, {"name":"NoId"}, {"uuid":"853c80ef-3c37-49fd-aa49-938b674adae6","name":"NoDate"}, + {"uuid":"853c80ef-3c37-49fd-aa49-938b674adae6","name":"BadDate","expiresOn":"soon"}, + {"uuid":"61699b2e-d327-4a01-9f1e-0ea8c3f06bc6","name":"Alex","expiresOn":"2099-01-01 00:00:00 +0000"}]"#, + ); + assert_eq!(warnings, vec!["Failed to parse date soon".to_owned()]); + assert_eq!( + c.to_json(), + r#"[{"uuid":"069a79f4-44e9-4726-a5be-fca90e38aaf5","name":"Notch","expiresOn":"2099-01-01 00:00:00 +0000"},{"uuid":"61699b2e-d327-4a01-9f1e-0ea8c3f06bc6","name":"Alex","expiresOn":"2099-01-01 00:00:00 +0000"}]"# + ); + } + + #[test] + fn names_resolve_through_the_cache() { + let (mut c, _) = cache( + r#"[{"uuid":"069a79f4-44e9-4726-a5be-fca90e38aaf5","name":"Notch","expiresOn":"2099-01-01 00:00:00 +0000"}, + {"uuid":"61699b2e-d327-4a01-9f1e-0ea8c3f06bc6","name":"Old","expiresOn":"2000-01-01 00:00:00 +0000"}]"#, + ); + let now = days_from_civil(2026, 10, 17) * DAY; + let never = |_: &str| -> Option { panic!("looked up") }; + // A hit in any case, and it becomes the most recent. + let notch = c.get_by_name_at("NOTCH", now, never).unwrap(); + assert_eq!(notch.name, "Notch"); + // Expired: dropped and looked up again, by its lower-case name. + let mut asked = Vec::new(); + let old = c + .get_by_name_at("Old", now, |n| { + asked.push(n.to_owned()); + None + }) + .unwrap(); + assert_eq!(asked, ["old"]); + assert_eq!(old, NameAndId::offline("old")); + // Not a valid name: never looked up. + let odd = c.get_by_name_at("a b", now, never).unwrap(); + assert_eq!(odd, NameAndId::offline("a b")); + c.resolve_offline_users = false; + assert_eq!(c.get_by_name_at("Nobody", now, |_| None), None); + let found = NameAndId { + id: 7, + name: "Steve".into(), + }; + let steve = c.get_by_name_at("steve", now, |_| Some(found.clone())); + assert_eq!(steve, Some(found)); + let json = c.to_json(); + let names: Vec<&str> = json + .split("\"name\":\"") + .skip(1) + .map(|s| s.split('"').next().unwrap()) + .collect(); + assert_eq!(names, ["Steve", "a b", "old", "Notch"]); + assert!(json.contains(r#""name":"Steve","expiresOn":"2026-11-17 00:00:00 +0000"}"#)); + } +} diff --git a/crates/horizon-server-files/src/user_lists.rs b/crates/horizon-server-files/src/user_lists.rs index 3d35aa62f..77de64e31 100644 --- a/crates/horizon-server-files/src/user_lists.rs +++ b/crates/horizon-server-files/src/user_lists.rs @@ -39,7 +39,7 @@ impl NameAndId { name: name.to_owned(), } } - fn from_json(o: &Json) -> Option { + pub(crate) fn from_json(o: &Json) -> Option { let uuid = o.get("uuid")?.as_string()?; let name = o.get("name")?.as_string()?; Some(Self { @@ -47,7 +47,7 @@ impl NameAndId { name, }) } - fn append_to(&self, o: &mut Json) { + pub(crate) fn append_to(&self, o: &mut Json) { o.add("uuid", Json::String(uuid_string(self.id))); o.add("name", Json::String(self.name.clone())); } diff --git a/docs/server.md b/docs/server.md index 0eb0aaf35..f3640c8b9 100644 --- a/docs/server.md +++ b/docs/server.md @@ -33,6 +33,12 @@ The dedicated server is the game itself, run headless: `whitelist on|off|list|reload|add|remove`, `ban`, `ban-ip`, `pardon`, `pardon-ip`, `banlist`, `kick` and `say` behave as in `DedicatedServer`. Every other command goes to the game's own dispatcher as the server (permission level 4). The log follows vanilla's Log4j pattern, in `logs/latest.log` and on stdout. +- **Player names.** `usercache.json` in the universe folder works as vanilla's `CachedUserNameToIdResolver`. + Players are added when they join and kept for a month, and the file keeps the 1000 most recently used, + newest first. The console finds a player by name in it, in any case. A name it doesn't hold is asked of the + profile service (`getByName`). If the service doesn't know the name, an offline server uses the offline + player of the lower-case name; an online server answers "That player does not exist". + `tools/test_user_cache_parity.py` checks this against vanilla, with a mock profile service. ### Measured (2026-10-16, Mac mini M4 Pro, main at bd8dff20, release build) diff --git a/tools/test_dedicated_server_parity.py b/tools/test_dedicated_server_parity.py index c30abe4b0..0f40893c8 100644 --- a/tools/test_dedicated_server_parity.py +++ b/tools/test_dedicated_server_parity.py @@ -35,7 +35,8 @@ exactly: shift-click changes and its lit and cooking totals; - the console lines about the player, the chat, `list`, `say` and `stop`; - the files the servers leave: server.properties (but the date line), - eula.txt, ops.json, whitelist.json, banned-players.json, banned-ips.json. + eula.txt, ops.json, whitelist.json, banned-players.json, banned-ips.json, + usercache.json (but the date the player's entry expires). Online mode needs Mojang's session servers and is not tested. """ @@ -238,7 +239,8 @@ def console_lines(lines): def server_files(directory): out = [] - for name in ['server.properties', 'eula.txt', 'ops.json', 'whitelist.json', 'banned-players.json', 'banned-ips.json']: + for name in ['server.properties', 'eula.txt', 'ops.json', 'whitelist.json', 'banned-players.json', 'banned-ips.json', + 'usercache.json']: path = directory / name if not path.exists(): out.append(f'{name}: missing') @@ -249,6 +251,9 @@ def server_files(directory): text = re.sub(r'^(#[^\n]*\n)#[^\n]*\n', r'\1#\n', text) # Random per server, and each side has its own port. text = re.sub(r'\n(management-server-secret|server-port)=[^\n]*', r'\n\1=', text) + if name == 'usercache.json': + # A month from when the player joined. + text = re.sub(r'"expiresOn":"[^"]*"', '"expiresOn":""', text) out.append(f'{name}: {text!r}') return out diff --git a/tools/test_user_cache_parity.py b/tools/test_user_cache_parity.py new file mode 100644 index 000000000..621514cc8 --- /dev/null +++ b/tools/test_user_cache_parity.py @@ -0,0 +1,246 @@ +#!/usr/bin/env python3 +"""usercache.json and the console's player names, against vanilla's dedicated server. + +The console finds a player by name through the user cache (CachedUserNameToIdResolver): a name it holds +in any case, else the profile service's `getByName` (asked with the lower-case name), else, in offline +mode, the offline player of that lower-case name; what it finds is cached for a month and the file +rewritten, most recently used first. A small mock of the Minecraft services (dev only: the discovery +document and `getByName`, which knows one account) stands in for Mojang's; vanilla finds it through +`-Dminecraft.api.discovery.host`, Horizon through `HORIZON_API_DISCOVERY`. + +Each server starts in offline mode in a folder holding a usercache.json with a live entry, an expired +one, entries without a date, with a bad date, without an id, and not an object. The console then +whitelists, ops, deops and bans players: the cached one in another case, the expired one, the account the +service knows, names it doesn't know, a name too long to ask about. The server restarts in online mode, +where a name nobody knows is no player at all, and stops. Compared exactly: the console's replies, the +names asked of the service in order, and usercache.json, whitelist.json, ops.json and +banned-players.json after each run (the dates the servers wrote are placeholders). + +Players who join are added by the dedicated-server harness, which compares usercache.json too. +""" +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +import json +import os +from pathlib import Path +import platform +import re +import shutil +import subprocess +import sys +import threading +import time +import urllib.parse + +try: + from . import lab, test_rust_parity as base + from .test_dedicated_server_parity import Server, free_port +except ImportError: + import lab + import test_rust_parity as base + from test_dedicated_server_parity import Server, free_port + +NAME = 'user-cache-parity' +PROPERTIES = { + 'online-mode': 'false', + 'level-seed': '12345', + 'generate-structures': 'false', + 'view-distance': '2', + 'simulation-distance': '2', + 'spawn-protection': '0', + 'pause-when-empty-seconds': '-1', +} +NOTCH = '069a79f444e94726a5befca90e38aaf5' +SEEDED = [ + {'uuid': '853c80ef-3c37-49fd-aa49-938b674adae6', 'name': 'Seeded', 'expiresOn': '2099-01-01 00:00:00 +0000'}, + {'uuid': '61699b2e-d327-4a01-9f1e-0ea8c3f06bc6', 'name': 'Expired', 'expiresOn': '2001-02-03 04:05:06 +0000'}, + {'uuid': '00000000-0000-0000-0000-000000000001', 'name': 'NoDate'}, + {'uuid': '00000000-0000-0000-0000-000000000002', 'name': 'BadDate', 'expiresOn': 'tomorrow'}, + {'name': 'NoId', 'expiresOn': '2099-01-01 00:00:00 +0000'}, + 'not an object', + {'uuid': '00000000-0000-0000-0000-000000000003', 'name': 'Later', 'expiresOn': '2098-05-06 07:08:09 +0100'}, +] +# (console command, the reply it waits for) +OFFLINE = [ + ('whitelist add SEEDED', r'System chat: (Added|Player is already)'), + ('whitelist add Notch', r'System chat: (Added|Player is already)'), + ('op expired', r'System chat: (Made|Nothing changed)'), + ('ban Nobody_Here', r'System chat: (Banned|Nothing changed)'), + ('whitelist add NameMuchTooLongForMinecraft', r'System chat: (Added|Player is already)'), + ('deop NOTCH', r'System chat: (Made|Nothing changed)'), + ('whitelist add later', r'System chat: (Added|Player is already)'), + ('whitelist remove notch', r'System chat: (Removed|Player is not)'), +] +ONLINE = [ + ('whitelist add Somebody_Else', r'System chat: (Added|That player does not exist)'), + ('whitelist add nobody_here', r'System chat: (Added|Player is already)'), +] +FILES = ['usercache.json', 'whitelist.json', 'ops.json', 'banned-players.json'] + + +class Services: + """The mock discovery document and profile lookup by name.""" + + def __init__(self): + self.asked = [] + self.lock = threading.Lock() + services = self + + class Handler(BaseHTTPRequestHandler): + def log_message(self, *args): + pass + + def reply(self, status, body): + data = json.dumps(body).encode() + self.send_response(status) + self.send_header('Content-Type', 'application/json') + self.send_header('Content-Length', str(len(data))) + self.end_headers() + self.wfile.write(data) + + def do_GET(self): + url = urllib.parse.urlparse(self.path) + if url.path == '/minecraft/client': + return self.reply(200, services.discovery()) + if url.path.startswith('/profiles/name/'): + name = urllib.parse.unquote(url.path.removeprefix('/profiles/name/')) + with services.lock: + services.asked.append(name) + if name.lower() == 'notch': + return self.reply(200, {'id': NOTCH, 'name': 'Notch'}) + return self.reply(404, {'path': url.path, 'errorMessage': f"Couldn't find any profile with name {name}"}) + self.reply(404, {'error': 'NotFound', 'path': url.path}) + + self.server = ThreadingHTTPServer(('127.0.0.1', 0), Handler) + self.url = f'http://127.0.0.1:{self.server.server_address[1]}' + threading.Thread(target=self.server.serve_forever, daemon=True).start() + + def discovery(self): + def endpoints(**names): + return {'endpoints': {k: {'uri': self.url + v} for k, v in names.items()}} + return {'environment': 'mock', 'product': 'minecraft', 'discovery': { + 'authentication': endpoints(getPublicKeys='/publickeys'), + 'session': endpoints(join='/session/minecraft/join', verify='/session/minecraft/hasJoined', + getProfileById='/session/minecraft/profile/{profileId}'), + 'player': endpoints(), + 'profiles': endpoints(getByName='/profiles/name/{name}'), + 'telemetry': endpoints(), + }} + + def take(self): + with self.lock: + asked, self.asked = self.asked, [] + return asked + + def stop(self): + self.server.shutdown() + + +def write_properties(directory, online): + props = dict(PROPERTIES, **{'server-port': str(free_port()), 'online-mode': str(online).lower()}) + (directory / 'server.properties').write_text(''.join(f'{k}={v}\n' for k, v in props.items()), + encoding='iso-8859-1') + + +def files(directory): + out = [] + for name in FILES: + path = directory / name + if not path.exists(): + out.append(f'{name}: missing') + continue + text = path.read_text(encoding='utf-8') + # The dates written now: a month on for the cache, the ban's creation. + text = re.sub(r'"(expiresOn|created)": ?"20[2-3]\d-[^"]*"', r'"\1":""', text) + out.append(f'{name}: {text}') + return out + + +def wait_after(server, seen, pattern, timeout=60): + """Waits for a line matching `pattern` after the first `seen` lines.""" + deadline = time.time() + timeout + regex = re.compile(pattern) + while not any(regex.search(l) for l in server.lines[seen:]): + if time.time() > deadline or server.process.poll() is not None: + raise AssertionError(f'{server.log_path.name}: no reply matching {pattern!r}:\n' + + '\n'.join(server.lines[-20:])) + time.sleep(0.1) + + +def run(label, command, build, services, steps, online, transcript): + directory = build / label + write_properties(directory, online) + server = Server(command, directory, f'{label}-{"online" if online else "offline"}') + try: + server.wait_for(r'Done \(') + services.take() + for line, reply in steps: + seen = len(server.lines) + server.console(line) + wait_after(server, seen, reply) + replies = [l for l in server.lines[seen:] if 'System chat:' in l] + transcript.append(f'> {line}') + transcript += [re.sub(r'^\[[^]]*\] ', '', l) for l in replies] + transcript.append(f'asked: {services.take()}') + finally: + server.stop() + transcript += files(directory) + + +def run_side(label, command, build, services): + directory = build / label + if directory.exists(): + shutil.rmtree(directory) + directory.mkdir(parents=True) + (directory / 'eula.txt').write_text('eula=true\n') + (directory / 'usercache.json').write_text(json.dumps(SEEDED), encoding='utf-8') + transcript = [] + run(label, command, build, services, OFFLINE, False, transcript) + run(label, command, build, services, ONLINE, True, transcript) + return transcript + + +def main(): + lock, jars, libraries = base.verify_artifacts() + build = lab.LOCAL / NAME + build.mkdir(parents=True, exist_ok=True) + report = build / 'report.json' + report.unlink(missing_ok=True) + subprocess.run(['cargo', 'build', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], cwd=lab.ROOT, + check=True, capture_output=True) + metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--no-deps', '--format-version', '1'], + cwd=lab.ROOT, text=True)) + binary = Path(metadata['target_directory']) / 'release' / ('horizon-client.exe' if os.name == 'nt' else 'horizon-client') + services = Services() + discovery = services.url + '/minecraft/client' + dependencies = os.pathsep.join(map(str, [jars['server'], *libraries])) + vanilla_server = ['java', '-Xmx2g', '-Duser.timezone=UTC', f'-Dminecraft.api.discovery.host={discovery}', + '-cp', dependencies, 'net.minecraft.server.Main', 'nogui'] + horizon_server = [binary, '--resources', lab.LOCAL / 'horizon-client/26.3', '--dedicated', '--nogui'] + os.environ['HORIZON_API_DISCOVERY'] = discovery + try: + vanilla = run_side('vanilla', vanilla_server, build, services) + horizon = run_side('horizon', horizon_server, build, services) + finally: + services.stop() + (build / 'vanilla.txt').write_text('\n'.join(vanilla) + '\n', encoding='utf-8') + (build / 'horizon.txt').write_text('\n'.join(horizon) + '\n', encoding='utf-8') + if vanilla != horizon: + import difflib + diff = '\n'.join(difflib.unified_diff(vanilla, horizon, 'vanilla', 'horizon', n=1, lineterm='')) + raise AssertionError(f'transcripts differ (see {build}/vanilla.txt and horizon.txt):\n' + diff[:6000]) + if not any('Notch' in l for l in vanilla if l.startswith('usercache.json')): + raise AssertionError(f'the cache never held the looked-up account (see {build})') + lab.write_json(report, dict( + schema=1, minecraft=lock['minecraft']['version'], platform=platform.platform(), + java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0], + lines=len(vanilla), output_sha256=lab.digest(build / 'horizon.txt', 'sha256'), + scope='usercache.json (load, expiry, most recently used order, Gson output) and the console finding ' + 'players by name through it and the profile service, offline and online.')) + print(f'user cache: {len(vanilla)} lines identical') + + +if __name__ == '__main__': + try: + main() + except AssertionError as error: + sys.exit(f'{NAME}: {error}')