use std::{path::Path, sync::Arc, time::Duration};
use tokio::io::{AsyncRead, AsyncWrite};
use tokio_rustls::{rustls::ServerConfig, server::TlsStream};
use super::future::RustlsAcceptorFuture;
use crate::server::accept::Accept;
const TLS_HANDSHAKE_TIMEOUT: Duration = Duration::from_secs(10);
#[derive(Clone)]
pub(crate) struct RustlsAcceptor {
mode: RustlsMode,
handshake_timeout: Duration,
}
#[derive(Clone)]
enum RustlsMode {
Fixed(Arc<ServerConfig>),
Acme {
default_config: Arc<ServerConfig>,
challenge_config: Arc<ServerConfig>,
},
}
impl RustlsAcceptor {
pub(crate) fn self_signed() -> crate::Result<Self> {
Ok(Self::new(crate::server::certificate::config_self_signed()?))
}
pub(crate) fn from_pem_files(cert: &Path, key: &Path) -> crate::Result<Self> {
Ok(Self::new(
crate::server::certificate::config_from_pem_chain_file(cert, key)?,
))
}
pub(in crate::server) fn new(config: RustlsConfig) -> Self {
Self {
mode: RustlsMode::Fixed(config.inner),
handshake_timeout: TLS_HANDSHAKE_TIMEOUT,
}
}
pub(super) fn new_acme(
default_config: Arc<ServerConfig>,
challenge_config: Arc<ServerConfig>,
) -> Self {
Self {
mode: RustlsMode::Acme {
default_config,
challenge_config,
},
handshake_timeout: TLS_HANDSHAKE_TIMEOUT,
}
}
pub(in crate::server) fn default_config(&self) -> Arc<ServerConfig> {
match &self.mode {
RustlsMode::Fixed(config) => config.clone(),
RustlsMode::Acme { default_config, .. } => default_config.clone(),
}
}
}
impl<I, S> Accept<I, S> for RustlsAcceptor
where
I: AsyncRead + AsyncWrite + Unpin + Send + 'static,
{
type Stream = TlsStream<I>;
type Service = S;
type Future = RustlsAcceptorFuture<I, S>;
fn accept(&self, stream: I, service: S) -> Self::Future {
match &self.mode {
RustlsMode::Fixed(config) => {
RustlsAcceptorFuture::new(stream, service, config.clone(), self.handshake_timeout)
}
RustlsMode::Acme {
default_config,
challenge_config,
} => RustlsAcceptorFuture::new_acme(
stream,
service,
default_config.clone(),
challenge_config.clone(),
self.handshake_timeout,
),
}
}
}
pub(in crate::server) fn set_http_alpn_protocols(config: &mut ServerConfig) {
config.alpn_protocols = vec![
b"h2".to_vec(),
b"http/1.1".to_vec(),
b"http/1.0".to_vec(),
b"http/0.9".to_vec(),
];
}
pub(crate) struct RustlsConfig {
inner: Arc<ServerConfig>,
}
impl RustlsConfig {
pub(crate) fn from_config(inner: Arc<ServerConfig>) -> Self {
Self { inner }
}
}