Restructure HashMap code
This commit is contained in:
parent
2e57ca4258
commit
7ab36f8ac3
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +1,2 @@
|
|||
/target
|
||||
config.toml
|
||||
|
|
24
src/main.rs
24
src/main.rs
|
@ -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<()> {
|
||||
|
|
Loading…
Reference in a new issue