Add HostServ: virtual hosts

New HostServ pseudo-service assigns and applies vhosts. A vhost is a typed
Option<Vhost>{host,setter,ts} on the account (VhostSet/VhostDeleted events),
applied to the displayed host on identify and re-applied by ON; OFF restores
the normal host for the session. Operators SET/DEL (applied at once to
online sessions) and LIST; hosts are validated as dot-separated labels.

New SetHost NetAction -> InspIRCd ENCAP <target-sid> CHGHOST <uid> <host>.
This commit is contained in:
Jean Chevronnet 2026-07-13 21:26:38 +00:00
parent 540d2f39d1
commit 1227e26b95
No known key found for this signature in database
17 changed files with 378 additions and 6 deletions

View file

@ -30,7 +30,7 @@ use super::scram::{self, Hash};
// fedserv-api SDK crate; re-exported so the engine keeps naming them locally and
// modules importing `crate::engine::db::{ChanError, ...}` are unaffected.
pub use fedserv_api::{
AccountView, AjoinView, BotView, MemoView, SuspensionView, ChanAccessView, ChanAkickView, ChanError, ChanSetting, ChannelView, CertError, CodeKind, Kicker, RegError, Store, TriggerView,
AccountView, AjoinView, BotView, MemoView, SuspensionView, ChanAccessView, ChanAkickView, ChanError, ChanSetting, ChannelView, CertError, CodeKind, Kicker, RegError, Store, TriggerView, VhostView,
};
#[derive(Debug, Clone, Serialize, Deserialize)]
@ -73,6 +73,17 @@ pub struct Account {
// Personal greet a bot shows when this account joins a greet-enabled channel.
#[serde(default)]
pub greet: String,
// Assigned vhost (HostServ), applied to the displayed host on identify.
#[serde(default)]
pub vhost: Option<Vhost>,
}
// A HostServ virtual host: the displayed host, who assigned it, and when.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Vhost {
pub host: String,
pub setter: String,
pub ts: u64,
}
fn verified_default() -> bool {
@ -95,6 +106,8 @@ pub enum Event {
AccountVerified { account: String },
AjoinAdded { account: String, channel: String, key: String },
AjoinRemoved { account: String, channel: String },
VhostSet { account: String, host: String, setter: String, ts: u64 },
VhostDeleted { account: String },
AccountSuspended { account: String, by: String, reason: String, ts: u64, expires: Option<u64> },
AccountUnsuspended { account: String },
MemoSent { account: String, from: String, text: String, ts: u64 },
@ -148,6 +161,8 @@ impl Event {
| Event::AccountVerified { .. }
| Event::AjoinAdded { .. }
| Event::AjoinRemoved { .. }
| Event::VhostSet { .. }
| Event::VhostDeleted { .. }
| Event::AccountSuspended { .. }
| Event::AccountUnsuspended { .. }
| Event::MemoSent { .. }
@ -970,6 +985,7 @@ impl Db {
suspension: None,
memos: Vec::new(),
greet: String::new(),
vhost: None,
};
self.log.append(Event::AccountRegistered(account.clone())).map_err(|_| RegError::Internal)?;
self.accounts.insert(key(name), account);
@ -1139,6 +1155,47 @@ impl Db {
Ok(())
}
/// Assign a vhost to `account`. `setter` is who did it, for the record.
pub fn set_vhost(&mut self, account: &str, host: &str, setter: &str) -> Result<(), RegError> {
let k = key(account);
if !self.accounts.contains_key(&k) {
return Err(RegError::Internal);
}
let ts = now();
self.log.append(Event::VhostSet { account: account.to_string(), host: host.to_string(), setter: setter.to_string(), ts }).map_err(|_| RegError::Internal)?;
self.accounts.get_mut(&k).unwrap().vhost = Some(Vhost { host: host.to_string(), setter: setter.to_string(), ts });
Ok(())
}
/// Remove `account`'s vhost. Returns whether one was set.
pub fn del_vhost(&mut self, account: &str) -> Result<bool, RegError> {
let k = key(account);
match self.accounts.get(&k) {
Some(a) if a.vhost.is_some() => {}
Some(_) => return Ok(false),
None => return Err(RegError::Internal),
}
self.log.append(Event::VhostDeleted { account: account.to_string() }).map_err(|_| RegError::Internal)?;
self.accounts.get_mut(&k).unwrap().vhost = None;
Ok(true)
}
/// `account`'s vhost, if any.
pub fn vhost(&self, account: &str) -> Option<&Vhost> {
self.accounts.get(&key(account)).and_then(|a| a.vhost.as_ref())
}
/// Every account that has a vhost, as (account, host, setter).
pub fn vhosts(&self) -> Vec<(String, String, String)> {
let mut out: Vec<(String, String, String)> = self
.accounts
.values()
.filter_map(|a| a.vhost.as_ref().map(|v| (a.name.clone(), v.host.clone(), v.setter.clone())))
.collect();
out.sort_by(|a, b| a.0.cmp(&b.0));
out
}
/// Set (or clear, when empty) `account`'s personal greet.
pub fn set_greet(&mut self, account: &str, greet: &str) -> Result<(), RegError> {
let k = key(account);
@ -1985,6 +2042,16 @@ fn apply(accounts: &mut HashMap<String, Account>, channels: &mut HashMap<String,
a.ajoin.retain(|e| !e.channel.eq_ignore_ascii_case(&channel));
}
}
Event::VhostSet { account, host, setter, ts } => {
if let Some(a) = accounts.get_mut(&key(&account)) {
a.vhost = Some(Vhost { host, setter, ts });
}
}
Event::VhostDeleted { account } => {
if let Some(a) = accounts.get_mut(&key(&account)) {
a.vhost = None;
}
}
Event::AccountSuspended { account, by, reason, ts, expires } => {
if let Some(a) = accounts.get_mut(&key(&account)) {
a.suspension = Some(Suspension { by, reason, ts, expires });
@ -2251,6 +2318,18 @@ impl Store for Db {
fn set_greet(&mut self, account: &str, greet: &str) -> Result<(), RegError> {
Db::set_greet(self, account, greet)
}
fn set_vhost(&mut self, account: &str, host: &str, setter: &str) -> Result<(), RegError> {
Db::set_vhost(self, account, host, setter)
}
fn del_vhost(&mut self, account: &str) -> Result<bool, RegError> {
Db::del_vhost(self, account)
}
fn vhost(&self, account: &str) -> Option<VhostView> {
Db::account(self, account).and_then(|a| a.vhost.as_ref().map(|v| VhostView { account: a.name.clone(), host: v.host.clone(), setter: v.setter.clone() }))
}
fn vhosts(&self) -> Vec<VhostView> {
Db::vhosts(self).into_iter().map(|(account, host, setter)| VhostView { account, host, setter }).collect()
}
fn group_nick(&mut self, nick: &str, account: &str) -> Result<(), RegError> {
Db::group_nick(self, nick, account)
}
@ -2490,7 +2569,7 @@ mod tests {
fn account_conflict_resolves_deterministically() {
let alice = |hash: &str, ts: u64, home: &str| Account {
name: "alice".into(), password_hash: hash.into(), email: None,
ts, home: home.into(), scram256: None, scram512: None, certfps: vec![], verified: true, ajoin: vec![], suspension: None, memos: vec![], greet: String::new(),
ts, home: home.into(), scram256: None, scram512: None, certfps: vec![], verified: true, ajoin: vec![], suspension: None, memos: vec![], greet: String::new(), vhost: None,
};
let converge = |first: &Account, second: &Account| {
let (mut acc, mut ch, mut gr, mut bo) = (HashMap::new(), HashMap::new(), HashMap::new(), HashMap::new());
@ -2766,7 +2845,7 @@ mod tests {
db.register("alice", "pw", None).unwrap();
let bob = Account {
name: "bob".into(), password_hash: "x".into(), email: None,
ts: 0, home: "peer".into(), scram256: None, scram512: None, certfps: vec![], verified: true, ajoin: vec![], suspension: None, memos: vec![], greet: String::new(),
ts: 0, home: "peer".into(), scram256: None, scram512: None, certfps: vec![], verified: true, ajoin: vec![], suspension: None, memos: vec![], greet: String::new(), vhost: None,
};
let entry = LogEntry { origin: "peer".into(), seq: 0, lamport: 1, event: Event::AccountRegistered(bob) };
db.ingest(entry).unwrap();