refractor into smaller modules

This commit is contained in:
2026-02-02 15:52:17 +01:00
parent 67220e8d11
commit 84b56a163a
6 changed files with 210 additions and 132 deletions

29
src/proxy.rs Normal file
View File

@@ -0,0 +1,29 @@
use std::error::Error;
use tokio::io::copy_bidirectional;
use tokio::net::{TcpListener, TcpStream};
pub async fn run_proxy(listen_addr: String, server_addr: String) -> Result<(), Box<dyn Error>> {
println!("Listening on {}", listen_addr);
println!("Proxying to {}", server_addr);
let listener = TcpListener::bind(&listen_addr).await?;
while let Ok((mut inbound, _)) = listener.accept().await {
let server_addr = server_addr.clone();
tokio::spawn(async move {
match TcpStream::connect(&server_addr).await {
Ok(mut outbound) => {
if let Err(e) = copy_bidirectional(&mut inbound, &mut outbound).await {
println!("Failed to transfer; error={e}");
}
}
Err(e) => {
println!("Failed to connect to server; error={e}");
}
}
});
}
Ok(())
}