diff options
author | hozan23 <hozan23@karyontech.net> | 2024-06-22 05:21:42 +0200 |
---|---|---|
committer | hozan23 <hozan23@karyontech.net> | 2024-06-22 05:21:42 +0200 |
commit | 741a83e1a59a9ed822ce6d1ab1a82d31a1cc854a (patch) | |
tree | 4fd02b04967aceed1a59b573f8e0b4677e7a9063 /net | |
parent | 9aa972dd83a85cec5da71e8e893eb6e07d5db8ca (diff) |
net: compile rustls only if tls feature is enabled
Diffstat (limited to 'net')
-rw-r--r-- | net/Cargo.toml | 26 | ||||
-rw-r--r-- | net/async_rustls/Cargo.toml | 19 | ||||
-rw-r--r-- | net/async_rustls/src/lib.rs | 12 | ||||
-rw-r--r-- | net/src/error.rs | 8 | ||||
-rw-r--r-- | net/src/lib.rs | 3 | ||||
-rw-r--r-- | net/src/stream/websocket.rs | 16 | ||||
-rw-r--r-- | net/src/transports/tls.rs | 8 | ||||
-rw-r--r-- | net/src/transports/ws.rs | 8 |
8 files changed, 62 insertions, 38 deletions
diff --git a/net/Cargo.toml b/net/Cargo.toml index 629918b..54cb19d 100644 --- a/net/Cargo.toml +++ b/net/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "karyon_net" -description = "Internal crate for Karyon project." +description = "Internal crate for Karyon library." version.workspace = true edition.workspace = true homepage.workspace = true @@ -13,38 +13,38 @@ authors.workspace = true [features] default = ["smol", "all-protocols"] all-protocols = ["tcp", "tls", "ws", "udp", "unix"] -stream = ["dep:pin-project-lite", "dep:futures-util"] +stream = ["pin-project-lite", "futures-util"] tcp = ["stream"] -tls = ["dep:rustls-pki-types", "tcp"] -ws = ["dep:async-tungstenite", "tcp"] +tls = ["tcp", "karyon_async_rustls", "rustls-pki-types"] +ws = ["tcp", "async-tungstenite"] udp = [] unix = ["stream"] smol = [ "karyon_core/smol", "async-tungstenite?/async-std-runtime", - # TODO: use tls feature - "futures-rustls", + "karyon_async_rustls?/smol", ] tokio = [ "karyon_core/tokio", "async-tungstenite?/tokio-runtime", + "karyon_async_rustls?/tokio", "dep:tokio", - # TODO: use tls feature - "tokio-rustls", ] serde = ["dep:serde"] - [dependencies] karyon_core = { version = "0.1.3", path = "../core", default-features = false } +karyon_async_rustls = { version = "0.1.3", path = "./async_rustls", default-features = false, optional = true } -async-trait = "0.1.77" +# utils log = "0.4.21" bincode = { version = "2.0.0-rc.3", features = ["derive"] } thiserror = "1.0.58" url = "2.5.0" -async-channel = "2.3.0" +# async utils +async-trait = "0.1.77" +async-channel = "2.3.0" futures-util = { version = "0.3.30", default-features = false, features = [ "sink", ], optional = true } @@ -55,8 +55,8 @@ async-tungstenite = { version = "0.25.0", default-features = false, optional = t # tls deps rustls-pki-types = { version = "1.7.0", optional = true } -futures-rustls = { version = "0.25.1", optional = true } -tokio-rustls = { version = "0.26.0", optional = true } + +# tokio util tokio = { version = "1.37.0", features = ["io-util"], optional = true } # serde diff --git a/net/async_rustls/Cargo.toml b/net/async_rustls/Cargo.toml new file mode 100644 index 0000000..0d40e8a --- /dev/null +++ b/net/async_rustls/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "karyon_async_rustls" +description = "Internal crate for Karyon library." +version.workspace = true +edition.workspace = true +homepage.workspace = true +repository.workspace = true +license.workspace = true +authors.workspace = true + +[features] +default = ["smol"] +smol = ["futures-rustls"] +tokio = ["tokio-rustls"] + +[dependencies] +futures-rustls = { version = "0.25.1", optional = true } +tokio-rustls = { version = "0.26.0", optional = true } + diff --git a/net/async_rustls/src/lib.rs b/net/async_rustls/src/lib.rs new file mode 100644 index 0000000..4365552 --- /dev/null +++ b/net/async_rustls/src/lib.rs @@ -0,0 +1,12 @@ +#[cfg(all(feature = "smol", feature = "tokio"))] +compile_error!("Only one async runtime feature should be enabled"); + +#[cfg(not(any(feature = "smol", feature = "tokio")))] +compile_error!("At least one async runtime feature must be enabled for this crate."); + +#[cfg(feature = "smol")] +pub use futures_rustls::{rustls, TlsAcceptor, TlsConnector, TlsStream}; + +#[cfg(feature = "tokio")] +pub use tokio_rustls::{rustls, TlsAcceptor, TlsConnector, TlsStream}; + diff --git a/net/src/error.rs b/net/src/error.rs index 102a343..3656128 100644 --- a/net/src/error.rs +++ b/net/src/error.rs @@ -41,13 +41,9 @@ pub enum Error { #[error("Ws Error: {0}")] WsError(#[from] async_tungstenite::tungstenite::Error), - #[cfg(feature = "smol")] - #[error("Tls Error: {0}")] - Rustls(#[from] futures_rustls::rustls::Error), - - #[cfg(feature = "tokio")] + #[cfg(feature = "tls")] #[error("Tls Error: {0}")] - Rustls(#[from] tokio_rustls::rustls::Error), + Rustls(#[from] karyon_async_rustls::rustls::Error), #[cfg(feature = "tls")] #[error("Invalid DNS Name: {0}")] diff --git a/net/src/lib.rs b/net/src/lib.rs index cd5fc8b..975331a 100644 --- a/net/src/lib.rs +++ b/net/src/lib.rs @@ -28,6 +28,9 @@ pub use transports::udp; #[cfg(all(feature = "unix", target_family = "unix"))] pub use transports::unix; +#[cfg(feature = "tls")] +pub use karyon_async_rustls as async_rustls; + /// Represents karyon's Net Error pub use error::Error; diff --git a/net/src/stream/websocket.rs b/net/src/stream/websocket.rs index eb4de21..9f4da46 100644 --- a/net/src/stream/websocket.rs +++ b/net/src/stream/websocket.rs @@ -10,21 +10,19 @@ use futures_util::{ }; use pin_project_lite::pin_project; -#[cfg(all(feature = "smol", feature = "tls"))] -use futures_rustls::TlsStream; -#[cfg(all(feature = "tokio", feature = "tls"))] -use tokio_rustls::TlsStream; - -use karyon_core::async_runtime::net::TcpStream; - -use crate::{codec::WebSocketCodec, Error, Result}; - #[cfg(feature = "tokio")] type WebSocketStream<T> = async_tungstenite::WebSocketStream<async_tungstenite::tokio::TokioAdapter<T>>; #[cfg(feature = "smol")] use async_tungstenite::WebSocketStream; +use karyon_core::async_runtime::net::TcpStream; + +#[cfg(feature = "tls")] +use crate::async_rustls::TlsStream; + +use crate::{codec::WebSocketCodec, Error, Result}; + pub struct WsStream<C> { inner: InnerWSConn, codec: C, diff --git a/net/src/transports/tls.rs b/net/src/transports/tls.rs index c972f63..d282ad2 100644 --- a/net/src/transports/tls.rs +++ b/net/src/transports/tls.rs @@ -4,17 +4,15 @@ use async_trait::async_trait; use futures_util::SinkExt; use rustls_pki_types as pki_types; -#[cfg(feature = "smol")] -use futures_rustls::{rustls, TlsAcceptor, TlsConnector, TlsStream}; -#[cfg(feature = "tokio")] -use tokio_rustls::{rustls, TlsAcceptor, TlsConnector, TlsStream}; - use karyon_core::async_runtime::{ io::{split, ReadHalf, WriteHalf}, lock::Mutex, net::{TcpListener, TcpStream}, }; +#[cfg(feature = "tls")] +use crate::async_rustls::{rustls, TlsAcceptor, TlsConnector, TlsStream}; + use crate::{ codec::Codec, connection::{Conn, Connection, ToConn}, diff --git a/net/src/transports/ws.rs b/net/src/transports/ws.rs index f2fde90..eb339ca 100644 --- a/net/src/transports/ws.rs +++ b/net/src/transports/ws.rs @@ -10,16 +10,14 @@ use rustls_pki_types as pki_types; #[cfg(feature = "tokio")] use async_tungstenite::tokio as async_tungstenite; -#[cfg(all(feature = "smol", feature = "tls"))] -use futures_rustls::{rustls, TlsAcceptor, TlsConnector}; -#[cfg(all(feature = "tokio", feature = "tls"))] -use tokio_rustls::{rustls, TlsAcceptor, TlsConnector}; - use karyon_core::async_runtime::{ lock::Mutex, net::{TcpListener, TcpStream}, }; +#[cfg(feature = "tls")] +use crate::async_rustls::{rustls, TlsAcceptor, TlsConnector}; + use crate::{ codec::WebSocketCodec, connection::{Conn, Connection, ToConn}, |