dns-resolver: create new UnboundResolver type

This commit is contained in:
Dirkjan Ochtman
2024-10-15 08:54:24 -07:00
committed by Wez Furlong
parent e309c8d69a
commit a9132e201d
3 changed files with 34 additions and 11 deletions
+3 -5
View File
@@ -14,6 +14,8 @@ use std::time::Instant;
mod resolver;
pub use resolver::Resolver;
#[cfg(feature = "default-unbound")]
pub use resolver::UnboundResolver;
static RESOLVER: LazyLock<ArcSwap<Resolver>> =
LazyLock::new(|| ArcSwap::from_pointee(default_resolver()));
@@ -28,11 +30,7 @@ static IP_CACHE: LazyLock<StdMutex<LruCacheWithTtl<Name, Arc<Vec<IpAddr>>>>> =
#[cfg(feature = "default-unbound")]
fn default_resolver() -> Resolver {
// This resolves directly against the root
let context = libunbound::Context::new().unwrap();
// and enables DNSSEC
context.add_builtin_trust_anchors().unwrap();
Resolver::Unbound(context.into_async().unwrap())
Resolver::Unbound(UnboundResolver::new())
}
#[cfg(not(feature = "default-unbound"))]
+29 -4
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::AsyncContext;
use libunbound::{Context, AsyncContext};
use std::net::IpAddr;
use std::time::{Duration, Instant};
@@ -47,10 +47,35 @@ impl Answer {
}
}
#[cfg(feature = "unbound")]
pub struct UnboundResolver {
cx: AsyncContext,
}
#[cfg(feature = "unbound")]
impl UnboundResolver {
pub fn new() -> Self {
// This resolves directly against the root
let context = Context::new().unwrap();
// and enables DNSSEC
context.add_builtin_trust_anchors().unwrap();
Self {
cx: context.into_async().unwrap(),
}
}
}
#[cfg(feature = "unbound")]
impl From<AsyncContext> for UnboundResolver {
fn from(cx: AsyncContext) -> Self {
Self { cx }
}
}
pub enum Resolver {
Tokio(TokioAsyncResolver),
#[cfg(feature = "unbound")]
Unbound(AsyncContext),
Unbound(UnboundResolver),
}
impl Resolver {
@@ -99,10 +124,10 @@ impl Resolver {
},
},
#[cfg(feature = "unbound")]
Self::Unbound(ctx) => {
Self::Unbound(resolver) => {
let name = name.into_name()?;
let name = name.to_ascii();
let answer = ctx.resolve(&name, rrtype, DNSClass::IN).await?;
let answer = resolver.cx.resolve(&name, rrtype, DNSClass::IN).await?;
let mut records = vec![];
for r in answer.rdata() {
if let Ok(r) = r {
+2 -2
View File
@@ -1,6 +1,6 @@
use anyhow::Context;
use config::{any_err, get_or_create_sub_module, serialize_options};
use dns_resolver::Resolver;
use dns_resolver::{Resolver, UnboundResolver};
use dns_resolver::{get_resolver, resolve_a_or_aaaa, MailExchanger};
use hickory_resolver::config::{NameServerConfig, Protocol, ResolverConfig, ResolverOpts};
use hickory_resolver::{Name, TokioAsyncResolver};
@@ -185,7 +185,7 @@ pub fn register(lua: &Lua) -> anyhow::Result<()> {
.context("make async resolver context")
.map_err(any_err)?;
dns_resolver::reconfigure_resolver(Resolver::Unbound(context));
dns_resolver::reconfigure_resolver(Resolver::Unbound(UnboundResolver::from(context)));
Ok(())
})?,