diff --git a/prototypes/quic_demo/src/bin/quic_demo_server.rs b/prototypes/quic_demo/src/bin/quic_demo_server.rs index 3e9cbd5..f28f873 100644 --- a/prototypes/quic_demo/src/bin/quic_demo_server.rs +++ b/prototypes/quic_demo/src/bin/quic_demo_server.rs @@ -6,7 +6,7 @@ use tracing::{ warn, }; -use quinn_utils::*; +use quic_demo::quinn_utils::*; #[tokio::main] async fn main () -> anyhow::Result <()> { @@ -84,49 +84,3 @@ async fn main () -> anyhow::Result <()> { Ok (()) } - -// I'm not sure where I got this module from, but it's probably from the -// quinn examples, so the license should be okay. - -mod quinn_utils { - use quinn::{ - Certificate, CertificateChain, ClientConfig, ClientConfigBuilder, Endpoint, Incoming, - PrivateKey, ServerConfig, ServerConfigBuilder, TransportConfig, - }; - use std::{error::Error, net::SocketAddr, sync::Arc}; - - /// 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)> { - let (server_config, server_cert) = configure_server()?; - let mut endpoint_builder = Endpoint::builder(); - endpoint_builder.listen(server_config); - let (_endpoint, incoming) = endpoint_builder.bind(&bind_addr)?; - Ok((incoming, server_cert)) - } - - /// Returns default server configuration along with its certificate. - #[allow(clippy::field_reassign_with_default)] // https://github.com/rust-lang/rust-clippy/issues/6527 - fn configure_server() -> anyhow::Result<(ServerConfig, Vec)> { - 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(); - let priv_key = PrivateKey::from_der(&priv_key)?; - - let mut transport_config = TransportConfig::default(); - transport_config.max_concurrent_uni_streams(0).unwrap(); - let mut server_config = ServerConfig::default(); - server_config.transport = Arc::new(transport_config); - let mut cfg_builder = ServerConfigBuilder::new(server_config); - let cert = Certificate::from_der(&cert_der)?; - cfg_builder.certificate(CertificateChain::from_certs(vec![cert]), priv_key)?; - - Ok((cfg_builder.build(), cert_der)) - } -} diff --git a/prototypes/quic_demo/src/lib.rs b/prototypes/quic_demo/src/lib.rs new file mode 100644 index 0000000..fdfefeb --- /dev/null +++ b/prototypes/quic_demo/src/lib.rs @@ -0,0 +1 @@ +pub mod quinn_utils; diff --git a/prototypes/quic_demo/src/quinn_utils.rs b/prototypes/quic_demo/src/quinn_utils.rs new file mode 100644 index 0000000..727110f --- /dev/null +++ b/prototypes/quic_demo/src/quinn_utils.rs @@ -0,0 +1,43 @@ +// I'm not sure where I got this module from, but it's probably from the +// quinn examples, so the license should be okay. + +use quinn::{ + Certificate, CertificateChain, ClientConfig, ClientConfigBuilder, Endpoint, Incoming, + PrivateKey, ServerConfig, ServerConfigBuilder, TransportConfig, +}; +use std::{error::Error, net::SocketAddr, sync::Arc}; + +/// 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)> { + let (server_config, server_cert) = configure_server()?; + let mut endpoint_builder = Endpoint::builder(); + endpoint_builder.listen(server_config); + let (_endpoint, incoming) = endpoint_builder.bind(&bind_addr)?; + Ok((incoming, server_cert)) +} + +/// Returns default server configuration along with its certificate. +#[allow(clippy::field_reassign_with_default)] // https://github.com/rust-lang/rust-clippy/issues/6527 +fn configure_server() -> anyhow::Result<(ServerConfig, Vec)> { + 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(); + let priv_key = PrivateKey::from_der(&priv_key)?; + + let mut transport_config = TransportConfig::default(); + transport_config.max_concurrent_uni_streams(0).unwrap(); + let mut server_config = ServerConfig::default(); + server_config.transport = Arc::new(transport_config); + let mut cfg_builder = ServerConfigBuilder::new(server_config); + let cert = Certificate::from_der(&cert_der)?; + cfg_builder.certificate(CertificateChain::from_certs(vec![cert]), priv_key)?; + + Ok((cfg_builder.build(), cert_der)) +}