diff --git a/crates/horizon-client/Cargo.toml b/crates/horizon-client/Cargo.toml index 1a7a4d25c..48e2e57d4 100644 --- a/crates/horizon-client/Cargo.toml +++ b/crates/horizon-client/Cargo.toml @@ -42,8 +42,9 @@ log = "0.4" chrono = { version = "0.4.42", default-features = false, features = ["clock", "std"] } flate2 = { version = "1.1.10", default-features = false, features = ["zlib-rs", "runtime_detection"] } horizon-theme = { path = "../horizon-theme" } -# Opt-in data collection: the game keeps, the launcher sends (docs/telemetry.md). -horizon-telemetry = { path = "../horizon-telemetry", default-features = false } +# Opt-in data collection: the game keeps, the launcher sends (docs/telemetry.md); `live` is +# the anonymous "I'm playing" ping the game sends itself (`heartbeat.rs`). +horizon-telemetry = { path = "../horizon-telemetry", default-features = false, features = ["live"] } horizon-bench = { path = "../horizon-bench" } arboard = { version = "3.6.1", default-features = false, features = ["wayland-data-control"] } zip = { version = "2", default-features = false, features = ["deflate"] } diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 9dba1c756..0c1efc251 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -11008,6 +11008,8 @@ impl ApplicationHandler for Game<'_> { } let updated = Instant::now(); self.track_toggle_screens(); + // For the "I'm playing" ping: an atomic, sent on when it changes. + horizon_client::heartbeat::set_lan(self.is_published()); let steps = self.clock.advance(elapsed); for _ in 0..steps { let started = Instant::now(); @@ -12815,6 +12817,16 @@ pub fn run() -> Result<()> { // 10 to 200 ms to get one back. horizon_threads::favour_current(); } + // Someone is playing: the anonymous ping behind the launcher's "N playing now". Not for a + // server, a benchmark or a run that quits by itself. + if options.dedicated.is_none() + && options.benchmark.is_none() + && !options.offscreen + && options.frames.is_none() + && options.world_frames.is_none() + { + horizon_client::heartbeat::start(); + } let started = Instant::now(); horizon_client::spans::enable(options.spikes.is_some()); let vanilla_root = options.resources.clone(); diff --git a/crates/horizon-client/src/heartbeat.rs b/crates/horizon-client/src/heartbeat.rs new file mode 100644 index 000000000..f73c7131f --- /dev/null +++ b/crates/horizon-client/src/heartbeat.rs @@ -0,0 +1,74 @@ +//! The anonymous "I'm playing" ping (`horizon_telemetry::live`, docs/telemetry.md) behind the +//! launcher's "N playing now". While the game runs, a thread of its own sends a random session +//! ID made here and kept only in memory, the version, the platform and whether a LAN or +//! play-with-friends world is open, every minute and at once when that last one changes. +//! It sends nothing when there is no server (`HORIZON_TELEMETRY_URL`, none by default) or +//! with `HORIZON_NO_HEARTBEAT=1` (automated runs). +use std::sync::OnceLock; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::thread::Thread; +use std::time::Duration; + +use horizon_telemetry::live::{self, Ping}; + +/// A world is open to LAN or to friends. +static LAN: AtomicBool = AtomicBool::new(false); +static PINGER: OnceLock = OnceLock::new(); + +/// After a failure the next ping waits longer, doubling up to this. +const MAX_WAIT: Duration = Duration::from_secs(600); + +/// Starts the pings, once. Call when the game itself starts (not a dedicated server, not a +/// benchmark or an automated run). +pub fn start() { + if PINGER.get().is_some() || std::env::var_os("HORIZON_NO_HEARTBEAT").is_some() { + return; + } + let Some(endpoint) = horizon_telemetry::endpoint() else { + return; + }; + let spawned = std::thread::Builder::new() + .name("Heartbeat".into()) + .spawn(move || run(&endpoint)); + match spawned { + Ok(handle) => { + let _ = PINGER.set(handle.thread().clone()); + } + Err(e) => log::warn!("Horizon: no live count ping: {e}"), + } +} + +/// Whether a world is open to LAN or friends; a change is sent at once. +pub fn set_lan(open: bool) { + if LAN.swap(open, Ordering::Relaxed) != open + && let Some(pinger) = PINGER.get() + { + pinger.unpark(); + } +} + +fn run(endpoint: &str) { + let session = live::session_id(); + let version = crate::telemetry::version(); + let mut wait = live::INTERVAL; + let mut failed = false; + loop { + let ping = Ping::new(&session, &version, LAN.load(Ordering::Relaxed)); + match live::send(endpoint, &ping) { + Ok(()) => { + wait = live::INTERVAL; + failed = false; + } + Err(e) => { + // Said once, not every minute the server is away. + if !failed { + log::info!("Horizon: the live count ping failed ({e}); trying again later"); + } + failed = true; + wait = (wait * 2).min(MAX_WAIT); + } + } + // Woken early by `set_lan`; a spurious wake only sends one ping more. + std::thread::park_timeout(wait); + } +} diff --git a/crates/horizon-client/src/lib.rs b/crates/horizon-client/src/lib.rs index 4cf0c5c48..a71418f18 100644 --- a/crates/horizon-client/src/lib.rs +++ b/crates/horizon-client/src/lib.rs @@ -80,6 +80,7 @@ pub mod graphics; pub mod graphics_style; pub mod graphics_ui; pub mod health; +pub mod heartbeat; pub mod hotbar; pub mod hud; pub mod hud_layout; diff --git a/crates/horizon-client/src/telemetry.rs b/crates/horizon-client/src/telemetry.rs index 2010df04a..ec67fe7f7 100644 --- a/crates/horizon-client/src/telemetry.rs +++ b/crates/horizon-client/src/telemetry.rs @@ -1,6 +1,7 @@ //! The game's part in opt-in data collection (`horizon_telemetry`, docs/telemetry.md). When the //! player allows it, the game keeps a scrubbed crash report, or a play session's performance -//! summary, in the outbox. It never sends anything itself: the launcher does, at its next start. +//! summary, in the outbox. It never sends those itself: the launcher does, at its next start (the +//! game's only sending is the anonymous "I'm playing" ping, `heartbeat.rs`). //! Without the player's yes for that kind nothing is kept, and crash-reports/ stays local. use std::path::Path; use std::sync::atomic::{AtomicU32, Ordering}; diff --git a/crates/horizon-telemetry-server/README.md b/crates/horizon-telemetry-server/README.md index cf2d52393..dc1f51248 100644 --- a/crates/horizon-telemetry-server/README.md +++ b/crates/horizon-telemetry-server/README.md @@ -1,6 +1,6 @@ # horizon-telemetry-server -This server receives what players choose to send (performance summaries, crash and bug reports, benchmark uploads: [docs/telemetry.md](../../docs/telemetry.md)) and stores it in one SQLite file. It serves anonymous totals to the website. It is one small Rust process, about 7 MB of memory when idle. +This server receives what players choose to send (performance summaries, crash and bug reports, benchmark uploads: [docs/telemetry.md](../../docs/telemetry.md)) and stores it in one SQLite file. It serves anonymous totals to the website. It also keeps the live player count from the games' anonymous "I'm playing" pings, in memory only. It is one small Rust process, about 7 MB of memory when idle. cargo run --release # in this folder: its own workspace, like the launcher's PORT=8080 HORIZON_TELEMETRY_DB=/data/telemetry.sqlite3 horizon-telemetry-server @@ -14,6 +14,8 @@ The launcher sends to it when `HORIZON_TELEMETRY_URL` is set (and to `DEFAULT_EN | `POST /v1/batch` | A zstd batch (`Content-Encoding: zstd`, at most 256 KiB, 1 MiB inflated, 64 items). Returns 204. Returns 400 for a bad batch, 413 for one too large, 415 if it is not zstd, 429 when over a limit. | | `DELETE /v1/installs/` | Deletes everything that install sent. Its share of the daily totals is rebuilt at once. | | `GET /v1/aggregate?days=30` | The totals the website reads. Cached 10 minutes, with `Access-Control-Allow-Origin` set to the website. | +| `POST /v1/live` | A game's "I'm playing" ping (JSON, at most 512 bytes). Returns 204. Returns 400 for a bad ping, 413 for one too large, 429 over the address's limit, 503 when it holds `LIVE_MAX_SESSIONS` already. | +| `GET /v1/live` | How many play now, rounded: `playing`, `lan`, `versions`, `platforms`. Made again at most every 15 seconds, with the same `Access-Control-Allow-Origin`. | | `GET /healthz` | `ok` | The totals are grouped per version, OS, GPU vendor and render distance. Each group has its sessions, install-days, hours played, the 14 frame-time buckets summed, and the mean p50 and p99 frame times. Groups with fewer than 5 install-days are left out. The totals also list crash counts per version and the newest 50 benchmark uploads. @@ -27,6 +29,8 @@ The totals are grouped per version, OS, GPU vendor and render distance. Each gro Measured: **about 190 bytes per play session** on disk, indexes included (`tests/server.rs`, 10 000 sessions). 1 000 players playing twice a day for 180 days is about 70 MB. +The live count is a table in memory: a session ID, when it last pinged, its version, platform and LAN flag, about 100 bytes a session. A session counts while its last ping is under 2 minutes old and is forgotten after that. Nothing of it reaches the disk, so a restart empties it; it is whole again a minute later. + The day stored is the day the server received an item. The sender's IP address is never written down. The per-address rate limit keeps addresses in memory for an hour at most. Raw rows are deleted after `HORIZON_TELEMETRY_RETENTION_DAYS` (180). The daily totals stay; they hold no install ID. @@ -43,6 +47,9 @@ All are environment variables: - `HORIZON_TELEMETRY_ITEMS_PER_DAY` per install (100) - `HORIZON_TELEMETRY_TRUST_PROXY=1` behind a proxy (the client's address is then the first of `X-Forwarded-For`) - `HORIZON_TELEMETRY_ALLOW_ORIGIN` (`https://playhorizon.dev`) +- `HORIZON_TELEMETRY_LIVE_PER_HOUR` per address, pings and reads (600, bursts of 60: ten players behind one address) +- `HORIZON_TELEMETRY_LIVE_WINDOW_SECONDS` (120) +- `HORIZON_TELEMETRY_LIVE_MAX_SESSIONS` (200 000) ## Hosting diff --git a/crates/horizon-telemetry-server/src/lib.rs b/crates/horizon-telemetry-server/src/lib.rs index b1521a714..061a9ca37 100644 --- a/crates/horizon-telemetry-server/src/lib.rs +++ b/crates/horizon-telemetry-server/src/lib.rs @@ -3,12 +3,15 @@ //! - `POST /v1/batch`: a zstd batch from the launcher, checked by `horizon_telemetry::batch`. //! - `DELETE /v1/installs/`: everything an install sent goes. //! - `GET /v1/aggregate?days=30`: the anonymous totals the website reads, cached. +//! - `POST /v1/live`: a game's anonymous "I'm playing" ping (`live`, in memory only). +//! - `GET /v1/live`: how many play now, rounded, cached briefly. //! - `GET /healthz`. //! //! Abuse limits: a token bucket per IP address (kept in memory for an hour, never stored), a -//! daily item cap per install, and the batch size limits. Raw rows expire after -//! `retention_days`; the daily totals stay. One small process and one SQLite file: see -//! `README.md` for running it. +//! daily item cap per install, and the batch size limits. The live count has its own, larger +//! bucket per address (several players can share one) and a cap on the sessions it holds. Raw +//! rows expire after `retention_days`; the daily totals stay. One small process and one SQLite +//! file: see `README.md` for running it. use std::collections::HashMap; use std::net::{IpAddr, SocketAddr}; use std::sync::{Arc, Mutex}; @@ -21,8 +24,11 @@ use axum::response::{IntoResponse, Response}; use axum::routing::{delete, get, post}; use horizon_telemetry::batch; +pub mod live; pub mod store; +use horizon_telemetry::live::{MAX_PING_BYTES, Ping}; +use live::Live; use store::Store; pub struct Config { @@ -38,6 +44,13 @@ pub struct Config { pub trust_proxy: bool, /// The website allowed to read the totals from a browser. pub allow_origin: String, + /// Live count requests (pings and reads) an IP address may make an hour, with a burst of a + /// tenth of it. A game pings once a minute, so the default lets ten play behind one address. + pub live_per_hour: f64, + /// How long a session counts after its last ping. + pub live_window: Duration, + /// Sessions the live count holds at most; a new one past that is refused. + pub live_max_sessions: usize, } impl Default for Config { @@ -49,13 +62,17 @@ impl Default for Config { items_per_day: 100, trust_proxy: false, allow_origin: "https://playhorizon.dev".into(), + live_per_hour: 600.0, + live_window: horizon_telemetry::live::WINDOW, + live_max_sessions: 200_000, } } } impl Config { /// The defaults, with `HORIZON_TELEMETRY_RETENTION_DAYS`, `…_MIN_INSTALL_DAYS`, - /// `…_REQUESTS_PER_HOUR`, `…_ITEMS_PER_DAY`, `…_TRUST_PROXY` (1) and `…_ALLOW_ORIGIN`. + /// `…_REQUESTS_PER_HOUR`, `…_ITEMS_PER_DAY`, `…_TRUST_PROXY` (1), `…_ALLOW_ORIGIN`, + /// `…_LIVE_PER_HOUR`, `…_LIVE_WINDOW_SECONDS` and `…_LIVE_MAX_SESSIONS`. pub fn from_env() -> Self { let var = |k: &str| std::env::var(format!("HORIZON_TELEMETRY_{k}")).ok(); let d = Self::default(); @@ -74,6 +91,15 @@ impl Config { .unwrap_or(d.items_per_day), trust_proxy: var("TRUST_PROXY").is_some_and(|v| v == "1"), allow_origin: var("ALLOW_ORIGIN").unwrap_or(d.allow_origin), + live_per_hour: var("LIVE_PER_HOUR") + .and_then(|v| v.parse().ok()) + .unwrap_or(d.live_per_hour), + live_window: var("LIVE_WINDOW_SECONDS") + .and_then(|v| v.parse().ok()) + .map_or(d.live_window, Duration::from_secs), + live_max_sessions: var("LIVE_MAX_SESSIONS") + .and_then(|v| v.parse().ok()) + .unwrap_or(d.live_max_sessions), } } } @@ -82,13 +108,21 @@ pub struct App { store: Mutex, config: Config, /// Tokens left and when they were counted, per client address. - buckets: Mutex>, + buckets: Mutex, + /// The same for the live count's requests. + live_buckets: Mutex, + /// Who is playing now (`live`). + live: Mutex, + /// `GET /v1/live`'s answer and when it was made. + live_cache: Mutex>, /// `/v1/aggregate` answers by `days`, with when they were made. cache: Mutex>, /// Today, in days since 1970 (a test moves it). today: Box u32 + Send + Sync>, } +type Buckets = HashMap; + const CACHE_FOR: Duration = Duration::from_secs(600); fn status(code: StatusCode, message: &str) -> Response { @@ -105,10 +139,14 @@ impl App { config: Config, today: Box u32 + Send + Sync>, ) -> Arc { + let live = Live::new(config.live_window, config.live_max_sessions); Arc::new(Self { store: Mutex::new(store), config, buckets: Mutex::default(), + live_buckets: Mutex::default(), + live: Mutex::new(live), + live_cache: Mutex::default(), cache: Mutex::default(), today, }) @@ -120,18 +158,16 @@ impl App { /// Takes a token for `ip`; false when it has none left. fn allow(&self, ip: IpAddr) -> bool { - let rate = self.config.requests_per_hour / 3600.0; - let burst = (self.config.requests_per_hour / 10.0).max(1.0); - let mut buckets = self.buckets.lock().unwrap_or_else(|e| e.into_inner()); - let now = Instant::now(); - let (tokens, at) = buckets.entry(ip).or_insert((burst, now)); - *tokens = (*tokens + now.duration_since(*at).as_secs_f64() * rate).min(burst); - *at = now; - if *tokens < 1.0 { - return false; - } - *tokens -= 1.0; - true + take(&self.buckets, self.config.requests_per_hour, ip) + } + + /// [`Self::allow`] for the live count's requests. + fn allow_live(&self, ip: IpAddr) -> bool { + take(&self.live_buckets, self.config.live_per_hour, ip) + } + + fn live(&self) -> std::sync::MutexGuard<'_, Live> { + self.live.lock().unwrap_or_else(|e| e.into_inner()) } fn client(&self, headers: &HeaderMap, peer: Option) -> IpAddr { @@ -160,14 +196,18 @@ impl App { self.store().newest_day(install).ok().flatten() } - /// Daily upkeep: totals rebuilt, expired rows removed, idle rate-limit entries forgotten. + /// Daily upkeep: totals rebuilt, expired rows removed, idle rate-limit entries and live + /// sessions forgotten. pub fn maintain(&self) -> rusqlite::Result { let oldest = (self.today)().saturating_sub(self.config.retention_days); let removed = self.store().expire(oldest)?; - self.buckets - .lock() - .unwrap_or_else(|e| e.into_inner()) - .retain(|_, (_, at)| at.elapsed() < Duration::from_secs(3600)); + for buckets in [&self.buckets, &self.live_buckets] { + buckets + .lock() + .unwrap_or_else(|e| e.into_inner()) + .retain(|_, (_, at)| at.elapsed() < Duration::from_secs(3600)); + } + self.live().forget_expired(Instant::now()); if removed > 0 { self.cache.lock().unwrap_or_else(|e| e.into_inner()).clear(); } @@ -175,6 +215,23 @@ impl App { } } +/// Takes a token for `ip` from its bucket in `buckets` (`per_hour`, bursts of a tenth of it); +/// false when it has none left. +fn take(buckets: &Mutex, per_hour: f64, ip: IpAddr) -> bool { + let rate = per_hour / 3600.0; + let burst = (per_hour / 10.0).max(1.0); + let mut buckets = buckets.lock().unwrap_or_else(|e| e.into_inner()); + let now = Instant::now(); + let (tokens, at) = buckets.entry(ip).or_insert((burst, now)); + *tokens = (*tokens + now.duration_since(*at).as_secs_f64() * rate).min(burst); + *at = now; + if *tokens < 1.0 { + return false; + } + *tokens -= 1.0; + true +} + fn peer(req: &Request) -> Option { req.extensions() .get::>() @@ -278,18 +335,65 @@ async fn aggregate(State(app): State>, Query(window): Query) -> } } }; + json_response(&app, json, CACHE_FOR.as_secs()) +} + +/// `POST /v1/live`: one "I'm playing". 204, or 400 for a bad ping, 413 for one too large, 429 +/// over the address's limit, 503 when the table is full. +async fn ping(State(app): State>, req: Request) -> Response { + if !app.allow_live(app.client(req.headers(), peer(&req))) { + return status(StatusCode::TOO_MANY_REQUESTS, "too many requests"); + } + let Ok(body) = axum::body::to_bytes(req.into_body(), MAX_PING_BYTES).await else { + return status(StatusCode::PAYLOAD_TOO_LARGE, "a ping is at most 512 bytes"); + }; + let ping: Ping = match serde_json::from_slice(&body) { + Ok(ping) => ping, + Err(e) => return status(StatusCode::BAD_REQUEST, &e.to_string()), + }; + if let Err(e) = ping.check() { + return status(StatusCode::BAD_REQUEST, e); + } + match app.live().ping(&ping, Instant::now()) { + Ok(()) => StatusCode::NO_CONTENT.into_response(), + Err(live::Full) => status(StatusCode::SERVICE_UNAVAILABLE, "try again later"), + } +} + +/// `GET /v1/live`: the rounded live count (`horizon_telemetry::live::Count`), made again at +/// most every eighth of the window. +async fn playing(State(app): State>, req: Request) -> Response { + if !app.allow_live(app.client(req.headers(), peer(&req))) { + return status(StatusCode::TOO_MANY_REQUESTS, "too many requests"); + } + let fresh_for = app.config.live_window / 8; + let json = { + let mut cache = app.live_cache.lock().unwrap_or_else(|e| e.into_inner()); + match &*cache { + Some((at, json)) if at.elapsed() < fresh_for => json.clone(), + _ => { + let json = + serde_json::to_string(&app.live().count(Instant::now())).unwrap_or_default(); + *cache = Some((Instant::now(), json.clone())); + json + } + } + }; + json_response(&app, json, fresh_for.as_secs()) +} + +fn json_response(app: &App, json: String, max_age: u64) -> Response { let origin = HeaderValue::from_str(&app.config.allow_origin).unwrap_or(HeaderValue::from_static("*")); + let cache = HeaderValue::from_str(&format!("public, max-age={max_age}")) + .unwrap_or(HeaderValue::from_static("no-cache")); ( [ ( header::CONTENT_TYPE, HeaderValue::from_static("application/json"), ), - ( - header::CACHE_CONTROL, - HeaderValue::from_static("public, max-age=600"), - ), + (header::CACHE_CONTROL, cache), (header::ACCESS_CONTROL_ALLOW_ORIGIN, origin), ], json, @@ -300,6 +404,7 @@ async fn aggregate(State(app): State>, Query(window): Query) -> pub fn router(app: Arc) -> Router { Router::new() .route("/v1/batch", post(receive)) + .route("/v1/live", post(ping).get(playing)) .route("/v1/installs/{id}", delete(forget)) .route("/v1/aggregate", get(aggregate)) .route("/healthz", get(|| async { "ok" })) diff --git a/crates/horizon-telemetry-server/src/live.rs b/crates/horizon-telemetry-server/src/live.rs new file mode 100644 index 000000000..f28165da5 --- /dev/null +++ b/crates/horizon-telemetry-server/src/live.rs @@ -0,0 +1,201 @@ +//! Who is playing now, from the games' "I'm playing" pings (`horizon_telemetry::live`). Kept in +//! memory only: a session ID, when it last pinged, its version, platform and LAN flag. A +//! session counts while its last ping is under the window old and is forgotten after it. No +//! address is kept, and nothing is written to disk, so a restart starts the count again (it +//! is whole again a minute later). +use std::collections::{BTreeMap, HashMap}; +use std::time::{Duration, Instant}; + +use horizon_telemetry::live::{Count, PLATFORMS, Ping, round}; + +/// Versions listed by name in a count; the rest are summed under `other`. +const VERSIONS_SHOWN: usize = 12; + +struct Seen { + at: Instant, + version: Box, + platform: u8, + lan: bool, +} + +pub struct Live { + sessions: HashMap, + window: Duration, + max_sessions: usize, +} + +/// The table is full and this session is new. +#[derive(Debug, PartialEq, Eq)] +pub struct Full; + +impl Live { + pub fn new(window: Duration, max_sessions: usize) -> Self { + Self { + sessions: HashMap::new(), + window, + max_sessions, + } + } + + /// Records `ping` (already checked) as seen at `now`. + pub fn ping(&mut self, ping: &Ping, now: Instant) -> Result<(), Full> { + let Ok(id) = u128::from_str_radix(&ping.session, 16) else { + return Ok(()); + }; + if !self.sessions.contains_key(&id) && self.sessions.len() >= self.max_sessions { + self.forget_expired(now); + if self.sessions.len() >= self.max_sessions { + return Err(Full); + } + } + let platform = PLATFORMS + .iter() + .position(|p| *p == ping.platform) + .unwrap_or(PLATFORMS.len() - 1) as u8; + self.sessions.insert( + id, + Seen { + at: now, + version: ping.version.as_str().into(), + platform, + lan: ping.lan, + }, + ); + Ok(()) + } + + /// Drops the sessions whose last ping is older than the window. + pub fn forget_expired(&mut self, now: Instant) { + let window = self.window; + self.sessions + .retain(|_, s| now.saturating_duration_since(s.at) < window); + } + + /// Who is playing at `now`, rounded. + pub fn count(&mut self, now: Instant) -> Count { + self.forget_expired(now); + let mut versions: HashMap<&str, u64> = HashMap::new(); + let mut platforms = [0u64; PLATFORMS.len()]; + let mut lan = 0; + for s in self.sessions.values() { + *versions.entry(&s.version).or_default() += 1; + platforms[usize::from(s.platform)] += 1; + lan += u64::from(s.lan); + } + let mut by_count: Vec<(&str, u64)> = versions.into_iter().collect(); + by_count.sort_by(|a, b| b.1.cmp(&a.1).then(a.0.cmp(b.0))); + let mut shown = BTreeMap::new(); + let mut other = 0; + for (i, (version, n)) in by_count.into_iter().enumerate() { + if i < VERSIONS_SHOWN { + shown.insert(version.to_owned(), round(n)); + } else { + other += n; + } + } + if other > 0 { + *shown.entry("other".to_owned()).or_default() += round(other); + } + Count { + playing: round(self.sessions.len() as u64), + lan: round(lan), + versions: shown, + platforms: PLATFORMS + .iter() + .zip(platforms) + .filter(|(_, n)| *n > 0) + .map(|(p, n)| ((*p).to_owned(), round(n))) + .collect(), + } + } + + /// Sessions held now, counted or not yet forgotten. + pub fn len(&self) -> usize { + self.sessions.len() + } + + pub fn is_empty(&self) -> bool { + self.sessions.is_empty() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn ping(n: u32, version: &str, platform: &str, lan: bool) -> Ping { + Ping { + session: format!("{n:032x}"), + version: version.into(), + platform: platform.into(), + lan, + } + } + + #[test] + fn sessions_count_until_the_window_passes() { + let t = Instant::now(); + let mut live = Live::new(Duration::from_secs(120), 100); + live.ping(&ping(1, "0.2.0", "linux", false), t).unwrap(); + live.ping(&ping(2, "0.2.0", "macos", true), t).unwrap(); + // The same session again is still one. + live.ping( + &ping(1, "0.2.0", "linux", false), + t + Duration::from_secs(60), + ) + .unwrap(); + let count = live.count(t + Duration::from_secs(61)); + assert_eq!((count.playing, count.lan), (2, 1)); + assert_eq!(count.versions, BTreeMap::from([("0.2.0".into(), 2)])); + assert_eq!( + count.platforms, + BTreeMap::from([("linux".into(), 1), ("macos".into(), 1)]) + ); + // Session 2 stopped pinging two minutes ago; session 1 pinged a minute later. + let count = live.count(t + Duration::from_secs(150)); + assert_eq!((count.playing, count.lan), (1, 0)); + assert_eq!(live.len(), 1); + assert_eq!(live.count(t + Duration::from_secs(181)), Count::default()); + assert!(live.is_empty()); + } + + #[test] + fn a_full_table_takes_no_new_session_until_one_expires() { + let t = Instant::now(); + let mut live = Live::new(Duration::from_secs(120), 2); + live.ping(&ping(1, "0.2.0", "linux", false), t).unwrap(); + live.ping(&ping(2, "0.2.0", "linux", false), t).unwrap(); + assert_eq!(live.ping(&ping(3, "0.2.0", "linux", false), t), Err(Full)); + // A known session still refreshes. + live.ping( + &ping(1, "0.2.0", "linux", false), + t + Duration::from_secs(100), + ) + .unwrap(); + let later = t + Duration::from_secs(130); + live.ping(&ping(3, "0.2.0", "linux", false), later).unwrap(); + assert_eq!(live.count(later).playing, 2); + } + + #[test] + fn rare_versions_are_summed_and_counts_rounded() { + let t = Instant::now(); + let mut live = Live::new(Duration::from_secs(120), 1000); + for n in 0..40 { + live.ping(&ping(n, "0.2.0", "windows", n % 4 == 0), t) + .unwrap(); + } + for n in 0..(VERSIONS_SHOWN as u32 + 3) { + live.ping(&ping(100 + n, &format!("dev {n}"), "other", false), t) + .unwrap(); + } + let count = live.count(t); + assert_eq!(count.playing, 55); + assert_eq!(count.lan, 10); + assert_eq!(count.versions["0.2.0"], 40); + assert_eq!(count.versions["other"], 4); + assert_eq!(count.versions.len(), VERSIONS_SHOWN + 1); + assert_eq!(count.platforms["windows"], 40); + assert_eq!(count.platforms["other"], 15); + } +} diff --git a/crates/horizon-telemetry-server/tests/live.rs b/crates/horizon-telemetry-server/tests/live.rs new file mode 100644 index 000000000..d8c7f33fe --- /dev/null +++ b/crates/horizon-telemetry-server/tests/live.rs @@ -0,0 +1,190 @@ +//! The live count through its HTTP interface, and end to end with the game's ping and the +//! launcher's read (`horizon_telemetry::live`). +use std::sync::Arc; +use std::time::Duration; + +use axum::body::Body; +use axum::http::{Request, StatusCode, header}; +use horizon_telemetry::live::{self, Count, Ping}; +use horizon_telemetry_server::store::Store; +use horizon_telemetry_server::{App, Config, router}; +use http_body_util::BodyExt; +use tower::ServiceExt; + +fn app(config: Config) -> Arc { + App::new(Store::open(":memory:").unwrap(), config) +} + +fn quick() -> Config { + Config { + live_per_hour: 100_000.0, + live_window: Duration::from_secs(1), + trust_proxy: true, + ..Config::default() + } +} + +fn ping(n: u32, version: &str, lan: bool) -> String { + serde_json::to_string(&Ping { + session: format!("{n:032x}"), + version: version.into(), + platform: "linux".into(), + lan, + }) + .unwrap() +} + +async fn send(app: &Arc, req: Request) -> (StatusCode, axum::http::HeaderMap, String) { + let res = router(app.clone()).oneshot(req).await.unwrap(); + let (code, headers) = (res.status(), res.headers().clone()); + let body = res.into_body().collect().await.unwrap().to_bytes(); + (code, headers, String::from_utf8_lossy(&body).into_owned()) +} + +async fn post(app: &Arc, body: impl Into, from: &str) -> StatusCode { + let req = Request::post("/v1/live") + .header("x-forwarded-for", from) + .body(body.into()) + .unwrap(); + send(app, req).await.0 +} + +async fn count(app: &Arc) -> Count { + let req = Request::get("/v1/live") + .header("x-forwarded-for", "198.51.100.1") + .body(Body::empty()) + .unwrap(); + let (code, headers, body) = send(app, req).await; + assert_eq!(code, StatusCode::OK, "{body}"); + assert_eq!(headers[header::CONTENT_TYPE], "application/json"); + assert_eq!( + headers[header::ACCESS_CONTROL_ALLOW_ORIGIN], + "https://playhorizon.dev" + ); + serde_json::from_str(&body).unwrap() +} + +#[tokio::test] +async fn sessions_count_while_they_ping_and_expire_after() { + let app = app(quick()); + assert_eq!( + post(&app, ping(1, "0.2.0", false), "203.0.113.1").await, + StatusCode::NO_CONTENT + ); + assert_eq!( + post(&app, ping(2, "0.2.0", true), "203.0.113.2").await, + StatusCode::NO_CONTENT + ); + // A second ping from the same session is still one player. + assert_eq!( + post(&app, ping(1, "0.2.0", false), "203.0.113.1").await, + StatusCode::NO_CONTENT + ); + let now = count(&app).await; + assert_eq!((now.playing, now.lan), (2, 1)); + assert_eq!(now.versions["0.2.0"], 2); + assert_eq!(now.platforms["linux"], 2); + + // Session 1 keeps pinging; session 2 stops. + for _ in 0..5 { + tokio::time::sleep(Duration::from_millis(250)).await; + post(&app, ping(1, "0.2.0", false), "203.0.113.1").await; + } + let later = count(&app).await; + assert_eq!((later.playing, later.lan), (1, 0)); + tokio::time::sleep(Duration::from_millis(1200)).await; + assert_eq!(count(&app).await, Count::default()); +} + +#[tokio::test] +async fn bad_pings_are_refused() { + let app = app(quick()); + let from = "203.0.113.3"; + assert_eq!(post(&app, "not json", from).await, StatusCode::BAD_REQUEST); + assert_eq!( + post(&app, ping(1, "0.2", false), from).await, + StatusCode::BAD_REQUEST + ); + let extra = r#"{"session":"00000000000000000000000000000001","version":"0.2.0","platform":"linux","lan":false,"name":"Steve"}"#; + assert_eq!(post(&app, extra, from).await, StatusCode::BAD_REQUEST); + assert_eq!( + post(&app, "x".repeat(4096), from).await, + StatusCode::PAYLOAD_TOO_LARGE + ); + assert_eq!(count(&app).await.playing, 0); +} + +#[tokio::test] +async fn an_address_is_rate_limited_on_its_own_bucket() { + let app = app(Config { + live_per_hour: 60.0, + trust_proxy: true, + ..Config::default() + }); + // A burst of a tenth of the hour's 60, then 429. + let mut codes = Vec::new(); + for n in 0..8 { + codes.push(post(&app, ping(n, "0.2.0", false), "203.0.113.4").await); + } + assert_eq!( + codes + .iter() + .filter(|c| **c == StatusCode::NO_CONTENT) + .count(), + 6 + ); + assert_eq!(codes[7], StatusCode::TOO_MANY_REQUESTS); + // Another address is not held back. + assert_eq!( + post(&app, ping(9, "0.2.0", false), "203.0.113.5").await, + StatusCode::NO_CONTENT + ); +} + +#[tokio::test] +async fn a_full_table_refuses_new_sessions() { + let app = app(Config { + live_max_sessions: 2, + ..quick() + }); + for n in 0..2 { + assert_eq!( + post(&app, ping(n, "0.2.0", false), "203.0.113.6").await, + StatusCode::NO_CONTENT + ); + } + assert_eq!( + post(&app, ping(2, "0.2.0", false), "203.0.113.6").await, + StatusCode::SERVICE_UNAVAILABLE + ); + assert_eq!( + post(&app, ping(1, "0.2.0", false), "203.0.113.6").await, + StatusCode::NO_CONTENT + ); +} + +#[tokio::test] +async fn the_games_ping_and_the_launchers_read_reach_it_end_to_end() { + let app = app(Config { + live_window: Duration::from_secs(120), + ..quick() + }); + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let url = format!("http://{}", listener.local_addr().unwrap()); + let service = router(app.clone()).into_make_service_with_connect_info::(); + tokio::spawn(async move { axum::serve(listener, service).await.unwrap() }); + + let count = tokio::task::spawn_blocking(move || { + let a = Ping::new(&live::session_id(), "0.2.0", false); + let b = Ping::new(&live::session_id(), "dev 0.1.0", true); + live::send(&url, &a)?; + live::send(&url, &b)?; + live::fetch(&url) + }) + .await + .unwrap() + .unwrap(); + assert_eq!((count.playing, count.lan), (2, 1)); + assert_eq!(count.versions.len(), 2); + assert_eq!(count.platforms[live::platform()], 2); +} diff --git a/crates/horizon-telemetry/Cargo.toml b/crates/horizon-telemetry/Cargo.toml index ea54b079d..913eac453 100644 --- a/crates/horizon-telemetry/Cargo.toml +++ b/crates/horizon-telemetry/Cargo.toml @@ -13,12 +13,14 @@ serde_json = "1" ruzstd = "0.9" # The random install ID and outbox file names. getrandom = "0.3" -# Sending, for the launcher; the ingest server builds without it. +# Sending, for the launcher (and the game's "I'm playing" ping); the ingest server builds without it. ureq = { version = "2", default-features = false, features = ["tls"], optional = true } [features] -default = ["upload"] +default = ["upload", "live"] upload = ["dep:ureq"] +# The "I'm playing" ping and reading the live count (`live`), for the game and the launcher. +live = ["dep:ureq"] [[test]] name = "consent_gating" diff --git a/crates/horizon-telemetry/src/lib.rs b/crates/horizon-telemetry/src/lib.rs index e903936db..8d2840a08 100644 --- a/crates/horizon-telemetry/src/lib.rs +++ b/crates/horizon-telemetry/src/lib.rs @@ -6,11 +6,15 @@ //! connection. Items are small summaries (percentiles and histogram buckets, never raw streams), //! dated to the day, with no account, world, server or address in them ([`scrub`]); they leave //! in zstd batches ([`batch`]). `docs/telemetry.md` lists every field. +//! +//! The one exception is [`live`]: the anonymous "I'm playing" ping the game sends while it +//! runs, for the live player count. It holds no install ID and nothing about the player. use std::path::{Path, PathBuf}; use std::time::{SystemTime, UNIX_EPOCH}; pub mod batch; pub mod consent; +pub mod live; pub mod outbox; pub mod payload; pub mod scrub; @@ -29,8 +33,9 @@ pub const SCHEMA: u32 = 1; /// and until the player answers nothing new is kept or sent. pub const CONSENT_VERSION: u32 = 1; -/// The ingest server, or none while there isn't one: then nothing is ever sent and the outbox -/// stays within its caps. `HORIZON_TELEMETRY_URL` overrides it (tests, a local server). +/// The ingest server, or none while there isn't one: then nothing is ever sent (no ping either) +/// and the outbox stays within its caps. `HORIZON_TELEMETRY_URL` overrides it (tests, a local +/// server). pub const DEFAULT_ENDPOINT: Option<&str> = None; /// Where items are sent, if anywhere. diff --git a/crates/horizon-telemetry/src/live.rs b/crates/horizon-telemetry/src/live.rs new file mode 100644 index 000000000..b1df57320 --- /dev/null +++ b/crates/horizon-telemetry/src/live.rs @@ -0,0 +1,215 @@ +//! The anonymous "I'm playing" ping, and the live count it feeds ("N playing now" in the +//! launcher). +//! +//! Unlike everything else in this crate it does not wait for consent: Ludovic decided +//! (2026-10-22) to count every player and tell them, in the launcher's first-run question, its +//! Settings and the privacy page. So a ping holds only what the count needs: a random session ID +//! made when the game starts and kept only in its memory (not the install ID), the Horizon +//! version, the platform and whether a LAN or play-with-friends world is open. The game sends +//! one every [`INTERVAL`]; the server counts a session while its last ping is under +//! [`WINDOW`] old, keeps nothing on disk and serves the counts rounded ([`round`]). +use std::collections::BTreeMap; +use std::time::Duration; + +use serde::{Deserialize, Serialize}; + +/// How often the game pings while it runs. +pub const INTERVAL: Duration = Duration::from_secs(60); + +/// How long a session counts after its last ping: two pings, so one lost on the way doesn't +/// drop it. +pub const WINDOW: Duration = Duration::from_secs(120); + +/// The platforms a ping may name; anything else is `other`. +pub const PLATFORMS: [&str; 4] = ["linux", "macos", "windows", "other"]; + +/// The largest ping body the server reads. +pub const MAX_PING_BYTES: usize = 512; + +const MAX_VERSION: usize = 40; + +/// One "I'm playing", as it is sent: `{"session":"…","version":"…","platform":"…","lan":false}`. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct Ping { + /// 128 random bits in hex, made when the game starts ([`session_id`]) and never written down. + pub session: String, + /// The release (`v2026.10.15`, `0.2.0`) or `dev 0.1.0` for a build from source. + pub version: String, + /// One of [`PLATFORMS`]. + pub platform: String, + /// A world is open to LAN or to friends over the internet. + pub lan: bool, +} + +impl Ping { + pub fn new(session: &str, version: &str, lan: bool) -> Self { + Self { + session: session.to_owned(), + version: version.to_owned(), + platform: platform().to_owned(), + lan, + } + } + + /// Why the server refuses this ping, if it does. + pub fn check(&self) -> Result<(), &'static str> { + if !crate::consent::is_install_id(&self.session) { + return Err("session: 32 lowercase hex digits"); + } + let version_ok = !self.version.is_empty() + && self.version.len() <= MAX_VERSION + && self + .version + .bytes() + .all(|b| b.is_ascii_alphanumeric() || b" .+-_".contains(&b)); + if !version_ok { + return Err("version: at most 40 letters, digits, spaces and .+-_"); + } + if !PLATFORMS.contains(&self.platform.as_str()) { + return Err("platform: linux, macos, windows or other"); + } + Ok(()) + } +} + +/// A new session ID: 128 random bits in hex. +pub fn session_id() -> String { + crate::random_hex(16) +} + +/// This machine's entry in [`PLATFORMS`]. +pub fn platform() -> &'static str { + match std::env::consts::OS { + os @ ("linux" | "macos" | "windows") => os, + _ => "other", + } +} + +/// The live count, as `GET /v1/live` serves it: every number already [`round`]ed. +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(default)] +pub struct Count { + /// Sessions that pinged within the window. + pub playing: u64, + /// Of those, the ones with a LAN or play-with-friends world open. + pub lan: u64, + /// Per version: the most played ones, the rest summed under `other`. + pub versions: BTreeMap, + /// Per platform. + pub platforms: BTreeMap, +} + +/// Rounds a count for showing: exact under 10, then to the nearest 5 under 100, the nearest 10 +/// under 1000 and the nearest 100 above. Small numbers stay true; large ones don't pretend to +/// a precision a two-minute window doesn't have. +pub fn round(n: u64) -> u64 { + let step = match n { + 0..10 => return n, + 10..100 => 5, + 100..1000 => 10, + _ => 100, + }; + (n + step / 2) / step * step +} + +/// Sends `ping` to `endpoint` (`https://host`, without a trailing slash). +#[cfg(feature = "live")] +pub fn send(endpoint: &str, ping: &Ping) -> Result<(), String> { + agent() + .post(&format!("{endpoint}/v1/live")) + .set("Content-Type", "application/json") + .send_string(&serde_json::to_string(ping).map_err(|e| e.to_string())?) + .map(drop) + .map_err(|e| e.to_string()) +} + +/// The live count at `endpoint`. +#[cfg(feature = "live")] +pub fn fetch(endpoint: &str) -> Result { + let body = agent() + .get(&format!("{endpoint}/v1/live")) + .call() + .map_err(|e| e.to_string())? + .into_string() + .map_err(|e| e.to_string())?; + serde_json::from_str(&body).map_err(|e| e.to_string()) +} + +#[cfg(feature = "live")] +fn agent() -> ureq::Agent { + ureq::AgentBuilder::new() + .timeout(Duration::from_secs(5)) + .build() +} + +#[cfg(test)] +mod tests { + use super::*; + + fn ping() -> Ping { + Ping::new(&session_id(), "v2026.10.15", false) + } + + #[test] + fn a_ping_holds_only_the_four_fields() { + let json = serde_json::to_value(Ping::new(&"a".repeat(32), "0.2.0", true)).unwrap(); + let mut keys: Vec<&str> = json + .as_object() + .unwrap() + .keys() + .map(|k| k.as_str()) + .collect(); + keys.sort_unstable(); + assert_eq!(keys, ["lan", "platform", "session", "version"]); + assert!(serde_json::to_string(&ping()).unwrap().len() < MAX_PING_BYTES); + } + + #[test] + fn session_ids_are_fresh_each_time() { + let (a, b) = (session_id(), session_id()); + assert_ne!(a, b); + assert!(ping().check().is_ok()); + } + + #[test] + fn bad_pings_are_refused() { + let bad = |f: fn(&mut Ping)| { + let mut p = ping(); + f(&mut p); + p.check().is_err() + }; + assert!(bad(|p| p.session = "ABC".into())); + assert!(bad(|p| p.session = "G".repeat(32))); + assert!(bad(|p| p.version = String::new())); + assert!(bad(|p| p.version = "1".repeat(41))); + assert!(bad(|p| p.version = "