This commit is contained in:
nora 2023-09-08 23:32:17 +02:00
parent 93053aa6eb
commit c71fc68d8e
2 changed files with 195 additions and 41 deletions

View file

@ -2,12 +2,10 @@ pub mod proto;
use std::{
fmt::Debug,
io::{self},
io::{self, BufWriter, Read, Write},
net::{TcpStream, ToSocketAddrs},
};
use crate::proto::CipherSuite;
type Result<T, E = Error> = std::result::Result<T, E>;
pub struct ClientConnection {}
@ -24,19 +22,26 @@ struct ClientSetupConnection {}
impl ClientSetupConnection {
fn establish(host: impl ToSocketAddrs) -> Result<Self> {
let mut stream = TcpStream::connect(host)?;
let mut stream = BufWriter::new(LoggingWriter(TcpStream::connect(host)?));
let handshake = proto::Handshake::ClientHello {
legacy_version: proto::LEGACY_VERSION,
random: rand::random(),
legacy_session_id: 0,
cipher_suites: vec![CipherSuite::TlsAes128GcmSha256].into(),
cipher_suites: vec![proto::CipherSuite::TlsAes128GcmSha256].into(),
legacy_compressions_methods: 0,
extensions: vec![].into(),
extensions: vec![proto::ExtensionCH::SupportedVersions {
versions: vec![proto::TLSV3].into(),
}]
.into(),
};
proto::write_handshake(&mut stream, handshake)?;
let plaintext = proto::TLSPlaintext::Handshake {
handshake,
};
plaintext.write(&mut stream)?;
stream.flush()?;
let res = proto::read_handshake(&mut stream)?;
dbg!(res);
// let res: proto::TLSPlaintext = proto::Value::read(&mut stream.get_mut())?;
// dbg!(res);
todo!()
}
@ -66,3 +71,30 @@ impl From<ErrorKind> for Error {
Self { kind: value }
}
}
#[derive(Debug)]
struct LoggingWriter<W>(W);
impl<W: io::Write> io::Write for LoggingWriter<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let len = self.0.write(buf);
if let Ok(len) = len {
eprintln!("wrote bytes: {:x?}", &buf[..len]);
}
len
}
fn flush(&mut self) -> io::Result<()> {
self.0.flush()
}
}
impl<R: Read> io::Read for LoggingWriter<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let len = self.0.read(buf);
if let Ok(len) = len {
eprintln!("read bytes: {:x?}", &buf[..len]);
}
len
}
}