32 lines
919 B
Rust
32 lines
919 B
Rust
use tokio::io::copy;
|
|
use tokio::net::{TcpListener, TcpStream};
|
|
|
|
#[tokio::main]
|
|
async fn main() -> anyhow::Result<()> {
|
|
let listener = TcpListener::bind("0.0.0.0:8000").await?;
|
|
|
|
loop {
|
|
let (stream, _addr) = listener.accept().await?;
|
|
// TODO: use a lookup to see where it should be proxied to
|
|
let to_stream = TcpStream::connect("192.168.1.28:6667").await?; // TODO: remove this placeholder
|
|
|
|
tokio::spawn(async move {
|
|
proxy(stream, to_stream).await?;
|
|
|
|
Ok::<(), anyhow::Error>(())
|
|
});
|
|
}
|
|
}
|
|
|
|
async fn proxy(left: TcpStream, right: TcpStream) -> anyhow::Result<()> {
|
|
let (mut left_read, mut left_write) = left.into_split();
|
|
let (mut right_read, mut right_write) = right.into_split();
|
|
|
|
tokio::select! {
|
|
r = copy(&mut left_read, &mut right_write) => r?,
|
|
r = copy(&mut right_read, &mut left_write) => r?
|
|
};
|
|
|
|
Ok(())
|
|
}
|