Skip to content

Commit

Permalink
WIP function interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
nyonson committed Apr 12, 2024
1 parent 51e722d commit 4b4e999
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
6 changes: 3 additions & 3 deletions proxy/src/bin/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@ async fn proxy_conn(client: TcpStream) -> Result<(), Box<dyn std::error::Error>>
let (mut remote_reader, mut remote_writer) = remote.split();
let (mut encrypter, mut decrypter) = packet_reader.split();

println!("Setting up proxy loop.")
println!("Setting up proxy loop.");
loop {
select! {
res = read_v1(&mut client_reader) => {
match res {
Ok(msg) => {
println!("Read {} message from client, writing to remote.", msg.cmd());
write_v1(&mut remote_writer, msg).await?;
write_v2(&mut remote_writer, &mut encrypter, msg).await?;
},
Err(e) => {
return Err(e);
},
}
},
res = read_v1(&mut remote_reader) => {
res = read_v2(&mut remote_reader, &mut decrypter) => {
match res {
Ok(msg) => {
println!("Read {} message from remote, writing to client.", msg.cmd());
Expand Down
19 changes: 19 additions & 0 deletions proxy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use std::fmt;
use std::net::SocketAddr;

use bip324::{PacketReader, PacketWriter};
use bitcoin::consensus::{Decodable, Encodable};
pub use bitcoin::p2p::message::RawNetworkMessage;
use bitcoin::p2p::{Address, Magic};
Expand Down Expand Up @@ -107,6 +108,12 @@ pub async fn read_v1<T: AsyncRead + Unpin>(input: &mut T) -> Result<RawNetworkMe
Ok(message)
}

pub async fn read_v2<T: AsyncRead + Unpin>(
input: &mut T,
decrypter: &mut PacketReader,
) -> Result<RawNetworkMessage, Error> {
}

/// Write the network message to the output stream.
pub async fn write_v1<T: AsyncWrite + Unpin>(
output: &mut T,
Expand All @@ -117,3 +124,15 @@ pub async fn write_v1<T: AsyncWrite + Unpin>(
.expect("write to vector");
Ok(output.write_all(&write_bytes).await?)
}

/// Write the network message to the output stream.
pub async fn write_v2<T: AsyncWrite + Unpin>(
output: &mut T,
encrypter: &mut PacketWriter,
msg: RawNetworkMessage,
) -> Result<(), Error> {
let mut write_bytes = vec![];
msg.consensus_encode(&mut write_bytes)
.expect("write to vector");
Ok(output.write_all(&write_bytes).await?)
}

0 comments on commit 4b4e999

Please sign in to comment.