2023-09-15 19:06:28 +00:00
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
|
|
|
fs, io,
|
|
|
|
sync::{Arc, Mutex},
|
|
|
|
time,
|
|
|
|
};
|
2023-06-08 07:01:34 +00:00
|
|
|
|
|
|
|
use anyhow::Context;
|
|
|
|
|
2023-09-15 19:06:28 +00:00
|
|
|
use moq_transport::model::broadcast;
|
2023-06-16 18:38:19 +00:00
|
|
|
use tokio::task::JoinSet;
|
|
|
|
|
2023-09-15 19:06:28 +00:00
|
|
|
use crate::{Config, Session};
|
|
|
|
|
2023-06-08 07:01:34 +00:00
|
|
|
pub struct Server {
|
2023-07-20 01:04:45 +00:00
|
|
|
server: quinn::Endpoint,
|
2023-06-08 07:01:34 +00:00
|
|
|
|
2023-07-20 01:04:45 +00:00
|
|
|
// The active connections.
|
|
|
|
conns: JoinSet<anyhow::Result<()>>,
|
2023-06-08 07:01:34 +00:00
|
|
|
|
2023-09-15 19:06:28 +00:00
|
|
|
// The map of active broadcasts by path.
|
|
|
|
broadcasts: Arc<Mutex<HashMap<String, broadcast::Subscriber>>>,
|
2023-06-08 07:01:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Server {
|
|
|
|
// Create a new server
|
2023-09-15 19:06:28 +00:00
|
|
|
pub fn new(config: Config) -> anyhow::Result<Self> {
|
2023-06-08 07:01:34 +00:00
|
|
|
// Read the PEM certificate chain
|
|
|
|
let certs = fs::File::open(config.cert).context("failed to open cert file")?;
|
|
|
|
let mut certs = io::BufReader::new(certs);
|
|
|
|
let certs = rustls_pemfile::certs(&mut certs)?
|
|
|
|
.into_iter()
|
|
|
|
.map(rustls::Certificate)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
// Read the PEM private key
|
|
|
|
let keys = fs::File::open(config.key).context("failed to open key file")?;
|
|
|
|
let mut keys = io::BufReader::new(keys);
|
|
|
|
let mut keys = rustls_pemfile::pkcs8_private_keys(&mut keys)?;
|
|
|
|
|
|
|
|
anyhow::ensure!(keys.len() == 1, "expected a single key");
|
|
|
|
let key = rustls::PrivateKey(keys.remove(0));
|
|
|
|
|
|
|
|
let mut tls_config = rustls::ServerConfig::builder()
|
|
|
|
.with_safe_default_cipher_suites()
|
|
|
|
.with_safe_default_kx_groups()
|
|
|
|
.with_protocol_versions(&[&rustls::version::TLS13])
|
|
|
|
.unwrap()
|
|
|
|
.with_no_client_auth()
|
|
|
|
.with_single_cert(certs, key)?;
|
|
|
|
|
|
|
|
tls_config.max_early_data_size = u32::MAX;
|
2023-07-20 01:04:45 +00:00
|
|
|
tls_config.alpn_protocols = vec![webtransport_quinn::ALPN.to_vec()];
|
2023-06-08 07:01:34 +00:00
|
|
|
|
2023-09-15 19:06:28 +00:00
|
|
|
let mut server_config = quinn::ServerConfig::with_crypto(Arc::new(tls_config));
|
2023-06-08 17:08:18 +00:00
|
|
|
|
|
|
|
// Enable BBR congestion control
|
|
|
|
// TODO validate the implementation
|
2023-06-08 07:01:34 +00:00
|
|
|
let mut transport_config = quinn::TransportConfig::default();
|
|
|
|
transport_config.keep_alive_interval(Some(time::Duration::from_secs(2)));
|
2023-09-15 19:06:28 +00:00
|
|
|
transport_config.congestion_controller_factory(Arc::new(quinn::congestion::BbrConfig::default()));
|
2023-06-08 07:01:34 +00:00
|
|
|
|
2023-09-15 19:06:28 +00:00
|
|
|
server_config.transport = Arc::new(transport_config);
|
|
|
|
let server = quinn::Endpoint::server(server_config, config.bind)?;
|
2023-06-16 18:38:19 +00:00
|
|
|
|
2023-09-15 19:06:28 +00:00
|
|
|
let broadcasts = Default::default();
|
2023-07-20 01:04:45 +00:00
|
|
|
let conns = JoinSet::new();
|
2023-06-08 07:01:34 +00:00
|
|
|
|
2023-09-15 19:06:28 +00:00
|
|
|
Ok(Self {
|
|
|
|
server,
|
|
|
|
broadcasts,
|
|
|
|
conns,
|
|
|
|
})
|
2023-06-08 07:01:34 +00:00
|
|
|
}
|
|
|
|
|
2023-06-26 18:51:06 +00:00
|
|
|
pub async fn run(mut self) -> anyhow::Result<()> {
|
2023-06-08 07:01:34 +00:00
|
|
|
loop {
|
2023-06-16 18:38:19 +00:00
|
|
|
tokio::select! {
|
|
|
|
res = self.server.accept() => {
|
2023-07-20 01:04:45 +00:00
|
|
|
let conn = res.context("failed to accept QUIC connection")?;
|
2023-09-15 19:06:28 +00:00
|
|
|
let mut session = Session::new(self.broadcasts.clone());
|
|
|
|
self.conns.spawn(async move { session.run(conn).await });
|
2023-06-16 18:38:19 +00:00
|
|
|
},
|
2023-07-20 01:04:45 +00:00
|
|
|
res = self.conns.join_next(), if !self.conns.is_empty() => {
|
2023-06-16 18:38:19 +00:00
|
|
|
let res = res.expect("no tasks").expect("task aborted");
|
|
|
|
if let Err(err) = res {
|
2023-09-15 19:06:28 +00:00
|
|
|
log::warn!("connection terminated: {:?}", err);
|
2023-06-16 18:38:19 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2023-06-08 07:01:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|