diff options
-rw-r--r-- | Cargo.lock | 60 | ||||
-rw-r--r-- | core/Cargo.toml | 2 | ||||
-rw-r--r-- | jsonrpc/Cargo.toml | 19 | ||||
-rw-r--r-- | jsonrpc/jsonrpc_macro/Cargo.toml | 2 | ||||
-rw-r--r-- | jsonrpc/src/client/builder.rs | 66 | ||||
-rw-r--r-- | jsonrpc/src/error.rs | 5 | ||||
-rw-r--r-- | jsonrpc/src/lib.rs | 5 | ||||
-rw-r--r-- | jsonrpc/src/server/builder.rs | 59 |
8 files changed, 109 insertions, 109 deletions
@@ -1230,6 +1230,14 @@ dependencies = [ ] [[package]] +name = "karyon_async_rustls" +version = "0.1.3" +dependencies = [ + "futures-rustls", + "tokio-rustls", +] + +[[package]] name = "karyon_core" version = "0.1.3" dependencies = [ @@ -1257,20 +1265,15 @@ dependencies = [ "async-trait", "async-tungstenite", "env_logger", - "futures-rustls", "karyon_core", "karyon_jsonrpc_macro", "karyon_net", "log", "rand", - "rcgen 0.13.1", - "rustls-pemfile", "serde", "serde_json", "smol", "thiserror", - "tokio-rustls", - "webpki-roots", ] [[package]] @@ -1291,8 +1294,8 @@ dependencies = [ "async-trait", "async-tungstenite", "bincode", - "futures-rustls", "futures-util", + "karyon_async_rustls", "karyon_core", "log", "pin-project-lite", @@ -1301,7 +1304,6 @@ dependencies = [ "smol", "thiserror", "tokio", - "tokio-rustls", "url", ] @@ -1326,7 +1328,7 @@ dependencies = [ "log", "parking_lot", "rand", - "rcgen 0.12.1", + "rcgen", "rustls-pki-types", "semver", "serde", @@ -1755,19 +1757,6 @@ dependencies = [ ] [[package]] -name = "rcgen" -version = "0.13.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "54077e1872c46788540de1ea3d7f4ccb1983d12f9aa909b234468676c1a36779" -dependencies = [ - "pem", - "ring", - "rustls-pki-types", - "time", - "yasna", -] - -[[package]] name = "redox_syscall" version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1904,9 +1893,9 @@ dependencies = [ [[package]] name = "rustls" -version = "0.23.5" +version = "0.23.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "afabcee0551bd1aa3e18e5adbf2c0544722014b899adb31bd186ec638d3da97e" +checksum = "05cff451f60db80f490f3c182b77c35260baace73209e9cdbbe526bfe3a4d402" dependencies = [ "aws-lc-rs", "log", @@ -1918,16 +1907,6 @@ dependencies = [ ] [[package]] -name = "rustls-pemfile" -version = "2.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29993a25686778eb88d4189742cd713c9bce943bc54251a33509dc63cbacf73d" -dependencies = [ - "base64", - "rustls-pki-types", -] - -[[package]] name = "rustls-pki-types" version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1935,9 +1914,9 @@ checksum = "976295e77ce332211c0d24d92c0e83e50f5c5f046d11082cea19f3df13a3562d" [[package]] name = "rustls-webpki" -version = "0.102.3" +version = "0.102.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3bce581c0dd41bce533ce695a1437fa16a7ab5ac3ccfa99fe1a620a7885eabf" +checksum = "ff448f7e92e913c4b7d4c6d8e4540a1724b319b4152b8aef6d4cf8339712b33e" dependencies = [ "aws-lc-rs", "ring", @@ -2255,7 +2234,7 @@ version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c7bc40d0e5a97695bb96e27995cd3a08538541b0a846f65bba7a359f36700d4" dependencies = [ - "rustls 0.23.5", + "rustls 0.23.10", "rustls-pki-types", "tokio", ] @@ -2464,15 +2443,6 @@ dependencies = [ ] [[package]] -name = "webpki-roots" -version = "0.26.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3de34ae270483955a94f4b21bdaaeb83d508bb84a01435f393818edb0012009" -dependencies = [ - "rustls-pki-types", -] - -[[package]] name = "which" version = "4.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/core/Cargo.toml b/core/Cargo.toml index dc13561..ee9bf72 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "karyon_core" -description = "Internal crate for Karyon project." +description = "Internal crate for Karyon library." version.workspace = true edition.workspace = true homepage.workspace = true diff --git a/jsonrpc/Cargo.toml b/jsonrpc/Cargo.toml index f33fc06..5a7c2ba 100644 --- a/jsonrpc/Cargo.toml +++ b/jsonrpc/Cargo.toml @@ -11,30 +11,27 @@ readme = "README.md" [features] -default = ["smol"] -ws = ["karyon_net/ws", "async-tungstenite"] +default = ["smol", "tcp", "unix"] +tcp = ["karyon_net/tcp"] +tls = ["tcp", "karyon_net/tls"] +ws = ["tcp", "karyon_net/ws", "async-tungstenite"] unix = ["karyon_net/unix"] smol = [ "karyon_core/smol", "karyon_net/smol", "karyon_jsonrpc_macro/smol", - "futures-rustls", "async-tungstenite?/async-std-runtime", ] tokio = [ "karyon_core/tokio", "karyon_net/tokio", "karyon_jsonrpc_macro/tokio", - "tokio-rustls", "async-tungstenite?/tokio-runtime", ] [dependencies] karyon_core = { version = "0.1.3", path = "../core", default-features = false } -karyon_net = { version = "0.1.3", path = "../net", default-features = false, features = [ - "tcp", - "tls", -] } +karyon_net = { version = "0.1.3", path = "../net", default-features = false } karyon_jsonrpc_macro = { version = "0.1.3", path = "jsonrpc_macro", default-features = false } @@ -48,12 +45,6 @@ async-channel = "2.3.1" async-tungstenite = { version = "0.25.0", default-features = false, optional = true } -futures-rustls = { version = "0.25.1", optional = true } -tokio-rustls = { version = "0.26.0", optional = true } - [dev-dependencies] env_logger = "0.11.3" -rcgen = "0.13.1" -rustls-pemfile = "2.1.2" smol = "2.0.0" -webpki-roots = "0.26.1" diff --git a/jsonrpc/jsonrpc_macro/Cargo.toml b/jsonrpc/jsonrpc_macro/Cargo.toml index 969bfea..36eabc0 100644 --- a/jsonrpc/jsonrpc_macro/Cargo.toml +++ b/jsonrpc/jsonrpc_macro/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "karyon_jsonrpc_macro" -description = "Internal crate for Karyon project." +description = "Internal crate for Karyon library." version.workspace = true edition.workspace = true homepage.workspace = true diff --git a/jsonrpc/src/client/builder.rs b/jsonrpc/src/client/builder.rs index c34d2da..5a7936c 100644 --- a/jsonrpc/src/client/builder.rs +++ b/jsonrpc/src/client/builder.rs @@ -1,20 +1,24 @@ use std::sync::{atomic::AtomicBool, Arc}; -#[cfg(feature = "smol")] -use futures_rustls::rustls; -#[cfg(feature = "tokio")] -use tokio_rustls::rustls; - use karyon_core::async_util::TaskGroup; -use karyon_net::{tls::ClientTlsConfig, Conn, Endpoint, ToEndpoint}; +use karyon_net::{Conn, Endpoint, ToEndpoint}; + +#[cfg(feature = "tls")] +use karyon_net::{async_rustls::rustls, tls::ClientTlsConfig}; #[cfg(feature = "ws")] -use karyon_net::ws::{ClientWsConfig, ClientWssConfig}; +use karyon_net::ws::ClientWsConfig; + +#[cfg(all(feature = "ws", feature = "tls"))] +use karyon_net::ws::ClientWssConfig; #[cfg(feature = "ws")] use crate::codec::WsJsonCodec; -use crate::{codec::JsonCodec, Error, Result, TcpConfig}; +#[cfg(feature = "tcp")] +use crate::TcpConfig; + +use crate::{codec::JsonCodec, Error, Result}; use super::{Client, MessageDispatcher, Subscriptions}; @@ -42,8 +46,10 @@ impl Client { Ok(ClientBuilder { endpoint, timeout: Some(DEFAULT_TIMEOUT), - tls_config: None, + #[cfg(feature = "tcp")] tcp_config: Default::default(), + #[cfg(feature = "tls")] + tls_config: None, subscription_buffer_size: DEFAULT_MAX_SUBSCRIPTION_BUFFER_SIZE, }) } @@ -52,8 +58,10 @@ impl Client { /// Builder for constructing an RPC [`Client`]. pub struct ClientBuilder { endpoint: Endpoint, - tls_config: Option<(rustls::ClientConfig, String)>, + #[cfg(feature = "tcp")] tcp_config: TcpConfig, + #[cfg(feature = "tls")] + tls_config: Option<(rustls::ClientConfig, String)>, timeout: Option<u64>, subscription_buffer_size: usize, } @@ -118,6 +126,7 @@ impl ClientBuilder { /// ``` /// /// This function will return an error if the endpoint does not support TCP protocols. + #[cfg(feature = "tcp")] pub fn tcp_config(mut self, config: TcpConfig) -> Result<Self> { match self.endpoint { Endpoint::Tcp(..) | Endpoint::Tls(..) | Endpoint::Ws(..) | Endpoint::Wss(..) => { @@ -146,13 +155,17 @@ impl ClientBuilder { /// ``` /// /// This function will return an error if the endpoint does not support TLS protocols. + #[cfg(feature = "tls")] pub fn tls_config(mut self, config: rustls::ClientConfig, dns_name: &str) -> Result<Self> { match self.endpoint { - Endpoint::Tcp(..) | Endpoint::Tls(..) | Endpoint::Ws(..) | Endpoint::Wss(..) => { + Endpoint::Tls(..) | Endpoint::Wss(..) => { self.tls_config = Some((config, dns_name.to_string())); Ok(self) } - _ => Err(Error::UnsupportedProtocol(self.endpoint.to_string())), + _ => Err(Error::UnsupportedProtocol(format!( + "Invalid tls config for endpoint: {}", + self.endpoint + ))), } } @@ -177,7 +190,12 @@ impl ClientBuilder { /// ``` pub async fn build(self) -> Result<Arc<Client>> { let conn: Conn<serde_json::Value> = match self.endpoint { - Endpoint::Tcp(..) | Endpoint::Tls(..) => match self.tls_config { + #[cfg(feature = "tcp")] + Endpoint::Tcp(..) => Box::new( + karyon_net::tcp::dial(&self.endpoint, self.tcp_config, JsonCodec {}).await?, + ), + #[cfg(feature = "tls")] + Endpoint::Tls(..) => match self.tls_config { Some((conf, dns_name)) => Box::new( karyon_net::tls::dial( &self.endpoint, @@ -190,12 +208,18 @@ impl ClientBuilder { ) .await?, ), - None => Box::new( - karyon_net::tcp::dial(&self.endpoint, self.tcp_config, JsonCodec {}).await?, - ), + None => return Err(Error::TLSConfigRequired), }, #[cfg(feature = "ws")] - Endpoint::Ws(..) | Endpoint::Wss(..) => match self.tls_config { + Endpoint::Ws(..) => { + let config = ClientWsConfig { + tcp_config: self.tcp_config, + wss_config: None, + }; + Box::new(karyon_net::ws::dial(&self.endpoint, config, WsJsonCodec {}).await?) + } + #[cfg(all(feature = "ws", feature = "tls"))] + Endpoint::Wss(..) => match self.tls_config { Some((conf, dns_name)) => Box::new( karyon_net::ws::dial( &self.endpoint, @@ -210,13 +234,7 @@ impl ClientBuilder { ) .await?, ), - None => { - let config = ClientWsConfig { - tcp_config: self.tcp_config, - wss_config: None, - }; - Box::new(karyon_net::ws::dial(&self.endpoint, config, WsJsonCodec {}).await?) - } + None => return Err(Error::TLSConfigRequired), }, #[cfg(all(feature = "unix", target_family = "unix"))] Endpoint::Unix(..) => Box::new( diff --git a/jsonrpc/src/error.rs b/jsonrpc/src/error.rs index f409c8d..7083e8a 100644 --- a/jsonrpc/src/error.rs +++ b/jsonrpc/src/error.rs @@ -20,9 +20,12 @@ pub enum Error { #[error(transparent)] ParseJSON(#[from] serde_json::Error), - #[error("Unsupported protocol: {0}")] + #[error("Unsupported Protocol: {0}")] UnsupportedProtocol(String), + #[error("Tls config is required")] + TLSConfigRequired, + #[error("Receive close message from connection: {0}")] CloseConnection(String), diff --git a/jsonrpc/src/lib.rs b/jsonrpc/src/lib.rs index c72f067..970f563 100644 --- a/jsonrpc/src/lib.rs +++ b/jsonrpc/src/lib.rs @@ -19,4 +19,7 @@ pub use server::{ pub use karyon_jsonrpc_macro::{rpc_impl, rpc_pubsub_impl}; -pub use karyon_net::{tcp::TcpConfig, Endpoint}; +pub use karyon_net::Endpoint; + +#[cfg(feature = "tcp")] +pub use karyon_net::tcp::TcpConfig; diff --git a/jsonrpc/src/server/builder.rs b/jsonrpc/src/server/builder.rs index ca6d1a7..cd79ac3 100644 --- a/jsonrpc/src/server/builder.rs +++ b/jsonrpc/src/server/builder.rs @@ -1,27 +1,30 @@ use std::{collections::HashMap, sync::Arc}; -#[cfg(feature = "smol")] -use futures_rustls::rustls; -#[cfg(feature = "tokio")] -use tokio_rustls::rustls; - use karyon_core::{async_runtime::Executor, async_util::TaskGroup}; use karyon_net::{Endpoint, Listener, ToEndpoint}; -#[cfg(feature = "ws")] -use crate::codec::WsJsonCodec; +#[cfg(feature = "tls")] +use karyon_net::async_rustls::rustls; #[cfg(feature = "ws")] use karyon_net::ws::ServerWsConfig; -use crate::{codec::JsonCodec, Error, PubSubRPCService, RPCService, Result, TcpConfig}; +#[cfg(feature = "ws")] +use crate::codec::WsJsonCodec; + +#[cfg(feature = "tcp")] +use crate::TcpConfig; + +use crate::{codec::JsonCodec, Error, PubSubRPCService, RPCService, Result}; use super::Server; /// Builder for constructing an RPC [`Server`]. pub struct ServerBuilder { endpoint: Endpoint, + #[cfg(feature = "tcp")] tcp_config: TcpConfig, + #[cfg(feature = "tls")] tls_config: Option<rustls::ServerConfig>, services: HashMap<String, Arc<dyn RPCService + 'static>>, pubsub_services: HashMap<String, Arc<dyn PubSubRPCService + 'static>>, @@ -135,6 +138,7 @@ impl ServerBuilder { /// ``` /// /// This function will return an error if the endpoint does not support TCP protocols. + #[cfg(feature = "tcp")] pub fn tcp_config(mut self, config: TcpConfig) -> Result<ServerBuilder> { match self.endpoint { Endpoint::Tcp(..) | Endpoint::Tls(..) | Endpoint::Ws(..) | Endpoint::Wss(..) => { @@ -162,13 +166,17 @@ impl ServerBuilder { /// ``` /// /// This function will return an error if the endpoint does not support TLS protocols. + #[cfg(feature = "tls")] pub fn tls_config(mut self, config: rustls::ServerConfig) -> Result<ServerBuilder> { match self.endpoint { - Endpoint::Tcp(..) | Endpoint::Tls(..) | Endpoint::Ws(..) | Endpoint::Wss(..) => { + Endpoint::Tls(..) | Endpoint::Wss(..) => { self.tls_config = Some(config); Ok(self) } - _ => Err(Error::UnsupportedProtocol(self.endpoint.to_string())), + _ => Err(Error::UnsupportedProtocol(format!( + "Invalid tls config for endpoint: {}", + self.endpoint + ))), } } @@ -184,7 +192,12 @@ impl ServerBuilder { async fn _build(self, task_group: TaskGroup) -> Result<Arc<Server>> { let listener: Listener<serde_json::Value> = match self.endpoint { - Endpoint::Tcp(..) | Endpoint::Tls(..) => match &self.tls_config { + #[cfg(feature = "tcp")] + Endpoint::Tcp(..) => Box::new( + karyon_net::tcp::listen(&self.endpoint, self.tcp_config, JsonCodec {}).await?, + ), + #[cfg(feature = "tls")] + Endpoint::Tls(..) => match &self.tls_config { Some(conf) => Box::new( karyon_net::tls::listen( &self.endpoint, @@ -196,12 +209,18 @@ impl ServerBuilder { ) .await?, ), - None => Box::new( - karyon_net::tcp::listen(&self.endpoint, self.tcp_config, JsonCodec {}).await?, - ), + None => return Err(Error::TLSConfigRequired), }, #[cfg(feature = "ws")] - Endpoint::Ws(..) | Endpoint::Wss(..) => match &self.tls_config { + Endpoint::Ws(..) => { + let config = ServerWsConfig { + tcp_config: self.tcp_config, + wss_config: None, + }; + Box::new(karyon_net::ws::listen(&self.endpoint, config, WsJsonCodec {}).await?) + } + #[cfg(all(feature = "ws", feature = "tls"))] + Endpoint::Wss(..) => match &self.tls_config { Some(conf) => Box::new( karyon_net::ws::listen( &self.endpoint, @@ -215,13 +234,7 @@ impl ServerBuilder { ) .await?, ), - None => { - let config = ServerWsConfig { - tcp_config: self.tcp_config, - wss_config: None, - }; - Box::new(karyon_net::ws::listen(&self.endpoint, config, WsJsonCodec {}).await?) - } + None => return Err(Error::TLSConfigRequired), }, #[cfg(all(feature = "unix", target_family = "unix"))] Endpoint::Unix(..) => Box::new(karyon_net::unix::listen( @@ -262,7 +275,9 @@ impl Server { endpoint, services: HashMap::new(), pubsub_services: HashMap::new(), + #[cfg(feature = "tcp")] tcp_config: Default::default(), + #[cfg(feature = "tls")] tls_config: None, }) } |