Restructure HashMap code
This commit is contained in:
parent
2e57ca4258
commit
7ab36f8ac3
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +1,2 @@
|
||||||
/target
|
/target
|
||||||
|
config.toml
|
||||||
|
|
24
src/main.rs
24
src/main.rs
|
@ -6,29 +6,27 @@ use tokio::net::{TcpListener, TcpStream};
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> anyhow::Result<()> {
|
async fn main() -> anyhow::Result<()> {
|
||||||
let filename = std::env::args()
|
let filename = std::env::args()
|
||||||
.next()
|
.nth(1)
|
||||||
.unwrap_or_else(|| "config.toml".to_string());
|
.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 ports: HashMap<SocketAddr, u16> = 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
|
|
||||||
|
|
||||||
|
for (&saddr, &port) in &ports {
|
||||||
tokio::spawn(async move {
|
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?;
|
proxy(stream, to_stream).await?;
|
||||||
|
|
||||||
Ok::<(), anyhow::Error>(())
|
Ok::<(), anyhow::Error>(())
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::empty_loop)]
|
||||||
|
loop {}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn proxy(left: TcpStream, right: TcpStream) -> anyhow::Result<()> {
|
async fn proxy(left: TcpStream, right: TcpStream) -> anyhow::Result<()> {
|
||||||
|
|
Loading…
Reference in a new issue