pub struct SniCertResolver { /* private fields */ }Expand description
SNI-based certificate resolver for dynamic TLS certificate selection
This resolver maintains a mapping of domain names to TLS certificates, allowing the proxy to serve different certificates for different domains. It supports:
- Exact domain matching (e.g.,
api.example.com) - Wildcard certificates (e.g.,
*.example.com) - A default/fallback certificate for unmatched domains
The resolver is thread-safe and supports concurrent certificate updates.
Implementations§
Source§impl SniCertResolver
impl SniCertResolver
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new empty SNI certificate resolver
§Example
use zlayer_proxy::SniCertResolver;
let resolver = SniCertResolver::new();Sourcepub fn load_cert(
&self,
domain: &str,
cert_pem: &str,
key_pem: &str,
) -> Result<()>
pub fn load_cert( &self, domain: &str, cert_pem: &str, key_pem: &str, ) -> Result<()>
Load a certificate for a specific domain
Parses the PEM-encoded certificate chain and private key, then stores
the resulting CertifiedKey for the given domain.
§Arguments
domain- The domain name (e.g.,example.comor*.example.com)cert_pem- PEM-encoded certificate chainkey_pem- PEM-encoded private key
§Errors
Returns an error if:
- The certificate PEM cannot be parsed
- The private key PEM cannot be parsed
- The key is not compatible with the certificate
§Example
resolver.load_cert("example.com", cert_pem, key_pem)?;Sourcepub fn set_default_cert(&self, cert_pem: &str, key_pem: &str) -> Result<()>
pub fn set_default_cert(&self, cert_pem: &str, key_pem: &str) -> Result<()>
Set the default/fallback certificate
This certificate is used when no domain-specific certificate matches the client’s SNI request.
§Arguments
cert_pem- PEM-encoded certificate chainkey_pem- PEM-encoded private key
§Errors
Returns an error if the certificate or key cannot be parsed.
§Example
resolver.set_default_cert(default_cert_pem, default_key_pem)?;§Panics
Panics if the internal RwLock is poisoned.
Sourcepub fn remove_cert(&self, domain: &str)
pub fn remove_cert(&self, domain: &str)
Sourcepub fn refresh_cert(
&self,
domain: &str,
cert_pem: &str,
key_pem: &str,
) -> Result<()>
pub fn refresh_cert( &self, domain: &str, cert_pem: &str, key_pem: &str, ) -> Result<()>
Refresh/update a certificate for an existing domain
This is equivalent to calling load_cert but semantically indicates
an update to an existing certificate (e.g., for certificate renewal).
§Arguments
domain- The domain namecert_pem- New PEM-encoded certificate chainkey_pem- New PEM-encoded private key
§Errors
Returns an error if the certificate or key cannot be parsed.
§Example
resolver.refresh_cert("example.com", new_cert_pem, new_key_pem)?;Sourcepub fn cert_count(&self) -> usize
pub fn cert_count(&self) -> usize
Get the number of loaded certificates
Sourcepub fn has_default_cert(&self) -> bool
pub fn has_default_cert(&self) -> bool
Check if a default/fallback certificate is configured