This commit is contained in:
41
src/main.rs
Normal file
41
src/main.rs
Normal file
@@ -0,0 +1,41 @@
|
||||
use std::{env, io};
|
||||
use tokio::{
|
||||
io::copy_bidirectional,
|
||||
net::{TcpListener, TcpStream},
|
||||
};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
let listen_addr = env::var("LISTEN_ADDR").expect("LISTEN_ADDR not set");
|
||||
let target_addr = env::var("UPSTREAM_ADDR").expect("UPSTREAM_ADDR not set");
|
||||
|
||||
println!("Listening on {listen_addr}");
|
||||
println!("Forwarding to {target_addr}");
|
||||
|
||||
let listener = TcpListener::bind(listen_addr).await?;
|
||||
|
||||
loop {
|
||||
match listener.accept().await {
|
||||
Ok((inbound, client_addr)) => {
|
||||
println!("Accepted connection from {client_addr}");
|
||||
|
||||
let target = target_addr.clone();
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = handle(inbound, &target).await {
|
||||
eprintln!("Error handling connection: {e}");
|
||||
}
|
||||
println!("Connection from {client_addr} closed");
|
||||
});
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Failed to accept connection: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle(mut inbound: TcpStream, target_addr: &str) -> io::Result<()> {
|
||||
let mut outbound = TcpStream::connect(target_addr).await?;
|
||||
copy_bidirectional(&mut inbound, &mut outbound).await?;
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user