From f6f44784fff5488bb59d563ee7ff7b94c08a48c1 Mon Sep 17 00:00:00 2001 From: hozan23 Date: Sun, 19 May 2024 23:41:31 +0200 Subject: use cargo features to enable/disable protocols for net crate --- net/src/transports/mod.rs | 5 +++++ net/src/transports/ws.rs | 23 ++++++++++++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) (limited to 'net/src/transports') diff --git a/net/src/transports/mod.rs b/net/src/transports/mod.rs index 14ef6f3..c7d684b 100644 --- a/net/src/transports/mod.rs +++ b/net/src/transports/mod.rs @@ -1,5 +1,10 @@ +#[cfg(feature = "tcp")] pub mod tcp; +#[cfg(feature = "tls")] pub mod tls; +#[cfg(feature = "udp")] pub mod udp; +#[cfg(all(feature = "unix", target_family = "unix"))] pub mod unix; +#[cfg(feature = "ws")] pub mod ws; diff --git a/net/src/transports/ws.rs b/net/src/transports/ws.rs index 17fe924..6107999 100644 --- a/net/src/transports/ws.rs +++ b/net/src/transports/ws.rs @@ -1,14 +1,18 @@ -use std::{net::SocketAddr, sync::Arc}; +use std::net::SocketAddr; + +#[cfg(feature = "tls")] +use std::sync::Arc; use async_trait::async_trait; +#[cfg(feature = "tls")] use rustls_pki_types as pki_types; #[cfg(feature = "tokio")] use async_tungstenite::tokio as async_tungstenite; -#[cfg(feature = "smol")] +#[cfg(all(feature = "smol", feature = "tls"))] use futures_rustls::{rustls, TlsAcceptor, TlsConnector}; -#[cfg(feature = "tokio")] +#[cfg(all(feature = "tokio", feature = "tls"))] use tokio_rustls::{rustls, TlsAcceptor, TlsConnector}; use karyon_core::async_runtime::{ @@ -30,12 +34,14 @@ use super::tcp::TcpConfig; /// WSS configuration #[derive(Clone)] pub struct ServerWssConfig { + #[cfg(feature = "tls")] pub server_config: rustls::ServerConfig, } /// WSS configuration #[derive(Clone)] pub struct ClientWssConfig { + #[cfg(feature = "tls")] pub client_config: rustls::ClientConfig, pub dns_name: String, } @@ -104,6 +110,7 @@ pub struct WsListener { inner: TcpListener, config: ServerWsConfig, codec: C, + #[cfg(feature = "tls")] tls_acceptor: Option, } @@ -125,6 +132,7 @@ where socket.set_nodelay(self.config.tcp_config.nodelay)?; match &self.config.wss_config { + #[cfg(feature = "tls")] Some(_) => match &self.tls_acceptor { Some(acceptor) => { let peer_endpoint = socket.peer_addr().map(Endpoint::new_wss_addr)?; @@ -152,6 +160,8 @@ where local_endpoint, ))) } + #[cfg(not(feature = "tls"))] + _ => unreachable!(), } } } @@ -166,6 +176,7 @@ where socket.set_nodelay(config.tcp_config.nodelay)?; match &config.wss_config { + #[cfg(feature = "tls")] Some(conf) => { let peer_endpoint = socket.peer_addr().map(Endpoint::new_wss_addr)?; let local_endpoint = socket.local_addr().map(Endpoint::new_wss_addr)?; @@ -193,6 +204,8 @@ where local_endpoint, )) } + #[cfg(not(feature = "tls"))] + _ => unreachable!(), } } @@ -206,6 +219,7 @@ pub async fn listen( let listener = TcpListener::bind(addr).await?; match &config.wss_config { + #[cfg(feature = "tls")] Some(conf) => { let acceptor = TlsAcceptor::from(Arc::new(conf.server_config.clone())); Ok(WsListener { @@ -219,8 +233,11 @@ pub async fn listen( inner: listener, config, codec, + #[cfg(feature = "tls")] tls_acceptor: None, }), + #[cfg(not(feature = "tls"))] + _ => unreachable!(), } } -- cgit v1.2.3