2021-07-17 00:45:24 +00:00
|
|
|
// I'm not sure where I got this module from, but it's probably from the
|
|
|
|
// quinn examples, so the license should be okay.
|
|
|
|
|
2021-07-17 01:10:30 +00:00
|
|
|
use std::{
|
|
|
|
net::SocketAddr,
|
|
|
|
sync::Arc,
|
2021-07-17 03:11:01 +00:00
|
|
|
time::Duration,
|
|
|
|
};
|
|
|
|
|
|
|
|
use quinn::{
|
2022-10-09 15:30:57 +00:00
|
|
|
ClientConfig, Endpoint, Incoming,
|
|
|
|
ServerConfig, TransportConfig,
|
2021-07-17 01:10:30 +00:00
|
|
|
};
|
2021-07-17 00:45:24 +00:00
|
|
|
|
2021-07-17 00:58:35 +00:00
|
|
|
/// Constructs a QUIC endpoint configured for use a client only.
|
|
|
|
///
|
|
|
|
/// ## Args
|
|
|
|
///
|
|
|
|
/// - server_certs: list of trusted certificates.
|
|
|
|
#[allow(unused)]
|
|
|
|
pub fn make_client_endpoint(
|
|
|
|
bind_addr: SocketAddr,
|
|
|
|
server_certs: &[&[u8]],
|
|
|
|
) -> anyhow::Result<Endpoint> {
|
2021-07-17 03:11:01 +00:00
|
|
|
let mut client_cfg = configure_client (server_certs)?;
|
|
|
|
let mut transport = quinn::TransportConfig::default ();
|
|
|
|
transport.keep_alive_interval (Some (Duration::from_millis (5_000)));
|
|
|
|
|
|
|
|
client_cfg.transport = Arc::new (transport);
|
|
|
|
|
2022-10-09 15:30:57 +00:00
|
|
|
let mut endpoint = Endpoint::client (bind_addr)?;
|
|
|
|
endpoint.set_default_client_config (client_cfg);
|
2021-07-17 00:58:35 +00:00
|
|
|
Ok(endpoint)
|
|
|
|
}
|
|
|
|
|
2021-07-17 00:45:24 +00:00
|
|
|
/// Constructs a QUIC endpoint configured to listen for incoming connections
|
|
|
|
/// on a certain address and port.
|
|
|
|
///
|
|
|
|
/// ## Returns
|
|
|
|
///
|
|
|
|
/// - a stream of incoming QUIC connections
|
|
|
|
/// - server certificate serialized into DER format
|
|
|
|
#[allow(unused)]
|
|
|
|
pub fn make_server_endpoint(bind_addr: SocketAddr) -> anyhow::Result<(Incoming, Vec<u8>)> {
|
|
|
|
let (server_config, server_cert) = configure_server()?;
|
2022-10-09 15:30:57 +00:00
|
|
|
let (_endpoint, incoming) = Endpoint::server (server_config, bind_addr)?;
|
2021-07-17 00:45:24 +00:00
|
|
|
Ok((incoming, server_cert))
|
|
|
|
}
|
|
|
|
|
2021-07-17 00:58:35 +00:00
|
|
|
/// Builds default quinn client config and trusts given certificates.
|
|
|
|
///
|
|
|
|
/// ## Args
|
|
|
|
///
|
|
|
|
/// - server_certs: a list of trusted certificates in DER format.
|
2022-10-09 15:30:57 +00:00
|
|
|
fn configure_client (server_certs: &[&[u8]]) -> anyhow::Result<ClientConfig> {
|
|
|
|
let mut certs = rustls::RootCertStore::empty ();
|
2021-07-17 00:58:35 +00:00
|
|
|
for cert in server_certs {
|
2022-10-09 15:30:57 +00:00
|
|
|
certs.add (&rustls::Certificate (cert.to_vec ()))?;
|
2021-07-17 00:58:35 +00:00
|
|
|
}
|
2022-10-09 15:30:57 +00:00
|
|
|
|
|
|
|
Ok (ClientConfig::with_root_certificates (certs))
|
2021-07-17 00:58:35 +00:00
|
|
|
}
|
|
|
|
|
2021-07-17 00:45:24 +00:00
|
|
|
/// Returns default server configuration along with its certificate.
|
|
|
|
#[allow(clippy::field_reassign_with_default)] // https://github.com/rust-lang/rust-clippy/issues/6527
|
2022-10-09 15:30:57 +00:00
|
|
|
fn configure_server () -> anyhow::Result<(ServerConfig, Vec<u8>)> {
|
2021-07-17 00:45:24 +00:00
|
|
|
let cert = rcgen::generate_simple_self_signed(vec!["localhost".into()]).unwrap();
|
|
|
|
let cert_der = cert.serialize_der().unwrap();
|
|
|
|
let priv_key = cert.serialize_private_key_der();
|
2022-10-09 15:30:57 +00:00
|
|
|
let priv_key = rustls::PrivateKey (priv_key);
|
|
|
|
let cert_chain = vec! [rustls::Certificate (cert_der.clone ())];
|
|
|
|
|
|
|
|
let mut server_config = ServerConfig::with_single_cert (cert_chain, priv_key)?;
|
|
|
|
Arc::get_mut (&mut server_config.transport)
|
|
|
|
.unwrap ()
|
|
|
|
.max_concurrent_uni_streams (0_u8.into ());
|
|
|
|
|
|
|
|
Ok ((server_config, cert_der))
|
2021-07-17 00:45:24 +00:00
|
|
|
}
|