Restructure HashMap code

This commit is contained in:
Yash Karandikar 2022-06-24 13:13:29 +05:30
parent 2e57ca4258
commit 7ab36f8ac3
2 changed files with 12 additions and 13 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
/target
config.toml

View file

@ -6,29 +6,27 @@ use tokio::net::{TcpListener, TcpStream};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let filename = std::env::args()
.next()
.nth(1)
.unwrap_or_else(|| "config.toml".to_string());
let contents = std::fs::read_to_string(filename)?;
let contents = std::fs::read_to_string(filename).unwrap();
let mut ports: HashMap<u16, SocketAddr> = toml::from_str(&contents)?;
let listener = TcpListener::bind("0.0.0.0:8000").await?;
loop {
let (stream, addr) = listener.accept().await?;
let to_addr = match ports.get(&addr.port()) {
Some(v) => *v,
None => continue,
};
let to_stream = TcpStream::connect(to_addr).await?; // TODO: remove this placeholder
let ports: HashMap<SocketAddr, u16> = toml::from_str(&contents)?;
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?;
proxy(stream, to_stream).await?;
Ok::<(), anyhow::Error>(())
});
}
#[allow(clippy::empty_loop)]
loop {}
}
async fn proxy(left: TcpStream, right: TcpStream) -> anyhow::Result<()> {