Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logger to proxy app to clean up the noise #80

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 101 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ tokio-util = { version = "0.7.12", features = ["compat"] }
hex = { package = "hex-conservative", version = "0.2.0" }
bip324 = { path = "../protocol", features = ["async"] }
configure_me = "0.4.0"
log = "0.4"
env_logger = "0.10"

[build-dependencies]
configure_me_codegen = { version = "0.4.8", default-features = false }
19 changes: 11 additions & 8 deletions proxy/src/bin/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use bip324::{
};
use bip324_proxy::{V1ProtocolReader, V1ProtocolWriter};
use bitcoin::Network;
use log::{debug, error, info};
use tokio::{
net::{TcpListener, TcpStream},
select,
Expand All @@ -22,12 +23,12 @@ async fn proxy_conn(client: TcpStream, network: Network) -> Result<(), bip324_pr
.await
.expect("peek address");

println!("Reaching out to {}.", remote_ip);
info!("Reaching out to {}.", remote_ip);
let remote = TcpStream::connect(remote_ip)
.await
.expect("connect to remote");

println!("Initiating handshake.");
info!("Initiating handshake.");
let (remote_reader, remote_writer) = remote.into_split();
// Convert to futures-compatible types.
let remote_reader = remote_reader.compat();
Expand All @@ -43,13 +44,13 @@ async fn proxy_conn(client: TcpStream, network: Network) -> Result<(), bip324_pr

let (mut remote_reader, mut remote_writer) = protocol.into_split();

println!("Setting up proxy.");
info!("Setting up proxy.");

loop {
select! {
result = v1_client_reader.read() => {
let msg = result?;
println!(
debug!(
"Read {} message from client, writing to remote.",
msg.command()
);
Expand All @@ -66,7 +67,7 @@ async fn proxy_conn(client: TcpStream, network: Network) -> Result<(), bip324_pr
if payload.packet_type() == PacketType::Genuine {
let msg = deserialize(payload.contents())
.expect("deserializable contents into network message");
println!(
debug!(
"Read {} message from remote, writing to client.",
msg.command()
);
Expand All @@ -79,13 +80,15 @@ async fn proxy_conn(client: TcpStream, network: Network) -> Result<(), bip324_pr

#[tokio::main]
async fn main() {
env_logger::init();

let (config, _) = Config::including_optional_config_files::<&[&str]>(&[]).unwrap_or_exit();
let network = Network::from_str(&config.network).expect("parse-able network");

let proxy = TcpListener::bind((&*config.bind_host, config.bind_port))
.await
.expect("Failed to bind to proxy port.");
println!(
info!(
"Listening for connections on {}:{}",
config.bind_host, config.bind_port,
);
Expand All @@ -98,10 +101,10 @@ async fn main() {
tokio::spawn(async move {
match proxy_conn(stream, network).await {
Ok(_) => {
println!("Proxy establilshed.");
info!("Proxy establilshed.");
}
Err(e) => {
println!("Connection ended with error: {e}.");
error!("Connection ended with error: {e}.");
}
};
});
Expand Down
17 changes: 10 additions & 7 deletions proxy/src/bin/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::str::FromStr;

use bip324_proxy::{V1ProtocolReader, V1ProtocolWriter};
use bitcoin::Network;
use log::{debug, error, info};
use tokio::{
net::{TcpListener, TcpStream},
select,
Expand All @@ -16,7 +17,7 @@ configure_me::include_config!();
async fn proxy_conn(client: TcpStream, network: Network) -> Result<(), bip324_proxy::Error> {
let remote_ip = bip324_proxy::peek_addr(&client, network).await?;

println!("Initialing remote connection {}.", remote_ip);
info!("Initialing remote connection {}.", remote_ip);
let remote = TcpStream::connect(remote_ip).await?;

let (client_reader, client_writer) = client.into_split();
Expand All @@ -27,21 +28,21 @@ async fn proxy_conn(client: TcpStream, network: Network) -> Result<(), bip324_pr
let mut v1_remote_reader = V1ProtocolReader::new(remote_reader);
let mut v1_remote_writer = V1ProtocolWriter::new(network, remote_writer);

println!("Setting up proxy loop.");
info!("Setting up proxy loop.");

loop {
select! {
result = v1_client_reader.read() => {
let msg = result?;
println!(
debug!(
"Read {} message from client, writing to remote.",
msg.command()
);
v1_remote_writer.write(msg).await?;
},
result = v1_remote_reader.read() => {
let msg = result?;
println!(
debug!(
"Read {} message from remote, writing to client.",
msg.command()
);
Expand All @@ -53,13 +54,15 @@ async fn proxy_conn(client: TcpStream, network: Network) -> Result<(), bip324_pr

#[tokio::main]
async fn main() {
env_logger::init();

let (config, _) = Config::including_optional_config_files::<&[&str]>(&[]).unwrap_or_exit();
let network = Network::from_str(&config.network).expect("parse-able network");

let proxy = TcpListener::bind((&*config.bind_host, config.bind_port))
.await
.expect("Failed to bind to proxy port.");
println!(
info!(
"Listening for connections on {}:{}",
config.bind_host, config.bind_port,
);
Expand All @@ -72,10 +75,10 @@ async fn main() {
tokio::spawn(async move {
match proxy_conn(stream, network).await {
Ok(_) => {
println!("Proxy establilshed.");
info!("Proxy establilshed.");
}
Err(e) => {
println!("Ended connection with error: {e}.");
error!("Ended connection with error: {e}.");
}
};
});
Expand Down
6 changes: 3 additions & 3 deletions proxy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use bitcoin::p2p::message::{NetworkMessage, RawNetworkMessage};
use bitcoin::p2p::Address;
use bitcoin::Network;
use hex::prelude::*;
use log::debug;
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
use tokio::net::TcpStream;

Expand Down Expand Up @@ -65,19 +66,18 @@ impl From<std::io::Error> for Error {

/// Peek the input stream and pluck the remote address based on the version message.
pub async fn peek_addr(client: &TcpStream, network: Network) -> Result<SocketAddr, Error> {
println!("Validating client connection.");
// Peek the first 70 bytes, 24 for the header and 46 for the first part of the version message.
let mut peek_bytes = [0; 70];
client.peek(&mut peek_bytes).await?;

// Check network magic.
println!("Got magic: {}", &peek_bytes[0..4].to_lower_hex_string());
debug!("Got magic: {}", &peek_bytes[0..4].to_lower_hex_string());
if network.magic().to_bytes().ne(&peek_bytes[0..4]) {
return Err(Error::WrongNetwork);
}

// Check command.
println!("Got command: {}", &peek_bytes[4..16].to_lower_hex_string());
debug!("Got command: {}", &peek_bytes[4..16].to_lower_hex_string());
if VERSION_COMMAND.ne(&peek_bytes[4..16]) {
return Err(Error::WrongCommand);
}
Expand Down
Loading