Cleanup code

This commit is contained in:
Pierre HUBERT 2025-02-25 11:18:57 +01:00
parent 715aed3a3c
commit 13fdef8053
2 changed files with 16 additions and 24 deletions

2
README.md Normal file
View File

@ -0,0 +1,2 @@
# Proxy saver
Proxify HTTP request to HTTPS upstream, saving requests and responses.

View File

@ -6,12 +6,10 @@ use std::fs::OpenOptions;
use std::io::Write; use std::io::Write;
use std::path::Path; use std::path::Path;
use std::sync::Arc; use std::sync::Arc;
use std::time::{Duration, SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpListener; use tokio::net::TcpListener;
use tokio::net::TcpStream; use tokio::net::TcpStream;
use tokio::pin;
use tokio::time::interval;
use tokio_rustls::TlsConnector; use tokio_rustls::TlsConnector;
use tokio_rustls::rustls::{ClientConfig, RootCertStore}; use tokio_rustls::rustls::{ClientConfig, RootCertStore};
@ -36,8 +34,6 @@ struct Args {
storage_path: String, storage_path: String,
} }
const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5);
/// Get the current time since epoch /// Get the current time since epoch
pub fn time() -> u64 { pub fn time() -> u64 {
SystemTime::now() SystemTime::now()
@ -78,12 +74,12 @@ async fn main() -> Result<(), Box<dyn Error>> {
let mut req_file = OpenOptions::new() let mut req_file = OpenOptions::new()
.create(true) .create(true)
.write(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"); .expect("Failed to create req file");
let mut res_file = OpenOptions::new() let mut res_file = OpenOptions::new()
.create(true) .create(true)
.write(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"); .expect("Failed to create req file");
let mut root_cert_store = RootCertStore::empty(); let mut root_cert_store = RootCertStore::empty();
@ -104,17 +100,12 @@ async fn main() -> Result<(), Box<dyn Error>> {
let (mut client_read, mut client_write) = client_socket.split(); 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_client = [0u8; 1024];
let mut buf_server = [0u8; 1024]; let mut buf_server = [0u8; 1024];
let mut modified_headers = false; let mut modified_headers = false;
loop { loop {
let tick = interval.tick();
// required for select()
pin!(tick);
tokio::select! { tokio::select! {
count = client_read.read(&mut buf_client) => { count = client_read.read(&mut buf_client) => {
let count = match count{ Ok(count) => count, Err(e) => { let count = match count{ Ok(count) => count, Err(e) => {
@ -180,16 +171,15 @@ fn manipulate_headers(buff: &[u8], host: &str) -> Vec<u8> {
} }
out out
}
/*// Work line per line
buff.to_vec() #[cfg(test)]
.split(&b'\n') mod test {
.map(|l| { use crate::Args;
if l.starts_with(b"Host:") {
format!("Host: {host}\r") #[test]
} else { fn verify_cli() {
l.to_owned() use clap::CommandFactory;
} Args::command().debug_assert()
}) }
.collect::<Vec<u8>>()*/
} }