implement a simple TCP proxy
This commit is contained in:
28
src/main.rs
28
src/main.rs
@@ -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(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user