//! How the world reaches a household — the door its serving Mac keeps open.//!//! A household is reached from outside its own network through exactly one//! door, and which one is a fact of the household's own row//! (`cotton_workspaces.front_door`, `front_door_address`), so every device//! reads the same answer and the Mac that serves runs that door and no other://!//! - the RELAY — a forwarder the Mac dials out to; Cotton's by default, the//! household's own when its row says so ([`crate::relay_address`]). Stored//! as NULL, which is every household until one chooses otherwise.//! - TAILSCALE FUNNEL — the Mac's own name on its tailnet, published by the//! Tailscale it already runs.//! - DIRECT — the household's router lets the Mac be reached, at a domain the//! household owns.//!//! THE RELAY NAME IS NOT A DOOR'S TO LOSE. The name, and the relay it lives//! under, are the household's identity to every device that ever joined, and//! they are written once with the row. A door is two other columns: changing//! it writes those and nothing else, and the name stays answered wherever the//! household is reached — across the room it is still what a device asks for.//!//! THE WORD STAYS HERE. "Front door" is this module's and the row's name for//! the thing; a person is never shown it (the product's language retired the//! metaphor), so every sentence below that a person can read says what the//! household is REACHED THROUGH.//!//! A HOST THE ROW DOES NOT NAME IS NOT THE HOUSEHOLD'S. A domain whose DNS//! happens to point at the house is answered only once it is the door's//! address ([`FrontDoor::answers`]); until then it gets nothing, not even a//! certificate.usecrate::relay_address::{is_hostname, HouseholdAddress};/// The DNS suffix every Tailscale Funnel name ends in. Tailscale publishes a/// machine under its tailnet's own `…ts.net` name and under no other.const TAILNET_SUFFIX: &str = ".ts.net";/// Which door.#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]pubenumDoor {Relay,TailscaleFunnel,Direct,}implDoor {/// In the order a person is offered them.pubconst ALL: [Door; 3] = [Door::Relay, Door::TailscaleFunnel, Door::Direct];/// The word a method and a page use for it.pubfn as_str(self) -> &'static str {matchself {Door::Relay => "relay",Door::TailscaleFunnel => "tailscale_funnel",Door::Direct => "direct", } }pubfn from_name(name: &str) -> Option<Door> {Door::ALL.into_iter().find(|door| door.as_str() == name) }/// What the row holds for it: nothing for the relay, so a row nobody has/// written is on the relay and stays there.pubfn stored(self) -> Option<&'static str> {matchself {Door::Relay => None, other => Some(other.as_str()), } }}/// Why a door was refused, in words a person can act on.#[derive(Debug, Clone, PartialEq, Eq)]pubenumFrontDoorRefusal {UnknownDoor(String),/// The relay's address is the household's name under its relay; it takes/// no other.RelayTakesNoAddress,NotAHostname(String),NotATailnetName(String),/// Direct is reached at a domain, and there is no door without one.DirectNeedsADomain,}impl std::fmt::DisplayforFrontDoorRefusal {fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {matchself {FrontDoorRefusal::UnknownDoor(door) => write!( f,"`{door}` is not a way a household is reached that this version knows — relay, tailscale_funnel or direct" ),FrontDoorRefusal::RelayTakesNoAddress => write!( f,"the relay is reached at the household's own name and takes no other address" ),FrontDoorRefusal::NotAHostname(address) => write!( f,"`{address}` is not a hostname — lower-case letters, digits and hyphens, with at least one dot and no scheme or port" ),FrontDoorRefusal::NotATailnetName(address) => write!( f,"`{address}` is not a Tailscale name — a Funnel is reached at the Mac's own name ending {TAILNET_SUFFIX}" ),FrontDoorRefusal::DirectNeedsADomain => write!( f,"a household reached directly is reached at a domain — attach one of its domains" ), } }}/// The household's door: which, and the host the world dials through it when/// it is not the relay.#[derive(Debug, Clone, PartialEq, Eq)]pubstructFrontDoor { door: Door, address: Option<String>,}implFrontDoor {/// The relay: where every household starts.pubfn relay() -> FrontDoor {FrontDoor { door: Door::Relay, address: None, } }/// A door and its address, checked.////// A Funnel may be chosen before it has an address: the name is the/// serving Mac's own, which that Mac writes when it opens the door. A/// direct door cannot — its address is the household's choice of domain,/// and without one there is nothing to open.pubfn new(door: Door, address: Option<&str>) -> Result<FrontDoor, FrontDoorRefusal> {let address = address.map(str::trim).filter(|said| !said.is_empty());ifletSome(said) = address {if !is_hostname(said) {returnErr(FrontDoorRefusal::NotAHostname(said.to_string())); } }match (door, address) { (Door::Relay, Some(_)) => Err(FrontDoorRefusal::RelayTakesNoAddress), (Door::TailscaleFunnel, Some(said)) if !said.ends_with(TAILNET_SUFFIX) => {Err(FrontDoorRefusal::NotATailnetName(said.to_string())) } (Door::Direct, None) => Err(FrontDoorRefusal::DirectNeedsADomain), (door, address) => Ok(FrontDoor { door, address: address.map(str::to_string), }), } }/// The door a row holds. NULL is the relay; a door this build does not/// know, or an address that is not one, is an error and NEVER the relay —/// a Mac that read a newer household's row as "the relay" would open a/// door the household had closed.pubfn from_stored(door: Option<&str>, address: Option<&str>) -> Result<FrontDoor, String> {let door = match door {None => Door::Relay,Some(name) => Door::from_name(name) .filter(|door| *door != Door::Relay) .ok_or_else(|| FrontDoorRefusal::UnknownDoor(name.to_string()).to_string())?, };FrontDoor::new(door, address).map_err(|refusal| refusal.to_string()) }pubfn door(&self) -> Door {self.door }/// The host the world dials when the door is not the relay.pubfn address(&self) -> Option<&str> {self.address.as_deref() }/// What the row is written with: `(front_door, front_door_address)`.pubfn stored(&self) -> (Option<&'static str>, Option<&str>) { (self.door.stored(), self.address()) }/// Where the world reaches this household: its name under its relay, or/// the door's own address. `None` while there is nowhere — a household/// with no name yet, or a Funnel no Mac has opened.pubfn origin(&self, household: &HouseholdAddress) -> Option<String> {matchself.door {Door::Relay => household.name().map(|name| household.relay().origin(name)),Door::TailscaleFunnel | Door::Direct => {self.address().map(|host| format!("https://{host}")) } } }/// Whether `host` is this household's to answer: its name under its relay/// — always, whichever door is open, because that name is never dropped —/// and the open door's own address. Nothing else: a domain that merely/// points here is not answered until it is the door's address.pubfn answers(&self, household: &HouseholdAddress, host: &str) -> bool {let host = host.trim_end_matches('.').to_ascii_lowercase();let named = household .name() .is_some_and(|name| household.relay().host(name) == host); named || self.address() == Some(host.as_str()) }}implDefaultforFrontDoor {fn default() -> FrontDoor {FrontDoor::relay() }}#[cfg(test)]#[path = "front_door_tests.rs"]mod front_door_tests;