Log requests and responses

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

118
Cargo.lock generated
View File

@ -328,7 +328,19 @@ checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
dependencies = [
"cfg-if 1.0.0",
"libc",
"wasi",
"wasi 0.11.0+wasi-snapshot-preview1",
]
[[package]]
name = "getrandom"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43a49c392881ce6d5c3b8cb70f98717b7c07aabbdff06687b9030dbfbe2725f8"
dependencies = [
"cfg-if 1.0.0",
"libc",
"wasi 0.13.3+wasi-0.2.2",
"windows-targets",
]
[[package]]
@ -482,7 +494,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd"
dependencies = [
"libc",
"wasi",
"wasi 0.11.0+wasi-snapshot-preview1",
"windows-sys 0.52.0",
]
@ -600,6 +612,15 @@ version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b"
[[package]]
name = "ppv-lite86"
version = "0.2.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04"
dependencies = [
"zerocopy 0.7.35",
]
[[package]]
name = "proc-macro2"
version = "1.0.93"
@ -618,6 +639,7 @@ dependencies = [
"future-utils",
"futures",
"log 0.4.26",
"rand",
"rustls-pki-types",
"tokio 1.43.0",
"tokio-rustls",
@ -633,6 +655,37 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3779b94aeb87e8bd4e834cee3650289ee9e0d5677f976ecdb6d219e5f4f6cd94"
dependencies = [
"rand_chacha",
"rand_core",
"zerocopy 0.8.20",
]
[[package]]
name = "rand_chacha"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a509b1a2ffbe92afab0e55c8fd99dea1c280e8171bd2d88682bb20bc41cbc2c"
dependencies = [
"getrandom 0.3.1",
"zerocopy 0.8.20",
]
[[package]]
name = "redox_syscall"
version = "0.1.57"
@ -685,7 +738,7 @@ checksum = "da5349ae27d3887ca812fb375b45a4fbb36d8d12d2df394968cd86e35683fe73"
dependencies = [
"cc",
"cfg-if 1.0.0",
"getrandom",
"getrandom 0.2.15",
"libc",
"untrusted",
"windows-sys 0.52.0",
@ -1087,6 +1140,15 @@ version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "wasi"
version = "0.13.3+wasi-0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26816d2e1a4a36a2940b96c5296ce403917633dff8f3440e9b236ed6f6bacad2"
dependencies = [
"wit-bindgen-rt",
]
[[package]]
name = "webpki-roots"
version = "0.26.8"
@ -1212,6 +1274,15 @@ version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "wit-bindgen-rt"
version = "0.33.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3268f3d866458b787f390cf61f4bbb563b922d091359f9608842999eaee3943c"
dependencies = [
"bitflags 2.8.0",
]
[[package]]
name = "ws2_32-sys"
version = "0.2.1"
@ -1222,6 +1293,47 @@ dependencies = [
"winapi-build",
]
[[package]]
name = "zerocopy"
version = "0.7.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0"
dependencies = [
"byteorder",
"zerocopy-derive 0.7.35",
]
[[package]]
name = "zerocopy"
version = "0.8.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dde3bb8c68a8f3f1ed4ac9221aad6b10cece3e60a8e2ea54a6a2dec806d0084c"
dependencies = [
"zerocopy-derive 0.8.20",
]
[[package]]
name = "zerocopy-derive"
version = "0.7.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "zerocopy-derive"
version = "0.8.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eea57037071898bf96a6da35fd626f4f27e9cee3ead2a6c703cf09d472b2e700"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "zeroize"
version = "1.8.1"

View File

@ -12,4 +12,5 @@ tokio-rustls = { version = "0.26.1", features = ["ring"], default-features = fal
rustls-pki-types = "1.11.0"
webpki-roots = "0.26.8"
future-utils = "0.12.1"
futures = "0.1.31"
futures = "0.1.31"
rand = "0.9.0"

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");
}
}
}