Log requests and responses

This commit is contained in:
2025-02-25 10:00:13 +01:00
parent a55a1b556d
commit a315cad346
3 changed files with 148 additions and 8 deletions

View File

@ -1,9 +1,13 @@
use std::error::Error;
use std::fs::{File, OpenOptions};
use std::io::Write;
use std::path::Path;
use clap::Parser;
use tokio::net::TcpListener;
use rustls_pki_types::ServerName;
use std::sync::Arc;
use std::time::Duration;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use rand::distr::{Alphanumeric, SampleString};
use tokio::net::TcpStream;
use tokio_rustls::rustls::{ClientConfig, RootCertStore};
use tokio_rustls::TlsConnector;
@ -32,6 +36,20 @@ struct Args {
storage_path: String,
}
const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5);
/// Get the current time since epoch
pub fn time() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs()
}
pub fn rand_str(len: usize) -> String {
Alphanumeric.sample_string(&mut rand::rng(), len)
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> { env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
@ -40,6 +58,8 @@ async fn main() -> Result<(), Box<dyn Error>> { env_logger::init_from_env(env
log::info!("Will start to listen on {}", args.listen_address);
let listener = TcpListener::bind(&args.listen_address).await?;
std::fs::create_dir_all(Path::new(args.storage_path.as_str())).unwrap();
loop {
// Asynchronously wait for an inbound socket.
@ -48,6 +68,13 @@ async fn main() -> Result<(), Box<dyn Error>> { env_logger::init_from_env(env
tokio::spawn(async move {
let args = Args::parse();
let base_file_name = format!("{}-{}-{}", client_socket.peer_addr().unwrap().ip(), time(), rand_str(10));
let mut req_file = OpenOptions::new().create(true).write(true).open(Path::new(&args.storage_path).join(format!("req-{base_file_name}")))
.expect("Failed to create req file");
let mut res_file = OpenOptions::new().create(true).write(true).open(Path::new(&args.storage_path).join(format!("res-{base_file_name}")))
.expect("Failed to create req file");
let mut root_cert_store = RootCertStore::empty();
root_cert_store.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
let config = ClientConfig::builder()
@ -61,9 +88,6 @@ async fn main() -> Result<(), Box<dyn Error>> { env_logger::init_from_env(env
let (mut client_read, mut client_write) = client_socket.split();
const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5);
let mut interval = interval(HEARTBEAT_INTERVAL);
let mut buf_client = [0u8; 1024];
let mut buf_server = [0u8; 1024];
@ -78,12 +102,15 @@ async fn main() -> Result<(), Box<dyn Error>> { env_logger::init_from_env(env
let count = count.expect("Failed to read from client socket");
log::info!("Got a new client read {count}");
upstream.write_all(&buf_client[..count]).await.expect("Failed to write to upstream");
req_file.write_all(&buf_client[..count]).expect("Failed to write to req");
}
count = upstream.read(&mut buf_server) => {
let count = count.expect("Failed to read from server socket");
log::info!("Got a new upstream read {count}");
client_write.write_all(&buf_server[..count]).await.expect("Failed to write to client");
res_file.write_all(&buf_server[..count]).expect("Failed to write to res");
}
}
}