diff --git a/Cargo.lock b/Cargo.lock index 37ecbe15db4..e7b813105d8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2392,6 +2392,7 @@ dependencies = [ "tracing", "tracing-subscriber", "url", + "z32", ] [[package]] diff --git a/iroh-dns/Cargo.toml b/iroh-dns/Cargo.toml index f451eeff8bd..251d4907a3a 100644 --- a/iroh-dns/Cargo.toml +++ b/iroh-dns/Cargo.toml @@ -30,6 +30,7 @@ rustls = "0.21" tokio = { version = "1", features = ["rt", "sync"] } tracing = "0.1" url = { version = "2", features = ["serde"] } +z32 = "1.0.3" [dev-dependencies] clap = { version = "4.5.1", features = ["derive"] } diff --git a/iroh-dns/examples/publish.rs b/iroh-dns/examples/publish.rs index 5602fb3e3f4..9faf5181524 100644 --- a/iroh-dns/examples/publish.rs +++ b/iroh-dns/examples/publish.rs @@ -9,6 +9,7 @@ use iroh_dns::{ packet::IROH_NODE_TXT_LABEL, publish::{Config, Publisher}, resolve::{EXAMPLE_DOMAIN, IROH_TEST_DOMAIN}, + to_z32, }; #[derive(ValueEnum, Clone, Debug, Default, Copy)] @@ -71,20 +72,20 @@ async fn main() -> Result<()> { }; // let an = NodeAnnounce::new(node_id, Some(args.home_derp), vec![]); publisher.publish_addr_info(&info).await?; - println!("published signed record to {}!", publisher.pkarr_relay()); + println!( + "published signed record to {}! Resolve with ", + publisher.pkarr_relay() + ); match args.env { - Env::IrohTest => println!( - "TXT record resolvable at {}", - node_domain(node_id, IROH_TEST_DOMAIN) - ), + Env::IrohTest => println!("dig {} TXT", node_domain(&node_id, IROH_TEST_DOMAIN)), Env::LocalDev => println!( - "TXT record resolvable at {}", - node_domain(node_id, EXAMPLE_DOMAIN) + "dig @localhost -p 5353 {} TXT", + node_domain(&node_id, EXAMPLE_DOMAIN) ), } Ok(()) } -fn node_domain(node_id: NodeId, origin: &str) -> String { - format!("{}.{}.{}", IROH_NODE_TXT_LABEL, node_id, origin) +fn node_domain(node_id: &NodeId, origin: &str) -> String { + format!("{}.{}.{}", IROH_NODE_TXT_LABEL, to_z32(node_id), origin) } diff --git a/iroh-dns/src/lib.rs b/iroh-dns/src/lib.rs index 20c55182476..915a69e2864 100644 --- a/iroh-dns/src/lib.rs +++ b/iroh-dns/src/lib.rs @@ -1,4 +1,18 @@ +use anyhow::{anyhow, Result}; +use iroh_net::NodeId; + pub mod discovery; pub mod packet; pub mod publish; pub mod resolve; + +pub fn to_z32(node_id: &NodeId) -> String { + z32::encode(node_id.as_bytes()) +} + +pub fn from_z32(s: &str) -> Result { + let bytes = z32::decode(s.as_bytes()).map_err(|_| anyhow!("invalid z32"))?; + let bytes: &[u8; 32] = &bytes.try_into().map_err(|_| anyhow!("not 32 bytes long"))?; + let node_id = NodeId::from_bytes(bytes)?; + Ok(node_id) +} diff --git a/iroh-dns/src/packet.rs b/iroh-dns/src/packet.rs index af95db509c7..aae1aa66ea3 100644 --- a/iroh-dns/src/packet.rs +++ b/iroh-dns/src/packet.rs @@ -6,6 +6,8 @@ use hickory_proto::error::ProtoError; use iroh_net::{AddrInfo, NodeAddr, NodeId}; use url::Url; +use crate::from_z32; + pub const IROH_ROOT_ZONE: &str = "iroh"; pub const IROH_NODE_TXT_LABEL: &str = "_iroh_node"; pub const DEFAULT_TTL: u32 = 30; @@ -231,7 +233,7 @@ fn is_hickory_node_info_name(name: &hickory_proto::rr::Name) -> Option { return None; } let label = std::str::from_utf8(labels.next().expect("num_labels checked")).ok()?; - let node_id = NodeId::from_str(label).ok()?; + let node_id = from_z32(label).ok()?; Some(node_id) } diff --git a/iroh-dns/src/resolve.rs b/iroh-dns/src/resolve.rs index 739005aaee6..47fbe4f8215 100644 --- a/iroh-dns/src/resolve.rs +++ b/iroh-dns/src/resolve.rs @@ -10,7 +10,10 @@ use hickory_resolver::{ use iroh_net::{AddrInfo, NodeAddr, NodeId}; use tracing::debug; -use crate::packet::{NodeAnnounce, IROH_NODE_TXT_LABEL}; +use crate::{ + packet::{NodeAnnounce, IROH_NODE_TXT_LABEL}, + to_z32, +}; pub const IROH_TEST_DNS_IPV4: Ipv4Addr = Ipv4Addr::new(5, 75, 181, 3); pub const IROH_TEST_DOMAIN: &str = "testdns.iroh.link."; @@ -93,7 +96,7 @@ impl Resolver { pub async fn resolve_node_by_id(&self, node_id: NodeId) -> Result { debug!(?node_id, "resolve node by id"); - let name = Name::parse(&node_id.to_string(), Some(&self.default_node_origin))?; + let name = Name::parse(&to_z32(&node_id), Some(&self.default_node_origin))?; let addr = self.resolve_node(name).await; debug!(?node_id, ?addr, "resolved"); let addr = addr?;