From 40b890bc13808fa9977f6c25575a167d580099df Mon Sep 17 00:00:00 2001 From: Jacob Kiers Date: Fri, 25 Aug 2023 22:54:41 +0200 Subject: [PATCH] Add much better debug logging of address resolution Signed-off-by: Jacob Kiers --- src/servers/upstream_address.rs | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/servers/upstream_address.rs b/src/servers/upstream_address.rs index 4c34dbf..5b07e04 100644 --- a/src/servers/upstream_address.rs +++ b/src/servers/upstream_address.rs @@ -51,20 +51,24 @@ impl UpstreamAddress { pub async fn resolve(&mut self, mode: ResolutionMode) -> Result> { if self.is_resolved() && self.is_valid() { debug!( - "Already got address {:?}, still valid for {}", + "Already got address {:?}, still valid for {:.3}s", &self.resolved_addresses, - self.time_remaining() + self.time_remaining().as_seconds_f64() ); return Ok(self.resolved_addresses.clone()); } - debug!("Resolving addresses for {}", &self.address); + debug!( + "Resolving addresses for {} with mode {:?}", + &self.address, &mode + ); let lookup_result = tokio::net::lookup_host(&self.address).await; - let resolved_addresses = match lookup_result { - Ok(resolved_addresses) => resolved_addresses, + let resolved_addresses: Vec = match lookup_result { + Ok(resolved_addresses) => resolved_addresses.into_iter().collect(), Err(e) => { + debug!("Failed looking up {}: {}", &self.address, &e); // Protect against DNS flooding. Cache the result for 1 second. self.resolved_time = Some(Instant::now()); self.ttl = Some(Duration::seconds(3)); @@ -72,6 +76,8 @@ impl UpstreamAddress { } }; + debug!("Resolved addresses: {:?}", &resolved_addresses); + let addresses: Vec = match mode { ResolutionMode::Ipv4 => resolved_addresses .into_iter() @@ -83,10 +89,13 @@ impl UpstreamAddress { .filter(|a| a.is_ipv6()) .collect(), - _ => resolved_addresses.collect(), + _ => resolved_addresses, }; - debug!("Got addresses for {}: {:?}", &self.address, &addresses); + debug!( + "Got {} addresses for {}: {:?}", + &mode, &self.address, &addresses + ); debug!( "Resolved at {}", OffsetDateTime::now_utc() @@ -120,3 +129,13 @@ impl From<&str> for ResolutionMode { } } } + +impl Display for ResolutionMode { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + match self { + ResolutionMode::Ipv4 => write!(f, "IPv4Only"), + ResolutionMode::Ipv6 => write!(f, "IPv6Only"), + ResolutionMode::Ipv4AndIpv6 => write!(f, "IPv4 and IPv6"), + } + } +}