From bf163e1bd0ad19462ac7b48bf34fafeaed195646 Mon Sep 17 00:00:00 2001 From: Martin Hoffmann Date: Mon, 29 Apr 2024 17:59:20 +0200 Subject: [PATCH] Make net not include tokio-rustls. (#305) This PR removes tokio-rustls as a dependency of the net feature so you only get TLS if you ask for it. --- Cargo.toml | 6 +++--- src/net/client/protocol.rs | 28 +++++++++++++++------------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7c6b88595..83e239aa0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -54,14 +54,14 @@ serde = ["dep:serde", "octseq/serde"] sign = ["std"] smallvec = ["dep:smallvec", "octseq/smallvec"] std = ["bytes?/std", "octseq/std", "time/std"] -net = ["bytes", "futures-util", "rand", "std", "tokio", "tokio-rustls"] +net = ["bytes", "futures-util", "rand", "std", "tokio"] tsig = ["bytes", "ring", "smallvec"] validate = ["std", "ring"] zonefile = ["bytes", "serde", "std"] # Unstable features -unstable-client-transport = [ "moka", "tracing" ] -unstable-server-transport = ["arc-swap", "chrono/clock", "hex", "libc", "tracing"] +unstable-client-transport = [ "moka", "net", "tracing" ] +unstable-server-transport = ["arc-swap", "chrono/clock", "hex", "libc", "net", "tracing"] unstable-zonetree = ["futures", "parking_lot", "serde", "tokio", "tracing"] # Test features diff --git a/src/net/client/protocol.rs b/src/net/client/protocol.rs index 648cdcd51..bd0a34ff0 100644 --- a/src/net/client/protocol.rs +++ b/src/net/client/protocol.rs @@ -6,14 +6,9 @@ use pin_project_lite::pin_project; use std::boxed::Box; use std::io; use std::net::SocketAddr; -use std::sync::Arc; use std::task::{Context, Poll}; use tokio::io::ReadBuf; use tokio::net::{TcpStream, UdpSocket}; -use tokio_rustls::client::TlsStream; -use tokio_rustls::rustls::pki_types::ServerName; -use tokio_rustls::rustls::ClientConfig; -use tokio_rustls::TlsConnector; /// How many times do we try a new random port if we get ‘address in use.’ const RETRY_RANDOM_PORT: usize = 10; @@ -72,25 +67,30 @@ impl AsyncConnect for TcpConnect { //------------ TlsConnect ----------------------------------------------------- /// Create new TLS connections +#[cfg(feature = "tokio-rustls")] #[derive(Clone, Debug)] pub struct TlsConnect { /// Configuration for setting up a TLS connection. - client_config: Arc, + client_config: std::sync::Arc, /// Server name for certificate verification. - server_name: ServerName<'static>, + server_name: tokio_rustls::rustls::pki_types::ServerName<'static>, /// Remote address to connect to. addr: SocketAddr, } +#[cfg(feature = "tokio-rustls")] impl TlsConnect { /// Function to create a new TLS connection stream - pub fn new( - client_config: impl Into>, - server_name: ServerName<'static>, + pub fn new( + client_config: Conf, + server_name: tokio_rustls::rustls::pki_types::ServerName<'static>, addr: SocketAddr, - ) -> Self { + ) -> Self + where + Conf: Into>, + { Self { client_config: client_config.into(), server_name, @@ -99,8 +99,9 @@ impl TlsConnect { } } +#[cfg(feature = "tokio-rustls")] impl AsyncConnect for TlsConnect { - type Connection = TlsStream; + type Connection = tokio_rustls::client::TlsStream; type Fut = Pin< Box< dyn Future> @@ -110,7 +111,8 @@ impl AsyncConnect for TlsConnect { >; fn connect(&self) -> Self::Fut { - let tls_connection = TlsConnector::from(self.client_config.clone()); + let tls_connection = + tokio_rustls::TlsConnector::from(self.client_config.clone()); let server_name = self.server_name.clone(); let addr = self.addr; Box::pin(async move {