use std::io;
use std::net::TcpStream;
#[cfg(feature = "security")]
pub mod rustls_connector;
#[cfg(feature = "security")]
pub use rustls_connector::RustlsConnector;
#[derive(Debug, Clone)]
pub struct TlsConfig {
pub verify_hostname: bool,
pub ca_cert_path: Option<String>,
pub client_cert_path: Option<String>,
pub client_key_path: Option<String>,
}
impl Default for TlsConfig {
fn default() -> Self {
TlsConfig {
verify_hostname: true,
ca_cert_path: None,
client_cert_path: None,
client_key_path: None,
}
}
}
impl TlsConfig {
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn with_hostname_verification(mut self, verify: bool) -> Self {
self.verify_hostname = verify;
self
}
#[must_use]
pub fn with_ca_cert(mut self, path: String) -> Self {
self.ca_cert_path = Some(path);
self
}
#[must_use]
pub fn with_client_cert(mut self, cert_path: String, key_path: String) -> Self {
self.client_cert_path = Some(cert_path);
self.client_key_path = Some(key_path);
self
}
}
#[allow(dead_code)] pub trait TlsStream: io::Read + io::Write + Send {
fn is_secured(&self) -> bool;
fn set_read_timeout(&mut self, dur: Option<std::time::Duration>) -> io::Result<()>;
fn set_write_timeout(&mut self, dur: Option<std::time::Duration>) -> io::Result<()>;
fn shutdown(&mut self) -> io::Result<()>;
}
#[allow(dead_code)] pub struct PlainStream {
inner: TcpStream,
}
#[allow(dead_code)] impl PlainStream {
pub fn new(stream: TcpStream) -> Self {
PlainStream { inner: stream }
}
}
impl TlsStream for PlainStream {
fn is_secured(&self) -> bool {
false
}
fn set_read_timeout(&mut self, dur: Option<std::time::Duration>) -> io::Result<()> {
self.inner.set_read_timeout(dur)
}
fn set_write_timeout(&mut self, dur: Option<std::time::Duration>) -> io::Result<()> {
self.inner.set_write_timeout(dur)
}
fn shutdown(&mut self) -> io::Result<()> {
use std::net::Shutdown;
self.inner.shutdown(Shutdown::Both)
}
}
impl io::Read for PlainStream {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.inner.read(buf)
}
}
impl io::Write for PlainStream {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.inner.write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.inner.flush()
}
}