rustls-letsencrypt 0.1.4

Automatic Let's Encrypt, ACME, and local-CA certificate management for rustls listeners
Documentation
//! Production-shaped Let's Encrypt configuration.
//!
//! Replace the hostname and address below with your real DNS name and public
//! server address before running this example.

use std::{net::IpAddr, path::PathBuf};
use rustls_letsencrypt::{
    register_alpn_handler, register_handler, AcmeEndpoint, AlpnHandlerConfig, DnsServer,
    EventOutput, LetsEncryptAcmeProfile,
};
use tokio::net::TcpListener;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let host = "api.example.com";
    let public_ip: IpAddr = "203.0.113.10".parse()?;
    let listener = TcpListener::bind("0.0.0.0:443").await?;
    let mut config = AlpnHandlerConfig::for_host(&host)?;
    config.public_ip_addresses = vec![public_ip];
    config.dns_servers = Some(vec!["udp://1.1.1.1:53".parse::<DnsServer>()?, "udp://8.8.8.8:53".parse::<DnsServer>()?]);
    config.verify_dns_before_issuance = true;
    config.cert_store_directory = PathBuf::from("./certs");
    config.acme_endpoint = AcmeEndpoint::LetsEncrypt(LetsEncryptAcmeProfile::Production);
    config.acme_contacts = vec!["mailto:ops@example.com".into()];
    config.events = EventOutput::Stderr;
    config.local_fallback_enabled = true;
    let mut alpn_listener = register_alpn_handler(listener, Some(config)).await?;
    tokio::spawn(async move {
        while let Ok(Some(connection)) = alpn_listener.accept().await {
            // Process incoming ACME/HTTPS traffic on 443 here, or close it if
            // this application only needs port 443 for certificate issuance.
            drop(connection.into_stream());
        }
    });

    // Your LDAP-over-TLS service belongs here. The handler selects the
    // certificate already obtained through 443, so LDAP clients on 636 do not
    // need to know anything about ACME.
    let ldap_listener = TcpListener::bind("0.0.0.0:636").await?;
    let mut ldap_listener = register_handler(ldap_listener).await?;
    while let Some(connection) = ldap_listener.accept().await? {
        tokio::spawn(async move { drop(connection.into_stream()); });
    }
    Ok(())
}