use anyhow::{Error, bail};
use hickory_resolver::TokioResolver;
use ordinary_config::OrdinaryConfig;
pub async fn custom_domain_lookup(
config: &OrdinaryConfig,
domain: &str,
custom: &str,
is_proxy: bool,
) -> Result<Result<(), Error>, Error> {
let mut is_valid = false;
let resolver = TokioResolver::builder_tokio()?.build()?;
match resolver.txt_lookup(format!("{custom}.")).await {
Ok(txt_records) => {
'outer: for record in txt_records.answers() {
if let Some(txt_domain) = record.data.to_string().strip_prefix(if is_proxy {
"ordinary-proxy="
} else {
"ordinary="
}) {
for txt_domain in txt_domain.split(',') {
if txt_domain == domain {
is_valid = true;
break 'outer;
}
}
}
}
}
Err(err) => {
tracing::error!(%err, "no TXT");
bail!(err);
}
}
Ok(if is_valid {
tracing::info!("valid");
Ok(())
} else {
tracing::error!("invalid");
bail!(
"could not find DNS TXT record ordinary{}={} on {custom}. you may need to wait a bit or double check that it was configured properly.",
if is_proxy { "-proxy" } else { "" },
&config.domain
);
})
}