Add Sender struct

This commit is contained in:
Nathan Fisher 2023-05-24 03:13:49 -04:00
parent 8d4f50e3f6
commit e25ebfa353
2 changed files with 84 additions and 0 deletions

View file

@ -1,3 +1,80 @@
use std::{io::{Read, Write, self}, fmt};
use crate::{request::{Request, ParseRequestError}, response::{Response, ParseResponseError}};
use self::{verifier::Verifier, store::CertificateStore};
pub mod store; pub mod store;
pub mod verifier; pub mod verifier;
#[derive(Debug)]
pub struct Sender<'a, S: CertificateStore, C: Sized, T: Read + Write + Sized> {
pub request: Request,
pub verifier: Verifier<'a, S>,
pub stream: rustls::StreamOwned<C, T>,
}
#[derive(Debug)]
pub enum Error {
TlsError(rustls::Error),
RequestError(ParseRequestError),
ResponseError(ParseResponseError),
IoError(io::Error),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::TlsError(e) => write!(f, "{e}"),
Self::RequestError(e) => write!(f, "{e}"),
Self::ResponseError(e) => write!(f, "{e}"),
Self::IoError(e) => write!(f, "{e}"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::RequestError(e) => Some(e),
Self::ResponseError(e) => Some(e),
Self::TlsError(e) => Some(e),
Self::IoError(e) => Some(e),
}
}
}
impl From<rustls::Error> for Error {
fn from(value: rustls::Error) -> Self {
Self::TlsError(value)
}
}
impl From<ParseRequestError> for Error {
fn from(value: ParseRequestError) -> Self {
Self::RequestError(value)
}
}
impl From<ParseResponseError> for Error {
fn from(value: ParseResponseError) -> Self {
Self::ResponseError(value)
}
}
impl From<io::Error> for Error {
fn from(value: io::Error) -> Self {
Self::IoError(value)
}
}
impl<'a, S, C, T> Sender<'a, S, C, T>
where S: CertificateStore + Sync, C: Sized, T: Read + Write + Sized {
pub fn new(request_str: &str, store: &'a S) -> Result<Self, Error> {
let request: Request = request_str.parse()?;
let verifier = Verifier::new(store);
unimplemented!();
}
pub fn send(&mut self) -> Result<Response, Error> {
unimplemented!();
}
}

View file

@ -2,6 +2,7 @@ use crate::fingerprint::Fingerprint;
use rustls::{client::{ServerCertVerified, ServerCertVerifier}, Certificate}; use rustls::{client::{ServerCertVerified, ServerCertVerifier}, Certificate};
use super::store::CertificateStore; use super::store::CertificateStore;
#[derive(Debug)]
pub struct Verifier<'a, T: CertificateStore> { pub struct Verifier<'a, T: CertificateStore> {
store: &'a T, store: &'a T,
} }
@ -37,3 +38,9 @@ impl<'a, T: CertificateStore + Sync> ServerCertVerifier for Verifier<'a, T> {
return Err(rustls::Error::General("Unrecognized certificate".to_string())); return Err(rustls::Error::General("Unrecognized certificate".to_string()));
} }
} }
impl<'a, T: CertificateStore + Sync> Verifier<'a, T> {
pub fn new(store: &'a T) -> Self {
Self { store }
}
}