const ROOT_DOMAIN: &str = "localharness.xyz";
pub(crate) fn is_trusted_lh_origin(origin: &str) -> bool {
let Some(host) = origin_host(origin) else { return false };
if host == ROOT_DOMAIN || host.ends_with(&format!(".{ROOT_DOMAIN}")) {
return true;
}
if self_is_localhost() && is_localhost_host(&host) {
return true;
}
false
}
pub(crate) fn is_apex_origin(origin: &str) -> bool {
let Some(host) = origin_host(origin) else { return false };
let host = host.strip_prefix("www.").unwrap_or(&host).to_string();
if host == ROOT_DOMAIN {
return true;
}
if self_is_localhost() && is_localhost_host(&host) {
return true;
}
false
}
fn origin_host(origin: &str) -> Option<String> {
let rest = origin
.strip_prefix("https://")
.or_else(|| origin.strip_prefix("http://"))?;
let host = rest.split('/').next().unwrap_or(rest);
let host = host.split(':').next().unwrap_or(host);
if host.is_empty() {
None
} else {
Some(host.to_ascii_lowercase())
}
}
fn is_localhost_host(host: &str) -> bool {
host == "localhost" || host.ends_with(".localhost") || host == "127.0.0.1"
}
fn self_is_localhost() -> bool {
super::dom::window()
.ok()
.and_then(|w| w.location().hostname().ok())
.map(|h| is_localhost_host(&h.to_ascii_lowercase()))
.unwrap_or(false)
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Host {
Apex,
Tenant(String),
Other(String),
}
impl Host {
#[allow(dead_code)]
pub(crate) fn label(&self) -> String {
match self {
Host::Apex => format!("{ROOT_DOMAIN} · home"),
Host::Tenant(name) => format!("{name}.{ROOT_DOMAIN}"),
Host::Other(h) => h.clone(),
}
}
#[allow(dead_code)]
pub(crate) fn tenant(&self) -> Option<&str> {
match self {
Host::Tenant(name) => Some(name.as_str()),
_ => None,
}
}
}
pub(crate) fn current() -> Host {
let hostname = web_sys::window()
.and_then(|w| w.location().hostname().ok())
.unwrap_or_else(|| "unknown".into());
classify(&hostname)
}
pub(crate) fn current_name() -> Option<String> {
match current() {
Host::Tenant(name) => Some(name),
_ => None,
}
}
pub(crate) fn require_tenant() -> Result<String, String> {
current_name().ok_or_else(|| "not running on a subdomain".to_string())
}
pub(crate) async fn current_tenant_owner() -> Result<(String, String), String> {
let name = require_tenant()?;
let owner = super::registry::owner_of_name(&name)
.await
.map_err(|e| format!("owner: {e}"))?
.ok_or_else(|| "no on-chain owner".to_string())?;
Ok((name, owner))
}
pub(crate) fn sanitize(input: &str) -> String {
let s: String = input
.trim()
.to_ascii_lowercase()
.chars()
.filter(|c| c.is_ascii_alphanumeric() || *c == '-')
.collect();
s.trim_matches('-').to_string()
}
fn classify(hostname: &str) -> Host {
let h = hostname.strip_prefix("www.").unwrap_or(hostname);
if h == ROOT_DOMAIN {
return Host::Apex;
}
if h.ends_with(".vercel.app") || h == "localhost" || h.ends_with(".localhost") {
return Host::Other(hostname.to_string());
}
if let Some(prefix) = h.strip_suffix(&format!(".{ROOT_DOMAIN}")) {
if !prefix.is_empty() && !prefix.contains('.') {
return Host::Tenant(prefix.to_string());
}
}
Host::Other(hostname.to_string())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn apex_is_apex() {
assert_eq!(classify("localharness.xyz"), Host::Apex);
assert_eq!(classify("www.localharness.xyz"), Host::Apex);
}
#[test]
fn tenant_extracts_prefix() {
assert_eq!(classify("john.localharness.xyz"), Host::Tenant("john".into()));
assert_eq!(classify("foo-bar.localharness.xyz"), Host::Tenant("foo-bar".into()));
}
#[test]
fn multi_label_subdomain_is_other() {
assert!(matches!(
classify("a.b.localharness.xyz"),
Host::Other(_)
));
}
#[test]
fn vercel_preview_is_other() {
assert!(matches!(
classify("antig-abc-compusophys-projects.vercel.app"),
Host::Other(_)
));
}
#[test]
fn localhost_is_other() {
assert!(matches!(classify("localhost"), Host::Other(_)));
assert!(matches!(classify("john.localhost"), Host::Other(_)));
}
#[test]
fn tenant_method_returns_slug() {
assert_eq!(Host::Tenant("john".into()).tenant(), Some("john"));
assert_eq!(Host::Apex.tenant(), None);
}
}