Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rust] Examples using QUIC #295

Open
rvolosatovs opened this issue Sep 3, 2024 · 0 comments
Open

[rust] Examples using QUIC #295

rvolosatovs opened this issue Sep 3, 2024 · 0 comments
Labels
documentation Improvements or additions to documentation help wanted Extra attention is needed rust wRPC Rust support

Comments

@rvolosatovs
Copy link
Member

rvolosatovs commented Sep 3, 2024

Currently, all examples use NATS, we should show examples of using QUIC transport as well.

Since Go QUIC transport is not implemented yet, this is currently Rust-specific (#296)

This may come in handy

wrpc/tests/common/mod.rs

Lines 85 to 158 in 3168f54

#[cfg(feature = "quic")]
pub async fn with_quic<T, Fut>(
names: &[&str],
f: impl FnOnce(u16, quinn::Endpoint, quinn::Endpoint) -> Fut,
) -> anyhow::Result<T>
where
Fut: Future<Output = anyhow::Result<T>>,
{
use quinn::crypto::rustls::QuicClientConfig;
use quinn::{ClientConfig, EndpointConfig, ServerConfig, TokioRuntime};
use rcgen::{generate_simple_self_signed, CertifiedKey};
use rustls::pki_types::{CertificateDer, PrivatePkcs8KeyDer};
use rustls::version::TLS13;
let CertifiedKey {
cert: srv_crt,
key_pair: srv_key,
} = generate_simple_self_signed(
names
.iter()
.map(|name| format!("{name}.server.wrpc"))
.collect::<Vec<_>>(),
)
.context("failed to generate server certificate")?;
let CertifiedKey {
cert: clt_crt,
key_pair: clt_key,
} = generate_simple_self_signed(
names
.iter()
.map(|name| format!("{name}.client.wrpc"))
.collect::<Vec<_>>(),
)
.context("failed to generate client certificate")?;
let srv_crt = CertificateDer::from(srv_crt);
let mut ca = rustls::RootCertStore::empty();
ca.add(srv_crt.clone())?;
let clt_cnf = rustls::ClientConfig::builder_with_protocol_versions(&[&TLS13])
.with_root_certificates(ca)
.with_client_auth_cert(
vec![clt_crt.into()],
PrivatePkcs8KeyDer::from(clt_key.serialize_der()).into(),
)
.context("failed to create client config")?;
let clt_cnf: QuicClientConfig = clt_cnf
.try_into()
.context("failed to convert rustls client config to QUIC client config")?;
let srv_cnf = ServerConfig::with_single_cert(
vec![srv_crt],
PrivatePkcs8KeyDer::from(srv_key.serialize_der()).into(),
)
.expect("failed to create server config");
let mut clt_ep = quinn::Endpoint::client((Ipv6Addr::LOCALHOST, 0).into())
.context("failed to create client endpoint")?;
clt_ep.set_default_client_config(ClientConfig::new(Arc::new(clt_cnf)));
let srv_sock = std::net::UdpSocket::bind((Ipv6Addr::LOCALHOST, 0))
.context("failed to open a UDP socket")?;
let srv_addr = srv_sock
.local_addr()
.context("failed to query server address")?;
let srv_ep = quinn::Endpoint::new(
EndpointConfig::default(),
Some(srv_cnf),
srv_sock,
Arc::new(TokioRuntime),
)
.context("failed to create server endpoint")?;
f(srv_addr.port(), clt_ep, srv_ep)
.await
.context("closure failed")
}

@rvolosatovs rvolosatovs added documentation Improvements or additions to documentation help wanted Extra attention is needed rust wRPC Rust support labels Sep 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation help wanted Extra attention is needed rust wRPC Rust support
Projects
None yet
Development

No branches or pull requests

1 participant