From 13fdef80533276fa72b0013e90fa7b7aedffe645 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 25 Feb 2025 11:18:57 +0100 Subject: [PATCH] Cleanup code --- README.md | 2 ++ src/main.rs | 38 ++++++++++++++------------------------ 2 files changed, 16 insertions(+), 24 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..30f395e --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# Proxy saver +Proxify HTTP request to HTTPS upstream, saving requests and responses. \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 8504f5b..ae6b0ce 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,12 +6,10 @@ use std::fs::OpenOptions; use std::io::Write; use std::path::Path; use std::sync::Arc; -use std::time::{Duration, SystemTime, UNIX_EPOCH}; +use std::time::{SystemTime, UNIX_EPOCH}; use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::net::TcpListener; use tokio::net::TcpStream; -use tokio::pin; -use tokio::time::interval; use tokio_rustls::TlsConnector; use tokio_rustls::rustls::{ClientConfig, RootCertStore}; @@ -36,8 +34,6 @@ 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() @@ -78,12 +74,12 @@ async fn main() -> Result<(), Box> { let mut req_file = OpenOptions::new() .create(true) .write(true) - .open(Path::new(&args.storage_path).join(format!("req-{base_file_name}"))) + .open(Path::new(&args.storage_path).join(format!("{base_file_name}-req"))) .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}"))) + .open(Path::new(&args.storage_path).join(format!("{base_file_name}-res"))) .expect("Failed to create req file"); let mut root_cert_store = RootCertStore::empty(); @@ -104,17 +100,12 @@ async fn main() -> Result<(), Box> { let (mut client_read, mut client_write) = client_socket.split(); - let mut interval = interval(HEARTBEAT_INTERVAL); let mut buf_client = [0u8; 1024]; let mut buf_server = [0u8; 1024]; let mut modified_headers = false; loop { - let tick = interval.tick(); - // required for select() - pin!(tick); - tokio::select! { count = client_read.read(&mut buf_client) => { let count = match count{ Ok(count) => count, Err(e) => { @@ -180,16 +171,15 @@ fn manipulate_headers(buff: &[u8], host: &str) -> Vec { } out - - /*// Work line per line - buff.to_vec() - .split(&b'\n') - .map(|l| { - if l.starts_with(b"Host:") { - format!("Host: {host}\r") - } else { - l.to_owned() - } - }) - .collect::>()*/ +} + +#[cfg(test)] +mod test { + use crate::Args; + + #[test] + fn verify_cli() { + use clap::CommandFactory; + Args::command().debug_assert() + } }