From 31e93ec8c7da5f77d3b92fefdaba8773daddecbf Mon Sep 17 00:00:00 2001 From: Nick Johnson Date: Wed, 15 May 2024 15:46:50 -0700 Subject: [PATCH] More panics --- proxy/src/bin/async.rs | 70 ++++++++++++------------------------------ 1 file changed, 19 insertions(+), 51 deletions(-) diff --git a/proxy/src/bin/async.rs b/proxy/src/bin/async.rs index 6045bee..d1bfabe 100644 --- a/proxy/src/bin/async.rs +++ b/proxy/src/bin/async.rs @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 -use core::fmt; +use core::panic; use bip324::{Handshake, Role}; use bip324_proxy::{read_v1, read_v2, write_v1, write_v2}; @@ -10,49 +10,16 @@ use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::net::{TcpListener, TcpStream}; use tokio::select; -#[derive(Debug)] -pub enum Error { - Handshake, - WriteV2(bip324_proxy::Error), - ReadV2(bip324_proxy::Error), - WriteV1(bip324_proxy::Error), - ReadV1(bip324_proxy::Error), -} - -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Error::WriteV2(e) => write!(f, "unable to write v2 message to remote {}", e), - Error::ReadV2(e) => write!(f, "unable to read v2 message from remote {}", e), - Error::WriteV1(e) => write!(f, "unable to write v1 message to client {}", e), - Error::ReadV1(e) => write!(f, "unable to read v1 message from client {}", e), - Error::Handshake => write!(f, "unable to establish handshake"), - } - } -} - -impl std::error::Error for Error { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { - match self { - Error::WriteV2(e) => Some(e), - Error::ReadV2(e) => Some(e), - Error::WriteV1(e) => Some(e), - Error::ReadV1(e) => Some(e), - Error::Handshake => None, - } - } -} - /// Validate and bootstrap proxy connection. -async fn proxy_conn(mut client: TcpStream) -> Result<(), Error> { +async fn proxy_conn(mut client: TcpStream) -> Result<(), bip324_proxy::Error> { let remote_ip = bip324_proxy::peek_addr(&client) .await - .map_err(|err| Error::Handshake)?; + .expect("peek address"); println!("Reaching out to {}.", remote_ip); let mut remote = TcpStream::connect(remote_ip) .await - .map_err(|err| Error::Handshake)?; + .expect("connect to remote"); println!("Initiating handshake."); let mut local_material_message = vec![0u8; 64]; @@ -62,11 +29,13 @@ async fn proxy_conn(mut client: TcpStream) -> Result<(), Error> { None, &mut local_material_message, ) - .unwrap(); + .expect("generate handshake"); + remote .write_all(&local_material_message) .await - .map_err(|err| Error::Handshake)?; + .expect("send local materials"); + println!("Sent handshake to remote."); // 64 bytes ES. @@ -75,7 +44,7 @@ async fn proxy_conn(mut client: TcpStream) -> Result<(), Error> { remote .read_exact(&mut remote_material_message) .await - .map_err(|err| Error::Handshake)?; + .expect("read remote materials"); println!("Completing materials."); let mut local_garbage_terminator_message = [0u8; 36]; @@ -84,16 +53,16 @@ async fn proxy_conn(mut client: TcpStream) -> Result<(), Error> { remote_material_message, &mut local_garbage_terminator_message, ) - .unwrap(); + .expect("complete materials"); println!("Sending garbage terminator and version packet."); remote .write_all(&local_garbage_terminator_message) .await - .map_err(|err| Error::Handshake)?; + .expect("send garbage and version"); // Keep pulling bytes from the buffer until the garbage is flushed. - // TODO: Fix arbitrary size. + // Capacity is arbitrary, could use some tuning. let mut remote_garbage_and_version_buffer = BytesMut::with_capacity(4096); loop { println!("Authenticating garbage and version packet..."); @@ -101,7 +70,7 @@ async fn proxy_conn(mut client: TcpStream) -> Result<(), Error> { .read_buf(&mut remote_garbage_and_version_buffer) .await; match read { - Err(e) => break Err(bip324_proxy::Error::Network(e)), + Err(e) => panic!("unable to read garbage {}", e), _ => { let auth = handshake.authenticate_garbage_and_version(&remote_garbage_and_version_buffer); @@ -109,14 +78,13 @@ async fn proxy_conn(mut client: TcpStream) -> Result<(), Error> { Err(e) => match e { // Read again if too small, other wise surface error. bip324::Error::MessageLengthTooSmall => continue, - e => break Err(bip324_proxy::Error::Cipher(e)), + e => panic!("unable to authenticate garbage {}", e), }, - _ => break Ok(()), + _ => break, } } } } - .map_err(|err| Error::Handshake)?; println!("Channel authenticated."); @@ -133,10 +101,10 @@ async fn proxy_conn(mut client: TcpStream) -> Result<(), Error> { match res { Ok(msg) => { println!("Read {} message from client, writing to remote.", msg.command()); - write_v2(&mut remote_writer, &mut encrypter, msg).await.map_err(|err| Error::WriteV2(err))?; + write_v2(&mut remote_writer, &mut encrypter, msg).await.expect("write v2 message"); }, Err(err) => { - return Err(Error::ReadV1(err)); + panic!("unable to read v1 from client {}", err); }, } }, @@ -144,10 +112,10 @@ async fn proxy_conn(mut client: TcpStream) -> Result<(), Error> { match res { Ok(msg) => { println!("Read {} message from remote, writing to client.", msg.command()); - write_v1(&mut client_writer, msg).await.map_err(|err| Error::WriteV1(err))?; + write_v1(&mut client_writer, msg).await.expect("write v1 message"); }, Err(err) => { - return Err(Error::ReadV2(err)); + panic!("unable to read v2 from client {}", err); }, } },