2023-05-18 20:30:10 -04:00
|
|
|
use crate::common::{certs_key::CertsKey, ALPN_XMPP_CLIENT, ALPN_XMPP_SERVER};
|
|
|
|
use rustls::{client::ServerCertVerifier, ClientConfig};
|
2022-07-16 23:23:01 -04:00
|
|
|
use std::sync::Arc;
|
|
|
|
use tokio_rustls::TlsConnector;
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct OutgoingConfig {
|
|
|
|
pub max_stanza_size_bytes: usize,
|
|
|
|
pub certs_key: Arc<CertsKey>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OutgoingConfig {
|
2023-05-18 20:30:10 -04:00
|
|
|
pub fn with_custom_certificate_verifier(&self, is_c2s: bool, cert_verifier: Arc<dyn ServerCertVerifier>) -> OutgoingVerifierConfig {
|
2022-07-16 23:23:01 -04:00
|
|
|
let config = match is_c2s {
|
|
|
|
false => ClientConfig::builder()
|
|
|
|
.with_safe_defaults()
|
2023-05-18 20:30:10 -04:00
|
|
|
.with_custom_certificate_verifier(cert_verifier)
|
2022-07-16 23:23:01 -04:00
|
|
|
.with_client_cert_resolver(self.certs_key.clone()),
|
2023-05-18 20:30:10 -04:00
|
|
|
_ => ClientConfig::builder().with_safe_defaults().with_custom_certificate_verifier(cert_verifier).with_no_client_auth(),
|
2022-07-16 23:23:01 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut config_alpn = config.clone();
|
|
|
|
config_alpn.alpn_protocols.push(if is_c2s { ALPN_XMPP_CLIENT } else { ALPN_XMPP_SERVER }.to_vec());
|
|
|
|
|
|
|
|
let config_alpn = Arc::new(config_alpn);
|
|
|
|
|
|
|
|
let connector_alpn: TlsConnector = config_alpn.clone().into();
|
|
|
|
|
|
|
|
let connector: TlsConnector = Arc::new(config).into();
|
|
|
|
|
|
|
|
OutgoingVerifierConfig {
|
|
|
|
max_stanza_size_bytes: self.max_stanza_size_bytes,
|
|
|
|
config_alpn,
|
|
|
|
connector_alpn,
|
|
|
|
connector,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct OutgoingVerifierConfig {
|
|
|
|
pub max_stanza_size_bytes: usize,
|
|
|
|
|
|
|
|
pub config_alpn: Arc<ClientConfig>,
|
|
|
|
pub connector_alpn: TlsConnector,
|
|
|
|
|
|
|
|
pub connector: TlsConnector,
|
|
|
|
}
|