implement a simple TCP proxy

This commit is contained in:
2026-02-02 11:06:56 +01:00
parent a4d7109a27
commit 4c9593283e
3 changed files with 312 additions and 2 deletions

View File

@@ -1,3 +1,27 @@
fn main() {
println!("Hello, world!");
use std::error::Error;
use tokio::{io::copy_bidirectional, main};
use tokio::net::{TcpListener, TcpStream};
#[main]
async fn main() -> Result<(), Box<dyn Error>> {
let listen_addr = "0.0.0.0:8080";
let server_addr = "127.0.0.1:25565";
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 mut outbound = TcpStream::connect(server_addr).await?;
tokio::spawn(async move {
if let Err(e) = copy_bidirectional(&mut inbound, &mut outbound).await {
println!("Failed to transfer; error={e}");
}
});
}
Ok(())
}