refractor into smaller modules
This commit is contained in:
29
src/proxy.rs
Normal file
29
src/proxy.rs
Normal 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(())
|
||||
}
|
||||
Reference in New Issue
Block a user