Skip to content

Commit 2e24f20

Browse files
committed
move the endpoint creation utils that don't really make sense in wasm to
a mod that does not get included if we compile to wasm.
1 parent 4596a15 commit 2e24f20

File tree

2 files changed

+51
-32
lines changed

2 files changed

+51
-32
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ trybuild = "1.0.104"
5858
# enable the remote transport
5959
rpc = ["dep:quinn", "dep:postcard", "dep:anyhow", "dep:smallvec", "dep:tracing", "dep:n0-future", "tokio/io-util"]
6060
# add test utilities
61-
quinn_endpoint_setup = ["rpc", "dep:rustls", "dep:rcgen", "dep:anyhow", "dep:futures-buffered"]
61+
quinn_endpoint_setup = ["rpc", "dep:rustls", "dep:rcgen", "dep:anyhow", "dep:futures-buffered", "quinn/rustls-ring"]
6262
# pick up parent span when creating channel messages
6363
message_spans = []
6464
default = ["rpc", "quinn_endpoint_setup", "message_spans"]

src/util.rs

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
#[cfg(feature = "quinn_endpoint_setup")]
66
#[cfg_attr(quicrpc_docsrs, doc(cfg(feature = "quinn_endpoint_setup")))]
77
mod quinn_setup_utils {
8-
use std::{net::SocketAddr, sync::Arc};
8+
use std::sync::Arc;
99

1010
use anyhow::Result;
11-
use quinn::{crypto::rustls::QuicClientConfig, ClientConfig, Endpoint, ServerConfig};
11+
use quinn::{crypto::rustls::QuicClientConfig, ClientConfig, ServerConfig};
1212

1313
/// Builds default quinn client config and trusts given certificates.
1414
///
@@ -35,27 +35,6 @@ mod quinn_setup_utils {
3535
Ok(ClientConfig::new(Arc::new(quic_client_config)))
3636
}
3737

38-
/// Constructs a QUIC endpoint configured for use a client only.
39-
///
40-
/// ## Args
41-
///
42-
/// - server_certs: list of trusted certificates.
43-
pub fn make_client_endpoint(bind_addr: SocketAddr, server_certs: &[&[u8]]) -> Result<Endpoint> {
44-
let client_cfg = configure_client(server_certs)?;
45-
let mut endpoint = Endpoint::client(bind_addr)?;
46-
endpoint.set_default_client_config(client_cfg);
47-
Ok(endpoint)
48-
}
49-
50-
/// Create a server endpoint with a self-signed certificate
51-
///
52-
/// Returns the server endpoint and the certificate in DER format
53-
pub fn make_server_endpoint(bind_addr: SocketAddr) -> Result<(Endpoint, Vec<u8>)> {
54-
let (server_config, server_cert) = configure_server()?;
55-
let endpoint = Endpoint::server(server_config, bind_addr)?;
56-
Ok((endpoint, server_cert))
57-
}
58-
5938
/// Create a quinn server config with a self-signed certificate
6039
///
6140
/// Returns the server config and the certificate in DER format
@@ -73,22 +52,62 @@ mod quinn_setup_utils {
7352
Ok((server_config, cert_der.to_vec()))
7453
}
7554

76-
/// Constructs a QUIC endpoint that trusts all certificates.
77-
///
78-
/// This is useful for testing and local connections, but should be used with care.
79-
pub fn make_insecure_client_endpoint(bind_addr: SocketAddr) -> Result<Endpoint> {
55+
pub fn configure_client_insecure() -> Result<ClientConfig> {
8056
let crypto = rustls::ClientConfig::builder()
8157
.dangerous()
8258
.with_custom_certificate_verifier(Arc::new(SkipServerVerification))
8359
.with_no_client_auth();
84-
8560
let client_cfg = QuicClientConfig::try_from(crypto)?;
8661
let client_cfg = ClientConfig::new(Arc::new(client_cfg));
87-
let mut endpoint = Endpoint::client(bind_addr)?;
88-
endpoint.set_default_client_config(client_cfg);
89-
Ok(endpoint)
62+
Ok(client_cfg)
9063
}
9164

65+
#[cfg(not(target_arch = "wasm32"))]
66+
mod non_wasm {
67+
use std::net::SocketAddr;
68+
69+
use quinn::Endpoint;
70+
71+
use super::*;
72+
73+
/// Constructs a QUIC endpoint configured for use a client only.
74+
///
75+
/// ## Args
76+
///
77+
/// - server_certs: list of trusted certificates.
78+
pub fn make_client_endpoint(
79+
bind_addr: SocketAddr,
80+
server_certs: &[&[u8]],
81+
) -> Result<Endpoint> {
82+
let client_cfg = configure_client(server_certs)?;
83+
let mut endpoint = Endpoint::client(bind_addr)?;
84+
endpoint.set_default_client_config(client_cfg);
85+
Ok(endpoint)
86+
}
87+
88+
/// Create a server endpoint with a self-signed certificate
89+
///
90+
/// Returns the server endpoint and the certificate in DER format
91+
pub fn make_server_endpoint(bind_addr: SocketAddr) -> Result<(Endpoint, Vec<u8>)> {
92+
let (server_config, server_cert) = configure_server()?;
93+
let endpoint = Endpoint::server(server_config, bind_addr)?;
94+
Ok((endpoint, server_cert))
95+
}
96+
97+
/// Constructs a QUIC endpoint that trusts all certificates.
98+
///
99+
/// This is useful for testing and local connections, but should be used with care.
100+
pub fn make_insecure_client_endpoint(bind_addr: SocketAddr) -> Result<Endpoint> {
101+
let client_cfg = configure_client_insecure()?;
102+
let mut endpoint = Endpoint::client(bind_addr)?;
103+
endpoint.set_default_client_config(client_cfg);
104+
Ok(endpoint)
105+
}
106+
}
107+
108+
#[cfg(not(target_arch = "wasm32"))]
109+
pub use non_wasm::{make_client_endpoint, make_insecure_client_endpoint, make_server_endpoint};
110+
92111
#[derive(Debug)]
93112
struct SkipServerVerification;
94113

0 commit comments

Comments
 (0)