♻️ refactor: move `quinn_utils` mod out
parent
e1ccb2bd57
commit
3e4ff7298e
|
@ -6,7 +6,7 @@ use tracing::{
|
||||||
warn,
|
warn,
|
||||||
};
|
};
|
||||||
|
|
||||||
use quinn_utils::*;
|
use quic_demo::quinn_utils::*;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main () -> anyhow::Result <()> {
|
async fn main () -> anyhow::Result <()> {
|
||||||
|
@ -84,49 +84,3 @@ async fn main () -> anyhow::Result <()> {
|
||||||
|
|
||||||
Ok (())
|
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<u8>)> {
|
|
||||||
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<u8>)> {
|
|
||||||
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))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
pub mod quinn_utils;
|
|
@ -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<u8>)> {
|
||||||
|
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<u8>)> {
|
||||||
|
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))
|
||||||
|
}
|
Loading…
Reference in New Issue