From e39cecb33317830b74105095939e52e64ba91559 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9?= Date: Wed, 7 Oct 2026 21:31:45 +0500 Subject: [PATCH] Phase 10.1: Registry protocol 2 (structured listing), migration 2, v1 refused explicitly Listing (rates, modules, region, account provisioning), population with players and bots apart, level cap and ruleset derived from the hash-checked capabilities. Realm records from protocol 1 keep their id, key and creation time and are republished in place. Host publishes the real values: rates from the effective worldserver configuration, modules from the module catalog, players/bots from the realm database. Co-Authored-By: Claude Sonnet 5.5 --- crates/coa-core/src/population.rs | 8 + .../coa-core/src/portable/service/access.rs | 6 +- .../src/portable/service/live_service.rs | 1 + crates/coa-core/src/realm_registry/advert.rs | 154 ++++++- crates/coa-core/src/realm_registry/client.rs | 2 +- crates/coa-core/src/realm_registry/host.rs | 83 ++-- crates/coa-core/src/realm_registry/runtime.rs | 10 +- .../coa-core/src/realm_registry/settings.rs | 8 +- crates/coa-registry-proto/src/caps.rs | 5 + crates/coa-registry-proto/src/lib.rs | 27 +- crates/coa-registry-proto/src/sign.rs | 30 +- crates/coa-registry-proto/src/wire.rs | 384 ++++++++++++++---- services/coa-registry-e2e/tests/host_e2e.rs | 64 ++- services/coa-registry-e2e/tests/live_vps.rs | 47 ++- services/coa-registry/deploy/gate-restarts.sh | 6 +- .../migrations/0002_protocol_v2.sql | 17 + services/coa-registry/src/api.rs | 39 +- services/coa-registry/src/main.rs | 2 +- services/coa-registry/src/pg.rs | 122 ++++-- services/coa-registry/src/store.rs | 109 ++--- services/coa-registry/tests/api.rs | 204 +++++++--- services/coa-registry/tests/common/mod.rs | 53 ++- src-tauri/src/lib.rs | 4 +- src/i18n/locales/en.ts | 3 +- src/i18n/locales/ru.ts | 3 +- src/lib/registry.ts | 5 +- src/screens/PublishCard.tsx | 7 +- tools/registry-vector-check.py | 6 +- 28 files changed, 1012 insertions(+), 397 deletions(-) create mode 100644 services/coa-registry/migrations/0002_protocol_v2.sql diff --git a/crates/coa-core/src/population.rs b/crates/coa-core/src/population.rs index aca46da..a0bf6f9 100644 --- a/crates/coa-core/src/population.rs +++ b/crates/coa-core/src/population.rs @@ -40,6 +40,14 @@ fn parse_counts(out: &str) -> Result { pub fn query(root: &Path) -> Result { let prefix = bot_account_prefix(root).to_uppercase(); let db = Db::from_repack(root, Account::App)?; + query_with(&db, &prefix) +} + +/// The same counts through any database handle (a realm known by its descriptor); `prefix` is the bot accounts' name prefix, upper case. +pub fn query_with(db: &Db, prefix: &str) -> Result { + if prefix.is_empty() || !prefix.chars().all(|c| c.is_ascii_alphanumeric()) { + return Err(Error::Invalid("the bot account prefix is not a plain name".into())); + } let sql = format!( "SELECT (SELECT COUNT(*) FROM acore_characters.characters WHERE online=1), \ (SELECT COUNT(*) FROM acore_characters.characters c JOIN acore_auth.account a ON a.id=c.account WHERE c.online=1 AND UPPER(a.username) LIKE '{prefix}%'), \ diff --git a/crates/coa-core/src/portable/service/access.rs b/crates/coa-core/src/portable/service/access.rs index 8b1a181..e308b05 100644 --- a/crates/coa-core/src/portable/service/access.rs +++ b/crates/coa-core/src/portable/service/access.rs @@ -82,6 +82,9 @@ pub struct Descriptor { /// The database users of the game servers: a session of one of them means the realm runs. #[serde(default = "default_game_users")] pub game_server_users: Vec, + /// The server's folder (`Core/configs`, the module catalog's files), when this machine has it: the realm's rates and modules are read from there for the Registry. + #[serde(default)] + pub server_root: Option, } fn default_game_users() -> Vec { @@ -178,7 +181,7 @@ impl RealmAccess { address: d.address.clone(), realm_name: d.realm_name.clone(), install_id: None, - root: None, + root: d.server_root.as_deref().map(PathBuf::from), job_dir: PathBuf::from(&d.job_dir), data_dir: PathBuf::from(&d.data_dir), game_server_users: d.game_server_users.clone(), @@ -288,6 +291,7 @@ mod tests { job_dir: "C:/x/jobs".into(), data_dir: "C:/x/Data".into(), game_server_users: vec!["acore".into()], + server_root: None, } } diff --git a/crates/coa-core/src/portable/service/live_service.rs b/crates/coa-core/src/portable/service/live_service.rs index 61a26b9..4d3855b 100644 --- a/crates/coa-core/src/portable/service/live_service.rs +++ b/crates/coa-core/src/portable/service/live_service.rs @@ -34,6 +34,7 @@ fn descriptor(dir: &Path) -> std::path::PathBuf { job_dir: jobs.to_string_lossy().into(), data_dir: DATA.into(), game_server_users: vec!["acore".into()], + server_root: None, }; let file = dir.join("scratch-cap60.json"); std::fs::write(&file, serde_json::to_vec(&d).unwrap()).unwrap(); diff --git a/crates/coa-core/src/realm_registry/advert.rs b/crates/coa-core/src/realm_registry/advert.rs index 66304df..cd581dc 100644 --- a/crates/coa-core/src/realm_registry/advert.rs +++ b/crates/coa-core/src/realm_registry/advert.rs @@ -1,11 +1,19 @@ -//! What a local realm advertises, and how the Host learns it. +//! What a local realm advertises, and how the Host learns it. Every value comes from the real thing, and what cannot be known is `None`, never a guess: +//! +//! | advertised | source | +//! |---|---| +//! | level cap, ruleset | the running core's capabilities (`probe_capabilities`: `MaxPlayerLevel` over RA, the ruleset of the realm's database) | +//! | rates | the realm's effective worldserver configuration (`allsettings`: the active file, else the documented default), when the server folder is known | +//! | modules | the Manager's module catalog against the server's `Core/configs/modules` (installed and enabled), when the server folder is known | +//! | players / bots | the realm's database: online characters of other accounts / of the bot accounts (the bot subsystem's account prefix) | +//! | capacity | the worldserver's `PlayerLimit` when it is above 0 | use std::collections::{HashMap, HashSet}; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::sync::{Arc, Mutex}; use std::time::{Duration, Instant}; -use coa_registry_proto::AdvertisedCapabilities; +use coa_registry_proto::{AccountProvisioning, AdvertisedCapabilities, ModuleEntry, Population, Rates}; use crate::portable::capabilities::RealmCapabilities; use crate::portable::extension::ExtensionRegistry; @@ -27,8 +35,10 @@ pub struct LocalAdvert { pub running: bool, /// What the realm advertises; the last known one while it is stopped, `None` before it was ever seen running. pub capabilities: Option, - pub player_count: Option, - pub player_capacity: Option, + pub population: Population, + pub rates: Rates, + pub modules: Vec, + pub account_provisioning: AccountProvisioning, pub manager_version: String, } @@ -37,6 +47,51 @@ pub trait AdvertSource: Send + Sync + 'static { fn current(&self, local_id: &str) -> Option; } +/// The configuration keys behind the advertised rates (the same ones the Manager's presets set). +fn rate(items: &[crate::allsettings::Item], key: &str) -> Option { + let v: f64 = items.iter().find(|i| i.key == key)?.value.trim().trim_matches('"').parse().ok()?; + (v.is_finite() && (0.0..=coa_registry_proto::MAX_RATE).contains(&v)).then_some(v) +} + +/// The realm's effective rates; every one the configuration does not give is `None`. +pub fn rates_of(root: &Path) -> Rates { + let Ok(items) = crate::allsettings::list(root) else { return Rates::default() }; + Rates { + xp_kill: rate(&items, "Rate.XP.Kill"), + xp_quest: rate(&items, "Rate.XP.Quest"), + xp_explore: rate(&items, "Rate.XP.Explore"), + loot: rate(&items, "Rate.Drop.Item.Normal"), + money: rate(&items, "Rate.Drop.Money"), + reputation: rate(&items, "Rate.Reputation.Gain"), + honor: rate(&items, "Rate.Honor"), + } +} + +/// `PlayerLimit` when the realm has one. +pub fn capacity_of(root: &Path) -> Option { + let items = crate::allsettings::list(root).ok()?; + let n: u32 = items.iter().find(|i| i.key == "PlayerLimit")?.value.trim().parse().ok()?; + (1..=coa_registry_proto::MAX_PLAYER_NUMBER).contains(&n).then_some(n) +} + +/// A module version fit for the Registry: its first token when that is a plain version, else nothing. +fn plain_version(v: Option) -> Option { + let first = v?.split_whitespace().next()?.to_string(); + coa_registry_proto::validate_version(&first).ok().map(|_| first) +} + +/// The modules this server build contains, with whether each is on. Hidden ones (settings offered elsewhere) are not listed. +pub fn modules_of(root: &Path) -> Vec { + let mut out: Vec = crate::modules::list(root) + .into_iter() + .filter(|m| m.installed && !m.hidden && coa_registry_proto::validate_module_id(&m.id).is_ok()) + .map(|m| ModuleEntry { id: m.id, version: plain_version(m.version), enabled: m.enabled }) + .collect(); + out.sort_by(|a, b| a.id.cmp(&b.id)); + out.truncate(coa_registry_proto::MAX_MODULES); + out +} + struct Cached { at: Instant, advert: LocalAdvert, @@ -47,6 +102,8 @@ struct Inner { descriptors: PathBuf, installs: Box Vec<(String, PathBuf)> + Send + Sync>, registry: Arc, + /// Can this Manager create game accounts for joining players (the control service runs)? + provisioning: Box bool + Send + Sync>, cache: Mutex>, inflight: Mutex>, fresh: Duration, @@ -63,11 +120,17 @@ pub struct LocalRealmsSource { impl LocalRealmsSource { pub fn new(descriptors: impl Into, installs: impl Fn() -> Vec<(String, PathBuf)> + Send + Sync + 'static) -> Self { + Self::with_provisioning(descriptors, installs, || false) + } + + /// `provisioning` says whether this Manager is serving account creation: a realm only advertises automatic accounts when that is true. + pub fn with_provisioning(descriptors: impl Into, installs: impl Fn() -> Vec<(String, PathBuf)> + Send + Sync + 'static, provisioning: impl Fn() -> bool + Send + Sync + 'static) -> Self { Self { inner: Arc::new(Inner { descriptors: descriptors.into(), installs: Box::new(installs), registry: Arc::new(ExtensionRegistry::new()), + provisioning: Box::new(provisioning), cache: Mutex::new(HashMap::new()), inflight: Mutex::new(HashSet::new()), fresh: Duration::from_secs(10), @@ -89,12 +152,35 @@ impl Inner { fn probe(&self, local_id: &str) { let previous = self.cache.lock().ok().and_then(|c| c.get(local_id).map(|c| (c.advert.clone(), c.caps_at))); let Some(access) = self.access(local_id) else { return }; - let mut advert = LocalAdvert { running: false, capabilities: previous.as_ref().and_then(|(a, _)| a.capabilities.clone()), player_count: None, player_capacity: None, manager_version: crate::MANAGER_VERSION.to_string() }; + let automatic = (self.provisioning)(); + let mut advert = LocalAdvert { + running: false, + capabilities: previous.as_ref().and_then(|(a, _)| a.capabilities.clone()), + population: Population::default(), + rates: Rates::default(), + modules: Vec::new(), + account_provisioning: AccountProvisioning { automatic, existing_only: !automatic }, + manager_version: crate::MANAGER_VERSION.to_string(), + }; + // configuration-derived values do not need the realm to be running + let mut prefix = "COABOTHOST".to_string(); + if let Some(root) = access.root.as_deref().filter(|r| r.join("Core/configs").is_dir()) { + advert.rates = rates_of(root); + advert.modules = modules_of(root); + advert.population.capacity = capacity_of(root); + prefix = crate::population::bot_account_prefix(root).to_uppercase(); + } let mut caps_at = previous.as_ref().and_then(|(_, t)| *t); if access.db_reachable() { if let (Ok(db), Ok(mut ra)) = (access.db(), access.ra()) { advert.running = true; - advert.player_count = ra.run("server info").ok().and_then(|text| connected_players(&text)); + if let Ok(p) = crate::population::query_with(&db, &prefix) { + advert.population.players = p.players_online; + advert.population.bots = p.bots_online; + } + if let Some(c) = advert.population.capacity { + advert.population.players = advert.population.players.min(c); + } if caps_at.is_none_or(|t| t.elapsed() >= self.caps_every) || advert.capabilities.is_none() { let data = access.data_dir.is_dir().then_some(access.data_dir.as_path()); if let Ok(caps) = probe_capabilities(&db, data, Some(&mut ra), &self.registry) { @@ -112,12 +198,6 @@ impl Inner { } } -/// `Connected players: 3. Characters in world: 3.` -pub fn connected_players(server_info: &str) -> Option { - let rest = server_info.split("Connected players:").nth(1)?; - rest.trim_start().split(|c: char| !c.is_ascii_digit()).next()?.parse().ok() -} - impl AdvertSource for LocalRealmsSource { fn current(&self, local_id: &str) -> Option { let (answer, stale) = match self.inner.cache.lock().ok()?.get(local_id) { @@ -145,14 +225,6 @@ impl AdvertSource for LocalRealmsSource { mod tests { use super::*; - #[test] - fn the_player_count_is_read_from_the_server_info_report() { - assert_eq!(connected_players("Connected players: 3. Characters in world: 3."), Some(3)); - assert_eq!(connected_players("x\nConnected players: 0.\n"), Some(0)); - assert_eq!(connected_players("nothing"), None); - assert_eq!(connected_players("Connected players: many"), None); - } - #[test] fn a_real_profile_is_advertised_without_changing_its_hash() { use crate::portable::capabilities::*; @@ -175,5 +247,45 @@ mod tests { let advert = advertise(&caps).unwrap(); assert_eq!(advert.content_profile_hash, caps.content_profile_hash, "the Registry sees the hash the Manager computed"); assert_eq!(serde_json::to_value(&advert).unwrap(), serde_json::to_value(&caps).unwrap(), "the same JSON, field for field"); + assert_eq!(advert.level_cap(), Some(60)); + } + + fn server(root: &Path, conf: &str, dist: &str) { + let configs = root.join("Core/configs"); + std::fs::create_dir_all(configs.join("modules")).unwrap(); + std::fs::write(configs.join("worldserver.conf"), conf).unwrap(); + std::fs::write(configs.join("worldserver.conf.dist"), dist).unwrap(); + } + + const DIST: &str = "[worldserver]\n\n#\n# Rate.XP.Kill\n# Default: 1\n#\n\nRate.XP.Kill = 1\n\n#\n# Rate.XP.Quest\n# Default: 1\n#\n\nRate.XP.Quest = 1\n\n#\n# Rate.Drop.Money\n# Default: 1\n#\n\nRate.Drop.Money = 1\n\n#\n# PlayerLimit\n# Default: 0\n#\n\nPlayerLimit = 0\n"; + + #[test] + fn rates_are_the_effective_configuration_and_unknown_ones_are_null() { + let dir = tempfile::tempdir().unwrap(); + server(dir.path(), "[worldserver]\nRate.XP.Kill = 2.5\nRate.Drop.Money = \"3\"\nPlayerLimit = 120\n", DIST); + let r = rates_of(dir.path()); + assert_eq!((r.xp_kill, r.xp_quest, r.money), (Some(2.5), Some(1.0), Some(3.0)), "the file, else the documented default"); + assert_eq!((r.loot, r.reputation, r.honor, r.xp_explore), (None, None, None, None), "a rate the server's configuration does not document is not guessed"); + assert_eq!(capacity_of(dir.path()), Some(120)); + std::fs::write(dir.path().join("Core/configs/worldserver.conf"), "[worldserver]\nRate.XP.Kill = banana\nPlayerLimit = 0\n").unwrap(); + let r = rates_of(dir.path()); + assert_eq!(r.xp_kill, None, "an unreadable value is unknown, not 1"); + assert_eq!(capacity_of(dir.path()), None, "PlayerLimit 0 is no limit"); + assert_eq!(rates_of(&dir.path().join("nothing")), Rates::default()); + } + + #[test] + fn module_ids_of_the_catalog_are_all_valid_registry_ids_and_versions_are_sanitised() { + for e in crate::modules::catalog() { + assert!(coa_registry_proto::validate_module_id(&e.id).is_ok(), "{}", e.id); + } + assert_eq!(plain_version(Some("v1.4.2 · 0123abcd".into())), Some("v1.4.2".into())); + assert_eq!(plain_version(Some("· abc".into())), None); + assert_eq!(plain_version(Some("".into())), None); + assert_eq!(plain_version(None), None); + let dir = tempfile::tempdir().unwrap(); + server(dir.path(), "[worldserver]\n", DIST); + let m = modules_of(dir.path()); + assert!(m.is_empty(), "a server folder without module files lists no module: {m:?}"); } } diff --git a/crates/coa-core/src/realm_registry/client.rs b/crates/coa-core/src/realm_registry/client.rs index 9de2a87..9c0ddcf 100644 --- a/crates/coa-core/src/realm_registry/client.rs +++ b/crates/coa-core/src/realm_registry/client.rs @@ -110,7 +110,7 @@ impl RegistryClient { } /// The Registry's own record of this realm (the authenticated read). - pub fn self_info(&self, key: &SigningKey, realm: &RealmId, ts: i64) -> Result { + pub fn self_info(&self, key: &SigningKey, realm: &RealmId, ts: i64) -> Result { self.send("GET", &path_self(realm), Vec::new(), key, realm, ts) } } diff --git a/crates/coa-core/src/realm_registry/host.rs b/crates/coa-core/src/realm_registry/host.rs index 977be0a..2045149 100644 --- a/crates/coa-core/src/realm_registry/host.rs +++ b/crates/coa-core/src/realm_registry/host.rs @@ -92,6 +92,7 @@ pub struct RealmPublishStatus { pub display_name: String, pub description: String, pub language: String, + pub region: Option, pub metadata_revision: Option, pub last_ok_unix: Option, pub last_error: Option, @@ -120,8 +121,8 @@ struct Publisher { failures: u32, clock_failures: u32, unpublish_attempts: u32, - acked_hash: Option, - sent_meta: Option<(String, String, String)>, + acked_caps: Option, + acked_listing: Option, revision: Option, last_ok: Option, last_error: Option, @@ -141,8 +142,8 @@ impl Publisher { failures: 0, clock_failures: 0, unpublish_attempts: 0, - acked_hash: None, - sent_meta: None, + acked_caps: None, + acked_listing: None, revision: None, last_ok: None, last_error: None, @@ -154,8 +155,8 @@ impl Publisher { self.phase = Phase::Register; self.failures = 0; self.clock_failures = 0; - self.acked_hash = None; - self.sent_meta = None; + self.acked_caps = None; + self.acked_listing = None; self.last_error = None; self.state = PublishState::Starting; self.due = Some(now); @@ -238,7 +239,7 @@ impl RegistryHost { } /// Start (or restart) publishing a local realm. The first time creates its identity; later times keep it. - pub fn publish(&mut self, local_id: &str, display_name: &str, description: &str, language: &str, now: Instant) -> Result<()> { + pub fn publish(&mut self, local_id: &str, display_name: &str, description: &str, language: &str, region: Option<&str>, now: Instant) -> Result<()> { if self.client.is_none() { return Err(Error::Invalid("set the Registry address first".into())); } @@ -246,6 +247,7 @@ impl RegistryHost { cfg.display_name = display_name.trim().to_string(); cfg.description = description.trim().to_string(); cfg.language = language.trim().to_string(); + cfg.region = region.map(str::trim).filter(|r| !r.is_empty()).map(str::to_string); cfg.enabled = true; cfg.validate()?; // an identity whose key is gone from this machine cannot sign for itself any more: publishing again makes a new identity, never a guess at the old key @@ -324,6 +326,7 @@ impl RegistryHost { display_name: p.cfg.display_name.clone(), description: p.cfg.description.clone(), language: p.cfg.language.clone(), + region: p.cfg.region.clone(), metadata_revision: p.revision, last_ok_unix: p.last_ok, last_error: p.last_error.clone(), @@ -339,7 +342,7 @@ impl RegistryHost { } /// What the Registry itself says about a published realm (the authenticated read; useful for support and for tests). - pub fn registry_record(&mut self, local_id: &str) -> std::result::Result { + pub fn registry_record(&mut self, local_id: &str) -> std::result::Result { let Some(mut p) = self.publishers.remove(local_id) else { return Err(ClientError::Protocol("the realm is not known here".into())) }; let out = match (self.client.as_ref(), p.realm, p.key.clone()) { (Some(client), Some(realm), Some(key)) => { @@ -394,31 +397,42 @@ impl RegistryHost { } } + /// What a player reads about this realm: the owner's words, and everything else as the realm itself reports it. + fn listing_of(&self, p: &Publisher, advert: &LocalAdvert) -> Listing { + Listing { + display_name: p.cfg.display_name.clone(), + description: p.cfg.description.clone(), + language: p.cfg.language.clone(), + region: p.cfg.region.clone(), + rates: advert.rates.clone(), + modules: advert.modules.clone(), + account_provisioning: advert.account_provisioning, + manager_version: advert.manager_version.clone(), + } + } + fn register(&self, client: &RegistryClient, realm: &RealmId, key: &SigningKey, p: &mut Publisher, advert: &LocalAdvert, now: Instant) -> std::result::Result<(), ClientError> { let Some(caps) = advert.capabilities.clone().filter(|_| advert.running) else { p.state = if advert.running { PublishState::WaitingForRealm } else { PublishState::RealmStopped }; p.due = Some(now + self.timing.idle_poll); return Ok(()); }; + let listing = self.listing_of(p, advert); let req = RegisterRequest { protocol_version: REGISTRY_PROTOCOL_VERSION, realm_id: *realm, public_key: encode_public_key(&key.verifying_key()), - display_name: p.cfg.display_name.clone(), - description: p.cfg.description.clone(), - language: p.cfg.language.clone(), - ruleset: caps.content.ruleset, - manager_version: advert.manager_version.clone(), + listing_hash: listing.hash(), + listing, capabilities_hash: caps.advert_hash(), capabilities: caps, - player_count: advert.player_count, - player_capacity: advert.player_capacity, + population: advert.population, }; let ts = self.next_ts(p); let resp = client.register(key, &req, ts)?; p.phase = Phase::Beat; - p.acked_hash = Some(resp.capabilities_hash); - p.sent_meta = Some((req.display_name, req.description, req.language)); + p.acked_caps = Some(resp.capabilities_hash); + p.acked_listing = Some(resp.listing_hash); self.ok(p, resp.metadata_revision, now); Ok(()) } @@ -429,32 +443,24 @@ impl RegistryHost { p.due = Some(now + self.timing.idle_poll); return Ok(()); }; - let hash = caps.advert_hash(); - let meta = (p.cfg.display_name.clone(), p.cfg.description.clone(), p.cfg.language.clone()); - let meta_changed = p.sent_meta.as_ref() != Some(&meta); + let listing = self.listing_of(p, advert); + let (listing_hash, caps_hash) = (listing.hash(), caps.advert_hash()); let req = HeartbeatRequest { protocol_version: REGISTRY_PROTOCOL_VERSION, - capabilities: (p.acked_hash.as_deref() != Some(hash.as_str())).then_some(caps), - capabilities_hash: hash, - manager_version: Some(advert.manager_version.clone()), - player_count: advert.player_count, - player_capacity: advert.player_capacity, - display_name: meta_changed.then(|| meta.0.clone()), - description: meta_changed.then(|| meta.1.clone()), - language: meta_changed.then(|| meta.2.clone()), + listing: (p.acked_listing.as_deref() != Some(listing_hash.as_str())).then_some(listing), + listing_hash, + capabilities: (p.acked_caps.as_deref() != Some(caps_hash.as_str())).then_some(caps), + capabilities_hash: caps_hash, + population: advert.population, }; let ts = self.next_ts(p); let resp = client.heartbeat(key, realm, &req, ts)?; - if meta_changed { - p.sent_meta = Some(meta); - } - if resp.resend_capabilities { - p.acked_hash = None; - self.ok(p, resp.metadata_revision, now); + // what the Registry holds now decides what is sent next; a part it asked for is sent again at once + p.acked_listing = if resp.resend_listing { None } else { Some(resp.listing_hash) }; + p.acked_caps = if resp.resend_capabilities { None } else { Some(resp.capabilities_hash) }; + self.ok(p, resp.metadata_revision, now); + if resp.resend_listing || resp.resend_capabilities { p.due = Some(now + Duration::from_secs(1)); - } else { - p.acked_hash = Some(resp.capabilities_hash); - self.ok(p, resp.metadata_revision, now); } Ok(()) } @@ -474,7 +480,8 @@ impl RegistryHost { match &e { ClientError::Rejected { code: ErrorCode::UnknownRealm | ErrorCode::NotPublished, .. } => { p.phase = Phase::Register; - p.acked_hash = None; + p.acked_caps = None; + p.acked_listing = None; p.failures += 1; p.state = PublishState::Retrying; p.due = Some(now + if p.failures <= 1 { Duration::from_secs(1) } else { backoff(p.failures, self.timing.backoff_base, self.timing.backoff_cap, unit()) }); diff --git a/crates/coa-core/src/realm_registry/runtime.rs b/crates/coa-core/src/realm_registry/runtime.rs index 229af3b..689c12b 100644 --- a/crates/coa-core/src/realm_registry/runtime.rs +++ b/crates/coa-core/src/realm_registry/runtime.rs @@ -14,7 +14,7 @@ use crate::{Error, Result}; enum Cmd { SetUrl(Option, Sender>), - Publish { local_id: String, name: String, description: String, language: String, reply: Sender> }, + Publish { local_id: String, name: String, description: String, language: String, region: Option, reply: Sender> }, Unpublish(String, Sender>), Retry(String), Shutdown, @@ -44,8 +44,8 @@ impl RegistryRuntime { Ok(Cmd::SetUrl(url, reply)) => { let _ = reply.send(host.set_url(url.as_deref(), Instant::now())); } - Ok(Cmd::Publish { local_id, name, description, language, reply }) => { - let _ = reply.send(host.publish(&local_id, &name, &description, &language, Instant::now())); + Ok(Cmd::Publish { local_id, name, description, language, region, reply }) => { + let _ = reply.send(host.publish(&local_id, &name, &description, &language, region.as_deref(), Instant::now())); } Ok(Cmd::Unpublish(local_id, reply)) => { let _ = reply.send(host.unpublish(&local_id, Instant::now())); @@ -77,8 +77,8 @@ impl RegistryRuntime { self.ask(|reply| Cmd::SetUrl(url, reply)) } - pub fn publish(&self, local_id: &str, name: &str, description: &str, language: &str) -> Result<()> { - self.ask(|reply| Cmd::Publish { local_id: local_id.into(), name: name.into(), description: description.into(), language: language.into(), reply }) + pub fn publish(&self, local_id: &str, name: &str, description: &str, language: &str, region: Option<&str>) -> Result<()> { + self.ask(|reply| Cmd::Publish { local_id: local_id.into(), name: name.into(), description: description.into(), language: language.into(), region: region.map(str::to_string), reply }) } pub fn unpublish(&self, local_id: &str) -> Result<()> { diff --git a/crates/coa-core/src/realm_registry/settings.rs b/crates/coa-core/src/realm_registry/settings.rs index 3cf289f..a74267e 100644 --- a/crates/coa-core/src/realm_registry/settings.rs +++ b/crates/coa-core/src/realm_registry/settings.rs @@ -19,10 +19,16 @@ pub struct PublishConfig { pub display_name: String, pub description: String, pub language: String, + /// A coarse region tag (`EU`, `NA`, `RU`…), or none. + #[serde(default)] + pub region: Option, } impl PublishConfig { pub fn validate(&self) -> Result<()> { + if let Some(r) = &self.region { + coa_registry_proto::validate_region(r).map_err(|e| Error::Invalid(e.to_string()))?; + } validate_display_name(&self.display_name).map_err(|e| Error::Invalid(e.to_string()))?; validate_description(&self.description).map_err(|e| Error::Invalid(e.to_string()))?; validate_language(&self.language).map_err(|e| Error::Invalid(e.to_string()))?; @@ -100,7 +106,7 @@ mod tests { let dir = tempfile::tempdir().unwrap(); assert_eq!(load(dir.path()).unwrap(), RegistrySettings::default()); let mut s = RegistrySettings { url: Some("http://x".into()), ..Default::default() }; - s.realms.insert("srv-1".into(), PublishConfig { realm_id: Some(RealmId::new()), enabled: true, display_name: "A".into(), description: String::new(), language: "en".into() }); + s.realms.insert("srv-1".into(), PublishConfig { realm_id: Some(RealmId::new()), enabled: true, display_name: "A".into(), description: String::new(), language: "en".into(), region: None }); save(dir.path(), &s).unwrap(); assert_eq!(load(dir.path()).unwrap(), s); let text = std::fs::read_to_string(file(dir.path())).unwrap(); diff --git a/crates/coa-registry-proto/src/caps.rs b/crates/coa-registry-proto/src/caps.rs index ce6ec91..e6d1d28 100644 --- a/crates/coa-registry-proto/src/caps.rs +++ b/crates/coa-registry-proto/src/caps.rs @@ -221,6 +221,11 @@ impl AdvertisedCapabilities { Ok(()) } + /// The running core's `MaxPlayerLevel`, when the realm has reported its progression. + pub fn level_cap(&self) -> Option { + self.progression.as_ref().map(|p| p.max_player_level) + } + /// The hash that identifies this advertisement (the content hash plus the progression signature: both change what a Host can take). pub fn advert_hash(&self) -> String { let mut h = Sha256::new(); diff --git a/crates/coa-registry-proto/src/lib.rs b/crates/coa-registry-proto/src/lib.rs index 83cc44c..e47f556 100644 --- a/crates/coa-registry-proto/src/lib.rs +++ b/crates/coa-registry-proto/src/lib.rs @@ -1,4 +1,5 @@ -//! The wire protocol of the CoA realm Registry, version 1 (Phase 10). +//! The wire protocol of the CoA realm Registry, version 2 (Phase 10.1; version 1 was the unstructured Phase-10 form and is refused, see +//! `docs/REGISTRY_PROTOCOL.md`). //! //! The Registry is discovery and presence infrastructure only. Everything in this crate is public, host-advertised metadata: //! it never carries a credential, a character, a snapshot or a session payload. The protocol version is the Registry's own and @@ -17,7 +18,7 @@ pub use sign::{RealmId, SignedHeaders}; pub use wire::*; /// The only protocol version this crate speaks. -pub const REGISTRY_PROTOCOL_VERSION: u32 = 1; +pub const REGISTRY_PROTOCOL_VERSION: u32 = 2; /// The largest request body any endpoint accepts. pub const MAX_REQUEST_BYTES: usize = 64 * 1024; @@ -27,10 +28,16 @@ pub const MAX_DISPLAY_NAME_CHARS: usize = 80; pub const MAX_DESCRIPTION_BYTES: usize = 1024; /// The longest language tag. pub const MAX_LANGUAGE_BYTES: usize = 16; +/// The longest region tag. +pub const MAX_REGION_BYTES: usize = 16; /// The longest Manager version string. pub const MAX_VERSION_BYTES: usize = 32; -/// Players or capacity above this are refused as nonsense. +/// Players, bots or capacity above this are refused as nonsense. pub const MAX_PLAYER_NUMBER: u32 = 100_000; +/// The most modules one realm lists. +pub const MAX_MODULES: usize = 64; +/// A rate above this is refused. +pub const MAX_RATE: f64 = 1000.0; /// How often a Host sends a heartbeat. pub const HEARTBEAT_INTERVAL_SECS: u64 = 30; @@ -39,9 +46,12 @@ pub const ONLINE_TTL_SECS: u64 = 120; /// How far a request's timestamp may be from the Registry's clock. pub const MAX_CLOCK_SKEW_SECS: i64 = 300; -pub const PATH_PREFIX: &str = "/registry/v1"; -pub const PATH_REGISTER: &str = "/registry/v1/realms/register"; -pub const PATH_HEALTHZ: &str = "/registry/v1/healthz"; +pub const PATH_PREFIX: &str = "/registry/v2"; +/// Version 1 is refused with an explicit error; this prefix is only used to recognise it. +pub const PATH_PREFIX_V1: &str = "/registry/v1"; +pub const PATH_REGISTER: &str = "/registry/v2/realms/register"; +pub const PATH_HEALTHZ: &str = "/registry/v2/healthz"; +pub const PATH_LIST: &str = "/registry/v2/realms"; pub fn path_heartbeat(realm: &RealmId) -> String { format!("{PATH_PREFIX}/realms/{realm}/heartbeat") @@ -49,7 +59,12 @@ pub fn path_heartbeat(realm: &RealmId) -> String { pub fn path_unpublish(realm: &RealmId) -> String { format!("{PATH_PREFIX}/realms/{realm}/unpublish") } +/// The authenticated (signed) read of a realm's own record, whether or not it is published. pub fn path_self(realm: &RealmId) -> String { + format!("{PATH_PREFIX}/realms/{realm}/self") +} +/// The public record of a published realm. +pub fn path_detail(realm: &RealmId) -> String { format!("{PATH_PREFIX}/realms/{realm}") } diff --git a/crates/coa-registry-proto/src/sign.rs b/crates/coa-registry-proto/src/sign.rs index 5cd5a35..f0aad80 100644 --- a/crates/coa-registry-proto/src/sign.rs +++ b/crates/coa-registry-proto/src/sign.rs @@ -1,7 +1,7 @@ //! Signed requests. One canonical input, one algorithm (Ed25519); see `docs/REGISTRY_PROTOCOL.md` for the format and test vectors. //! //! ```text -//! coa-registry-sig-v1 +//! coa-registry-sig-v2 //! decimal //! upper case //! exactly as sent, no query @@ -20,7 +20,7 @@ use uuid::Uuid; use crate::{invalid, Result, REGISTRY_PROTOCOL_VERSION}; -pub const SIGNATURE_DOMAIN: &str = "coa-registry-sig-v1"; +pub const SIGNATURE_DOMAIN: &str = "coa-registry-sig-v2"; pub const HEADER_VERSION: &str = "x-coa-registry-version"; pub const HEADER_REALM: &str = "x-coa-realm"; @@ -185,43 +185,37 @@ mod tests { let realm = RealmId::parse(REALM).unwrap(); let key = key(); assert_eq!(encode_public_key(&key.verifying_key()), VECTOR_PUBLIC_KEY); - let body = br#"{"protocol_version":1}"#; - let input = signature_input("post", "/registry/v1/realms/018f2d9e-5c3a-7b21-8c4d-0e5f6a7b8c9d/heartbeat", &realm, 1_790_000_000, body); + let body = br#"{"protocol_version":2}"#; + let input = signature_input("post", "/registry/v2/realms/018f2d9e-5c3a-7b21-8c4d-0e5f6a7b8c9d/heartbeat", &realm, 1_790_000_000, body); assert_eq!(String::from_utf8(input).unwrap(), VECTOR_INPUT); - let headers = sign_request(&key, "POST", "/registry/v1/realms/018f2d9e-5c3a-7b21-8c4d-0e5f6a7b8c9d/heartbeat", &realm, 1_790_000_000, body); + let headers = sign_request(&key, "POST", "/registry/v2/realms/018f2d9e-5c3a-7b21-8c4d-0e5f6a7b8c9d/heartbeat", &realm, 1_790_000_000, body); assert_eq!(B64.encode(headers.signature), VECTOR_SIGNATURE); - assert!(verify_request(&key.verifying_key(), &headers, "POST", "/registry/v1/realms/018f2d9e-5c3a-7b21-8c4d-0e5f6a7b8c9d/heartbeat", body)); + assert!(verify_request(&key.verifying_key(), &headers, "POST", "/registry/v2/realms/018f2d9e-5c3a-7b21-8c4d-0e5f6a7b8c9d/heartbeat", body)); } /// The second documented vector: a GET has the empty body, whose SHA-256 is `e3b0c442...`. #[test] fn the_documented_vector_of_a_read() { let realm = RealmId::parse(REALM).unwrap(); - let headers = sign_request(&key(), "GET", "/registry/v1/realms/018f2d9e-5c3a-7b21-8c4d-0e5f6a7b8c9d", &realm, 1_790_000_030, b""); - assert_eq!(B64.encode(headers.signature), "38nHMS6yvl8yrTbLmG1w5LiLVkvTaiRdQPrgT2GSvtR8jW_W0z50IRxD_kInBYP5Yl1Qwwm-r6BVDdTR5ts-BQ"); + let headers = sign_request(&key(), "GET", "/registry/v2/realms/018f2d9e-5c3a-7b21-8c4d-0e5f6a7b8c9d/self", &realm, 1_790_000_030, b""); + assert_eq!(B64.encode(headers.signature), "yqd_TFTooZcWb0GhLw3Tl9FwnBWCHo3Q2AYpoNn4CCJljENOgW9WaNlODoInEVglrCNKJmoYuahPyZfcEJk_AA"); } const VECTOR_PUBLIC_KEY: &str = "6kpsY-KcUgq-9VB7Ey7F-ZVHdq6-vnuSQh7qaRRG0iw"; - const VECTOR_INPUT: &str = "coa-registry-sig-v1 -1 -POST -/registry/v1/realms/018f2d9e-5c3a-7b21-8c4d-0e5f6a7b8c9d/heartbeat -018f2d9e-5c3a-7b21-8c4d-0e5f6a7b8c9d -1790000000 -00aa4c2c857995eb8e19cb0fade07e4b49aac774e37a99c37a0c9f549204d9de"; - const VECTOR_SIGNATURE: &str = "1ys2C7RVjhEtTl0bVrdowpuPELwOnhuiYZKp88dAWdcA3cezP9QuI903OJi2N2mFA9kifblD_fvdT2B_8F5_Dw"; + const VECTOR_INPUT: &str = "coa-registry-sig-v2\n2\nPOST\n/registry/v2/realms/018f2d9e-5c3a-7b21-8c4d-0e5f6a7b8c9d/heartbeat\n018f2d9e-5c3a-7b21-8c4d-0e5f6a7b8c9d\n1790000000\ne68cb099ab8a3557572bff7e669270ac75fe321c1920b12a2aa7a535d1d16155"; + const VECTOR_SIGNATURE: &str = "MjXmYOIM1Bap5C5XNPspElOTNIAnKBN4pTfvhF6r29zq7A0elzXi7Qk8D3FyfL1CtVmpliUAr3MY1TGrGPyqAQ"; #[test] fn every_part_of_the_input_is_covered() { let realm = RealmId::parse(REALM).unwrap(); let other = RealmId::parse("018f2d9e-5c3a-7b21-8c4d-0e5f6a7b8c9e").unwrap(); let key = key(); - let path = "/registry/v1/realms/x"; + let path = "/registry/v2/realms/x"; let h = sign_request(&key, "POST", path, &realm, 100, b"body"); let public = key.verifying_key(); assert!(verify_request(&public, &h, "POST", path, b"body")); assert!(!verify_request(&public, &h, "POST", path, b"bodY"), "the body"); - assert!(!verify_request(&public, &h, "POST", "/registry/v1/realms/y", b"body"), "the path"); + assert!(!verify_request(&public, &h, "POST", "/registry/v2/realms/y", b"body"), "the path"); assert!(!verify_request(&public, &h, "GET", path, b"body"), "the method"); assert!(!verify_request(&public, &SignedHeaders { realm: other, ..h.clone() }, "POST", path, b"body"), "the realm"); assert!(!verify_request(&public, &SignedHeaders { timestamp: 101, ..h.clone() }, "POST", path, b"body"), "the timestamp"); diff --git a/crates/coa-registry-proto/src/wire.rs b/crates/coa-registry-proto/src/wire.rs index ec1c94a..bf797a0 100644 --- a/crates/coa-registry-proto/src/wire.rs +++ b/crates/coa-registry-proto/src/wire.rs @@ -1,10 +1,16 @@ -//! Request and response bodies of Registry protocol 1. Every request type refuses unknown fields and validates its own bounds. +//! Request and response bodies of Registry protocol 2. Every request type refuses unknown fields and validates its own bounds. +//! +//! A realm's advertisement has three parts with three lifetimes: the **listing** (what a player reads: name, description, language, region, rates, modules, +//! how accounts are made; changes rarely), the **capabilities** (what the realm can take, hash-checked; changes rarely) and the **population** (players and +//! bots; changes every heartbeat). The level cap and the ruleset are not claimed separately: the Registry derives them from the hash-checked capabilities, +//! which come from the running core. use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; use crate::caps::{AdvertisedCapabilities, Ruleset}; use crate::sign::RealmId; -use crate::{invalid, Result, MAX_DESCRIPTION_BYTES, MAX_DISPLAY_NAME_CHARS, MAX_LANGUAGE_BYTES, MAX_PLAYER_NUMBER, MAX_VERSION_BYTES, REGISTRY_PROTOCOL_VERSION}; +use crate::{invalid, Result, MAX_DESCRIPTION_BYTES, MAX_DISPLAY_NAME_CHARS, MAX_LANGUAGE_BYTES, MAX_MODULES, MAX_PLAYER_NUMBER, MAX_RATE, MAX_REGION_BYTES, MAX_VERSION_BYTES, REGISTRY_PROTOCOL_VERSION}; fn bidi_or_control(c: char, allow_newline: bool) -> bool { (c.is_control() && !(allow_newline && c == '\n')) || matches!(c, '\u{200E}' | '\u{200F}' | '\u{202A}'..='\u{202E}' | '\u{2066}'..='\u{2069}' | '\u{FEFF}' | '\u{2028}' | '\u{2029}') @@ -41,21 +47,18 @@ pub fn validate_language(s: &str) -> Result<()> { Ok(()) } -pub fn validate_version(s: &str) -> Result<()> { - if s.is_empty() || s.len() > MAX_VERSION_BYTES || !s.bytes().all(|b| b.is_ascii_alphanumeric() || matches!(b, b'.' | b'-' | b'+' | b'_')) { - return invalid("the Manager version is not a short version string"); +/// A coarse region such as `EU`, `NA`, `RU`, `EU-West`. +pub fn validate_region(s: &str) -> Result<()> { + let ok = (2..=MAX_REGION_BYTES).contains(&s.len()) && s.split('-').all(|p| !p.is_empty() && p.bytes().all(|b| b.is_ascii_alphanumeric())); + if !ok { + return invalid("the region is not a short region tag"); } Ok(()) } -pub fn validate_players(count: Option, capacity: Option) -> Result<()> { - if count.is_some_and(|n| n > MAX_PLAYER_NUMBER) || capacity.is_some_and(|n| n > MAX_PLAYER_NUMBER) { - return invalid("a player number is out of range"); - } - if let (Some(c), Some(cap)) = (count, capacity) { - if c > cap { - return invalid("more players than capacity"); - } +pub fn validate_version(s: &str) -> Result<()> { + if s.is_empty() || s.len() > MAX_VERSION_BYTES || !s.bytes().all(|b| b.is_ascii_alphanumeric() || matches!(b, b'.' | b'-' | b'+' | b'_')) { + return invalid("a version is not a short version string"); } Ok(()) } @@ -71,75 +74,216 @@ fn hex64(s: &str) -> bool { s.len() == 64 && s.bytes().all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)) } +/// What the realm's configuration says; `None` is "not known" and is never a guess. +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct Rates { + /// `Rate.XP.Kill` + #[serde(default)] + pub xp_kill: Option, + /// `Rate.XP.Quest` + #[serde(default)] + pub xp_quest: Option, + /// `Rate.XP.Explore` + #[serde(default)] + pub xp_explore: Option, + /// `Rate.Drop.Item.Normal` + #[serde(default)] + pub loot: Option, + /// `Rate.Drop.Money` + #[serde(default)] + pub money: Option, + /// `Rate.Reputation.Gain` + #[serde(default)] + pub reputation: Option, + /// `Rate.Honor` + #[serde(default)] + pub honor: Option, +} + +impl Rates { + pub fn validate(&self) -> Result<()> { + for v in [self.xp_kill, self.xp_quest, self.xp_explore, self.loot, self.money, self.reputation, self.honor].into_iter().flatten() { + if !v.is_finite() || !(0.0..=MAX_RATE).contains(&v) { + return invalid(format!("a rate is not a number between 0 and {MAX_RATE}")); + } + } + Ok(()) + } +} + +/// A module the realm's server build contains. The Registry carries the stable id and the version only; a Player Manager renders names and descriptions +/// from its own module catalog, and shows an id it does not know as plain text. #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] +pub struct ModuleEntry { + pub id: String, + #[serde(default)] + pub version: Option, + pub enabled: bool, +} + +pub fn validate_module_id(s: &str) -> Result<()> { + let ok = !s.is_empty() && s.len() <= 64 && s.bytes().next().is_some_and(|b| b.is_ascii_lowercase() || b.is_ascii_digit()) && s.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || matches!(b, b'.' | b'-' | b'_')); + if !ok { + return invalid("a module id is not a short lower-case token"); + } + Ok(()) +} + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct Population { + /// Human players with a game session. + pub players: u32, + /// Characters played by the bot subsystem. + pub bots: u32, + /// The player limit, when the realm has one. + #[serde(default)] + pub capacity: Option, +} + +impl Population { + pub fn validate(&self) -> Result<()> { + if self.players > MAX_PLAYER_NUMBER || self.bots > MAX_PLAYER_NUMBER || self.capacity.is_some_and(|c| c > MAX_PLAYER_NUMBER) { + return invalid("a population number is out of range"); + } + if self.capacity.is_some_and(|c| self.players > c) { + return invalid("more players than capacity"); + } + Ok(()) + } +} + +/// How a player gets a game account on this realm. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct AccountProvisioning { + /// The Host creates the account when a player joins. + pub automatic: bool, + /// A player can link an account the realm already has. + pub existing_only: bool, +} + +impl AccountProvisioning { + pub fn validate(&self) -> Result<()> { + if !self.automatic && !self.existing_only { + return invalid("account provisioning offers no way to get an account"); + } + Ok(()) + } +} + +/// What a player reads about a realm. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct Listing { + pub display_name: String, + pub description: String, + pub language: String, + #[serde(default)] + pub region: Option, + pub rates: Rates, + pub modules: Vec, + pub account_provisioning: AccountProvisioning, + pub manager_version: String, +} + +impl Listing { + pub fn validate(&self) -> Result<()> { + validate_display_name(&self.display_name)?; + validate_description(&self.description)?; + validate_language(&self.language)?; + if let Some(r) = &self.region { + validate_region(r)?; + } + validate_version(&self.manager_version)?; + self.rates.validate()?; + self.account_provisioning.validate()?; + if self.modules.len() > MAX_MODULES { + return invalid("too many modules"); + } + let mut seen = std::collections::BTreeSet::new(); + for m in &self.modules { + validate_module_id(&m.id)?; + if let Some(v) = &m.version { + validate_version(v)?; + } + if !seen.insert(&m.id) { + return invalid("a module is listed twice"); + } + } + Ok(()) + } + + /// SHA-256 over `coa-registry-listing-v2\0` and the JSON of the listing in its fixed field order (the Registry recomputes it from what it parsed). + pub fn hash(&self) -> String { + let mut h = Sha256::new(); + h.update(b"coa-registry-listing-v2\0"); + h.update(serde_json::to_vec(self).expect("a listing serialises")); + hex::encode(h.finalize()) + } +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] pub struct RegisterRequest { pub protocol_version: u32, pub realm_id: RealmId, /// Base64url (no padding) of the 32-byte Ed25519 public key. pub public_key: String, - pub display_name: String, - pub description: String, - pub language: String, - pub ruleset: Ruleset, - pub manager_version: String, + pub listing: Listing, + /// [`Listing::hash`] + pub listing_hash: String, pub capabilities: AdvertisedCapabilities, /// [`AdvertisedCapabilities::advert_hash`] pub capabilities_hash: String, - #[serde(default)] - pub player_count: Option, - #[serde(default)] - pub player_capacity: Option, + pub population: Population, } impl RegisterRequest { pub fn validate(&self) -> Result<()> { check_version(self.protocol_version)?; crate::sign::decode_public_key(&self.public_key)?; - validate_display_name(&self.display_name)?; - validate_description(&self.description)?; - validate_language(&self.language)?; - validate_version(&self.manager_version)?; - validate_players(self.player_count, self.player_capacity)?; - self.capabilities.validate()?; - if self.capabilities.content.ruleset != self.ruleset { - return invalid("the ruleset is not the ruleset of the capabilities"); + self.listing.validate()?; + if !hex64(&self.listing_hash) || self.listing_hash != self.listing.hash() { + return invalid("the listing hash is not the hash of the listing"); } + self.capabilities.validate()?; if !hex64(&self.capabilities_hash) || self.capabilities_hash != self.capabilities.advert_hash() { return invalid("the capabilities hash is not the hash of the capabilities"); } - Ok(()) + self.population.validate() } } -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct HeartbeatRequest { pub protocol_version: u32, - /// The hash of the capabilities the Host advertises now. + /// The hash of the listing the Host advertises now. + pub listing_hash: String, + /// Present when the Host's listing hash differs from the one the Registry last acknowledged. + #[serde(default)] + pub listing: Option, pub capabilities_hash: String, - /// Present when the Host's hash differs from the one the Registry last acknowledged. + /// Present when the Host's capabilities hash differs from the one the Registry last acknowledged. #[serde(default)] pub capabilities: Option, - #[serde(default)] - pub manager_version: Option, - #[serde(default)] - pub player_count: Option, - #[serde(default)] - pub player_capacity: Option, - #[serde(default)] - pub display_name: Option, - #[serde(default)] - pub description: Option, - #[serde(default)] - pub language: Option, + pub population: Population, } impl HeartbeatRequest { pub fn validate(&self) -> Result<()> { check_version(self.protocol_version)?; - if !hex64(&self.capabilities_hash) { - return invalid("the capabilities hash is not a SHA-256"); + if !hex64(&self.listing_hash) || !hex64(&self.capabilities_hash) { + return invalid("a hash is not a SHA-256"); + } + if let Some(l) = &self.listing { + l.validate()?; + if l.hash() != self.listing_hash { + return invalid("the listing hash is not the hash of the listing"); + } } if let Some(c) = &self.capabilities { c.validate()?; @@ -147,19 +291,7 @@ impl HeartbeatRequest { return invalid("the capabilities hash is not the hash of the capabilities"); } } - if let Some(v) = &self.manager_version { - validate_version(v)?; - } - if let Some(n) = &self.display_name { - validate_display_name(n)?; - } - if let Some(d) = &self.description { - validate_description(d)?; - } - if let Some(l) = &self.language { - validate_language(l)?; - } - validate_players(self.player_count, self.player_capacity) + self.population.validate() } } @@ -182,6 +314,7 @@ pub struct RegisterResponse { pub created: bool, pub metadata_revision: u64, pub published: bool, + pub listing_hash: String, pub capabilities_hash: String, pub server_time: i64, pub heartbeat_interval_secs: u64, @@ -193,9 +326,11 @@ pub struct HeartbeatResponse { pub protocol_version: u32, pub metadata_revision: u64, pub published: bool, - /// The hash the Registry holds now. + /// The hashes the Registry holds now. + pub listing_hash: String, pub capabilities_hash: String, - /// The heartbeat carried another hash without the capabilities: send them with the next one. + /// The heartbeat carried another hash without the part: send it with the next one (nothing of it was applied). + pub resend_listing: bool, pub resend_capabilities: bool, pub server_time: i64, } @@ -208,19 +343,21 @@ pub struct UnpublishResponse { pub server_time: i64, } -/// What the Registry holds about one realm (the authenticated self endpoint; there is no public list yet). +/// What the Registry holds about one realm: the public detail, and the realm's own authenticated read (which also works while unpublished). #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] -pub struct SelfResponse { +pub struct RealmDetail { pub protocol_version: u32, pub realm_id: RealmId, pub public_key: String, - pub display_name: String, - pub description: String, - pub language: String, + pub listing: Listing, + /// Derived from the capabilities, never claimed separately. pub ruleset: Ruleset, - pub manager_version: String, + /// The running core's `MaxPlayerLevel`, from the capabilities' progression; `None` when the realm has not reported one. + pub level_cap: Option, + pub population: Population, pub capabilities: AdvertisedCapabilities, pub capabilities_hash: String, + pub listing_hash: String, pub metadata_revision: u64, pub published: bool, /// Published and heard of within the online TTL. @@ -228,8 +365,6 @@ pub struct SelfResponse { pub created_at: i64, pub updated_at: i64, pub last_seen_at: i64, - pub player_count: Option, - pub player_capacity: Option, pub server_time: i64, } @@ -277,26 +412,36 @@ pub struct Health { } #[cfg(test)] -mod tests { +pub(crate) mod tests { use super::*; use crate::caps::tests::sample; + pub(crate) fn listing() -> Listing { + Listing { + display_name: "Friends' realm".into(), + description: "A small realm.\nPlain text.".into(), + language: "en".into(), + region: Some("EU".into()), + rates: Rates { xp_kill: Some(2.0), xp_quest: Some(2.0), xp_explore: None, loot: Some(1.0), money: Some(1.5), reputation: None, honor: Some(1.0) }, + modules: vec![ModuleEntry { id: "playerbots".into(), version: Some("1.4.2".into()), enabled: true }, ModuleEntry { id: "content-scaling".into(), version: None, enabled: false }], + account_provisioning: AccountProvisioning { automatic: true, existing_only: false }, + manager_version: "0.6.6".into(), + } + } + pub(crate) fn register() -> RegisterRequest { let key = ed25519_dalek::SigningKey::from_bytes(&[7u8; 32]); let capabilities = sample(); + let listing = listing(); RegisterRequest { - protocol_version: 1, + protocol_version: 2, realm_id: RealmId::parse("018f2d9e-5c3a-7b21-8c4d-0e5f6a7b8c9d").unwrap(), public_key: crate::sign::encode_public_key(&key.verifying_key()), - display_name: "Friends' realm".into(), - description: "A small realm.\nPlain text.".into(), - language: "en".into(), - ruleset: Ruleset::Coa, - manager_version: "0.6.6".into(), + listing_hash: listing.hash(), + listing, capabilities_hash: capabilities.advert_hash(), capabilities, - player_count: Some(2), - player_capacity: Some(100), + population: Population { players: 18, bots: 46, capacity: Some(100) }, } } @@ -320,34 +465,95 @@ mod tests { for bad in ["", "E", "EN", "en_US", "en-", "-en", "", "x".repeat(17).as_str()] { assert!(validate_language(bad).is_err(), "{bad:?}"); } + assert!(validate_region("EU").is_ok() && validate_region("EU-West").is_ok()); + for bad in ["", "E", "", "EU West", "x".repeat(17).as_str()] { + assert!(validate_region(bad).is_err(), "{bad:?}"); + } assert!(validate_version("0.6.6").is_ok() && validate_version("0.6.6-rc.1+b2").is_ok()); - assert!(validate_version("1 2").is_err() && validate_version("").is_err()); - assert!(validate_players(Some(5), Some(4)).is_err() && validate_players(Some(100_001), None).is_err() && validate_players(None, None).is_ok()); + assert!(validate_version("1 2").is_err() && validate_version("").is_err() && validate_version("1.4 · abc").is_err()); + } + + #[test] + fn structured_fields_are_bounded() { + let mut l = listing(); + l.rates.xp_kill = Some(f64::NAN); + assert!(l.validate().is_err()); + l.rates.xp_kill = Some(-1.0); + assert!(l.validate().is_err()); + l.rates.xp_kill = Some(1000.5); + assert!(l.validate().is_err()); + l.rates.xp_kill = Some(1000.0); + l.validate().unwrap(); + let mut l = listing(); + l.modules.push(ModuleEntry { id: "playerbots".into(), version: None, enabled: true }); + assert!(l.validate().is_err(), "a module twice"); + let mut l = listing(); + l.modules = (0..65).map(|i| ModuleEntry { id: format!("m{i}"), version: None, enabled: true }).collect(); + assert!(l.validate().is_err(), "too many modules"); + for bad in ["", "Bad", "")), ("display_name", json!("evil\u{202E}name"))] { let mut v = base_body.clone(); - v[field] = value; + v["listing"][field] = value; let r = hostile.send("POST", PATH_REGISTER, &serde_json::to_vec(&v).unwrap()); assert_eq!(r.status, 400, "{field}"); } @@ -232,7 +232,8 @@ fn gate_persist_probe_read() { h.key = ed25519_dalek::SigningKey::from_bytes(&bytes.try_into().unwrap()); h.sync_clock(); let me = h.me(); - assert_eq!((me.status, me.body["display_name"].as_str(), me.body["published"].as_bool()), (200, Some("Persist realm"), Some(true)), "{:?}", me.body); + assert_eq!((me.status, me.body["listing"]["display_name"].as_str(), me.body["published"].as_bool()), (200, Some("Persist realm"), Some(true)), "{:?}", me.body); + assert_eq!((me.body["listing"]["rates"]["xp_kill"].as_f64(), me.body["listing"]["modules"][0]["id"].as_str(), me.body["population"]["bots"].as_u64(), me.body["level_cap"].as_u64()), (Some(2.0), Some("playerbots"), Some(4), Some(60)), "the structured metadata survived: {:?}", me.body); h.sync_clock(); assert_eq!(h.heartbeat(60, false).status, 200, "and it can still heartbeat with its own key"); println!("PERSIST_OK {realm}"); @@ -253,3 +254,31 @@ fn gate_registration_rate_limit() { assert!(ok >= 1 && limited >= 1 && ok + limited == 13, "{codes:?}"); println!("GATE LIMIT PASS registrations from one address: {ok} accepted, {limited} refused with 429 ({codes:?})"); } + +/// A record written by protocol 1 (`PERSIST_REALM`/`PERSIST_SEED` of the Phase-10 probe) is republished under protocol 2 by the same key: the identity and creation time are kept. +#[test] +#[ignore] +fn gate_v1_record_republishes_under_v2() { + let Some(base) = base() else { return }; + let (Ok(realm), Ok(seed)) = (std::env::var("PERSIST_REALM"), std::env::var("PERSIST_SEED")) else { return }; + let mut h = Host::remote(&base); + h.realm = RealmId::parse(&realm).unwrap(); + let bytes: Vec = (0..seed.len() / 2).map(|i| u8::from_str_radix(&seed[2 * i..2 * i + 2], 16).unwrap()).collect(); + h.key = ed25519_dalek::SigningKey::from_bytes(&bytes.try_into().unwrap()); + h.sync_clock(); + let before = h.me(); + assert_eq!(before.status, 200, "the protocol-1 record is still there and its key still works: {:?}", before.body); + let created = before.body["created_at"].as_i64().unwrap(); + assert!(before.body["listing"]["rates"]["xp_kill"].is_null(), "nothing structured was ever advertised for it"); + assert_eq!(before.body["level_cap"], 60, "the cap of the capabilities it already held"); + h.sync_clock(); + let revision_before = before.body["metadata_revision"].as_u64().unwrap(); + let r = h.register("Persist realm", 60); + assert_eq!((r.status, r.body["created"].as_bool()), (200, Some(false)), "{:?}", r.body); + assert_eq!(r.body["metadata_revision"].as_u64(), Some(revision_before + 1), "the upgrade is one metadata revision"); + h.sync_clock(); + let after = h.me(); + assert_eq!(after.body["created_at"].as_i64(), Some(created), "the creation time is kept"); + assert_eq!((after.body["listing"]["rates"]["xp_kill"].as_f64(), after.body["public_key"].as_str()), (Some(2.0), before.body["public_key"].as_str())); + println!("GATE_UPGRADE realm {realm} created_at {created} revision {revision_before} -> {}", revision_before + 1); +} diff --git a/services/coa-registry/deploy/gate-restarts.sh b/services/coa-registry/deploy/gate-restarts.sh index f1b92c4..f09adb0 100644 --- a/services/coa-registry/deploy/gate-restarts.sh +++ b/services/coa-registry/deploy/gate-restarts.sh @@ -9,7 +9,7 @@ remote() { ssh "$VPS" "$1"; } wait_healthy() { # $1 container, $2 seconds i=0; while [ $i -lt "$2" ]; do s=$(remote "sudo docker inspect -f '{{.State.Health.Status}}' $1 2>/dev/null"); [ "$s" = healthy ] && return 0; i=$((i+2)); sleep 2; done; return 1; } -wait_api() { i=0; while [ $i -lt 90 ]; do code=$(curl -s -o /dev/null -m 5 -w '%{http_code}' "$REGISTRY_URL/registry/v1/healthz"); [ "$code" = 200 ] && return 0; i=$((i+2)); sleep 2; done; return 1; } +wait_api() { i=0; while [ $i -lt 90 ]; do code=$(curl -s -o /dev/null -m 5 -w '%{http_code}' "$REGISTRY_URL/registry/v2/healthz"); [ "$code" = 200 ] && return 0; i=$((i+2)); sleep 2; done; return 1; } out=$(run cargo test --test live_vps gate_persist_probe_write -- --ignored --nocapture | grep PERSIST_REALM) set -- $out; REALM=$2; SEED=$3 @@ -24,12 +24,12 @@ remote "sudo docker restart coa-registry-registry-1 >/dev/null"; wait_healthy co echo "--- 14a. PostgreSQL container restart (health is degraded meanwhile, then recovers by itself)" remote "sudo docker restart coa-postgres-postgres-1 >/dev/null" -sleep 1; echo "health during the restart: $(curl -s -m 5 -o /dev/null -w '%{http_code}' "$REGISTRY_URL/registry/v1/healthz")" +sleep 1; echo "health during the restart: $(curl -s -m 5 -o /dev/null -w '%{http_code}' "$REGISTRY_URL/registry/v2/healthz")" wait_healthy coa-postgres-postgres-1 90 && wait_api && check "14a: after restarting PostgreSQL (the Registry container was not restarted)" echo "--- 14b. both stopped, PostgreSQL started later than the Registry (the Registry waits for it)" remote "sudo docker stop coa-registry-registry-1 coa-postgres-postgres-1 >/dev/null; sudo docker start coa-registry-registry-1 >/dev/null" -sleep 6; echo "Registry up without a database: health $(curl -s -m 5 -o /dev/null -w '%{http_code}' "$REGISTRY_URL/registry/v1/healthz")" +sleep 6; echo "Registry up without a database: health $(curl -s -m 5 -o /dev/null -w '%{http_code}' "$REGISTRY_URL/registry/v2/healthz")" remote "sudo docker start coa-postgres-postgres-1 >/dev/null" wait_healthy coa-postgres-postgres-1 90 && wait_healthy coa-registry-registry-1 120 && wait_api && check "14b: after a cold start in the wrong order" diff --git a/services/coa-registry/migrations/0002_protocol_v2.sql b/services/coa-registry/migrations/0002_protocol_v2.sql new file mode 100644 index 0000000..9929fa2 --- /dev/null +++ b/services/coa-registry/migrations/0002_protocol_v2.sql @@ -0,0 +1,17 @@ +-- Registry schema, migration 2 (protocol 2: the structured listing). Forward-only; every realm keeps its id, its key, its creation time and its capabilities. +-- A record written under protocol 1 has advert_version = 1: it is kept, and it is not shown in a public list until its Host republishes under protocol 2. +ALTER TABLE realms RENAME COLUMN player_count TO players; +ALTER TABLE realms RENAME COLUMN player_capacity TO capacity; +ALTER TABLE realms + ADD COLUMN bots integer NOT NULL DEFAULT 0 CHECK (bots BETWEEN 0 AND 100000), + ADD COLUMN region text CHECK (region IS NULL OR char_length(region) BETWEEN 2 AND 16), + ADD COLUMN rates jsonb NOT NULL DEFAULT '{}'::jsonb CHECK (jsonb_typeof(rates) = 'object' AND octet_length(rates::text) <= 1024), + ADD COLUMN modules jsonb NOT NULL DEFAULT '[]'::jsonb CHECK (jsonb_typeof(modules) = 'array' AND octet_length(modules::text) <= 16384), + ADD COLUMN account_automatic boolean NOT NULL DEFAULT false, + ADD COLUMN account_existing_only boolean NOT NULL DEFAULT true, + ADD COLUMN listing_hash text CHECK (listing_hash IS NULL OR listing_hash ~ '^[0-9a-f]{64}$'), + ADD COLUMN level_cap integer CHECK (level_cap IS NULL OR level_cap BETWEEN 1 AND 255), + ADD COLUMN advert_version smallint NOT NULL DEFAULT 1 CHECK (advert_version IN (1, 2)); +-- the level cap of a protocol-1 record is in the capabilities it already holds +UPDATE realms SET level_cap = (capabilities -> 'progression' ->> 'max_player_level')::integer + WHERE jsonb_typeof(capabilities -> 'progression') = 'object' AND (capabilities -> 'progression' ->> 'max_player_level') ~ '^[0-9]{1,3}$'; diff --git a/services/coa-registry/src/api.rs b/services/coa-registry/src/api.rs index 047a698..b79a17c 100644 --- a/services/coa-registry/src/api.rs +++ b/services/coa-registry/src/api.rs @@ -111,15 +111,23 @@ impl From for ApiError { pub fn router(state: Arc>) -> Router { Router::new() - .route("/registry/v1/healthz", get(healthz::)) - .route("/registry/v1/realms/register", post(register::)) - .route("/registry/v1/realms/{realm_id}/heartbeat", post(heartbeat::)) - .route("/registry/v1/realms/{realm_id}/unpublish", post(unpublish::)) - .route("/registry/v1/realms/{realm_id}", get(self_info::)) - .fallback(|| async { ApiError::new(StatusCode::NOT_FOUND, ErrorCode::MalformedRequest, "no such endpoint") }) + .route("/registry/v2/healthz", get(healthz::)) + .route("/registry/v2/realms/register", post(register::)) + .route("/registry/v2/realms/{realm_id}/heartbeat", post(heartbeat::)) + .route("/registry/v2/realms/{realm_id}/unpublish", post(unpublish::)) + .route("/registry/v2/realms/{realm_id}/self", get(self_info::)) + .fallback(fallback) .with_state(state) } +/// Protocol 1 is refused explicitly (the policy of `docs/REGISTRY_PROTOCOL.md`): a Host that still speaks it is told so and stops; nothing is silently accepted or migrated. +async fn fallback(uri: Uri) -> ApiError { + if uri.path() == PATH_PREFIX_V1 || uri.path().starts_with("/registry/v1/") { + return ApiError::new(StatusCode::BAD_REQUEST, ErrorCode::UnsupportedProtocolVersion, format!("protocol version 1 is no longer supported; this Registry speaks protocol {REGISTRY_PROTOCOL_VERSION} under {PATH_PREFIX}")); + } + ApiError::new(StatusCode::NOT_FOUND, ErrorCode::MalformedRequest, "no such endpoint") +} + fn client_ip(headers: &HeaderMap, peer: SocketAddr, trust_proxy: bool) -> IpAddr { if trust_proxy { if let Some(last) = headers.get("x-forwarded-for").and_then(|v| v.to_str().ok()).and_then(|v| v.rsplit(',').next()) { @@ -247,6 +255,7 @@ async fn register(State(st): State>>, ConnectInfo(Peer created, metadata_revision: row.metadata_revision, published: row.published, + listing_hash: row.listing_hash.clone(), capabilities_hash: row.capabilities_hash.clone(), server_time: now, heartbeat_interval_secs: HEARTBEAT_INTERVAL_SECS, @@ -300,7 +309,7 @@ async fn heartbeat(State(st): State>>, ConnectInfo(Pee hb.validate().map_err(|e| ApiError::new(StatusCode::BAD_REQUEST, ErrorCode::InvalidMetadata, e.to_string()))?; let now = st.now(); let out = st.store.heartbeat(&realm, &hb, now, ts).await?; - let resp = HeartbeatResponse { protocol_version: REGISTRY_PROTOCOL_VERSION, metadata_revision: out.metadata_revision, published: true, capabilities_hash: out.capabilities_hash.clone(), resend_capabilities: out.resend_capabilities, server_time: now }; + let resp = HeartbeatResponse { protocol_version: REGISTRY_PROTOCOL_VERSION, metadata_revision: out.metadata_revision, published: true, listing_hash: out.listing_hash.clone(), capabilities_hash: out.capabilities_hash.clone(), resend_listing: out.resend_listing, resend_capabilities: out.resend_capabilities, server_time: now }; let (status, response) = json(&resp); Ok((status, response, out.capabilities_hash)) } @@ -331,27 +340,25 @@ async fn unpublish(State(st): State>>, ConnectInfo(Pee finish("unpublish", realm_for_log.as_ref(), started, result) } -fn self_response(row: RealmRow, now: i64, ttl: u64) -> SelfResponse { +pub fn detail_of(row: RealmRow, now: i64, ttl: u64) -> RealmDetail { let online = row.published && now - row.last_seen_at <= ttl as i64; - SelfResponse { + RealmDetail { protocol_version: REGISTRY_PROTOCOL_VERSION, realm_id: row.realm_id, public_key: sign::encode_public_key(&ed25519_dalek::VerifyingKey::from_bytes(&row.public_key).expect("a stored key was validated on the way in")), - display_name: row.display_name, - description: row.description, - language: row.language, + listing: row.listing, ruleset: row.ruleset, - manager_version: row.manager_version, + level_cap: row.level_cap, + population: row.population, capabilities: row.capabilities, capabilities_hash: row.capabilities_hash, + listing_hash: row.listing_hash, metadata_revision: row.metadata_revision, published: row.published, online, created_at: row.created_at, updated_at: row.updated_at, last_seen_at: row.last_seen_at, - player_count: row.player_count, - player_capacity: row.player_capacity, server_time: now, } } @@ -372,7 +379,7 @@ async fn self_info(State(st): State>>, ConnectInfo(Pee realm_for_log = Some(realm); let row = st.store.get(&realm).await?.ok_or(StoreError::Unknown)?; let caps = row.capabilities_hash.clone(); - let (status, response) = json(&self_response(row, st.now(), st.cfg.online_ttl_secs)); + let (status, response) = json(&detail_of(row, st.now(), st.cfg.online_ttl_secs)); Ok((status, response, caps)) } .await; diff --git a/services/coa-registry/src/main.rs b/services/coa-registry/src/main.rs index 41505d0..0b9ef15 100644 --- a/services/coa-registry/src/main.rs +++ b/services/coa-registry/src/main.rs @@ -32,7 +32,7 @@ fn healthcheck() -> i32 { let port = addr.rsplit(':').next().unwrap_or("8080"); let Ok(mut s) = std::net::TcpStream::connect(format!("127.0.0.1:{port}")) else { return 1 }; let _ = s.set_read_timeout(Some(Duration::from_secs(3))); - if s.write_all(b"GET /registry/v1/healthz HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n").is_err() { + if s.write_all(b"GET /registry/v2/healthz HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n").is_err() { return 1; } let mut out = String::new(); diff --git a/services/coa-registry/src/pg.rs b/services/coa-registry/src/pg.rs index 66b5cad..a7aebd7 100644 --- a/services/coa-registry/src/pg.rs +++ b/services/coa-registry/src/pg.rs @@ -3,13 +3,16 @@ use std::time::Duration; use coa_registry_proto::caps::{AdvertisedCapabilities, Ruleset}; -use coa_registry_proto::{HeartbeatRequest, RealmId, RegisterRequest}; +use coa_registry_proto::{AccountProvisioning, HeartbeatRequest, Listing, ModuleEntry, Population, Rates, RealmId, RegisterRequest}; use deadpool_postgres::{Manager, ManagerConfig, Object, Pool, RecyclingMethod, Runtime}; use tokio_postgres::{NoTls, Row}; use crate::store::*; -const MIGRATIONS: &[(i32, &str, &str)] = &[(1, "realms", include_str!("../migrations/0001_realms.sql"))]; +const MIGRATIONS: &[(i32, &str, &str)] = &[ + (1, "realms", include_str!("../migrations/0001_realms.sql")), + (2, "protocol_v2", include_str!("../migrations/0002_protocol_v2.sql")), +]; pub struct PgConfig { pub host: String, @@ -47,7 +50,7 @@ impl PgStore { Ok(Self { pool }) } - async fn conn(&self) -> StoreResult { + pub(crate) async fn conn(&self) -> StoreResult { self.pool.get().await.map_err(backend) } @@ -81,16 +84,32 @@ impl PgStore { } } -const COLUMNS: &str = "realm_id::text, public_key, EXTRACT(EPOCH FROM created_at)::bigint, EXTRACT(EPOCH FROM updated_at)::bigint, EXTRACT(EPOCH FROM last_seen_at)::bigint, published, display_name, description, language, ruleset, manager_version, capabilities, capabilities_hash, metadata_revision, last_request_ts, player_count, player_capacity"; +pub(crate) const COLUMNS: &str = "realm_id::text, public_key, EXTRACT(EPOCH FROM created_at)::bigint, EXTRACT(EPOCH FROM updated_at)::bigint, EXTRACT(EPOCH FROM last_seen_at)::bigint, published, display_name, description, language, region, rates, modules, account_automatic, account_existing_only, manager_version, listing_hash, ruleset, level_cap, capabilities, capabilities_hash, players, bots, capacity, metadata_revision, last_request_ts, advert_version"; -fn to_row(r: &Row) -> StoreResult { +pub(crate) fn to_row(r: &Row) -> StoreResult { let id: String = r.get(0); let key: Vec = r.get(1); - let ruleset: String = r.get(9); - let caps: serde_json::Value = r.get(11); - let revision: i64 = r.get(13); - let count: Option = r.get(15); - let capacity: Option = r.get(16); + let rates: serde_json::Value = r.get(10); + let modules: serde_json::Value = r.get(11); + let ruleset: String = r.get(16); + let level_cap: Option = r.get(17); + let caps: serde_json::Value = r.get(18); + let players: Option = r.get(20); + let bots: i32 = r.get(21); + let capacity: Option = r.get(22); + let revision: i64 = r.get(23); + let version: i16 = r.get(25); + let listing = Listing { + display_name: r.get(6), + description: r.get(7), + language: r.get(8), + region: r.get(9), + rates: serde_json::from_value::(rates).unwrap_or_default(), + modules: serde_json::from_value::>(modules).unwrap_or_default(), + account_provisioning: AccountProvisioning { automatic: r.get(12), existing_only: r.get(13) }, + manager_version: r.get(14), + }; + let stored_hash: Option = r.get(15); Ok(RealmRow { realm_id: RealmId::parse(&id).map_err(backend)?, public_key: key.try_into().map_err(|_| StoreError::Backend("a stored key is not 32 bytes".into()))?, @@ -98,21 +117,20 @@ fn to_row(r: &Row) -> StoreResult { updated_at: r.get(3), last_seen_at: r.get(4), published: r.get(5), - display_name: r.get(6), - description: r.get(7), - language: r.get(8), + listing_hash: stored_hash.unwrap_or_else(|| listing.hash()), + listing, ruleset: match ruleset.as_str() { "coa" => Ruleset::Coa, "wildcard" => Ruleset::Wildcard, other => return Err(StoreError::Backend(format!("a stored ruleset is unknown: {other}"))), }, - manager_version: r.get(10), + level_cap: level_cap.map(|n| n as u32), capabilities: serde_json::from_value::(caps).map_err(backend)?, - capabilities_hash: r.get(12), + capabilities_hash: r.get(19), + population: Population { players: players.unwrap_or(0) as u32, bots: bots as u32, capacity: capacity.map(|n| n as u32) }, metadata_revision: revision as u64, - last_request_ts: r.get(14), - player_count: count.map(|n| n as u32), - player_capacity: capacity.map(|n| n as u32), + last_request_ts: r.get(24), + advert_version: version as u8, }) } @@ -120,6 +138,15 @@ fn caps_json(c: &AdvertisedCapabilities) -> StoreResult { serde_json::to_value(c).map_err(backend) } +struct ListingColumns { + rates: serde_json::Value, + modules: serde_json::Value, +} + +fn listing_columns(l: &Listing) -> StoreResult { + Ok(ListingColumns { rates: serde_json::to_value(&l.rates).map_err(backend)?, modules: serde_json::to_value(&l.modules).map_err(backend)? }) +} + impl Store for PgStore { async fn ping(&self) -> bool { match self.pool.get().await { @@ -149,14 +176,17 @@ impl Store for PgStore { let existing = existing.as_ref().map(to_row).transpose()?; let (row, created) = plan_register(existing.as_ref(), req, key, now, ts)?; let caps = caps_json(&row.capabilities)?; + let cols = listing_columns(&row.listing)?; let ruleset = row.ruleset.as_str(); - let players = (row.player_count.map(|n| n as i32), row.player_capacity.map(|n| n as i32)); + let (players, bots, capacity) = (row.population.players as i32, row.population.bots as i32, row.population.capacity.map(|n| n as i32)); + let level_cap = row.level_cap.map(|n| n as i32); + let l = &row.listing; if created { let n = tx .execute( - "INSERT INTO realms (realm_id, public_key, created_at, updated_at, last_seen_at, published, display_name, description, language, ruleset, manager_version, capabilities, capabilities_hash, metadata_revision, last_request_ts, player_count, player_capacity) \ - VALUES ($1::text::uuid, $2, to_timestamp($3::bigint), to_timestamp($3::bigint), to_timestamp($3::bigint), true, $4, $5, $6, $7, $8, $9, $10, 1, $11, $12, $13) ON CONFLICT (realm_id) DO NOTHING", - &[&id, &row.public_key.as_slice(), &now, &row.display_name, &row.description, &row.language, &ruleset, &row.manager_version, &caps, &row.capabilities_hash, &ts, &players.0, &players.1], + "INSERT INTO realms (realm_id, public_key, created_at, updated_at, last_seen_at, published, display_name, description, language, region, rates, modules, account_automatic, account_existing_only, manager_version, listing_hash, ruleset, level_cap, capabilities, capabilities_hash, players, bots, capacity, metadata_revision, last_request_ts, advert_version) \ + VALUES ($1::text::uuid, $2, to_timestamp($3::bigint), to_timestamp($3::bigint), to_timestamp($3::bigint), true, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, 1, $21, 2) ON CONFLICT (realm_id) DO NOTHING", + &[&id, &row.public_key.as_slice(), &now, &l.display_name, &l.description, &l.language, &l.region, &cols.rates, &cols.modules, &l.account_provisioning.automatic, &l.account_provisioning.existing_only, &l.manager_version, &row.listing_hash, &ruleset, &level_cap, &caps, &row.capabilities_hash, &players, &bots, &capacity, &ts], ) .await .map_err(backend)?; @@ -166,8 +196,8 @@ impl Store for PgStore { } } else { tx.execute( - "UPDATE realms SET updated_at = to_timestamp($2::bigint), last_seen_at = to_timestamp($3::bigint), published = true, display_name = $4, description = $5, language = $6, ruleset = $7, manager_version = $8, capabilities = $9, capabilities_hash = $10, metadata_revision = $11, last_request_ts = $12, player_count = $13, player_capacity = $14 WHERE realm_id = $1::text::uuid", - &[&id, &row.updated_at, &now, &row.display_name, &row.description, &row.language, &ruleset, &row.manager_version, &caps, &row.capabilities_hash, &(row.metadata_revision as i64), &ts, &players.0, &players.1], + "UPDATE realms SET updated_at = to_timestamp($2::bigint), last_seen_at = to_timestamp($3::bigint), published = true, display_name = $4, description = $5, language = $6, region = $7, rates = $8, modules = $9, account_automatic = $10, account_existing_only = $11, manager_version = $12, listing_hash = $13, ruleset = $14, level_cap = $15, capabilities = $16, capabilities_hash = $17, players = $18, bots = $19, capacity = $20, metadata_revision = $21, last_request_ts = $22, advert_version = 2 WHERE realm_id = $1::text::uuid", + &[&id, &row.updated_at, &now, &l.display_name, &l.description, &l.language, &l.region, &cols.rates, &cols.modules, &l.account_provisioning.automatic, &l.account_provisioning.existing_only, &l.manager_version, &row.listing_hash, &ruleset, &level_cap, &caps, &row.capabilities_hash, &players, &bots, &capacity, &(row.metadata_revision as i64), &ts], ) .await .map_err(backend)?; @@ -183,26 +213,54 @@ impl Store for PgStore { let text = id.to_string(); let tx = c.transaction().await.map_err(backend)?; let r = tx - .query_opt("SELECT published, last_request_ts, capabilities_hash, display_name, description, language, manager_version, metadata_revision FROM realms WHERE realm_id = $1::text::uuid FOR UPDATE", &[&text]) + .query_opt("SELECT published, last_request_ts, capabilities_hash, listing_hash, metadata_revision, advert_version, display_name, description, language, region, rates, modules, account_automatic, account_existing_only, manager_version FROM realms WHERE realm_id = $1::text::uuid FOR UPDATE", &[&text]) .await .map_err(backend)? .ok_or(StoreError::Unknown)?; - let revision: i64 = r.get(7); - let view = HeartbeatView { published: r.get(0), last_request_ts: r.get(1), capabilities_hash: r.get(2), display_name: r.get(3), description: r.get(4), language: r.get(5), manager_version: r.get(6), metadata_revision: revision as u64 }; + let revision: i64 = r.get(4); + let version: i16 = r.get(5); + let stored_listing_hash: Option = r.get(3); + let listing_hash = match stored_listing_hash { + Some(h) => h, + None => { + let l = Listing { + display_name: r.get(6), + description: r.get(7), + language: r.get(8), + region: r.get(9), + rates: serde_json::from_value::(r.get(10)).unwrap_or_default(), + modules: serde_json::from_value::>(r.get(11)).unwrap_or_default(), + account_provisioning: AccountProvisioning { automatic: r.get(12), existing_only: r.get(13) }, + manager_version: r.get(14), + }; + l.hash() + } + }; + let view = HeartbeatView { published: r.get(0), last_request_ts: r.get(1), capabilities_hash: r.get(2), listing_hash, metadata_revision: revision as u64, advert_version: version as u8 }; let plan = plan_heartbeat(&view, hb, ts)?; - let players = (hb.player_count.map(|n| n as i32), hb.player_capacity.map(|n| n as i32)); + let (players, bots, capacity) = (hb.population.players as i32, hb.population.bots as i32, hb.population.capacity.map(|n| n as i32)); tx.execute( - "UPDATE realms SET last_seen_at = to_timestamp($2::bigint), last_request_ts = $3, player_count = $4, player_capacity = $5, display_name = $6, description = $7, language = $8, manager_version = $9, capabilities_hash = $10, metadata_revision = $11, updated_at = CASE WHEN $12 THEN to_timestamp($2::bigint) ELSE updated_at END WHERE realm_id = $1::text::uuid", - &[&text, &now, &ts, &players.0, &players.1, &plan.display_name, &plan.description, &plan.language, &plan.manager_version, &plan.capabilities_hash, &(plan.metadata_revision as i64), &plan.metadata_changed], + "UPDATE realms SET last_seen_at = to_timestamp($2::bigint), last_request_ts = $3, players = $4, bots = $5, capacity = $6, listing_hash = $7, capabilities_hash = $8, metadata_revision = $9, updated_at = CASE WHEN $10 THEN to_timestamp($2::bigint) ELSE updated_at END WHERE realm_id = $1::text::uuid", + &[&text, &now, &ts, &players, &bots, &capacity, &plan.listing_hash, &plan.capabilities_hash, &(plan.metadata_revision as i64), &plan.metadata_changed], ) .await .map_err(backend)?; + if let Some(l) = &plan.listing { + let cols = listing_columns(l)?; + tx.execute( + "UPDATE realms SET display_name = $2, description = $3, language = $4, region = $5, rates = $6, modules = $7, account_automatic = $8, account_existing_only = $9, manager_version = $10, advert_version = 2 WHERE realm_id = $1::text::uuid", + &[&text, &l.display_name, &l.description, &l.language, &l.region, &cols.rates, &cols.modules, &l.account_provisioning.automatic, &l.account_provisioning.existing_only, &l.manager_version], + ) + .await + .map_err(backend)?; + } if let Some(caps) = &plan.capabilities { let json = caps_json(caps)?; - tx.execute("UPDATE realms SET capabilities = $2, ruleset = $3 WHERE realm_id = $1::text::uuid", &[&text, &json, &caps.content.ruleset.as_str()]).await.map_err(backend)?; + let level_cap = caps.level_cap().map(|n| n as i32); + tx.execute("UPDATE realms SET capabilities = $2, ruleset = $3, level_cap = $4 WHERE realm_id = $1::text::uuid", &[&text, &json, &caps.content.ruleset.as_str(), &level_cap]).await.map_err(backend)?; } tx.commit().await.map_err(backend)?; - Ok(HeartbeatOutcome { metadata_revision: plan.metadata_revision, capabilities_hash: plan.capabilities_hash, resend_capabilities: plan.resend_capabilities }) + Ok(HeartbeatOutcome { metadata_revision: plan.metadata_revision, listing_hash: plan.listing_hash, capabilities_hash: plan.capabilities_hash, resend_listing: plan.resend_listing, resend_capabilities: plan.resend_capabilities }) } async fn unpublish(&self, id: &RealmId, now: i64, ts: i64) -> StoreResult { diff --git a/services/coa-registry/src/store.rs b/services/coa-registry/src/store.rs index 9d51620..8e4cbac 100644 --- a/services/coa-registry/src/store.rs +++ b/services/coa-registry/src/store.rs @@ -6,7 +6,7 @@ use std::future::Future; use std::sync::Mutex; use coa_registry_proto::caps::{AdvertisedCapabilities, Ruleset}; -use coa_registry_proto::{HeartbeatRequest, RealmId, RegisterRequest}; +use coa_registry_proto::{HeartbeatRequest, Listing, Population, RealmId, RegisterRequest}; #[derive(Debug, thiserror::Error, PartialEq, Eq)] pub enum StoreError { @@ -24,6 +24,9 @@ pub enum StoreError { pub type StoreResult = Result; +/// The version of advertisement a row holds: 1 is a Phase-10 record (identity, key and capabilities kept, nothing structured), 2 is a full v2 listing. +pub const ADVERT_VERSION: u8 = 2; + #[derive(Clone, Debug, PartialEq)] pub struct RealmRow { pub realm_id: RealmId, @@ -32,17 +35,17 @@ pub struct RealmRow { pub updated_at: i64, pub last_seen_at: i64, pub published: bool, - pub display_name: String, - pub description: String, - pub language: String, + pub listing: Listing, + pub listing_hash: String, + /// Derived from the capabilities. pub ruleset: Ruleset, - pub manager_version: String, + pub level_cap: Option, pub capabilities: AdvertisedCapabilities, pub capabilities_hash: String, + pub population: Population, pub metadata_revision: u64, pub last_request_ts: i64, - pub player_count: Option, - pub player_capacity: Option, + pub advert_version: u8, } /// The light part of a row a heartbeat decides on (the capabilities JSON stays in the database unless it changes). @@ -50,40 +53,29 @@ pub struct RealmRow { pub struct HeartbeatView { pub published: bool, pub last_request_ts: i64, + pub listing_hash: String, pub capabilities_hash: String, - pub display_name: String, - pub description: String, - pub language: String, - pub manager_version: String, pub metadata_revision: u64, + pub advert_version: u8, } impl From<&RealmRow> for HeartbeatView { fn from(r: &RealmRow) -> Self { - Self { - published: r.published, - last_request_ts: r.last_request_ts, - capabilities_hash: r.capabilities_hash.clone(), - display_name: r.display_name.clone(), - description: r.description.clone(), - language: r.language.clone(), - manager_version: r.manager_version.clone(), - metadata_revision: r.metadata_revision, - } + Self { published: r.published, last_request_ts: r.last_request_ts, listing_hash: r.listing_hash.clone(), capabilities_hash: r.capabilities_hash.clone(), metadata_revision: r.metadata_revision, advert_version: r.advert_version } } } #[derive(Clone, Debug, PartialEq)] pub struct HeartbeatPlan { - pub display_name: String, - pub description: String, - pub language: String, - pub manager_version: String, + /// Set only when the listing is applied: the only case in which its columns are written. + pub listing: Option, + pub listing_hash: String, /// Set only when the hash changed: the only case in which the large JSON is written. pub capabilities: Option, pub capabilities_hash: String, pub metadata_revision: u64, pub metadata_changed: bool, + pub resend_listing: bool, pub resend_capabilities: bool, } @@ -95,17 +87,26 @@ pub fn plan_heartbeat(view: &HeartbeatView, hb: &HeartbeatRequest, ts: i64) -> S return Err(StoreError::NotPublished); } let mut plan = HeartbeatPlan { - display_name: hb.display_name.clone().unwrap_or_else(|| view.display_name.clone()), - description: hb.description.clone().unwrap_or_else(|| view.description.clone()), - language: hb.language.clone().unwrap_or_else(|| view.language.clone()), - manager_version: hb.manager_version.clone().unwrap_or_else(|| view.manager_version.clone()), + listing: None, + listing_hash: view.listing_hash.clone(), capabilities: None, capabilities_hash: view.capabilities_hash.clone(), metadata_revision: view.metadata_revision, metadata_changed: false, + resend_listing: false, resend_capabilities: false, }; - plan.metadata_changed = plan.display_name != view.display_name || plan.description != view.description || plan.language != view.language || plan.manager_version != view.manager_version; + // a record from before protocol 2 has no structured listing: it takes the first one it is given, whatever the hashes say + let legacy = view.advert_version < ADVERT_VERSION; + match &hb.listing { + Some(l) if legacy || hb.listing_hash != view.listing_hash => { + plan.listing = Some(l.clone()); + plan.listing_hash = hb.listing_hash.clone(); + plan.metadata_changed = true; + } + None if legacy || hb.listing_hash != view.listing_hash => plan.resend_listing = true, + _ => {} + } match &hb.capabilities { Some(c) if hb.capabilities_hash != view.capabilities_hash => { plan.capabilities = Some(c.clone()); @@ -121,7 +122,7 @@ pub fn plan_heartbeat(view: &HeartbeatView, hb: &HeartbeatRequest, ts: i64) -> S Ok(plan) } -/// The new row for a registration: a first one, or the same key announcing itself again (a restart, a changed name, a republication). +/// The new row for a registration: a first one, or the same key announcing itself again (a restart, a changed name, a republication, a record upgraded from protocol 1). pub fn plan_register(existing: Option<&RealmRow>, req: &RegisterRequest, key: [u8; 32], now: i64, ts: i64) -> StoreResult<(RealmRow, bool)> { let mut row = RealmRow { realm_id: req.realm_id, @@ -130,17 +131,16 @@ pub fn plan_register(existing: Option<&RealmRow>, req: &RegisterRequest, key: [u updated_at: now, last_seen_at: now, published: true, - display_name: req.display_name.clone(), - description: req.description.clone(), - language: req.language.clone(), - ruleset: req.ruleset, - manager_version: req.manager_version.clone(), + listing: req.listing.clone(), + listing_hash: req.listing_hash.clone(), + ruleset: req.capabilities.content.ruleset, + level_cap: req.capabilities.level_cap(), capabilities: req.capabilities.clone(), capabilities_hash: req.capabilities_hash.clone(), + population: req.population, metadata_revision: 1, last_request_ts: ts, - player_count: req.player_count, - player_capacity: req.player_capacity, + advert_version: ADVERT_VERSION, }; let Some(old) = existing else { return Ok((row, true)) }; if old.public_key != key { @@ -150,13 +150,7 @@ pub fn plan_register(existing: Option<&RealmRow>, req: &RegisterRequest, key: [u return Err(StoreError::Stale); } row.created_at = old.created_at; - let changed = !old.published - || old.display_name != row.display_name - || old.description != row.description - || old.language != row.language - || old.ruleset != row.ruleset - || old.manager_version != row.manager_version - || old.capabilities_hash != row.capabilities_hash; + let changed = !old.published || old.advert_version < ADVERT_VERSION || old.listing_hash != row.listing_hash || old.capabilities_hash != row.capabilities_hash; row.metadata_revision = old.metadata_revision + u64::from(changed); row.updated_at = if changed { now } else { old.updated_at }; Ok((row, false)) @@ -177,7 +171,9 @@ pub trait Store: Send + Sync + 'static { #[derive(Clone, Debug, PartialEq, Eq)] pub struct HeartbeatOutcome { pub metadata_revision: u64, + pub listing_hash: String, pub capabilities_hash: String, + pub resend_listing: bool, pub resend_capabilities: bool, } @@ -196,6 +192,12 @@ impl MemoryStore { self.rows.lock().unwrap().get(id).cloned() } + /// Put a row in as it is (tests: a record left by an earlier protocol). + #[doc(hidden)] + pub fn put_for_test(&self, row: RealmRow) { + self.rows.lock().unwrap().insert(row.realm_id, row); + } + /// Forget everything (tests: a Registry that lost its data). #[doc(hidden)] pub fn forget_all(&self) { @@ -249,12 +251,14 @@ impl Store for MemoryStore { let mut rows = self.rows.lock().unwrap(); let row = rows.get_mut(id).ok_or(StoreError::Unknown)?; let plan = plan_heartbeat(&HeartbeatView::from(&*row), hb, ts)?; - row.display_name = plan.display_name; - row.description = plan.description; - row.language = plan.language; - row.manager_version = plan.manager_version; - if let Some(c) = plan.capabilities { + if let Some(l) = plan.listing.clone() { + row.listing = l; + row.advert_version = ADVERT_VERSION; + } + row.listing_hash = plan.listing_hash.clone(); + if let Some(c) = plan.capabilities.clone() { row.ruleset = c.content.ruleset; + row.level_cap = c.level_cap(); row.capabilities = c; } row.capabilities_hash = plan.capabilities_hash.clone(); @@ -264,9 +268,8 @@ impl Store for MemoryStore { row.metadata_revision = plan.metadata_revision; row.last_seen_at = now; row.last_request_ts = ts; - row.player_count = hb.player_count; - row.player_capacity = hb.player_capacity; - Ok(HeartbeatOutcome { metadata_revision: plan.metadata_revision, capabilities_hash: plan.capabilities_hash, resend_capabilities: plan.resend_capabilities }) + row.population = hb.population; + Ok(HeartbeatOutcome { metadata_revision: plan.metadata_revision, listing_hash: plan.listing_hash, capabilities_hash: plan.capabilities_hash, resend_listing: plan.resend_listing, resend_capabilities: plan.resend_capabilities }) } async fn unpublish(&self, id: &RealmId, now: i64, ts: i64) -> StoreResult { diff --git a/services/coa-registry/tests/api.rs b/services/coa-registry/tests/api.rs index b2f4f0d..02ab819 100644 --- a/services/coa-registry/tests/api.rs +++ b/services/coa-registry/tests/api.rs @@ -4,8 +4,9 @@ mod common; use coa_registry::api::ApiConfig; use coa_registry::limits::Rate; -use coa_registry::store::MemoryStore; +use coa_registry::store::{MemoryStore, RealmRow, ADVERT_VERSION}; use coa_registry_proto::sign::{sign_request, HEADER_SIGNATURE, HEADER_TIMESTAMP, HEADER_VERSION}; +use coa_registry_proto::caps::Ruleset; use coa_registry_proto::*; use common::*; use ed25519_dalek::SigningKey; @@ -27,22 +28,28 @@ fn lifecycle(s: &TestServer) { assert_eq!(r.status, 200, "{:?}", r.body); assert_eq!((r.body["created"].as_bool(), r.body["metadata_revision"].as_u64(), r.body["published"].as_bool()), (Some(true), Some(1), Some(true))); assert_eq!((r.body["heartbeat_interval_secs"].as_u64(), r.body["online_ttl_secs"].as_u64()), (Some(30), Some(120))); + assert_eq!(r.body["protocol_version"], 2); let me = h.me(); - assert_eq!(me.status, 200); - assert_eq!((me.body["online"].as_bool(), me.body["display_name"].as_str(), me.body["last_seen_at"].as_i64()), (Some(true), Some("Friends' realm"), Some(s.time()))); + assert_eq!(me.status, 200, "{:?}", me.body); + assert_eq!((me.body["online"].as_bool(), me.body["listing"]["display_name"].as_str(), me.body["last_seen_at"].as_i64()), (Some(true), Some("Friends' realm"), Some(s.time()))); assert_eq!(me.body["capabilities"]["progression"]["max_player_level"], 60); + assert_eq!((me.body["level_cap"].as_u64(), me.body["ruleset"].as_str()), (Some(60), Some("coa")), "derived from the capabilities"); + assert_eq!((me.body["population"]["players"].as_u64(), me.body["population"]["bots"].as_u64()), (Some(1), Some(4)), "players and bots are separate"); + assert_eq!(me.body["listing"]["rates"]["xp_kill"], 2.0); + assert!(me.body["listing"]["rates"]["xp_explore"].is_null(), "an unknown rate stays null"); + assert_eq!(me.body["listing"]["modules"][0]["id"], "playerbots"); s.advance(30); h.ts = s.time(); let hb = h.heartbeat(60, false); assert_eq!(hb.status, 200, "{:?}", hb.body); - assert_eq!((hb.body["metadata_revision"].as_u64(), hb.body["resend_capabilities"].as_bool()), (Some(1), Some(false))); + assert_eq!((hb.body["metadata_revision"].as_u64(), hb.body["resend_capabilities"].as_bool(), hb.body["resend_listing"].as_bool()), (Some(1), Some(false), Some(false))); let me = h.me(); - assert_eq!((me.body["last_seen_at"].as_i64(), me.body["player_count"].as_u64()), (Some(s.time()), Some(2)), "the heartbeat moved last_seen and the player count"); + assert_eq!((me.body["last_seen_at"].as_i64(), me.body["population"]["players"].as_u64(), me.body["population"]["bots"].as_u64()), (Some(s.time()), Some(2), Some(4)), "the heartbeat moved last_seen and the counts"); assert_eq!(me.body["metadata_revision"], 1, "a heartbeat of unchanged metadata is not a metadata update"); - // the same key announcing itself again (a Manager restart) keeps the identity; unchanged metadata keeps the revision + // the same key announcing itself again (a restart) keeps the identity; unchanged metadata keeps the revision let again = h.register("Friends' realm", 60); assert_eq!((again.status, again.body["created"].as_bool(), again.body["metadata_revision"].as_u64()), (200, Some(false), Some(1))); let renamed = h.register("Friends' realm II", 60); @@ -65,7 +72,7 @@ fn presence(s: &TestServer) { assert_eq!(h.me().body["online"], true); } -/// 6: new capabilities arrive with a heartbeat and move the metadata revision; a changed hash without them asks for them. +/// 6: new capabilities and a new listing arrive with a heartbeat and move the metadata revision; a changed hash without the part asks for it. fn metadata_changes(s: &TestServer) { let mut h = Host::new(s, 3); assert_eq!(h.register("Caps", 60).status, 200); @@ -77,16 +84,28 @@ fn metadata_changes(s: &TestServer) { let hb = h.heartbeat(70, true); assert_eq!((hb.status, hb.body["resend_capabilities"].as_bool(), hb.body["metadata_revision"].as_u64()), (200, Some(false), Some(2))); let me = h.me(); - assert_eq!((me.body["capabilities"]["progression"]["max_player_level"].as_u64(), me.body["metadata_revision"].as_u64()), (Some(70), Some(2))); + assert_eq!((me.body["capabilities"]["progression"]["max_player_level"].as_u64(), me.body["level_cap"].as_u64(), me.body["metadata_revision"].as_u64()), (Some(70), Some(70), Some(2)), "the level cap follows the capabilities"); let hb = h.heartbeat(70, true); assert_eq!(hb.body["metadata_revision"], 2, "the same capabilities again change nothing"); - // a heartbeat may also rename + // a changed listing: first only its hash (asks for it), then the listing let mut body = h.heartbeat_body(70, false); - body.display_name = Some("Renamed".into()); + let renamed = h.listing("Renamed"); + body.listing_hash = renamed.hash(); let path = path_heartbeat(&h.realm); let r = h.send("POST", &path, &serde_json::to_vec(&body).unwrap()); + assert_eq!((r.status, r.body["resend_listing"].as_bool(), r.body["metadata_revision"].as_u64()), (200, Some(true), Some(2))); + h.name = "Renamed".into(); + let body = h.heartbeat_with_listing(70, "Renamed"); + let r = h.send("POST", &path, &serde_json::to_vec(&body).unwrap()); + assert_eq!((r.status, r.body["resend_listing"].as_bool(), r.body["metadata_revision"].as_u64()), (200, Some(false), Some(3))); + assert_eq!(h.me().body["listing"]["display_name"], "Renamed"); + // players and bots move without touching the revision + let mut body = h.heartbeat_body(70, false); + body.population = Population { players: 18, bots: 46, capacity: None }; + let r = h.send("POST", &path, &serde_json::to_vec(&body).unwrap()); assert_eq!((r.status, r.body["metadata_revision"].as_u64()), (200, Some(3))); - assert_eq!(h.me().body["display_name"], "Renamed"); + let me = h.me(); + assert_eq!((me.body["population"]["players"].as_u64(), me.body["population"]["bots"].as_u64(), me.body["metadata_revision"].as_u64()), (Some(18), Some(46), Some(3))); } /// 7: an unrelated key can neither take a realm id over nor speak for it. @@ -105,7 +124,7 @@ fn takeover(s: &TestServer) { let r = thief.me(); assert_eq!(r.status, 401); let me = owner.me(); - assert_eq!((me.body["display_name"].as_str(), me.body["published"].as_bool()), (Some("Mine"), Some(true)), "the owner's record is untouched"); + assert_eq!((me.body["listing"]["display_name"].as_str(), me.body["published"].as_bool()), (Some("Mine"), Some(true)), "the owner's record is untouched"); } /// 8, 9, 10: tampering, time, and protocol version. @@ -115,28 +134,24 @@ fn authentication(s: &TestServer) { let path = path_heartbeat(&h.realm); let body = serde_json::to_vec(&h.heartbeat_body(60, false)).unwrap(); - // a body changed after it was signed h.ts += 1; let headers = sign_request(&h.key, "POST", &path, &h.realm, h.ts, &body); let mut tampered = body.clone(); tampered.extend_from_slice(b" "); let r = h.send_with("POST", &path, &tampered, &headers.pairs()); assert_eq!((r.status, r.code().as_str()), (401, "invalid_signature")); - // the method and the path are signed too - let r = h.send_with("POST", &path_unpublish(&h.realm), &serde_json::to_vec(&UnpublishRequest { protocol_version: 1 }).unwrap(), &headers.pairs()); + let r = h.send_with("POST", &path_unpublish(&h.realm), &serde_json::to_vec(&UnpublishRequest { protocol_version: 2 }).unwrap(), &headers.pairs()); assert_eq!((r.status, r.code().as_str()), (401, "invalid_signature"), "a signature for one endpoint is not valid for another"); let r = h.send_with("POST", &path, &body, &headers.pairs()); assert_eq!(r.status, 200, "the untampered request is fine: {:?}", r.body); let r = h.send_with("POST", &path, &body, &headers.pairs()); assert_eq!((r.status, r.code().as_str()), (401, "timestamp_not_monotonic"), "a captured request cannot be replayed"); - // timestamps outside the window, either way for skew in [-400, 400] { h.ts = s.time() + skew; let r = h.send("POST", &path, &body); assert_eq!((r.status, r.code().as_str()), (401, "bad_timestamp"), "skew {skew}"); } - // a timestamp that is in the window but not later than the last applied one h.ts = s.time() + 100; assert_eq!(h.send("POST", &path, &body).status, 200); h.ts = s.time() + 50; @@ -145,17 +160,18 @@ fn authentication(s: &TestServer) { h.ts = s.time() + 101; assert_eq!(h.send("POST", &path, &body).status, 200, "later ones are accepted again"); - // protocol version in the header and in the body - h.ts += 1; - let mut headers = sign_request(&h.key, "POST", &path, &h.realm, h.ts, &body).pairs(); - headers[0] = (HEADER_VERSION, "2".into()); - let r = h.send_with("POST", &path, &body, &headers); + // protocol version in the header and in the body (version 1 is refused like any other) + for wrong in ["1", "3"] { + h.ts += 1; + let mut headers = sign_request(&h.key, "POST", &path, &h.realm, h.ts, &body).pairs(); + headers[0] = (HEADER_VERSION, wrong.into()); + let r = h.send_with("POST", &path, &body, &headers); + assert_eq!((r.status, r.code().as_str()), (400, "unsupported_protocol_version"), "header {wrong}"); + } + let mut v1 = h.heartbeat_body(60, false); + v1.protocol_version = 1; + let r = h.send("POST", &path, &serde_json::to_vec(&v1).unwrap()); assert_eq!((r.status, r.code().as_str()), (400, "unsupported_protocol_version")); - let mut v2 = h.heartbeat_body(60, false); - v2.protocol_version = 2; - let r = h.send("POST", &path, &serde_json::to_vec(&v2).unwrap()); - assert_eq!((r.status, r.code().as_str()), (400, "unsupported_protocol_version")); - // missing or garbled headers let r = h.send_with("POST", &path, &body, &[]); assert_eq!(r.status, 400); let mut bad = sign_request(&h.key, "POST", &path, &h.realm, h.ts + 1, &body).pairs(); @@ -166,67 +182,91 @@ fn authentication(s: &TestServer) { assert_eq!(h.send_with("POST", &path, &body, &bad).status, 400); } +/// The one documented policy for protocol 1: it is refused, explicitly, on every endpoint, and nothing is stored or silently migrated. +fn old_protocol_is_refused(s: &TestServer) { + let mut h = Host::new(s, 11); + for (method, path) in [("POST", "/registry/v1/realms/register"), ("POST", &*format!("/registry/v1/realms/{}/heartbeat", h.realm)), ("POST", &*format!("/registry/v1/realms/{}/unpublish", h.realm)), ("GET", &*format!("/registry/v1/realms/{}", h.realm)), ("GET", "/registry/v1/healthz"), ("GET", "/registry/v1")] { + let r = h.send(method, path, if method == "GET" { b"" } else { br#"{"protocol_version":1}"# }); + assert_eq!((r.status, r.code().as_str()), (400, "unsupported_protocol_version"), "{method} {path}: {:?}", r.body); + assert!(r.body["error"]["message"].as_str().unwrap().contains("protocol version 1 is no longer supported")); + } + let mut check = Host::new(s, 11); + check.realm = h.realm; + check.ts = h.ts + 10; + assert_eq!(check.me().status, 404, "a refused v1 request created nothing"); +} + /// 11: hostile input is refused safely and stores nothing. fn hostile_input(s: &TestServer) { let mut h = Host::new(s, 7); - // oversized let big = vec![b'x'; MAX_REQUEST_BYTES + 1]; let r = h.send("POST", PATH_REGISTER, &big); assert_eq!((r.status, r.code().as_str()), (413, "request_too_large")); - // not JSON, not an object, deeply nested for body in [b"not json".as_slice(), b"[]", b"null", &b"{\"a\":".repeat(1)] { let r = h.send("POST", PATH_REGISTER, body); assert_eq!(r.status, 400, "{:?}", String::from_utf8_lossy(body)); } let nested = format!("{}1{}", "[".repeat(30_000), "]".repeat(30_000)); assert_eq!(h.send("POST", PATH_REGISTER, nested.as_bytes()).status, 400); - // metadata outside its limits, one at a time let base = serde_json::to_value(h.register_body("Hostile", 60)).unwrap(); - let cases: Vec<(&str, serde_json::Value)> = vec![ - ("display_name", json!("x".repeat(81))), - ("display_name", json!("")), - ("display_name", json!("evil\u{202E}name")), - ("display_name", json!("new\nline")), - ("description", json!("d".repeat(1025))), - ("language", json!("", "enabled": true}])), + (vec!["listing", "modules"], json!([{"id": "a", "enabled": true}, {"id": "a", "enabled": false}])), + (vec!["listing", "modules"], json!((0..65).map(|i| json!({"id": format!("m{i}"), "enabled": true})).collect::>())), + (vec!["listing", "modules"], json!([{"id": "ok", "version": "v <1>", "enabled": true}])), + (vec!["listing", "account_provisioning"], json!({"automatic": false, "existing_only": false})), + (vec!["population", "players"], json!(1_000_000)), + (vec!["population", "bots"], json!(-5)), + (vec!["listing_hash"], json!("0".repeat(64))), + (vec!["capabilities_hash"], json!("0".repeat(64))), + (vec!["public_key"], json!("short")), ]; - for (field, value) in cases { + for (path, value) in cases { let mut v = base.clone(); - v[field] = value; + let mut at = &mut v; + for key in &path[..path.len() - 1] { + at = &mut at[*key]; + } + at[path[path.len() - 1]] = value; let r = h.send("POST", PATH_REGISTER, &serde_json::to_vec(&v).unwrap()); - assert!(r.status == 400, "{field}: {} {:?}", r.status, r.body); + assert!(r.status == 400, "{path:?}: {} {:?}", r.status, r.body); } - // what the protocol has no place for is not accepted, whatever it is - for extra in ["character", "snapshot", "password", "ra_password", "db_credentials", "canonical_revision"] { + for extra in ["character", "snapshot", "password", "ra_password", "db_credentials", "canonical_revision", "ruleset", "level_cap", "game_mode", "ping"] { let mut v = base.clone(); v[extra] = json!("secret"); let r = h.send("POST", PATH_REGISTER, &serde_json::to_vec(&v).unwrap()); assert_eq!((r.status, r.code().as_str()), (400, "malformed_request"), "{extra}"); } let mut v = base.clone(); + v["listing"]["rates"]["ping"] = json!(5.0); + assert_eq!(h.send("POST", PATH_REGISTER, &serde_json::to_vec(&v).unwrap()).status, 400, "no unknown rate"); + let mut v = base.clone(); v["capabilities"]["content"]["client_catalog"]["../../etc/passwd"] = json!({"sha256": "a".repeat(64), "records": 1}); assert_eq!(h.send("POST", PATH_REGISTER, &serde_json::to_vec(&v).unwrap()).status, 400, "capabilities are checked field by field"); let mut v = base.clone(); v["capabilities"]["notes"] = json!("free text to store"); assert_eq!(h.send("POST", PATH_REGISTER, &serde_json::to_vec(&v).unwrap()).status, 400, "no free-form storage in the capabilities"); - // ids and paths let mut other = Host::new(s, 7); other.ts = h.ts; - let r = h.send("GET", "/registry/v1/realms/not-a-uuid", b""); + let r = h.send("GET", "/registry/v2/realms/not-a-uuid/self", b""); assert_eq!(r.status, 400); - let path = format!("/registry/v1/realms/{}", other.realm.to_string().to_uppercase()); + let path = format!("/registry/v2/realms/{}/self", other.realm.to_string().to_uppercase()); assert_eq!(h.send("GET", &path, b"").status, 400, "upper case is not the canonical spelling"); assert_eq!(h.send("GET", &format!("{}?x=1", path_self(&h.realm)), b"").status, 400, "no query on a signed endpoint"); let r = h.send("GET", &path_self(&other.realm), b""); assert_eq!(r.status, 400, "the realm header names another realm than the path"); - let r = h.send("GET", "/registry/v1/realms/018f2d9e-5c3a-4b21-8c4d-0e5f6a7b8c9d", b""); + let r = h.send("GET", "/registry/v2/realms/018f2d9e-5c3a-4b21-8c4d-0e5f6a7b8c9d/self", b""); assert_eq!(r.status, 400, "a version-4 UUID is not a realm id"); - // nothing was stored by any of it let mut clean = Host::new(s, 7); clean.realm = h.realm; clean.ts = h.ts + 100; @@ -257,11 +297,11 @@ fn unpublishing(s: &TestServer) { } fn healthz(s: &TestServer) { - let r = reqwest::blocking::get(format!("{}/registry/v1/healthz", s.base)).unwrap(); + let r = reqwest::blocking::get(format!("{}/registry/v2/healthz", s.base)).unwrap(); assert_eq!(r.status(), 200); let v: serde_json::Value = r.json().unwrap(); - assert_eq!((v["status"].as_str(), v["protocol_version"].as_u64()), (Some("ok"), Some(1))); - assert_eq!(reqwest::blocking::get(format!("{}/registry/v1/realms", s.base)).unwrap().status(), 404, "there is no public list"); + assert_eq!((v["status"].as_str(), v["protocol_version"].as_u64()), (Some("ok"), Some(2))); + assert_eq!(reqwest::blocking::get(format!("{}/registry/v2/nothing", s.base)).unwrap().status(), 404); assert_eq!(reqwest::blocking::get(format!("{}/admin", s.base)).unwrap().status(), 404, "and no admin surface"); } @@ -272,6 +312,7 @@ fn all(s: &TestServer) { metadata_changes(s); takeover(s); authentication(s); + old_protocol_is_refused(s); hostile_input(s); unpublishing(s); } @@ -281,6 +322,57 @@ fn the_protocol_on_the_memory_store() { all(&memory()); } +/// A record left by protocol 1 keeps its identity, key and creation time when its Host republishes under protocol 2. +#[test] +fn a_protocol_1_record_is_upgraded_in_place() { + let store = std::sync::Arc::new(MemoryStore::new()); + let s = spawn(store.clone(), lax()); + let mut h = Host::new(&s, 60); + let caps = caps(60); + let created = START - 86_400; + let old = RealmRow { + realm_id: h.realm, + public_key: *h.key.verifying_key().as_bytes(), + created_at: created, + updated_at: created, + last_seen_at: created, + published: true, + listing: Listing { display_name: "Old realm".into(), description: "from protocol 1".into(), language: "en".into(), region: None, rates: Rates::default(), modules: vec![], account_provisioning: AccountProvisioning { automatic: false, existing_only: true }, manager_version: "0.6.6".into() }, + listing_hash: "f".repeat(64), + ruleset: Ruleset::Coa, + level_cap: Some(60), + capabilities: caps.clone(), + capabilities_hash: caps.advert_hash(), + population: Population::default(), + metadata_revision: 5, + last_request_ts: s.time() - 1000, + advert_version: 1, + }; + store.put_for_test(old); + // a heartbeat of the new protocol with only a hash cannot upgrade it: the listing is asked for + h.ts = s.time(); + h.name = "Old realm".into(); + let r = h.heartbeat(60, false); + assert_eq!((r.status, r.body["resend_listing"].as_bool()), (200, Some(true)), "{:?}", r.body); + assert_eq!(store.get_for_test(&h.realm).unwrap().advert_version, 1, "nothing was applied"); + let body = h.heartbeat_with_listing(60, "Old realm"); + let path = path_heartbeat(&h.realm); + let r = h.send("POST", &path, &serde_json::to_vec(&body).unwrap()); + assert_eq!((r.status, r.body["resend_listing"].as_bool(), r.body["metadata_revision"].as_u64()), (200, Some(false), Some(6)), "{:?}", r.body); + let row = store.get_for_test(&h.realm).unwrap(); + assert_eq!((row.advert_version, ADVERT_VERSION, row.created_at, row.public_key), (2, 2, created, *h.key.verifying_key().as_bytes()), "identity, key and creation time are kept"); + assert_eq!(row.listing.rates.xp_kill, Some(2.0)); + + // and the other way: registering again as itself + let mut h2 = Host::new(&s, 61); + let old2 = RealmRow { realm_id: h2.realm, public_key: *h2.key.verifying_key().as_bytes(), advert_version: 1, last_request_ts: s.time() - 1000, ..store.get_for_test(&h.realm).unwrap() }; + store.put_for_test(old2); + h2.ts = s.time(); + let r = h2.register("Republished", 60); + assert_eq!((r.status, r.body["created"].as_bool(), r.body["metadata_revision"].as_u64()), (200, Some(false), Some(7)), "{:?}", r.body); + assert_eq!(store.get_for_test(&h2.realm).unwrap().created_at, created); +} + #[test] fn a_flood_is_limited() { let cfg = ApiConfig { register: Rate { burst: 3.0, refill: 0.0 }, heartbeat: Rate { burst: 2.0, refill: 0.0 }, general: Rate { burst: 1000.0, refill: 0.0 }, ..ApiConfig::default() }; @@ -305,7 +397,6 @@ fn a_flood_is_limited() { assert_eq!(other.register("Other", 60).status, 200); assert_eq!(other.heartbeat(60, false).status, 200, "another realm has its own allowance"); - // forged requests cost their sender, and never the realm they pretend to be let s = spawn(MemoryStore::new(), ApiConfig { general: Rate { burst: 12.0, refill: 0.0 }, heartbeat: Rate { burst: 2.0, refill: 0.0 }, ..ApiConfig::default() }); let mut victim = Host::new(&s, 40); assert_eq!(victim.register("Victim", 60).status, 200); @@ -318,7 +409,6 @@ fn a_flood_is_limited() { } assert_eq!(&statuses[..2], &[401, 401]); assert_eq!(*statuses.last().unwrap(), 429, "the forger runs out of allowance: {statuses:?}"); - // the victim's own per-realm allowance was never spent by the forgeries (its address allowance may be, which is the forger's address too here) } #[test] diff --git a/services/coa-registry/tests/common/mod.rs b/services/coa-registry/tests/common/mod.rs index edc37df..281468f 100644 --- a/services/coa-registry/tests/common/mod.rs +++ b/services/coa-registry/tests/common/mod.rs @@ -82,6 +82,8 @@ pub struct Host { pub key: SigningKey, pub realm: RealmId, pub ts: i64, + /// The display name the next heartbeats advertise. + pub name: String, http: reqwest::blocking::Client, } @@ -102,7 +104,7 @@ impl Reply { impl Host { pub fn new(server: &TestServer, seed: u8) -> Self { - Self { base: server.base.clone(), key: SigningKey::from_bytes(&[seed; 32]), realm: RealmId::new(), ts: server.time(), http: reqwest::blocking::Client::builder().timeout(std::time::Duration::from_secs(10)).build().unwrap() } + Self { base: server.base.clone(), key: SigningKey::from_bytes(&[seed; 32]), realm: RealmId::new(), ts: server.time(), name: "Test realm".into(), http: reqwest::blocking::Client::builder().timeout(std::time::Duration::from_secs(10)).build().unwrap() } } /// A client for a Registry that is not in this process (a real node): a random key, the real clock. @@ -111,7 +113,7 @@ impl Host { seed[..16].copy_from_slice(uuid_bytes().as_slice()); seed[16..].copy_from_slice(uuid_bytes().as_slice()); let ts = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() as i64; - Self { base: base.trim_end_matches('/').to_string(), key: SigningKey::from_bytes(&seed), realm: RealmId::new(), ts, http: reqwest::blocking::Client::builder().timeout(std::time::Duration::from_secs(20)).build().unwrap() } + Self { base: base.trim_end_matches('/').to_string(), key: SigningKey::from_bytes(&seed), realm: RealmId::new(), ts, name: "Test realm".into(), http: reqwest::blocking::Client::builder().timeout(std::time::Duration::from_secs(20)).build().unwrap() } } /// Bring the timestamp up to the real clock (after a pause); it never goes backwards. @@ -120,27 +122,47 @@ impl Host { self.ts = self.ts.max(now); } - pub fn register_body(&self, name: &str, level: u32) -> RegisterRequest { - let capabilities = caps(level); - RegisterRequest { - protocol_version: 1, - realm_id: self.realm, - public_key: encode_public_key(&self.key.verifying_key()), + pub fn listing(&self, name: &str) -> Listing { + Listing { display_name: name.into(), description: "A test realm.".into(), language: "en".into(), - ruleset: Ruleset::Coa, + region: Some("EU".into()), + rates: Rates { xp_kill: Some(2.0), xp_quest: Some(2.0), xp_explore: None, loot: Some(1.0), money: Some(1.5), reputation: None, honor: Some(1.0) }, + modules: vec![ModuleEntry { id: "playerbots".into(), version: Some("1.4.2".into()), enabled: true }, ModuleEntry { id: "content-scaling".into(), version: None, enabled: false }], + account_provisioning: AccountProvisioning { automatic: true, existing_only: false }, manager_version: "0.6.6".into(), - capabilities_hash: capabilities.advert_hash(), - capabilities, - player_count: Some(1), - player_capacity: Some(50), } } + pub fn register_body(&self, name: &str, level: u32) -> RegisterRequest { + let capabilities = caps(level); + let listing = self.listing(name); + RegisterRequest { + protocol_version: 2, + realm_id: self.realm, + public_key: encode_public_key(&self.key.verifying_key()), + listing_hash: listing.hash(), + listing, + capabilities_hash: capabilities.advert_hash(), + capabilities, + population: Population { players: 1, bots: 4, capacity: Some(50) }, + } + } + + /// A heartbeat of the realm as `register` described it, with the capabilities of `level` (and their hash) and optionally the parts themselves. pub fn heartbeat_body(&self, level: u32, with_caps: bool) -> HeartbeatRequest { let capabilities = caps(level); - HeartbeatRequest { protocol_version: 1, capabilities_hash: capabilities.advert_hash(), capabilities: with_caps.then_some(capabilities), manager_version: None, player_count: Some(2), player_capacity: Some(50), display_name: None, description: None, language: None } + let listing = self.listing(&self.name); + HeartbeatRequest { protocol_version: 2, listing_hash: listing.hash(), listing: None, capabilities_hash: capabilities.advert_hash(), capabilities: with_caps.then_some(capabilities), population: Population { players: 2, bots: 4, capacity: Some(50) } } + } + + pub fn heartbeat_with_listing(&self, level: u32, name: &str) -> HeartbeatRequest { + let mut hb = self.heartbeat_body(level, false); + let listing = self.listing(name); + hb.listing_hash = listing.hash(); + hb.listing = Some(listing); + hb } /// A fully signed request; the timestamp advances by one for every call. @@ -166,6 +188,7 @@ impl Host { } pub fn register(&mut self, name: &str, level: u32) -> Reply { + self.name = name.into(); let body = serde_json::to_vec(&self.register_body(name, level)).unwrap(); self.send("POST", PATH_REGISTER, &body) } @@ -177,7 +200,7 @@ impl Host { } pub fn unpublish(&mut self) -> Reply { - let body = serde_json::to_vec(&UnpublishRequest { protocol_version: 1 }).unwrap(); + let body = serde_json::to_vec(&UnpublishRequest { protocol_version: 2 }).unwrap(); let path = path_unpublish(&self.realm); self.send("POST", &path, &body) } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 650912d..5c11d95 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1788,9 +1788,9 @@ async fn registry_set_url(state: State<'_, AppState>, url: Option) -> st } #[tauri::command] -async fn registry_publish(state: State<'_, AppState>, local_id: String, display_name: String, description: String, language: String) -> std::result::Result<(), UiError> { +async fn registry_publish(state: State<'_, AppState>, local_id: String, display_name: String, description: String, language: String, region: Option) -> std::result::Result<(), UiError> { let rt = realm_registry(&state)?; - tauri::async_runtime::spawn_blocking(move || rt.publish(&local_id, &display_name, &description, &language)).await.map_err(|e| Error::Invalid(e.to_string()))?.map_err(UiError::from) + tauri::async_runtime::spawn_blocking(move || rt.publish(&local_id, &display_name, &description, &language, region.as_deref())).await.map_err(|e| Error::Invalid(e.to_string()))?.map_err(UiError::from) } #[tauri::command] diff --git a/src/i18n/locales/en.ts b/src/i18n/locales/en.ts index ad8b271..741d8f6 100644 --- a/src/i18n/locales/en.ts +++ b/src/i18n/locales/en.ts @@ -985,13 +985,14 @@ const en = { "fr.lan.unavailableText": "The selected address is not currently assigned to a detected local interface. Choose another address or switch to Automatic, or keep this address intentionally.", "fr.lan.invalid": "Invalid IPv4 address", "registry.title": "Public listing", - "registry.text": "Publish this realm in the central list so that players can find it. Only its name, description, language, version and capabilities are shared: no passwords, no characters, no accounts. The realm keeps running if the list is unreachable.", + "registry.text": "Publish this realm in the central list so that players can find it. Only its name, description, language, region, rates, modules, player counts, version and capabilities are shared: no passwords, no characters, no accounts. The realm keeps running if the list is unreachable.", "registry.url": "Registry address", "registry.urlSave": "Save address", "registry.name": "Realm name", "registry.description": "Description", "registry.descriptionHint": "Plain text, up to 1000 characters.", "registry.language": "Language", + "registry.region": "Region (optional)", "registry.publish": "Publish this realm", "registry.stop": "Stop publishing", "registry.retry": "Try again", diff --git a/src/i18n/locales/ru.ts b/src/i18n/locales/ru.ts index 8c93e3d..1befdff 100644 --- a/src/i18n/locales/ru.ts +++ b/src/i18n/locales/ru.ts @@ -983,13 +983,14 @@ const ru: Partial> = { "fr.lan.unavailableText": "Выбранный адрес сейчас не назначен обнаруженному сетевому интерфейсу. Выберите другой адрес, переключитесь на автоматический выбор или намеренно сохраните этот адрес.", "fr.lan.invalid": "Некорректный IPv4-адрес", "registry.title": "Публичный список", - "registry.text": "Опубликуйте этот реалм в общем списке, чтобы игроки могли его найти. Передаются только название, описание, язык, версия и возможности реалма: никаких паролей, персонажей и аккаунтов. Если список недоступен, реалм продолжает работать.", + "registry.text": "Опубликуйте этот реалм в общем списке, чтобы игроки могли его найти. Передаются только название, описание, язык, регион, рейты, модули, число игроков и ботов, версия и возможности реалма: никаких паролей, персонажей и аккаунтов. Если список недоступен, реалм продолжает работать.", "registry.url": "Адрес реестра", "registry.urlSave": "Сохранить адрес", "registry.name": "Название реалма", "registry.description": "Описание", "registry.descriptionHint": "Простой текст, до 1000 символов.", "registry.language": "Язык", + "registry.region": "Регион (необязательно)", "registry.publish": "Опубликовать реалм", "registry.stop": "Снять с публикации", "registry.retry": "Повторить", diff --git a/src/lib/registry.ts b/src/lib/registry.ts index 95032f8..90e5813 100644 --- a/src/lib/registry.ts +++ b/src/lib/registry.ts @@ -11,6 +11,7 @@ export interface RealmPublishStatus { display_name: string; description: string; language: string; + region: string | null; metadata_revision: number | null; last_ok_unix: number | null; last_error: string | null; @@ -25,8 +26,8 @@ export interface RegistryStatus { export const realmRegistry = { status: () => invoke("registry_status"), setUrl: (url: string | null) => invoke("registry_set_url", { url }), - publish: (localId: string, displayName: string, description: string, language: string) => - invoke("registry_publish", { localId, displayName, description, language }), + publish: (localId: string, displayName: string, description: string, language: string, region: string | null) => + invoke("registry_publish", { localId, displayName, description, language, region }), unpublish: (localId: string) => invoke("registry_unpublish", { localId }), retry: (localId: string) => invoke("registry_retry", { localId }), }; diff --git a/src/screens/PublishCard.tsx b/src/screens/PublishCard.tsx index 9a20b64..9ca70f3 100644 --- a/src/screens/PublishCard.tsx +++ b/src/screens/PublishCard.tsx @@ -20,12 +20,13 @@ export function PublishCard({ serverId, serverName }: { serverId: string; server const [name, setName] = useState(serverName); const [description, setDescription] = useState(""); const [language, setLanguage] = useState("en"); + const [region, setRegion] = useState(""); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); useEffect(() => { if (status && status.url !== null) setUrl((u) => (u === "" ? status.url ?? "" : u)); }, [status?.url]); useEffect(() => { - if (mine && mine.display_name) { setName(mine.display_name); setDescription(mine.description); setLanguage(mine.language || "en"); } + if (mine && mine.display_name) { setName(mine.display_name); setDescription(mine.description); setLanguage(mine.language || "en"); setRegion(mine.region ?? ""); } }, [mine?.display_name, mine?.description, mine?.language]); async function run(action: () => Promise) { @@ -57,8 +58,10 @@ export function PublishCard({ serverId, serverName }: { serverId: string; server + + setRegion(e.target.value)} className="mt-1 w-32 rounded-md border border-line bg-card px-3 py-2" />
- {!status?.url &&

{t("registry.noUrl")}

} diff --git a/tools/registry-vector-check.py b/tools/registry-vector-check.py index 72aaf6e..8cc464d 100644 --- a/tools/registry-vector-check.py +++ b/tools/registry-vector-check.py @@ -1,4 +1,4 @@ -"""Independent check of the documented Registry test vector with the RFC 8032 reference algorithm (pure Python, not the code under test).""" +"""Independent check of the documented Registry (protocol 2) test vector with the RFC 8032 reference algorithm (pure Python, not the code under test).""" import base64 import hashlib @@ -81,7 +81,7 @@ def sign(seed, msg): b64 = lambda b: base64.urlsafe_b64encode(b).decode().rstrip("=") seed = bytes([7]) * 32 realm = "018f2d9e-5c3a-7b21-8c4d-0e5f6a7b8c9d" -body = b'{"protocol_version":1}' -msg = "\n".join(["coa-registry-sig-v1", "1", "POST", f"/registry/v1/realms/{realm}/heartbeat", realm, "1790000000", hashlib.sha256(body).hexdigest()]).encode() +body = b'{"protocol_version":2}' +msg = "\n".join(["coa-registry-sig-v2", "2", "POST", f"/registry/v2/realms/{realm}/heartbeat", realm, "1790000000", hashlib.sha256(body).hexdigest()]).encode() print("public key ", b64(public(seed))) print("signature ", b64(sign(seed, msg)))