Use tokio concurrency knowlege to speed up DNS queries

This commit is contained in:
Slatian 2023-03-05 14:07:09 +01:00
parent 2a530b6f11
commit 3bcf8fde39

View File

@ -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);
}