From 137ee9334c941e5dc083c6b096bf2800b983020b Mon Sep 17 00:00:00 2001 From: link2xt Date: Sun, 25 Aug 2024 01:53:16 +0000 Subject: [PATCH] feat: always use preloaded DNS results Otherwise if DNS server returns incorrect results, we may never try preloaded DNS results. For example, we may get our first results from a captive portal. To test, add `127.0.0.1 example.org` and try to create an account. Without this change we only try 127.0.0.1 and fail. With this change preloaded DNS results are tried as well. --- src/net/dns.rs | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/src/net/dns.rs b/src/net/dns.rs index 99c61a2f1..d77081877 100644 --- a/src/net/dns.rs +++ b/src/net/dns.rs @@ -108,6 +108,10 @@ pub(crate) async fn update_connect_timestamp( Ok(()) } +/// Preloaded DNS results that can be used in case of DNS server failures. +/// +/// See and +/// for reasons. static DNS_PRELOAD: Lazy>> = Lazy::new(|| { HashMap::from([ ( @@ -501,21 +505,6 @@ static DNS_PRELOAD: Lazy>> = Lazy::new(|| { ]) }); -/// Load hardcoded cache if everything else fails. -/// -/// See and -/// for reasons. -/// -/// In the future we may pre-resolve all provider database addresses -/// and build them in. -fn load_hardcoded_cache(hostname: &str, port: u16) -> Vec { - if let Some(ips) = DNS_PRELOAD.get(hostname) { - ips.iter().map(|ip| SocketAddr::new(*ip, port)).collect() - } else { - Vec::new() - } -} - async fn lookup_cache( context: &Context, host: &str, @@ -631,8 +620,13 @@ pub(crate) async fn lookup_host_with_cache( } } - if resolved_addrs.is_empty() { - return Ok(load_hardcoded_cache(hostname, port)); + if let Some(ips) = DNS_PRELOAD.get(hostname) { + for ip in ips { + let addr = SocketAddr::new(*ip, port); + if !resolved_addrs.contains(&addr) { + resolved_addrs.push(addr); + } + } } }