// This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. //! The validated model. Values of these types only come out of [`crate::load`], so every //! compiler can rely on them being valid: the structs are `#[non_exhaustive]` and cannot be //! built outside this crate. use std::collections::BTreeSet; use std::fmt; /// A validated, lowercase DNS hostname (no wildcard, no trailing dot, not an IP address). #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Hostname(pub(crate) String); impl Hostname { pub fn as_str(&self) -> &str { &self.0 } } impl fmt::Display for Hostname { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(&self.0) } } /// A validated secret name. It is also the credential file name under `$CREDENTIALS_DIRECTORY`, /// so it can never contain a path separator. #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct SecretName(pub(crate) String); impl SecretName { pub fn as_str(&self) -> &str { &self.0 } } impl fmt::Display for SecretName { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(&self.0) } } impl From<&str> for SecretName { /// For tests and comparisons only; validation happens in [`crate::load`]. fn from(s: &str) -> Self { SecretName(s.to_owned()) } } #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum Scheme { Http, Https, } impl Scheme { pub fn as_str(self) -> &'static str { match self { Scheme::Http => "http", Scheme::Https => "https", } } } /// Where the upstream host is: a DNS name, an IP literal (IPv6 without brackets), or a Unix /// socket path (`unix:/abs/path`, plain HTTP; which socket a host may use is control's call). #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum UpstreamHost { Name(String), Ip(std::net::IpAddr), Unix(String), } /// A validated upstream: scheme, host and explicit port. No path, query, fragment or userinfo. #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[non_exhaustive] pub struct Upstream { pub scheme: Scheme, pub host: UpstreamHost, pub port: u16, } impl Upstream { /// `host:port` as used for dialing, with IPv6 in brackets; `unix:` for a socket. pub fn authority(&self) -> String { match &self.host { UpstreamHost::Name(n) => format!("{n}:{}", self.port), UpstreamHost::Ip(std::net::IpAddr::V6(ip)) => format!("[{ip}]:{}", self.port), UpstreamHost::Ip(ip) => format!("{ip}:{}", self.port), UpstreamHost::Unix(p) => format!("unix:{p}"), } } /// The host part for TLS SNI and certificate verification (empty for a socket, which is /// always plain HTTP). pub fn host_str(&self) -> String { match &self.host { UpstreamHost::Name(n) => n.clone(), UpstreamHost::Ip(ip) => ip.to_string(), UpstreamHost::Unix(_) => String::new(), } } /// The socket path of a `unix:` upstream. pub fn unix_path(&self) -> Option<&str> { match &self.host { UpstreamHost::Unix(p) => Some(p), _ => None, } } pub fn is_unix(&self) -> bool { self.unix_path().is_some() } } impl fmt::Display for Upstream { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { if self.is_unix() { return f.write_str(&self.authority()); } write!(f, "{}://{}", self.scheme.as_str(), self.authority()) } } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum TlsMode { /// Public certificate from Let's Encrypt via ACME (HTTP-01). LetsEncrypt, /// Certificate issued by the Domi CA, read from the certificate directory. Domi, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum AuthMode { None, /// Every request is first checked with the site's forward-auth endpoint (Authelia). ForwardAuth, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum AccessLog { Off, /// Logged with IPv4 truncated to /24 and IPv6 to /48. Truncated, } #[derive(Debug, Clone, PartialEq, Eq)] pub enum HeaderValue { Literal(String), /// Resolved by the engine at load time; never part of any compiled output. Secret(SecretName), } #[derive(Debug, Clone, PartialEq, Eq)] pub enum HeaderRule { Set { name: String, value: HeaderValue }, Remove { name: String }, } impl HeaderRule { pub fn name(&self) -> &str { match self { HeaderRule::Set { name, .. } | HeaderRule::Remove { name } => name, } } } #[derive(Debug, Clone, PartialEq, Eq)] #[non_exhaustive] pub struct HealthCheck { pub path: String, pub interval_secs: u32, } /// One validated host file. #[derive(Debug, Clone, PartialEq, Eq)] #[non_exhaustive] pub struct Host { pub hostname: Hostname, /// The route file this host came from, for messages. pub source: String, pub upstreams: Vec, pub tls: TlsMode, pub auth: AuthMode, pub request_headers: Vec, pub response_headers: Vec, pub websockets: bool, pub health_check: Option, pub share_links: bool, pub access_log: AccessLog, /// Visitors must present a Domi device certificate (mTLS) that is on the allow list. pub device_certs: bool, } /// The forward-auth endpoint (Authelia's `/api/authz/forward-auth`). #[derive(Debug, Clone, PartialEq, Eq)] #[non_exhaustive] pub struct ForwardAuth { pub upstream: Upstream, /// Path plus optional query, e.g. `/api/authz/forward-auth`. pub path_and_query: String, /// Headers copied from a 2xx auth response onto the upstream request. pub copy_headers: Vec, } #[derive(Debug, Clone, PartialEq, Eq)] #[non_exhaustive] pub struct Acme { pub email: Option, pub directory: String, } /// Site-wide settings from `ianitor.toml`. #[derive(Debug, Clone, PartialEq, Eq)] #[non_exhaustive] pub struct Site { pub secrets: BTreeSet, pub forward_auth: Option, pub acme: Acme, } /// The whole validated routes tree. Hosts are sorted by hostname. #[derive(Debug, Clone, PartialEq, Eq)] pub struct Model { pub(crate) site: Site, pub(crate) hosts: Vec, } impl Model { pub fn site(&self) -> &Site { &self.site } pub fn hosts(&self) -> &[Host] { &self.hosts } } /// The secret that carries the web UI's per-boot proof (docs/api.md, "Sign-in"). Only the admin /// host, behind forward-auth, may send it. pub const UI_PROOF_SECRET: &str = "ui-proof"; /// Let's Encrypt production directory, the default when `[acme] directory` is not set. pub const LETSENCRYPT_PRODUCTION: &str = "https://acme-v02.api.letsencrypt.org/directory"; /// Let's Encrypt staging directory. pub const LETSENCRYPT_STAGING: &str = "https://acme-staging-v02.api.letsencrypt.org/directory"; /// Largest route file accepted (site or host). pub const MAX_FILE_BYTES: usize = 1 << 20; /// Most upstreams one host may list. pub const MAX_UPSTREAMS: usize = 64; /// Identity headers Authelia returns on success (Authelia's Caddy integration guide). pub const AUTHELIA_COPY_HEADERS: [&str; 4] = [ "Remote-User", "Remote-Groups", "Remote-Email", "Remote-Name", ];