From 09b5403390c2649c57f05dc1b8454ef5fa5269dc Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Tue, 30 Aug 2022 11:01:24 +0200 Subject: [PATCH] wip --- tcp_relay_client/src/lib.rs | 1 + tcp_relay_client/src/main.rs | 19 ++++++++++++++++++- tcp_relay_client/src/relay_client.rs | 14 ++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 tcp_relay_client/src/relay_client.rs diff --git a/tcp_relay_client/src/lib.rs b/tcp_relay_client/src/lib.rs index e69de29..dfc2cc9 100644 --- a/tcp_relay_client/src/lib.rs +++ b/tcp_relay_client/src/lib.rs @@ -0,0 +1 @@ +pub mod relay_client; \ No newline at end of file diff --git a/tcp_relay_client/src/main.rs b/tcp_relay_client/src/main.rs index 97357c6..e963693 100644 --- a/tcp_relay_client/src/main.rs +++ b/tcp_relay_client/src/main.rs @@ -3,6 +3,7 @@ use std::sync::Arc; use clap::Parser; use base::RemoteConfig; +use tcp_relay_client::relay_client::relay_client; /// TCP relay client #[derive(Parser, Debug, Clone)] @@ -41,7 +42,23 @@ async fn main() -> Result<(), Box> { } let conf = req.json::() .await?; - println!("{:#?}", conf); + + // Start to listen port + let mut handles = vec![]; + for port in conf { + let listen_address = format!("{}:{}", args.listen_address, port.port); + + let args_clone = args.clone(); + let handle = std::thread::spawn(move || { + relay_client(&args_clone.token, port.id, + &args_clone.listen_address, &listen_address); + }); + handles.push(handle); + } + + for h in handles { + h.join().unwrap(); + } Ok(()) } \ No newline at end of file diff --git a/tcp_relay_client/src/relay_client.rs b/tcp_relay_client/src/relay_client.rs new file mode 100644 index 0000000..94581c9 --- /dev/null +++ b/tcp_relay_client/src/relay_client.rs @@ -0,0 +1,14 @@ +use std::future::Future; +use std::net::TcpListener; + +pub fn relay_client(token: &str, port_id: usize, server: &str, listen_address: &str) { + log::info!("({}) Start to listen on {}", port_id, listen_address); + let listener = match TcpListener::bind(&listen_address) { + Ok(l) => l, + Err(e) => { + log::error!("Failed to start to listen on {}!", listen_address); + std::process::exit(3); + } + }; + +} \ No newline at end of file