use anyhow::Result;
use iroh_base::NodeId;
pub use iroh_relay::dns::{N0_DNS_NODE_ORIGIN_PROD, N0_DNS_NODE_ORIGIN_STAGING};
use n0_future::boxed::BoxStream;
use crate::{
discovery::{Discovery, DiscoveryItem},
endpoint::force_staging_infra,
Endpoint,
};
const DNS_STAGGERING_MS: &[u64] = &[200, 300];
#[derive(Debug)]
pub struct DnsDiscovery {
origin_domain: String,
}
impl DnsDiscovery {
pub fn new(origin_domain: String) -> Self {
Self { origin_domain }
}
pub fn n0_dns() -> Self {
if force_staging_infra() {
Self::new(N0_DNS_NODE_ORIGIN_STAGING.to_string())
} else {
Self::new(N0_DNS_NODE_ORIGIN_PROD.to_string())
}
}
}
impl Discovery for DnsDiscovery {
fn resolve(&self, ep: Endpoint, node_id: NodeId) -> Option<BoxStream<Result<DiscoveryItem>>> {
let resolver = ep.dns_resolver().clone();
let origin_domain = self.origin_domain.clone();
let fut = async move {
let node_info = resolver
.lookup_node_by_id_staggered(&node_id, &origin_domain, DNS_STAGGERING_MS)
.await?;
Ok(DiscoveryItem::new(node_info, "dns", None))
};
let stream = n0_future::stream::once_future(fut);
Some(Box::pin(stream))
}
}