use std::{future::Future, io, net::SocketAddr, sync::Arc};
use axum::Router;
use futures_util::StreamExt;
use rustls_acme::{
caches::DirCache, tower::TowerHttp01ChallengeService, AcmeConfig, AcmeState, UseChallenge,
};
use super::rustls::{set_http_alpn_protocols, RustlsAcceptor, RustlsConfig};
use crate::{
args::{AcmeChallenge, AcmeOptions},
server::Handle,
};
const HTTP01_CHALLENGE_PATH: &str = "/.well-known/acme-challenge/{challenge_token}";
pub(crate) struct AcmeRuntime {
acceptor: RustlsAcceptor,
http01: Option<Http01Challenge>,
state: AcmeState<io::Error>,
}
impl AcmeRuntime {
pub(crate) fn new(options: AcmeOptions) -> crate::Result<Self> {
let AcmeOptions {
domains,
contacts,
challenge,
http_bind,
production,
} = options;
let cache_dir = crate::state::directory().join("acme");
crate::state::prepare_private_directory(&cache_dir)?;
let challenge_type = match challenge {
AcmeChallenge::TlsAlpn01 => UseChallenge::TlsAlpn01,
AcmeChallenge::Http01 => UseChallenge::Http01,
};
let state = AcmeConfig::new(domains)
.contact(contacts)
.cache(DirCache::new(cache_dir))
.directory_lets_encrypt(production)
.challenge_type(challenge_type)
.state();
let mut default_config = state.default_rustls_config();
let config = Arc::get_mut(&mut default_config).ok_or_else(|| {
io::Error::other("ACME rustls configuration was shared before initialization")
})?;
set_http_alpn_protocols(config);
let (acceptor, http01) = match challenge {
AcmeChallenge::TlsAlpn01 => (
RustlsAcceptor::new_acme(default_config, state.challenge_rustls_config()),
None,
),
AcmeChallenge::Http01 => {
let bind = http_bind.ok_or_else(|| {
io::Error::new(
io::ErrorKind::InvalidInput,
"HTTP-01 requires a challenge listener address",
)
})?;
let service = state.http01_challenge_tower_service();
(
RustlsAcceptor::new(RustlsConfig::from_config(default_config)),
Some(Http01Challenge { bind, service }),
)
}
};
Ok(Self {
acceptor,
http01,
state,
})
}
pub(crate) fn into_parts(
self,
shutdown: Handle,
) -> (
RustlsAcceptor,
Option<Http01Challenge>,
impl Future<Output = ()> + Send + 'static,
) {
(
self.acceptor,
self.http01,
drive_state(self.state, shutdown),
)
}
}
pub(crate) struct Http01Challenge {
bind: SocketAddr,
service: TowerHttp01ChallengeService,
}
impl Http01Challenge {
pub(crate) fn bind(&self) -> SocketAddr {
self.bind
}
pub(crate) fn into_router(self) -> Router {
Router::new().route_service(HTTP01_CHALLENGE_PATH, self.service)
}
}
async fn drive_state(mut state: AcmeState<io::Error>, shutdown: Handle) {
loop {
tokio::select! {
_ = shutdown.wait_graceful_shutdown() => return,
event = state.next() => match event {
Some(Ok(event)) => {
tracing::info!(?event, "ACME certificate state changed");
}
Some(Err(error)) => {
tracing::error!(%error, "ACME certificate operation failed");
}
None => {
tracing::warn!("ACME certificate state stream ended");
return;
}
},
}
}
}