From 3bcf8fde39dd1d221facf06544c1a08a2daf21cf Mon Sep 17 00:00:00 2001 From: Slatian Date: Sun, 5 Mar 2023 14:07:09 +0100 Subject: [PATCH] Use tokio concurrency knowlege to speed up DNS queries --- src/simple_dns.rs | 58 +++++++++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/src/simple_dns.rs b/src/simple_dns.rs index 527ce1e..b1c8181 100644 --- a/src/simple_dns.rs +++ b/src/simple_dns.rs @@ -18,6 +18,8 @@ use trust_dns_resolver::{ TokioAsyncResolver, }; +use tokio::join; + use std::net::IpAddr; @@ -211,34 +213,50 @@ pub async fn lookup( name: &String, do_full_lookup: bool, ) -> DnsLookupResult { - let ipv4_lookup_res = resolver.lookup(name, RecordType::A); - let ipv6_lookup_res = resolver.lookup(name, RecordType::AAAA); - let cname_lookup_res = resolver.lookup(name, RecordType::CNAME); - let aname_lookup_res = resolver.lookup(name, RecordType::ANAME); + let ( + ipv4_lookup_res, + ipv6_lookup_res, + cname_lookup_res, + aname_lookup_res + ) = join!( + resolver.lookup(name, RecordType::A), + resolver.lookup(name, RecordType::AAAA), + resolver.lookup(name, RecordType::CNAME), + resolver.lookup(name, RecordType::ANAME), + ); // initlize an empty lookup result let mut dig_result: DnsLookupResult = Default::default(); - integrate_lookup_result(&mut dig_result, ipv4_lookup_res.await); - integrate_lookup_result(&mut dig_result, ipv6_lookup_res.await); - integrate_lookup_result(&mut dig_result, cname_lookup_res.await); - integrate_lookup_result(&mut dig_result, aname_lookup_res.await); + integrate_lookup_result(&mut dig_result, ipv4_lookup_res); + integrate_lookup_result(&mut dig_result, ipv6_lookup_res); + integrate_lookup_result(&mut dig_result, cname_lookup_res); + integrate_lookup_result(&mut dig_result, aname_lookup_res); //Don't do an extented lookup if the domain seemingly doesn't exist if do_full_lookup && !dig_result.nxdomain { - let mx_lookup_res = resolver.lookup(name, RecordType::MX); - let ns_lookup_res = resolver.lookup(name, RecordType::NS); - let soa_lookup_res = resolver.lookup(name, RecordType::SOA); - let caa_lookup_res = resolver.lookup(name, RecordType::CAA); - let srv_lookup_res = resolver.lookup(name, RecordType::SRV); - let txt_lookup_res = resolver.lookup(name, RecordType::TXT); + let ( + mx_lookup_res, + ns_lookup_res, + soa_lookup_res, + caa_lookup_res, + srv_lookup_res, + txt_lookup_res + ) = join!( + resolver.lookup(name, RecordType::MX), + resolver.lookup(name, RecordType::NS), + resolver.lookup(name, RecordType::SOA), + resolver.lookup(name, RecordType::CAA), + resolver.lookup(name, RecordType::SRV), + resolver.lookup(name, RecordType::TXT), + ); - integrate_lookup_result(&mut dig_result, mx_lookup_res.await); - integrate_lookup_result(&mut dig_result, ns_lookup_res.await); - integrate_lookup_result(&mut dig_result, soa_lookup_res.await); - integrate_lookup_result(&mut dig_result, caa_lookup_res.await); - integrate_lookup_result(&mut dig_result, srv_lookup_res.await); - integrate_lookup_result(&mut dig_result, txt_lookup_res.await); + integrate_lookup_result(&mut dig_result, mx_lookup_res); + integrate_lookup_result(&mut dig_result, ns_lookup_res); + integrate_lookup_result(&mut dig_result, soa_lookup_res); + integrate_lookup_result(&mut dig_result, caa_lookup_res); + integrate_lookup_result(&mut dig_result, srv_lookup_res); + integrate_lookup_result(&mut dig_result, txt_lookup_res); }