Skip to content

Commit

Permalink
chore: fmt and clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
Frando committed Feb 27, 2024
1 parent 576e52c commit 510c616
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 27 deletions.
2 changes: 1 addition & 1 deletion iroh-dns/examples/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ async fn main() -> Result<()> {
let publisher = Publisher::new(config);

let info = AddrInfo {
derp_url: Some(args.derp_url),
derp_url: Some(args.derp_url.into()),
direct_addresses: Default::default(),
};
// let an = NodeAnnounce::new(node_id, Some(args.home_derp), vec![]);
Expand Down
37 changes: 16 additions & 21 deletions iroh-dns/src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use hickory_proto::error::ProtoError;
use iroh_net::{AddrInfo, NodeAddr, NodeId};
use url::Url;

pub const IROH_ROOT_ZONE: &'static str = "iroh";
pub const IROH_NODE_TXT_LABEL: &'static str = "_iroh_node";
pub const IROH_ROOT_ZONE: &str = "iroh";
pub const IROH_NODE_TXT_LABEL: &str = "_iroh_node";
pub const DEFAULT_TTL: u32 = 30;

pub const ATTR_DERP: &'static str = "derp";
pub const ATTR_NODE_ID: &'static str = "node";
pub const ATTR_DNS: &'static str = "dns";
pub const ATTR_DERP: &str = "derp";
pub const ATTR_NODE_ID: &str = "node";
pub const ATTR_DNS: &str = "dns";

#[derive(derive_more::Debug, Clone, Eq, PartialEq)]
pub struct NodeAnnounce {
Expand Down Expand Up @@ -51,12 +51,12 @@ impl NodeAnnounce {

pub fn to_attr_string(&self) -> String {
let mut attrs = vec![];
attrs.push(fmt_attr(ATTR_NODE_ID, &self.node_id));
attrs.push(fmt_attr(ATTR_NODE_ID, self.node_id));
if let Some(derp) = &self.home_derp {
attrs.push(fmt_attr(ATTR_DERP, &derp));
attrs.push(fmt_attr(ATTR_DERP, derp));
}
for dns in &self.home_dns {
attrs.push(fmt_attr(ATTR_DNS, &dns));
attrs.push(fmt_attr(ATTR_DNS, dns));
}
attrs.join(" ")
}
Expand Down Expand Up @@ -100,7 +100,7 @@ impl NodeAnnounce {
) -> Result<hickory_proto::rr::Record> {
use hickory_proto::rr;
let zone = rr::Name::from_str(&self.node_id.to_string())?;
let zone = zone.append_domain(&origin)?;
let zone = zone.append_domain(origin)?;
let name = rr::Name::parse(IROH_NODE_TXT_LABEL, Some(&zone))?;
let txt_value = self.to_attr_string();
let txt_data = rr::rdata::TXT::new(vec![txt_value]);
Expand All @@ -114,7 +114,7 @@ impl NodeAnnounce {
let mut packet = dns::Packet::new_reply(0);
// let name = format!("{}.{}", IROH_NODE_TXT_NAME, self.zone());
let name = IROH_NODE_TXT_LABEL;
let name = dns::Name::new(&name)?.into_owned();
let name = dns::Name::new(name)?.into_owned();
let txt_value = self.to_attr_string();
let txt_data = rdata::TXT::new().with_string(&txt_value)?.into_owned();
let rdata = rdata::RData::TXT(txt_data);
Expand Down Expand Up @@ -150,7 +150,7 @@ impl NodeAnnounce {
.iter()
.find_map(|rr| match &rr.rdata {
RData::TXT(txt) => match rr.name.without(&zone) {
Some(name) if &name.to_string() == IROH_NODE_TXT_LABEL => Some(txt),
Some(name) if name.to_string() == IROH_NODE_TXT_LABEL => Some(txt),
Some(_) | None => None,
},
_ => None,
Expand Down Expand Up @@ -180,11 +180,7 @@ impl NodeAnnounce {
.iter()
.find_map(|rr| match rr.data() {
Some(rr::RData::TXT(txt)) => {
if let Some(node_id) = is_hickory_node_info_name(rr.name()) {
Some((node_id, txt))
} else {
None
}
is_hickory_node_info_name(rr.name()).map(|node_id| (node_id, txt))
}
_ => None,
})
Expand All @@ -205,7 +201,7 @@ impl NodeAnnounce {
if node.len() != 1 {
bail!("more than one node attr is not allowed");
}
let node_id = NodeId::from_str(&node[0])?;
let node_id = NodeId::from_str(node[0])?;
let home_derp: Option<Url> = attrs
.get(ATTR_DERP)
.into_iter()
Expand All @@ -214,8 +210,7 @@ impl NodeAnnounce {
let home_dns: Vec<String> = attrs
.get(ATTR_DNS)
.into_iter()
.map(|x| x.into_iter())
.flatten()
.flat_map(|x| x.iter())
.map(|s| s.to_string())
.collect();
Ok(Self {
Expand All @@ -242,9 +237,9 @@ fn is_hickory_node_info_name(name: &hickory_proto::rr::Name) -> Option<NodeId> {

fn parse_attrs<'a>(s: &'a str) -> HashMap<&'a str, Vec<&'a str>> {
let mut map: HashMap<&'a str, Vec<&'a str>> = HashMap::new();
let parts = s.split(" ");
let parts = s.split(' ');
for part in parts {
if let Some((name, value)) = part.split_once("=") {
if let Some((name, value)) = part.split_once('=') {
map.entry(name).or_default().push(value);
}
}
Expand Down
4 changes: 2 additions & 2 deletions iroh-dns/src/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use url::Url;

use crate::packet::NodeAnnounce;

pub const IROH_TEST_PKARR_RELAY: &'static str = "https://testdns.iroh.link/pkarr";
pub const LOCALHOST_PKARR_RELAY: &'static str = "http://localhost:8080/pkarr";
pub const IROH_TEST_PKARR_RELAY: &str = "https://testdns.iroh.link/pkarr";
pub const LOCALHOST_PKARR_RELAY: &str = "http://localhost:8080/pkarr";

/// Publisher config
pub struct Config {
Expand Down
6 changes: 3 additions & 3 deletions iroh-dns/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use tracing::debug;
use crate::packet::{NodeAnnounce, IROH_NODE_TXT_LABEL};

pub const IROH_TEST_DNS_IPV4: Ipv4Addr = Ipv4Addr::new(5, 75, 181, 3);
pub const IROH_TEST_DOMAIN: &'static str = "testdns.iroh.link.";
pub const EXAMPLE_DOMAIN: &'static str = "irohdns.example.";
pub const IROH_TEST_DOMAIN: &str = "testdns.iroh.link.";
pub const EXAMPLE_DOMAIN: &str = "irohdns.example.";

pub type HickoryResolver = AsyncResolver<GenericConnector<TokioRuntimeProvider>>;

Expand Down Expand Up @@ -87,7 +87,7 @@ impl Resolver {
}

pub async fn resolve_node_by_domain(&self, domain: &str) -> Result<NodeAddr> {
let name = Name::from_str(&domain)?;
let name = Name::from_str(domain)?;
self.resolve_node(name).await
}

Expand Down

0 comments on commit 510c616

Please sign in to comment.