use crate::consts::DFLT_PORT;
use crate::util::{get_cert, get_cert_and_key, CertReadError};
use quinn::{Certificate, CertificateChain, PrivateKey};
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::path::Path;
#[derive(Clone, Debug)]
pub struct CoordConfig {
pub(super) laddr: SocketAddr,
pub(super) keylog: bool,
pub(super) stateless_retry: bool,
pub(super) cert_and_key: (CertificateChain, PrivateKey),
pub(super) client_ca: Option<Certificate>,
}
impl CoordConfig {
pub fn new_from_file(cert_path: &Path, key_path: &Path) -> Result<Self, CertReadError> {
let (cert, key, _) = get_cert_and_key(cert_path, key_path)?;
Ok(Self::new(cert, key))
}
pub fn new(cert: CertificateChain, key: PrivateKey) -> Self {
Self {
laddr: SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), DFLT_PORT),
keylog: false,
stateless_retry: false,
cert_and_key: (cert, key),
client_ca: None,
}
}
pub fn set_port(&mut self, port: u16) -> &mut Self {
self.laddr.set_port(port);
self
}
pub fn set_ip(&mut self, ip: IpAddr) -> &mut Self {
self.laddr.set_ip(ip);
self
}
pub fn enable_keylog(&mut self) -> &mut Self {
self.keylog = true;
self
}
pub fn enable_stateless_retry(&mut self) -> &mut Self {
self.stateless_retry = true;
self
}
pub fn set_client_ca(&mut self, ca: Certificate) -> &mut Self {
self.client_ca = Some(ca);
self
}
pub fn set_client_ca_from_file(&mut self, cert_path: &Path) -> Result<&mut Self, CertReadError> {
Ok(self.set_client_ca(get_cert(cert_path)?))
}
}