Skip to content

Commit

Permalink
refactor(client+transport)!: split tls into tls and
Browse files Browse the repository at this point in the history
`tls-rustls-platform-verifier` features
  • Loading branch information
oleonardolima committed Jun 28, 2024
1 parent 9ebc4c1 commit 056e0e1
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 8 deletions.
4 changes: 3 additions & 1 deletion client/transport/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ gloo-net = { version = "0.5.0", default-features = false, features = ["json", "w
futures-channel = { version = "0.3.14", default-features = false, optional = true }

[features]
tls = ["tokio-rustls", "rustls-pki-types", "rustls-platform-verifier", "rustls"]
tls = ["rustls", "tokio-rustls", "rustls-pki-types"]
tls-rustls-platform-verifier = ["tls", "rustls-platform-verifier"]
default = ["tls-rustls-platform-verifier"]

ws = [
"base64",
Expand Down
16 changes: 15 additions & 1 deletion client/transport/src/ws/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub type CustomCertStore = rustls::ClientConfig;
#[cfg(feature = "tls")]
#[derive(Debug, Clone)]
pub enum CertificateStore {
#[cfg(feature = "tls-rustls-platform-verifier")]
/// Native.
Native,
/// Custom certificate store.
Expand Down Expand Up @@ -101,9 +102,9 @@ pub struct WsTransportClientBuilder {
}

impl Default for WsTransportClientBuilder {
#[cfg(feature = "tls-rustls-platform-verifier")]
fn default() -> Self {
Self {
#[cfg(feature = "tls")]
certificate_store: CertificateStore::Native,
max_request_size: TEN_MB_SIZE_BYTES,
max_response_size: TEN_MB_SIZE_BYTES,
Expand All @@ -113,6 +114,18 @@ impl Default for WsTransportClientBuilder {
tcp_no_delay: true,
}
}

#[cfg(not(feature = "tls"))]
fn default() -> Self {
Self {
max_request_size: TEN_MB_SIZE_BYTES,
max_response_size: TEN_MB_SIZE_BYTES,
connection_timeout: Duration::from_secs(10),
headers: http::HeaderMap::new(),
max_redirections: 5,
tcp_no_delay: true,
}
}
}

impl WsTransportClientBuilder {
Expand Down Expand Up @@ -614,6 +627,7 @@ impl TryFrom<url::Url> for Target {
#[cfg(feature = "tls")]
fn build_tls_config(cert_store: &CertificateStore) -> Result<tokio_rustls::TlsConnector, WsHandshakeError> {
let config = match cert_store {
#[cfg(feature = "tls-rustls-platform-verifier")]
CertificateStore::Native => rustls_platform_verifier::tls_config(),
CertificateStore::Custom(cfg) => cfg.clone(),
};
Expand Down
3 changes: 2 additions & 1 deletion client/ws-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ rustls = { version = "0.23.7", default-features = false, features = ["logging",

[features]
tls = ["jsonrpsee-client-transport/tls"]
default = ["tls"]
tls-rustls-platform-verifier = ["jsonrpsee-client-transport/tls-rustls-platform-verifier"]
default = ["tls-rustls-platform-verifier"]

[package.metadata.docs.rs]
all-features = true
Expand Down
49 changes: 44 additions & 5 deletions client/ws-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ use url::Url;
#[cfg(feature = "tls")]
pub use jsonrpsee_client_transport::ws::CustomCertStore;

#[cfg(feature = "tls")]
#[cfg(any(feature = "tls", feature = "tls-rustls-platform-verifier"))]
use jsonrpsee_client_transport::ws::CertificateStore;

/// Builder for [`WsClient`].
Expand Down Expand Up @@ -83,7 +83,7 @@ use jsonrpsee_client_transport::ws::CertificateStore;
/// ```
#[derive(Clone, Debug)]
pub struct WsClientBuilder {
#[cfg(feature = "tls")]
#[cfg(any(feature = "tls", feature = "tls-rustls-platform-verifier"))]
certificate_store: CertificateStore,
max_request_size: u32,
max_response_size: u32,
Expand All @@ -100,9 +100,9 @@ pub struct WsClientBuilder {
}

impl Default for WsClientBuilder {
#[cfg(feature = "tls-rustls-platform-verifier")]
fn default() -> Self {
Self {
#[cfg(feature = "tls")]
certificate_store: CertificateStore::Native,
max_request_size: TEN_MB_SIZE_BYTES,
max_response_size: TEN_MB_SIZE_BYTES,
Expand All @@ -118,14 +118,53 @@ impl Default for WsClientBuilder {
tcp_no_delay: true,
}
}

#[cfg(not(any(feature = "tls", feature = "tls-rustls-platform-verifier")))]
fn default() -> Self {
Self {
max_request_size: TEN_MB_SIZE_BYTES,
max_response_size: TEN_MB_SIZE_BYTES,
request_timeout: Duration::from_secs(60),
connection_timeout: Duration::from_secs(10),
ping_config: None,
headers: HeaderMap::new(),
max_concurrent_requests: 256,
max_buffer_capacity_per_subscription: 1024,
max_redirections: 5,
id_kind: IdKind::Number,
max_log_length: 4096,
tcp_no_delay: true,
}
}
}

impl WsClientBuilder {
/// Create a new WebSocket client builder.
#[cfg(any(not(feature = "tls"), feature = "tls-rustls-platform-verifier"))]
pub fn new() -> WsClientBuilder {
WsClientBuilder::default()
}

/// Create a new WebSocket client builder.
#[cfg(feature = "tls")]
pub fn new(cfg: CustomCertStore) -> WsClientBuilder {

Check failure on line 150 in client/ws-client/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check style

duplicate definitions with name `new`
WsClientBuilder {
certificate_store: CertificateStore::Custom(cfg),
max_request_size: TEN_MB_SIZE_BYTES,
max_response_size: TEN_MB_SIZE_BYTES,
request_timeout: Duration::from_secs(60),
connection_timeout: Duration::from_secs(10),
ping_config: None,
headers: HeaderMap::new(),
max_concurrent_requests: 256,
max_buffer_capacity_per_subscription: 1024,
max_redirections: 5,
id_kind: IdKind::Number,
max_log_length: 4096,
tcp_no_delay: true,
}
}

/// Force to use a custom certificate store.
///
/// # Optional
Expand Down Expand Up @@ -320,7 +359,7 @@ impl WsClientBuilder {
T: AsyncRead + AsyncWrite + Unpin + MaybeSend + 'static,
{
let transport_builder = WsTransportClientBuilder {
#[cfg(feature = "tls")]
#[cfg(any(feature = "tls", feature = "tls-rustls-platform-verifier"))]
certificate_store: self.certificate_store.clone(),
connection_timeout: self.connection_timeout,
headers: self.headers.clone(),
Expand All @@ -346,7 +385,7 @@ impl WsClientBuilder {
/// Panics if being called outside of `tokio` runtime context.
pub async fn build(self, url: impl AsRef<str>) -> Result<WsClient, Error> {
let transport_builder = WsTransportClientBuilder {
#[cfg(feature = "tls")]
#[cfg(any(feature = "tls", feature = "tls-rustls-platform-verifier"))]
certificate_store: self.certificate_store.clone(),
connection_timeout: self.connection_timeout,
headers: self.headers.clone(),
Expand Down

0 comments on commit 056e0e1

Please sign in to comment.