Skip to content

Commit

Permalink
refactor: Migrate trust-dns to hickory-dns
Browse files Browse the repository at this point in the history
Signed-off-by: Xuanwo <[email protected]>
  • Loading branch information
Xuanwo authored and seanmonstar committed Mar 18, 2024
1 parent 6904889 commit 2fe53c5
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 52 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ jobs:
- "feat.: stream"
- "feat.: socks/default-tls"
- "feat.: socks/rustls-tls"
- "feat.: trust-dns"
- "feat.: hickory-dns"

include:
- name: linux / stable
Expand Down Expand Up @@ -151,8 +151,8 @@ jobs:
features: "--features socks"
- name: "feat.: socks/rustls-tls"
features: "--features socks,rustls-tls"
- name: "feat.: trust-dns"
features: "--features trust-dns"
- name: "feat.: hickory-dns"
features: "--features hickory-dns"

steps:
- name: Checkout
Expand Down
8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ json = ["serde_json"]

multipart = ["mime_guess"]

trust-dns = ["trust-dns-resolver"]
# Deprecated, remove this feature while bumping minor versions.
trust-dns = ["hickory-dns"]
hickory-dns = ["hickory-resolver"]

stream = ["tokio/fs", "tokio-util", "wasm-streams"]

Expand Down Expand Up @@ -137,8 +139,8 @@ tokio-util = { version = "0.7.1", default-features = false, features = ["codec",
## socks
tokio-socks = { version = "0.5.1", optional = true }

## trust-dns
trust-dns-resolver = { version = "0.23", optional = true, features = ["tokio-runtime"] }
## hickory-dns
hickory-resolver = { version = "0.24", optional = true, features = ["tokio-runtime"] }

# HTTP/3 experimental support
h3 = { version = "0.0.3", optional = true }
Expand Down
79 changes: 57 additions & 22 deletions src/async_impl/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ use crate::async_impl::h3_client::{H3Client, H3ResponseFuture};
use crate::connect::Connector;
#[cfg(feature = "cookies")]
use crate::cookie;
#[cfg(feature = "trust-dns")]
use crate::dns::trust_dns::TrustDnsResolver;
#[cfg(feature = "hickory-dns")]
use crate::dns::hickory::HickoryDnsResolver;
use crate::dns::{gai::GaiResolver, DnsResolverWithOverrides, DynResolver, Resolve};
use crate::error;
use crate::into_url::try_uri;
Expand Down Expand Up @@ -135,7 +135,7 @@ struct Config {
nodelay: bool,
#[cfg(feature = "cookies")]
cookie_store: Option<Arc<dyn cookie::CookieStore>>,
trust_dns: bool,
hickory_dns: bool,
error: Option<crate::Error>,
https_only: bool,
#[cfg(feature = "http3")]
Expand Down Expand Up @@ -218,7 +218,7 @@ impl ClientBuilder {
http2_keep_alive_while_idle: false,
local_address: None,
nodelay: true,
trust_dns: cfg!(feature = "trust-dns"),
hickory_dns: cfg!(feature = "hickory-dns"),
#[cfg(feature = "cookies")]
cookie_store: None,
https_only: false,
Expand Down Expand Up @@ -267,12 +267,12 @@ impl ClientBuilder {
headers.get(USER_AGENT).cloned()
}

let mut resolver: Arc<dyn Resolve> = match config.trust_dns {
let mut resolver: Arc<dyn Resolve> = match config.hickory_dns {
false => Arc::new(GaiResolver::new()),
#[cfg(feature = "trust-dns")]
true => Arc::new(TrustDnsResolver::default()),
#[cfg(not(feature = "trust-dns"))]
true => unreachable!("trust-dns shouldn't be enabled unless the feature is"),
#[cfg(feature = "hickory-dns")]
true => Arc::new(HickoryDnsResolver::default()),
#[cfg(not(feature = "hickory-dns"))]
true => unreachable!("hickory-dns shouldn't be enabled unless the feature is"),
};
if let Some(dns_resolver) = config.dns_resolver {
resolver = dns_resolver;
Expand Down Expand Up @@ -1526,32 +1526,67 @@ impl ClientBuilder {
self
}

/// Enables the [trust-dns](trust_dns_resolver) async resolver instead of a default threadpool using `getaddrinfo`.
/// Enables the [hickory-dns](hickory_resolver) async resolver instead of a default threadpool
/// using `getaddrinfo`.
///
/// If the `trust-dns` feature is turned on, the default option is enabled.
/// If the `hickory-dns` feature is turned on, the default option is enabled.
///
/// # Optional
///
/// This requires the optional `trust-dns` feature to be enabled
#[cfg(feature = "trust-dns")]
#[cfg_attr(docsrs, doc(cfg(feature = "trust-dns")))]
/// This requires the optional `hickory-dns` feature to be enabled
#[cfg(feature = "hickory-dns")]
#[cfg_attr(docsrs, doc(cfg(feature = "hickory-dns")))]
#[deprecated(note = "use `hickory_dns` instead")]
pub fn trust_dns(mut self, enable: bool) -> ClientBuilder {
self.config.trust_dns = enable;
self.config.hickory_dns = enable;
self
}

/// Disables the trust-dns async resolver.
/// Enables the [hickory-dns](hickory_resolver) async resolver instead of a default threadpool
/// using `getaddrinfo`.
///
/// This method exists even if the optional `trust-dns` feature is not enabled.
/// This can be used to ensure a `Client` doesn't use the trust-dns async resolver
/// even if another dependency were to enable the optional `trust-dns` feature.
/// If the `hickory-dns` feature is turned on, the default option is enabled.
///
/// # Optional
///
/// This requires the optional `hickory-dns` feature to be enabled
#[cfg(feature = "hickory-dns")]
#[cfg_attr(docsrs, doc(cfg(feature = "hickory-dns")))]
pub fn hickory_dns(mut self, enable: bool) -> ClientBuilder {
self.config.hickory_dns = enable;
self
}

/// Disables the hickory-dns async resolver.
///
/// This method exists even if the optional `hickory-dns` feature is not enabled.
/// This can be used to ensure a `Client` doesn't use the hickory-dns async resolver
/// even if another dependency were to enable the optional `hickory-dns` feature.
#[deprecated(note = "use `no_hickory_dns` instead")]
pub fn no_trust_dns(self) -> ClientBuilder {
#[cfg(feature = "trust-dns")]
#[cfg(feature = "hickory-dns")]
{
self.hickory_dns(false)
}

#[cfg(not(feature = "hickory-dns"))]
{
self
}
}

/// Disables the hickory-dns async resolver.
///
/// This method exists even if the optional `hickory-dns` feature is not enabled.
/// This can be used to ensure a `Client` doesn't use the hickory-dns async resolver
/// even if another dependency were to enable the optional `hickory-dns` feature.
pub fn no_hickory_dns(self) -> ClientBuilder {
#[cfg(feature = "hickory-dns")]
{
self.trust_dns(false)
self.hickory_dns(false)
}

#[cfg(not(feature = "trust-dns"))]
#[cfg(not(feature = "hickory-dns"))]
{
self
}
Expand Down
46 changes: 35 additions & 11 deletions src/blocking/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -789,26 +789,50 @@ impl ClientBuilder {
self.with_inner(move |inner| inner.use_preconfigured_tls(tls))
}

/// Enables the [trust-dns](trust_dns_resolver) async resolver instead of a default threadpool using `getaddrinfo`.
/// Enables the [hickory-dns](hickory_resolver) async resolver instead of a default threadpool using `getaddrinfo`.
///
/// If the `trust-dns` feature is turned on, the default option is enabled.
/// If the `hickory-dns` feature is turned on, the default option is enabled.
///
/// # Optional
///
/// This requires the optional `trust-dns` feature to be enabled
#[cfg(feature = "trust-dns")]
#[cfg_attr(docsrs, doc(cfg(feature = "trust-dns")))]
/// This requires the optional `hickory-dns` feature to be enabled
#[cfg(feature = "hickory-dns")]
#[cfg_attr(docsrs, doc(cfg(feature = "hickory-dns")))]
#[deprecated(note = "use `hickory_dns` instead", since = "0.12.0")]
pub fn trust_dns(self, enable: bool) -> ClientBuilder {
self.with_inner(|inner| inner.trust_dns(enable))
self.with_inner(|inner| inner.hickory_dns(enable))
}

/// Disables the trust-dns async resolver.
/// Enables the [hickory-dns](hickory_resolver) async resolver instead of a default threadpool using `getaddrinfo`.
///
/// This method exists even if the optional `trust-dns` feature is not enabled.
/// This can be used to ensure a `Client` doesn't use the trust-dns async resolver
/// even if another dependency were to enable the optional `trust-dns` feature.
/// If the `hickory-dns` feature is turned on, the default option is enabled.
///
/// # Optional
///
/// This requires the optional `hickory-dns` feature to be enabled
#[cfg(feature = "hickory-dns")]
#[cfg_attr(docsrs, doc(cfg(feature = "hickory-dns")))]
pub fn hickory_dns(self, enable: bool) -> ClientBuilder {
self.with_inner(|inner| inner.hickory_dns(enable))
}

/// Disables the hickory-dns async resolver.
///
/// This method exists even if the optional `hickory-dns` feature is not enabled.
/// This can be used to ensure a `Client` doesn't use the hickory-dns async resolver
/// even if another dependency were to enable the optional `hickory-dns` feature.
#[deprecated(note = "use `no_hickory_dns` instead", since = "0.12.0")]
pub fn no_trust_dns(self) -> ClientBuilder {
self.with_inner(|inner| inner.no_trust_dns())
self.with_inner(|inner| inner.no_hickory_dns())
}

/// Disables the hickory-dns async resolver.
///
/// This method exists even if the optional `hickory-dns` feature is not enabled.
/// This can be used to ensure a `Client` doesn't use the hickory-dns async resolver
/// even if another dependency were to enable the optional `hickory-dns` feature.
pub fn no_hickory_dns(self) -> ClientBuilder {
self.with_inner(|inner| inner.no_hickory_dns())
}

/// Restrict the Client to be used with HTTPS only requests.
Expand Down
8 changes: 4 additions & 4 deletions src/dns/trust_dns.rs → src/dns/hickory.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! DNS resolution via the [trust_dns_resolver](https://github.com/bluejekyll/trust-dns) crate
//! DNS resolution via the [hickory-resolver](https://github.com/hickory-dns/hickory-dns) crate
use hickory_resolver::{lookup_ip::LookupIpIntoIter, system_conf, TokioAsyncResolver};
use hyper::client::connect::dns::Name;
use once_cell::sync::OnceCell;
use trust_dns_resolver::{lookup_ip::LookupIpIntoIter, system_conf, TokioAsyncResolver};

use std::io;
use std::net::SocketAddr;
Expand All @@ -12,7 +12,7 @@ use super::{Addrs, Resolve, Resolving};

/// Wrapper around an `AsyncResolver`, which implements the `Resolve` trait.
#[derive(Debug, Default, Clone)]
pub(crate) struct TrustDnsResolver {
pub(crate) struct HickoryDnsResolver {
/// Since we might not have been called in the context of a
/// Tokio Runtime in initialization, so we must delay the actual
/// construction of the resolver.
Expand All @@ -23,7 +23,7 @@ struct SocketAddrs {
iter: LookupIpIntoIter,
}

impl Resolve for TrustDnsResolver {
impl Resolve for HickoryDnsResolver {
fn resolve(&self, name: Name) -> Resolving {
let resolver = self.clone();
Box::pin(async move {
Expand Down
4 changes: 2 additions & 2 deletions src/dns/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ pub use resolve::{Addrs, Resolve, Resolving};
pub(crate) use resolve::{DnsResolverWithOverrides, DynResolver};

pub(crate) mod gai;
#[cfg(feature = "hickory-dns")]
pub(crate) mod hickory;
pub(crate) mod resolve;
#[cfg(feature = "trust-dns")]
pub(crate) mod trust_dns;
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@
//! - **multipart**: Provides functionality for multipart forms.
//! - **stream**: Adds support for `futures::Stream`.
//! - **socks**: Provides SOCKS5 proxy support.
//! - **trust-dns**: Enables a trust-dns async resolver instead of default
//! - **hickory-dns**: Enables a hickory-dns async resolver instead of default
//! threadpool using `getaddrinfo`.
//!
//! ## Unstable Features
Expand Down
12 changes: 6 additions & 6 deletions tests/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,9 @@ async fn overridden_dns_resolution_with_gai_multiple() {
assert_eq!("Hello", text);
}

#[cfg(feature = "trust-dns")]
#[cfg(feature = "hickory-dns")]
#[tokio::test]
async fn overridden_dns_resolution_with_trust_dns() {
async fn overridden_dns_resolution_with_hickory_dns() {
let _ = env_logger::builder().is_test(true).try_init();
let server = server::http(move |_req| async { http::Response::new("Hello".into()) });

Expand All @@ -242,7 +242,7 @@ async fn overridden_dns_resolution_with_trust_dns() {
);
let client = reqwest::Client::builder()
.resolve(overridden_domain, server.addr())
.trust_dns(true)
.hickory_dns(true)
.build()
.expect("client builder");
let req = client.get(&url);
Expand All @@ -253,9 +253,9 @@ async fn overridden_dns_resolution_with_trust_dns() {
assert_eq!("Hello", text);
}

#[cfg(feature = "trust-dns")]
#[cfg(feature = "hickory-dns")]
#[tokio::test]
async fn overridden_dns_resolution_with_trust_dns_multiple() {
async fn overridden_dns_resolution_with_hickory_dns_multiple() {
let _ = env_logger::builder().is_test(true).try_init();
let server = server::http(move |_req| async { http::Response::new("Hello".into()) });

Expand All @@ -277,7 +277,7 @@ async fn overridden_dns_resolution_with_trust_dns_multiple() {
server.addr(),
],
)
.trust_dns(true)
.hickory_dns(true)
.build()
.expect("client builder");
let req = client.get(&url);
Expand Down

0 comments on commit 2fe53c5

Please sign in to comment.