Phase 10: central realm Registry, Host publishing lifecycle, deployment

crates/coa-registry-proto (protocol 1: types, limits, signed-request canonicalisation, test vectors),
services/coa-registry (axum + PostgreSQL, migrations, rate limits, Dockerfile, deploy files),
services/coa-registry-e2e (Host lifecycle against the real service, live VPS gate),
coa-core realm_registry (identity keys, client, publishing loop on its own thread),
Settings -> Public listing card, docs REGISTRY_PROTOCOL.md and REGISTRY_DEPLOYMENT.md.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
ДмитрийandClaude Sonnet 5.5 committed 2026-10-07 20:23:13 +05:00
1 parent ec40f146aa
commit 1267bcc41e
56 files changed
+10198 -13

No files matched your search

+1
View File
@@ -36,6 +36,7 @@ pub mod allsettings;
pub mod modules;
pub mod realmlist;
pub mod realms;
pub mod realm_registry;
pub mod registry;
pub mod release;
pub mod report;
@@ -0,0 +1,179 @@
//! What a local realm advertises, and how the Host learns it.
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use coa_registry_proto::AdvertisedCapabilities;
use crate::portable::capabilities::RealmCapabilities;
use crate::portable::extension::ExtensionRegistry;
use crate::portable::realm::profile::probe_capabilities;
use crate::portable::service::access::{load_descriptors, RealmAccess};
use crate::{Error, Result};
/// The public form of a realm's capabilities: the profile as it is (same fields, same hash), bounded.
pub fn advertise(caps: &RealmCapabilities) -> Result<AdvertisedCapabilities> {
let value = serde_json::to_value(caps)?;
let advert: AdvertisedCapabilities = serde_json::from_value(value).map_err(|e| Error::Invalid(format!("the capabilities cannot be advertised: {e}")))?;
advert.validate().map_err(|e| Error::Invalid(e.to_string()))?;
Ok(advert)
}
#[derive(Clone, Debug, PartialEq)]
pub struct LocalAdvert {
/// The worldserver answers (database and console): only a running realm sends heartbeats.
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<AdvertisedCapabilities>,
pub player_count: Option<u32>,
pub player_capacity: Option<u32>,
pub manager_version: String,
}
/// Where the publishing loop gets a realm's current advertisement. Implementations must be quick or cache; they are called from the loop's thread.
pub trait AdvertSource: Send + Sync + 'static {
fn current(&self, local_id: &str) -> Option<LocalAdvert>;
}
struct Cached {
at: Instant,
advert: LocalAdvert,
caps_at: Option<Instant>,
}
struct Inner {
descriptors: PathBuf,
installs: Box<dyn Fn() -> Vec<(String, PathBuf)> + Send + Sync>,
registry: Arc<ExtensionRegistry>,
cache: Mutex<HashMap<String, Cached>>,
inflight: Mutex<HashSet<String>>,
fresh: Duration,
caps_every: Duration,
}
/// Probes the realms this Manager knows (installed servers and prepared descriptors) with the same code the portable engine uses.
///
/// Probing talks to the realm's database and console and can take many seconds, so it never runs on the caller's thread: `current` answers from
/// the last probe at once (`None` before the first one has finished) and starts a refresh in the background when that probe is older than a few seconds.
pub struct LocalRealmsSource {
inner: Arc<Inner>,
}
impl LocalRealmsSource {
pub fn new(descriptors: impl Into<PathBuf>, installs: impl Fn() -> Vec<(String, PathBuf)> + Send + Sync + 'static) -> Self {
Self {
inner: Arc::new(Inner {
descriptors: descriptors.into(),
installs: Box::new(installs),
registry: Arc::new(ExtensionRegistry::new()),
cache: Mutex::new(HashMap::new()),
inflight: Mutex::new(HashSet::new()),
fresh: Duration::from_secs(10),
caps_every: Duration::from_secs(60),
}),
}
}
}
impl Inner {
fn access(&self, local_id: &str) -> Option<RealmAccess> {
if let Some((id, root)) = (self.installs)().into_iter().find(|(id, _)| format!("srv-{id}") == local_id || id == local_id) {
return Some(RealmAccess::from_install(&id, &root));
}
let (descriptors, _) = load_descriptors(&self.descriptors);
descriptors.into_iter().find(|d| d.id == local_id).and_then(|d| RealmAccess::from_descriptor(d).ok())
}
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 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 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) {
if let Ok(a) = advertise(&caps) {
advert.capabilities = Some(a);
caps_at = Some(Instant::now());
}
}
}
}
}
if let Ok(mut cache) = self.cache.lock() {
cache.insert(local_id.to_string(), Cached { at: Instant::now(), advert, caps_at });
}
}
}
/// `Connected players: 3. Characters in world: 3.`
pub fn connected_players(server_info: &str) -> Option<u32> {
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<LocalAdvert> {
let (answer, stale) = match self.inner.cache.lock().ok()?.get(local_id) {
Some(c) => (Some(c.advert.clone()), c.at.elapsed() >= self.inner.fresh),
None => (None, true),
};
if stale && self.inner.inflight.lock().ok()?.insert(local_id.to_string()) {
let inner = self.inner.clone();
let id = local_id.to_string();
let spawned = std::thread::Builder::new().name("realm-probe".into()).spawn(move || {
inner.probe(&id);
if let Ok(mut f) = inner.inflight.lock() {
f.remove(&id);
}
});
if spawned.is_err() {
self.inner.inflight.lock().ok()?.remove(local_id);
}
}
answer
}
}
#[cfg(test)]
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::*;
use crate::portable::model::Ruleset;
use std::collections::{BTreeMap, BTreeSet};
let content = ContentProfile {
ruleset: Ruleset::Coa,
character_formats: ContentProfile::manager_formats(),
online_import_job_formats: vec![2],
session_protocol: 2,
collection_protocol: 1,
features: [Feature::RuntimeSessions, Feature::Wardrobe, Feature::Collections, Feature::LevelProjection].into_iter().collect::<BTreeSet<_>>(),
collection_kinds: ["coa:appearance".to_string(), "coa:vanity".to_string()].into_iter().collect(),
extensions: vec![ExtensionSupport { namespace: "coa".into(), module_version: "1".into(), formats: FormatRange::single(1) }],
client_catalog: BTreeMap::from([("Appearances.dbc".to_string(), CatalogEntry { sha256: "c".repeat(64), records: 7 })]),
};
let progression = Progression { max_player_level: 60, projection_protocol: 1, projection_policy_version: 1, progression_signature: "d".repeat(64), scaling_enabled: true };
let core = Some(CoreIdentity { commit: "0f3737574afd".into(), branch: "feat/x".into(), date: "2026-10-01".into() });
let caps = RealmCapabilities::build(core, content, Some(progression)).unwrap();
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");
}
}
@@ -0,0 +1,116 @@
//! The Host's HTTP client of Registry protocol 1. Blocking, with hard timeouts; every mutating request is signed with the realm's own key.
use std::time::Duration;
use coa_registry_proto::sign::sign_request;
use coa_registry_proto::*;
use ed25519_dalek::SigningKey;
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::result::Result;
/// Why a request did not succeed, in the terms the retry policy needs.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ClientError {
/// The network, a timeout, a 5xx or a rate limit: the same request may work later.
Transient(String),
/// The Registry understood the request and refused it with this code.
Rejected { code: ErrorCode, status: u16, message: String },
/// The answer is not Registry protocol 1.
Protocol(String),
}
impl ClientError {
/// Repeating cannot help: the Host stops instead of retrying.
pub fn is_permanent(&self) -> bool {
match self {
ClientError::Transient(_) => false,
ClientError::Rejected { code, .. } => code.is_permanent(),
ClientError::Protocol(_) => true,
}
}
pub fn code(&self) -> Option<ErrorCode> {
match self {
ClientError::Rejected { code, .. } => Some(*code),
_ => None,
}
}
}
impl std::fmt::Display for ClientError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ClientError::Transient(m) => write!(f, "the Registry could not be reached: {m}"),
ClientError::Rejected { code, status, message } => write!(f, "the Registry refused the request ({status} {code:?}): {message}"),
ClientError::Protocol(m) => write!(f, "the Registry answered something that is not protocol {REGISTRY_PROTOCOL_VERSION}: {m}"),
}
}
}
impl std::error::Error for ClientError {}
pub struct RegistryClient {
base: String,
http: reqwest::blocking::Client,
}
impl RegistryClient {
pub fn new(base: &str) -> crate::Result<Self> {
let base = super::settings::validate_url(base)?;
let http = reqwest::blocking::Client::builder()
.timeout(Duration::from_secs(10))
.connect_timeout(Duration::from_secs(5))
.redirect(reqwest::redirect::Policy::none())
.user_agent(concat!("coa-manager/", env!("CARGO_PKG_VERSION")))
.build()
.map_err(|e| crate::Error::Invalid(e.to_string()))?;
Ok(Self { base, http })
}
pub fn base(&self) -> &str {
&self.base
}
fn send<R: DeserializeOwned>(&self, method: &str, path: &str, body: Vec<u8>, key: &SigningKey, realm: &RealmId, ts: i64) -> Result<R, ClientError> {
let headers = sign_request(key, method, path, realm, ts, &body);
let url = format!("{}{}", self.base, path);
let mut req = if method == "GET" { self.http.get(url) } else { self.http.post(url).header("content-type", "application/json").body(body) };
for (name, value) in headers.pairs() {
req = req.header(name, value);
}
let resp = req.send().map_err(|e| ClientError::Transient(e.without_url().to_string()))?;
let status = resp.status();
let bytes = resp.bytes().map_err(|e| ClientError::Transient(e.without_url().to_string()))?;
if status.is_success() {
return serde_json::from_slice(&bytes).map_err(|_| ClientError::Protocol("the answer cannot be read".into()));
}
match serde_json::from_slice::<ErrorBody>(&bytes) {
Ok(e) if status.as_u16() == 429 || status.as_u16() == 503 => Err(ClientError::Transient(format!("{:?}: {}", e.error.code, e.error.message))),
Ok(e) => Err(ClientError::Rejected { code: e.error.code, status: status.as_u16(), message: e.error.message }),
Err(_) if status.is_server_error() || status.as_u16() == 408 || status.as_u16() == 429 => Err(ClientError::Transient(format!("HTTP {status}"))),
Err(_) => Err(ClientError::Protocol(format!("HTTP {status} without a Registry error body"))),
}
}
fn post<B: Serialize, R: DeserializeOwned>(&self, path: &str, body: &B, key: &SigningKey, realm: &RealmId, ts: i64) -> Result<R, ClientError> {
let bytes = serde_json::to_vec(body).map_err(|e| ClientError::Protocol(e.to_string()))?;
self.send("POST", path, bytes, key, realm, ts)
}
pub fn register(&self, key: &SigningKey, req: &RegisterRequest, ts: i64) -> Result<RegisterResponse, ClientError> {
self.post(PATH_REGISTER, req, key, &req.realm_id, ts)
}
pub fn heartbeat(&self, key: &SigningKey, realm: &RealmId, req: &HeartbeatRequest, ts: i64) -> Result<HeartbeatResponse, ClientError> {
self.post(&path_heartbeat(realm), req, key, realm, ts)
}
pub fn unpublish(&self, key: &SigningKey, realm: &RealmId, ts: i64) -> Result<UnpublishResponse, ClientError> {
self.post(&path_unpublish(realm), &UnpublishRequest { protocol_version: REGISTRY_PROTOCOL_VERSION }, key, realm, ts)
}
/// The Registry's own record of this realm (the authenticated read).
pub fn self_info(&self, key: &SigningKey, realm: &RealmId, ts: i64) -> Result<SelfResponse, ClientError> {
self.send("GET", &path_self(realm), Vec::new(), key, realm, ts)
}
}
+550
View File
@@ -0,0 +1,550 @@
//! The publishing lifecycle of a Host Manager, independent of Player Mode and of the portable engine.
//!
//! ```text
//! publish enabled -> ensure the realm's identity (UUIDv7 + Ed25519 key) -> register -> heartbeat every ~30 s
//! the realm's capabilities changed -> the next heartbeat carries them
//! Manager restart -> the same RealmId and key -> register again as itself -> heartbeat
//! publish disabled -> unpublish (retried in the background until it is delivered or refused)
//! ```
//!
//! Nothing here touches the realm's database writes, the worldserver or any portable store: the loop only *reads* what a realm advertises, and a
//! Registry that is down, slow or hostile changes nothing but this module's own status. Transient failures are retried with a capped exponential
//! backoff and jitter; a refusal that repeating cannot fix (bad signature, wrong key, unsupported protocol, invalid metadata) stops the realm's
//! publication until the owner acts.
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::{Duration, Instant};
use coa_registry_proto::sign::encode_public_key;
use coa_registry_proto::*;
use ed25519_dalek::SigningKey;
use serde::Serialize;
use super::advert::{AdvertSource, LocalAdvert};
use super::client::{ClientError, RegistryClient};
use super::keys::KeyStore;
use super::settings::{self, PublishConfig, RegistrySettings};
use crate::{Error, Result};
pub type Clock = Arc<dyn Fn() -> i64 + Send + Sync>;
pub fn system_clock() -> Clock {
Arc::new(|| std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_secs() as i64).unwrap_or(0))
}
#[derive(Clone, Debug)]
pub struct Timing {
pub heartbeat: Duration,
pub backoff_base: Duration,
pub backoff_cap: Duration,
/// How long to wait before looking again at a realm that is not running or not known yet (no network involved).
pub idle_poll: Duration,
/// Clock-skew refusals are retried this often at most; a clock that stays wrong is the owner's to fix.
pub max_clock_failures: u32,
pub max_unpublish_attempts: u32,
}
impl Default for Timing {
fn default() -> Self {
Self { heartbeat: Duration::from_secs(HEARTBEAT_INTERVAL_SECS), backoff_base: Duration::from_secs(5), backoff_cap: Duration::from_secs(300), idle_poll: Duration::from_secs(5), max_clock_failures: 20, max_unpublish_attempts: 12 }
}
}
/// `base * 2^(attempt-1)` capped, then spread by +-25% with `unit` in `[0, 1)`.
pub fn backoff(attempt: u32, base: Duration, cap: Duration, unit: f64) -> Duration {
let exp = attempt.saturating_sub(1).min(20);
let raw = base.as_secs_f64() * f64::from(1u32 << exp);
let capped = raw.min(cap.as_secs_f64());
Duration::from_secs_f64(capped * (0.75 + 0.5 * unit.clamp(0.0, 0.999_999)))
}
fn unit() -> f64 {
let b = uuid::Uuid::new_v4();
let n = u64::from_le_bytes(b.as_bytes()[..8].try_into().expect("8 bytes"));
(n >> 11) as f64 / (1u64 << 53) as f64
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum PublishState {
/// Waiting for the first attempt.
Starting,
/// The realm is not known, not running or has no capabilities yet: nothing is sent.
WaitingForRealm,
RealmStopped,
Online,
/// The Registry could not be reached (or asked to slow down); trying again.
Retrying,
/// The Registry refused the request and repeating cannot change that.
Rejected,
Unpublishing,
Disabled,
}
#[derive(Clone, Debug, Serialize)]
pub struct RealmPublishStatus {
pub local_id: String,
pub realm_id: Option<String>,
pub enabled: bool,
pub state: PublishState,
pub display_name: String,
pub description: String,
pub language: String,
pub metadata_revision: Option<u64>,
pub last_ok_unix: Option<i64>,
pub last_error: Option<String>,
pub retry_in_secs: Option<u64>,
}
#[derive(Clone, Debug, Default, Serialize)]
pub struct RegistryStatus {
pub url: Option<String>,
pub realms: Vec<RealmPublishStatus>,
}
enum Phase {
Register,
Beat,
}
struct Publisher {
cfg: PublishConfig,
realm: Option<RealmId>,
key: Option<SigningKey>,
phase: Phase,
state: PublishState,
/// `None`: nothing to do until the owner acts (a permanent refusal).
due: Option<Instant>,
failures: u32,
clock_failures: u32,
unpublish_attempts: u32,
acked_hash: Option<String>,
sent_meta: Option<(String, String, String)>,
revision: Option<u64>,
last_ok: Option<i64>,
last_error: Option<String>,
last_ts: i64,
}
impl Publisher {
fn new(cfg: PublishConfig, now: Instant) -> Self {
let enabled = cfg.enabled;
Self {
cfg,
realm: None,
key: None,
phase: Phase::Register,
state: if enabled { PublishState::Starting } else { PublishState::Disabled },
due: enabled.then_some(now),
failures: 0,
clock_failures: 0,
unpublish_attempts: 0,
acked_hash: None,
sent_meta: None,
revision: None,
last_ok: None,
last_error: None,
last_ts: 0,
}
}
fn reset_progress(&mut self, now: Instant) {
self.phase = Phase::Register;
self.failures = 0;
self.clock_failures = 0;
self.acked_hash = None;
self.sent_meta = None;
self.last_error = None;
self.state = PublishState::Starting;
self.due = Some(now);
}
}
pub struct RegistryHost {
dir: PathBuf,
settings: RegistrySettings,
keys: Arc<dyn KeyStore>,
source: Arc<dyn AdvertSource>,
clock: Clock,
timing: Timing,
client: Option<RegistryClient>,
publishers: BTreeMap<String, Publisher>,
}
impl RegistryHost {
pub fn open(dir: &Path, keys: Arc<dyn KeyStore>, source: Arc<dyn AdvertSource>, clock: Clock, timing: Timing, now: Instant) -> Result<Self> {
let settings = settings::load(dir)?;
let client = settings.url.as_deref().map(RegistryClient::new).transpose()?;
let mut host = Self { dir: dir.to_path_buf(), settings, keys, source, clock, timing, client, publishers: BTreeMap::new() };
for (local, cfg) in host.settings.realms.clone() {
let mut p = Publisher::new(cfg.clone(), now);
if cfg.enabled {
host.attach_identity(&mut p);
}
host.publishers.insert(local, p);
}
Ok(host)
}
pub fn url(&self) -> Option<&str> {
self.settings.url.as_deref()
}
/// The identity of an enabled realm: its id and key must both exist; a missing key is not replaced behind the owner's back.
fn attach_identity(&self, p: &mut Publisher) {
let Some(id) = p.cfg.realm_id else {
p.state = PublishState::Rejected;
p.due = None;
p.last_error = Some("the realm has no identity yet".into());
return;
};
p.realm = Some(id);
match self.keys.load(&id) {
Ok(Some(key)) => p.key = Some(key),
Ok(None) => {
p.state = PublishState::Rejected;
p.due = None;
p.last_error = Some("the realm's key is missing on this machine; stop publishing and publish again to create a new identity".into());
}
Err(e) => {
p.state = PublishState::Rejected;
p.due = None;
p.last_error = Some(format!("the realm's key cannot be read: {e}"));
}
}
}
fn save(&self) -> Result<()> {
settings::save(&self.dir, &self.settings)
}
pub fn set_url(&mut self, url: Option<&str>, now: Instant) -> Result<()> {
let url = match url.map(str::trim).filter(|u| !u.is_empty()) {
Some(u) => Some(settings::validate_url(u)?),
None => None,
};
if url == self.settings.url {
return Ok(());
}
self.client = url.as_deref().map(RegistryClient::new).transpose()?;
self.settings.url = url;
self.save()?;
for p in self.publishers.values_mut().filter(|p| p.cfg.enabled && p.key.is_some()) {
p.reset_progress(now);
}
Ok(())
}
/// 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<()> {
if self.client.is_none() {
return Err(Error::Invalid("set the Registry address first".into()));
}
let mut cfg = self.settings.realms.get(local_id).cloned().unwrap_or_default();
cfg.display_name = display_name.trim().to_string();
cfg.description = description.trim().to_string();
cfg.language = language.trim().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
if let Some(id) = cfg.realm_id {
if matches!(self.keys.load(&id), Ok(None)) {
cfg.realm_id = None;
}
}
let created = if cfg.realm_id.is_none() {
let id = RealmId::new();
self.keys.create(&id)?;
cfg.realm_id = Some(id);
true
} else {
false
};
self.settings.realms.insert(local_id.to_string(), cfg.clone());
if let Err(e) = self.save() {
if created {
if let Some(id) = cfg.realm_id {
let _ = self.keys.remove(&id);
}
}
self.settings.realms.remove(local_id);
return Err(e);
}
let mut p = Publisher::new(cfg, now);
self.attach_identity(&mut p);
if let Some(old) = self.publishers.get(local_id) {
p.last_ok = old.last_ok;
p.last_ts = old.last_ts;
}
self.publishers.insert(local_id.to_string(), p);
Ok(())
}
/// Stop publishing: the realm is unpublished at the Registry (retried in the background); its identity is kept for a later republication.
pub fn unpublish(&mut self, local_id: &str, now: Instant) -> Result<()> {
let Some(cfg) = self.settings.realms.get_mut(local_id) else { return Ok(()) };
cfg.enabled = false;
let cfg = cfg.clone();
self.save()?;
let Some(p) = self.publishers.get_mut(local_id) else { return Ok(()) };
let was_registered = p.realm.is_some() && p.key.is_some() && (p.revision.is_some() || matches!(p.phase, Phase::Beat));
p.cfg = cfg;
p.unpublish_attempts = 0;
p.failures = 0;
if was_registered && self.client.is_some() {
p.state = PublishState::Unpublishing;
p.due = Some(now);
} else {
p.state = PublishState::Disabled;
p.due = None;
}
Ok(())
}
/// Try again now (after a refusal the owner has dealt with).
pub fn retry(&mut self, local_id: &str, now: Instant) {
if let Some(p) = self.publishers.get_mut(local_id) {
if p.cfg.enabled && p.key.is_some() {
p.reset_progress(now);
}
}
}
pub fn status(&self, now: Instant) -> RegistryStatus {
let realms = self
.publishers
.iter()
.map(|(local, p)| RealmPublishStatus {
local_id: local.clone(),
realm_id: p.cfg.realm_id.map(|r| r.to_string()),
enabled: p.cfg.enabled,
state: p.state,
display_name: p.cfg.display_name.clone(),
description: p.cfg.description.clone(),
language: p.cfg.language.clone(),
metadata_revision: p.revision,
last_ok_unix: p.last_ok,
last_error: p.last_error.clone(),
retry_in_secs: p.due.map(|d| d.saturating_duration_since(now).as_secs()),
})
.collect();
RegistryStatus { url: self.settings.url.clone(), realms }
}
/// The public key a published realm is known by (never the private one).
pub fn public_key(&self, local_id: &str) -> Option<String> {
self.publishers.get(local_id).and_then(|p| p.key.as_ref()).map(|k| encode_public_key(&k.verifying_key()))
}
/// 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<SelfResponse, ClientError> {
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)) => {
let ts = self.next_ts(&mut p);
client.self_info(&key, &realm, ts)
}
_ => Err(ClientError::Protocol("the realm has no identity or the Registry address is not set".into())),
};
self.publishers.insert(local_id.to_string(), p);
out
}
pub fn realm_id(&self, local_id: &str) -> Option<RealmId> {
self.publishers.get(local_id).and_then(|p| p.realm)
}
fn next_ts(&self, p: &mut Publisher) -> i64 {
let ts = (self.clock)().max(p.last_ts + 1);
p.last_ts = ts;
ts
}
/// One pass over the realms that are due. Network calls block (with their own timeouts); nothing else does.
pub fn tick(&mut self, now: Instant) {
let due: Vec<String> = self.publishers.iter().filter(|(_, p)| p.due.is_some_and(|d| d <= now)).map(|(k, _)| k.clone()).collect();
for local in due {
let Some(mut p) = self.publishers.remove(&local) else { continue };
self.step(&local, &mut p, now);
self.publishers.insert(local, p);
}
}
fn step(&self, local: &str, p: &mut Publisher, now: Instant) {
let (Some(client), Some(realm), Some(key)) = (self.client.as_ref(), p.realm, p.key.clone()) else {
p.due = None;
return;
};
if p.state == PublishState::Unpublishing {
return self.step_unpublish(client, &realm, &key, p, now);
}
let Some(advert) = self.source.current(local) else {
p.state = PublishState::WaitingForRealm;
p.due = Some(now + self.timing.idle_poll);
return;
};
let result = match p.phase {
Phase::Register => self.register(client, &realm, &key, p, &advert, now),
Phase::Beat => self.beat(client, &realm, &key, p, &advert, now),
};
if let Err(e) = result {
self.on_error(p, e, now);
}
}
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 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(),
capabilities_hash: caps.advert_hash(),
capabilities: caps,
player_count: advert.player_count,
player_capacity: advert.player_capacity,
};
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));
self.ok(p, resp.metadata_revision, now);
Ok(())
}
fn beat(&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 = PublishState::RealmStopped;
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 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()),
};
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);
p.due = Some(now + Duration::from_secs(1));
} else {
p.acked_hash = Some(resp.capabilities_hash);
self.ok(p, resp.metadata_revision, now);
}
Ok(())
}
fn ok(&self, p: &mut Publisher, revision: u64, now: Instant) {
p.state = PublishState::Online;
p.failures = 0;
p.clock_failures = 0;
p.last_error = None;
p.revision = Some(revision);
p.last_ok = Some((self.clock)());
p.due = Some(now + self.timing.heartbeat.mul_f64(0.9 + 0.2 * unit()));
}
fn on_error(&self, p: &mut Publisher, e: ClientError, now: Instant) {
p.last_error = Some(e.to_string());
match &e {
ClientError::Rejected { code: ErrorCode::UnknownRealm | ErrorCode::NotPublished, .. } => {
p.phase = Phase::Register;
p.acked_hash = 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()) });
}
ClientError::Rejected { code: ErrorCode::BadTimestamp | ErrorCode::TimestampNotMonotonic, .. } => {
p.clock_failures += 1;
p.failures += 1;
if p.clock_failures > self.timing.max_clock_failures {
p.state = PublishState::Rejected;
p.due = None;
} else {
p.state = PublishState::Retrying;
p.due = Some(now + backoff(p.failures, self.timing.backoff_base, self.timing.backoff_cap, unit()));
}
}
e if e.is_permanent() => {
p.state = PublishState::Rejected;
p.due = None;
}
_ => {
p.failures += 1;
p.state = PublishState::Retrying;
p.due = Some(now + backoff(p.failures, self.timing.backoff_base, self.timing.backoff_cap, unit()));
}
}
}
fn step_unpublish(&self, client: &RegistryClient, realm: &RealmId, key: &SigningKey, p: &mut Publisher, now: Instant) {
let ts = self.next_ts(p);
match client.unpublish(key, realm, ts) {
Ok(resp) => {
p.revision = Some(resp.metadata_revision);
p.last_error = None;
p.state = PublishState::Disabled;
p.due = None;
}
Err(ClientError::Rejected { code: ErrorCode::UnknownRealm, .. }) => {
p.state = PublishState::Disabled;
p.due = None;
}
Err(e) => {
p.last_error = Some(e.to_string());
p.unpublish_attempts += 1;
if e.is_permanent() || p.unpublish_attempts >= self.timing.max_unpublish_attempts {
p.state = PublishState::Disabled;
p.due = None;
} else {
p.due = Some(now + backoff(p.unpublish_attempts, self.timing.backoff_base, self.timing.backoff_cap, unit()));
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_backoff_grows_is_capped_and_is_spread() {
let (base, cap) = (Duration::from_secs(5), Duration::from_secs(300));
let mid = |n| backoff(n, base, cap, 0.5).as_secs_f64();
assert!((mid(1) - 5.0).abs() < 0.01 && (mid(2) - 10.0).abs() < 0.01 && (mid(3) - 20.0).abs() < 0.01);
assert!((mid(10) - 300.0).abs() < 0.01 && (mid(500) - 300.0).abs() < 0.01, "capped");
assert!(backoff(4, base, cap, 0.0) < backoff(4, base, cap, 0.999), "jitter spreads");
assert!(backoff(30, base, cap, 0.999) <= Duration::from_secs_f64(300.0 * 1.25));
assert!(backoff(1, base, cap, 0.0).as_secs_f64() >= 3.7);
for _ in 0..1000 {
let u = unit();
assert!((0.0..1.0).contains(&u));
}
}
}
+160
View File
@@ -0,0 +1,160 @@
//! A realm's Ed25519 identity key: one per realm, generated on first publication, never leaving this machine.
//!
//! Storage is behind [`KeyStore`]. The implementation here keeps one file per realm in the Manager's own data folder, readable by the current user
//! only (mode 0600 on Unix; on Windows the inherited permissions are removed and the current user is granted access with `icacls`). That is
//! weaker than an OS-protected secret store (DPAPI / Credential Manager), which this project does not use yet: anybody who can read the user's
//! files can read the key. The key is never written to the realm descriptor, a log, the diagnostics or the Registry.
use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};
use base64::engine::general_purpose::URL_SAFE_NO_PAD as B64;
use base64::Engine;
use coa_registry_proto::RealmId;
use ed25519_dalek::SigningKey;
use crate::{Error, Result};
pub trait KeyStore: Send + Sync {
/// The key of this realm, if one was created.
fn load(&self, realm: &RealmId) -> Result<Option<SigningKey>>;
/// A new random key for a realm that has none. Refuses to replace an existing one.
fn create(&self, realm: &RealmId) -> Result<SigningKey>;
fn remove(&self, realm: &RealmId) -> Result<()>;
}
pub struct FileKeyStore {
dir: PathBuf,
}
impl FileKeyStore {
pub fn new(dir: impl Into<PathBuf>) -> Self {
Self { dir: dir.into() }
}
fn file(&self, realm: &RealmId) -> PathBuf {
self.dir.join(format!("{realm}.key"))
}
}
fn random_seed() -> [u8; 32] {
let mut seed = [0u8; 32];
seed[..16].copy_from_slice(uuid::Uuid::new_v4().as_bytes());
seed[16..].copy_from_slice(uuid::Uuid::new_v4().as_bytes());
seed
}
#[cfg(unix)]
fn restrict(file: &Path) -> Result<()> {
use std::os::unix::fs::PermissionsExt;
fs::set_permissions(file, fs::Permissions::from_mode(0o600))?;
Ok(())
}
#[cfg(windows)]
fn restrict(file: &Path) -> Result<()> {
let user = std::env::var("USERNAME").map_err(|_| Error::Invalid("the current user is unknown".into()))?;
let domain = std::env::var("USERDOMAIN").unwrap_or_default();
let who = if domain.is_empty() { user } else { format!("{domain}\\{user}") };
let out = std::process::Command::new("icacls").arg(file).arg("/inheritance:r").arg("/grant:r").arg(format!("{who}:(F)")).output()?;
if !out.status.success() {
return Err(Error::Invalid("the key file's permissions could not be restricted".into()));
}
Ok(())
}
#[cfg(not(any(unix, windows)))]
fn restrict(_file: &Path) -> Result<()> {
Ok(())
}
impl KeyStore for FileKeyStore {
fn load(&self, realm: &RealmId) -> Result<Option<SigningKey>> {
let text = match fs::read_to_string(self.file(realm)) {
Ok(t) => t,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
Err(e) => return Err(e.into()),
};
let bytes = B64.decode(text.trim()).map_err(|_| Error::Invalid("a realm key file is not valid".into()))?;
let seed: [u8; 32] = bytes.try_into().map_err(|_| Error::Invalid("a realm key file is not valid".into()))?;
Ok(Some(SigningKey::from_bytes(&seed)))
}
fn create(&self, realm: &RealmId) -> Result<SigningKey> {
fs::create_dir_all(&self.dir)?;
let path = self.file(realm);
if path.exists() {
return Err(Error::Invalid("this realm already has a key".into()));
}
let seed = random_seed();
let tmp = self.dir.join(format!(".{realm}.{}.tmp", uuid::Uuid::new_v4().simple()));
let write = || -> Result<()> {
let mut f = fs::OpenOptions::new().write(true).create_new(true).open(&tmp)?;
restrict(&tmp)?;
f.write_all(B64.encode(seed).as_bytes())?;
f.sync_all()?;
drop(f);
fs::rename(&tmp, &path)?;
Ok(())
};
if let Err(e) = write() {
let _ = fs::remove_file(&tmp);
return Err(e);
}
Ok(SigningKey::from_bytes(&seed))
}
fn remove(&self, realm: &RealmId) -> Result<()> {
match fs::remove_file(self.file(realm)) {
Ok(()) => Ok(()),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
Err(e) => Err(e.into()),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_key_is_created_once_and_read_back() {
let dir = tempfile::tempdir().unwrap();
let store = FileKeyStore::new(dir.path().join("keys"));
let realm = RealmId::new();
assert!(store.load(&realm).unwrap().is_none());
let key = store.create(&realm).unwrap();
assert_eq!(store.load(&realm).unwrap().unwrap().to_bytes(), key.to_bytes());
assert!(store.create(&realm).is_err(), "a key is never replaced");
let other = RealmId::new();
assert_ne!(store.create(&other).unwrap().to_bytes(), key.to_bytes(), "one key per realm");
store.remove(&realm).unwrap();
assert!(store.load(&realm).unwrap().is_none());
store.remove(&realm).unwrap();
let leftovers: Vec<_> = fs::read_dir(dir.path().join("keys")).unwrap().flatten().filter(|e| e.file_name().to_string_lossy().ends_with(".tmp")).collect();
assert!(leftovers.is_empty());
}
#[test]
fn a_damaged_key_file_is_an_error_not_a_new_identity() {
let dir = tempfile::tempdir().unwrap();
let store = FileKeyStore::new(dir.path());
let realm = RealmId::new();
fs::write(dir.path().join(format!("{realm}.key")), "garbage").unwrap();
assert!(store.load(&realm).is_err());
assert!(store.create(&realm).is_err(), "the broken file is not silently replaced");
}
#[cfg(windows)]
#[test]
fn the_key_file_is_not_inherited_from_the_folder() {
let dir = tempfile::tempdir().unwrap();
let store = FileKeyStore::new(dir.path());
let realm = RealmId::new();
store.create(&realm).unwrap();
let out = std::process::Command::new("icacls").arg(dir.path().join(format!("{realm}.key"))).output().unwrap();
let text = String::from_utf8_lossy(&out.stdout).to_string();
assert!(!text.contains("BUILTIN\\Users") && !text.contains("Everyone") && !text.contains("Authenticated Users"), "{text}");
}
}
+19
View File
@@ -0,0 +1,19 @@
//! Publishing a realm to the central Registry (Phase 10): the Host side of registration and presence.
//!
//! The Registry is discovery infrastructure only. What this module sends is the realm's public identity (a UUIDv7 and an Ed25519 public key), a
//! display name, a description, a language, the Manager version, the realm's advertised capabilities and optionally its player count. It never
//! sends or reads a database or console credential, a game account, a character, a canonical revision or a session payload, and it never changes
//! portable state. See `docs/REGISTRY_PROTOCOL.md`.
pub mod advert;
pub mod client;
pub mod host;
pub mod keys;
pub mod runtime;
pub mod settings;
pub use advert::{advertise, AdvertSource, LocalAdvert, LocalRealmsSource};
pub use client::{ClientError, RegistryClient};
pub use host::{PublishState, RealmPublishStatus, RegistryHost, RegistryStatus, Timing};
pub use keys::{FileKeyStore, KeyStore};
pub use runtime::RegistryRuntime;
@@ -0,0 +1,107 @@
//! The publishing loop on its own thread: it starts with the application, keeps going while the interface is elsewhere, and stops with the Manager.
//! Commands are answered between passes; a pass blocks only on the Registry's own timeouts.
use std::path::Path;
use std::sync::mpsc::{self, RecvTimeoutError, Sender};
use std::sync::{Arc, Mutex};
use std::thread::JoinHandle;
use std::time::{Duration, Instant};
use super::advert::AdvertSource;
use super::host::{system_clock, Clock, RegistryHost, RegistryStatus, Timing};
use super::keys::KeyStore;
use crate::{Error, Result};
enum Cmd {
SetUrl(Option<String>, Sender<Result<()>>),
Publish { local_id: String, name: String, description: String, language: String, reply: Sender<Result<()>> },
Unpublish(String, Sender<Result<()>>),
Retry(String),
Shutdown,
}
pub struct RegistryRuntime {
tx: Sender<Cmd>,
status: Arc<Mutex<RegistryStatus>>,
join: Mutex<Option<JoinHandle<()>>>,
}
impl RegistryRuntime {
pub fn start(dir: &Path, keys: Arc<dyn KeyStore>, source: Arc<dyn AdvertSource>) -> Result<Self> {
Self::start_with(dir, keys, source, system_clock(), Timing::default())
}
pub fn start_with(dir: &Path, keys: Arc<dyn KeyStore>, source: Arc<dyn AdvertSource>, clock: Clock, timing: Timing) -> Result<Self> {
let mut host = RegistryHost::open(dir, keys, source, clock, timing, Instant::now())?;
let status = Arc::new(Mutex::new(host.status(Instant::now())));
let (tx, rx) = mpsc::channel::<Cmd>();
let shared = status.clone();
let join = std::thread::Builder::new()
.name("realm-registry".into())
.spawn(move || loop {
match rx.recv_timeout(Duration::from_millis(500)) {
Ok(Cmd::Shutdown) | Err(RecvTimeoutError::Disconnected) => return,
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::Unpublish(local_id, reply)) => {
let _ = reply.send(host.unpublish(&local_id, Instant::now()));
}
Ok(Cmd::Retry(local_id)) => host.retry(&local_id, Instant::now()),
Err(RecvTimeoutError::Timeout) => {}
}
let now = Instant::now();
host.tick(now);
if let Ok(mut s) = shared.lock() {
*s = host.status(now);
}
})
.map_err(Error::Io)?;
Ok(Self { tx, status, join: Mutex::new(Some(join)) })
}
pub fn status(&self) -> RegistryStatus {
self.status.lock().map(|s| s.clone()).unwrap_or_default()
}
fn ask(&self, make: impl FnOnce(Sender<Result<()>>) -> Cmd) -> Result<()> {
let (reply, answer) = mpsc::channel();
self.tx.send(make(reply)).map_err(|_| Error::Invalid("the registry loop has stopped".into()))?;
answer.recv_timeout(Duration::from_secs(30)).map_err(|_| Error::Invalid("the registry loop did not answer".into()))?
}
pub fn set_url(&self, url: Option<String>) -> Result<()> {
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 unpublish(&self, local_id: &str) -> Result<()> {
self.ask(|reply| Cmd::Unpublish(local_id.into(), reply))
}
pub fn retry(&self, local_id: &str) {
let _ = self.tx.send(Cmd::Retry(local_id.into()));
}
/// Stop the loop. A published realm is left published: it simply stops being heard of and goes offline after the TTL.
pub fn shutdown(&self) {
let _ = self.tx.send(Cmd::Shutdown);
if let Ok(mut guard) = self.join.lock() {
if let Some(j) = guard.take() {
let _ = j.join();
}
}
}
}
impl Drop for RegistryRuntime {
fn drop(&mut self) {
self.shutdown();
}
}
@@ -0,0 +1,111 @@
//! What the Host remembers about publishing: the Registry's address and, per local realm, whether it is published and under which public identity.
//! No key and no credential is stored here (keys live behind [`KeyStore`](super::keys::KeyStore)).
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use coa_registry_proto::{validate_description, validate_display_name, validate_language, RealmId};
use serde::{Deserialize, Serialize};
use crate::{fsx, Error, Result};
pub const SETTINGS_SCHEMA: u32 = 1;
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct PublishConfig {
pub realm_id: Option<RealmId>,
pub enabled: bool,
pub display_name: String,
pub description: String,
pub language: String,
}
impl PublishConfig {
pub fn validate(&self) -> Result<()> {
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()))?;
Ok(())
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct RegistrySettings {
pub schema: u32,
/// The Registry's base address, e.g. `http://host` or `https://registry.example` (no path).
pub url: Option<String>,
/// Keyed by the local realm id (`srv-<install id>` or a prepared realm's id).
pub realms: BTreeMap<String, PublishConfig>,
}
impl Default for RegistrySettings {
fn default() -> Self {
Self { schema: SETTINGS_SCHEMA, url: None, realms: BTreeMap::new() }
}
}
/// A Registry address: http or https, a host, an optional port, nothing else.
pub fn validate_url(url: &str) -> Result<String> {
let trimmed = url.trim().trim_end_matches('/');
let rest = trimmed.strip_prefix("http://").or_else(|| trimmed.strip_prefix("https://")).ok_or_else(|| Error::Invalid("the Registry address must start with http:// or https://".into()))?;
if rest.is_empty() || rest.contains(['/', '?', '#', '@', ' ']) {
return Err(Error::Invalid("the Registry address is a host and an optional port, nothing else".into()));
}
let host = rest.rsplit_once(':').map(|(h, p)| if p.bytes().all(|b| b.is_ascii_digit()) && !p.is_empty() { h } else { rest }).unwrap_or(rest);
if !crate::client::host_ok(host) || host.contains(':') {
return Err(Error::Invalid("the Registry host is not a host name or an IPv4 address".into()));
}
Ok(trimmed.to_string())
}
pub fn file(dir: &Path) -> PathBuf {
dir.join("registry.json")
}
pub fn load(dir: &Path) -> Result<RegistrySettings> {
match std::fs::read(file(dir)) {
Ok(bytes) => {
let s: RegistrySettings = serde_json::from_slice(&bytes)?;
if s.schema != SETTINGS_SCHEMA {
return Err(Error::Invalid(format!("registry settings schema {} is not supported", s.schema)));
}
Ok(s)
}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(RegistrySettings::default()),
Err(e) => Err(e.into()),
}
}
pub fn save(dir: &Path, settings: &RegistrySettings) -> Result<()> {
fsx::atomic_write(&file(dir), &serde_json::to_vec_pretty(settings)?)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn addresses_are_a_host_and_a_port_only() {
assert_eq!(validate_url("http://127.0.0.1:8080/").unwrap(), "http://127.0.0.1:8080");
assert_eq!(validate_url(" https://registry.example ").unwrap(), "https://registry.example");
for bad in ["", "ftp://x", "registry.example", "http://", "http://x/registry", "http://user:pw@x", "http://x?y=1", "http://x y", "http://[::1]:80", "http://x:abc"] {
assert!(validate_url(bad).is_err(), "{bad}");
}
}
#[test]
fn settings_round_trip_and_refuse_what_they_do_not_know() {
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() });
save(dir.path(), &s).unwrap();
assert_eq!(load(dir.path()).unwrap(), s);
let text = std::fs::read_to_string(file(dir.path())).unwrap();
assert!(!text.contains("key") && !text.contains("password"), "{text}");
std::fs::write(file(dir.path()), r#"{"schema":1,"url":null,"realms":{},"private_key":"x"}"#).unwrap();
assert!(load(dir.path()).is_err());
}
}