dns-resolver: create HickoryResolver wrapper

This commit is contained in:
Dirkjan Ochtman
2024-10-15 08:54:24 -07:00
committed by Wez Furlong
parent 4553cafeca
commit bc7bf2ffa8
3 changed files with 24 additions and 9 deletions
+2 -5
View File
@@ -13,9 +13,9 @@ use std::sync::{Arc, LazyLock, Mutex as StdMutex};
use std::time::Instant;
mod resolver;
pub use resolver::Resolver;
#[cfg(feature = "default-unbound")]
pub use resolver::UnboundResolver;
pub use resolver::{HickoryResolver, Resolver};
static RESOLVER: LazyLock<ArcSwap<Resolver>> =
LazyLock::new(|| ArcSwap::from_pointee(default_resolver()));
@@ -35,10 +35,7 @@ fn default_resolver() -> Resolver {
#[cfg(not(feature = "default-unbound"))]
fn default_resolver() -> Resolver {
Resolver::Tokio(
hickory_resolver::TokioAsyncResolver::tokio_from_system_conf()
.expect("Parsing /etc/resolv.conf failed"),
)
Resolver::Tokio(HickoryResolver::new().expect("Parsing /etc/resolv.conf failed"))
}
fn mx_cache_get(name: &Name) -> Option<Arc<MailExchanger>> {
+21 -3
View File
@@ -5,7 +5,7 @@ use hickory_resolver::proto::rr::DNSClass;
use hickory_resolver::proto::rr::{RData, RecordType};
use hickory_resolver::{IntoName, TokioAsyncResolver, TryParseIp};
#[cfg(feature = "unbound")]
use libunbound::{Context, AsyncContext};
use libunbound::{AsyncContext, Context};
use std::net::IpAddr;
use std::time::{Duration, Instant};
@@ -72,8 +72,26 @@ impl From<AsyncContext> for UnboundResolver {
}
}
pub struct HickoryResolver {
inner: TokioAsyncResolver,
}
impl HickoryResolver {
pub fn new() -> Result<Self, hickory_resolver::error::ResolveError> {
Ok(Self {
inner: TokioAsyncResolver::tokio_from_system_conf()?,
})
}
}
impl From<TokioAsyncResolver> for HickoryResolver {
fn from(inner: TokioAsyncResolver) -> Self {
Self { inner }
}
}
pub enum Resolver {
Tokio(TokioAsyncResolver),
Tokio(HickoryResolver),
#[cfg(feature = "unbound")]
Unbound(UnboundResolver),
}
@@ -89,7 +107,7 @@ impl Resolver {
rrtype: RecordType,
) -> anyhow::Result<Answer> {
match self {
Self::Tokio(t) => match t.lookup(name, rrtype).await {
Self::Tokio(t) => match t.inner.lookup(name, rrtype).await {
Ok(result) => {
let expires = result.valid_until();
let records = result.iter().cloned().collect();
+1 -1
View File
@@ -128,7 +128,7 @@ pub fn register(lua: &Lua) -> anyhow::Result<()> {
let resolver = TokioAsyncResolver::tokio(r_config, config.options);
dns_resolver::reconfigure_resolver(Resolver::Tokio(resolver));
dns_resolver::reconfigure_resolver(Resolver::Tokio(resolver.into()));
Ok(())
})?,