Skip to content

Commit

Permalink
More panics
Browse files Browse the repository at this point in the history
  • Loading branch information
nyonson committed May 15, 2024
1 parent 8b44d03 commit 31e93ec
Showing 1 changed file with 19 additions and 51 deletions.
70 changes: 19 additions & 51 deletions proxy/src/bin/async.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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];
Expand All @@ -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.
Expand All @@ -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];
Expand All @@ -84,39 +53,38 @@ 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...");
let read = remote
.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);
match auth {
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.");

Expand All @@ -133,21 +101,21 @@ 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);
},
}
},
res = read_v2(&mut remote_reader, &mut decrypter) => {
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);
},
}
},
Expand Down

0 comments on commit 31e93ec

Please sign in to comment.