From 74a359061c5b226c458ce97ae23674b248d8a17c Mon Sep 17 00:00:00 2001 From: Yash Karandikar Date: Fri, 24 Jun 2022 18:58:12 +0530 Subject: [PATCH] Don't drop the TcpListener after closing the proxy --- src/main.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3304a54..ab0701f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,11 +16,18 @@ async fn main() -> anyhow::Result<()> { for (&saddr, &port) in &ports { tokio::spawn(async move { let listener = TcpListener::bind(SocketAddr::new("0.0.0.0".parse()?, port)).await?; - let (stream, _) = listener.accept().await?; - let to_stream = TcpStream::connect(saddr).await?; + loop { + let (stream, _) = listener.accept().await?; + let to_stream = TcpStream::connect(saddr).await?; - proxy(stream, to_stream).await?; + tokio::spawn(async move { + proxy(stream, to_stream).await?; + Ok::<(), anyhow::Error>(()) + }); + } + // We still need this statement in order to specify the return type + #[allow(unreachable_code)] Ok::<(), anyhow::Error>(()) }); }