use crate::responses::ChallengeType;
use std::{
collections::HashMap,
fmt::{Debug, Formatter},
time::Duration,
};
mod common;
#[cfg(feature = "dns-01")]
#[cfg_attr(docsrs, doc(cfg(feature = "dns-01")))]
pub mod dns;
#[cfg(feature = "http-01")]
mod http;
#[cfg(feature = "tls-alpn-01")]
mod tls_alpn;
#[cfg(any(feature = "http-01", feature = "tls-alpn-01"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "http-01", feature = "tls-alpn-01"))))]
pub use common::SolverHandle;
#[cfg(feature = "http-01")]
#[cfg_attr(docsrs, doc(cfg(feature = "http-01")))]
pub use http::Http01Solver;
#[cfg(feature = "tls-alpn-01")]
#[cfg_attr(docsrs, doc(cfg(feature = "tls-alpn-01")))]
pub use tls_alpn::TlsAlpn01Solver;
#[async_trait::async_trait]
pub trait Solver {
async fn present(
&self,
domain: String,
token: String,
key_authorization: String,
) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>;
async fn cleanup(
&self,
token: &str,
) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>;
fn attempts(&self) -> usize {
30
}
fn interval(&self) -> Duration {
Duration::from_secs(2)
}
}
pub fn boxed_err<E>(e: E) -> Box<dyn std::error::Error + Send + Sync + 'static>
where
E: std::error::Error + Send + Sync + 'static,
{
Box::new(e)
}
#[derive(Default)]
pub(crate) struct SolverManager {
solvers: HashMap<ChallengeType, Box<dyn Solver>>,
}
impl SolverManager {
pub fn set_dns01_solver(&mut self, solver: Box<dyn Solver>) {
self.solvers.insert(ChallengeType::Dns01, solver);
}
pub fn set_http01_solver(&mut self, solver: Box<dyn Solver>) {
self.solvers.insert(ChallengeType::Http01, solver);
}
pub fn set_tls_alpn01_solver(&mut self, solver: Box<dyn Solver>) {
self.solvers.insert(ChallengeType::TlsAlpn01, solver);
}
pub fn get(&self, type_: ChallengeType) -> Option<&dyn Solver> {
self.solvers.get(&type_).map(AsRef::as_ref)
}
}
impl Debug for SolverManager {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let solvers = self.solvers.keys().collect::<Vec<&ChallengeType>>();
f.debug_struct("SolverManager")
.field("registered", &solvers)
.finish()
}
}