Open ports on client

This commit is contained in:
2022-08-30 11:16:22 +02:00
parent 09b5403390
commit c67d53a4df
4 changed files with 76 additions and 13 deletions

View File

@ -9,4 +9,5 @@ clap = { version = "3.2.18", features = ["derive", "env"] }
log = "0.4.17"
env_logger = "0.9.0"
reqwest = { version = "0.11", features = ["json"] }
tokio = { version = "1", features = ["full"] }
tokio = { version = "1", features = ["full"] }
futures = "0.3.24"

View File

@ -1,6 +1,7 @@
use std::sync::Arc;
use clap::Parser;
use futures::future::join_all;
use base::RemoteConfig;
use tcp_relay_client::relay_client::relay_client;
@ -48,17 +49,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
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);
let h = tokio::spawn(relay_client(
args.token.clone(),
port.id,
args.listen_address.clone(),
listen_address,
));
handles.push(h);
}
for h in handles {
h.join().unwrap();
}
join_all(handles).await;
Ok(())
}

View File

@ -1,9 +1,11 @@
use std::future::Future;
use std::net::TcpListener;
use std::time::Duration;
pub fn relay_client(token: &str, port_id: usize, server: &str, listen_address: &str) {
use tokio::net::TcpListener;
pub async fn relay_client(token: String, port_id: usize, server: String, listen_address: String) {
log::info!("({}) Start to listen on {}", port_id, listen_address);
let listener = match TcpListener::bind(&listen_address) {
let listener = match TcpListener::bind(&listen_address).await {
Ok(l) => l,
Err(e) => {
log::error!("Failed to start to listen on {}!", listen_address);
@ -11,4 +13,14 @@ pub fn relay_client(token: &str, port_id: usize, server: &str, listen_address: &
}
};
loop {
let (mut socket, _) = listener.accept().await
.expect("Failed to accept new connection!");
let token = token.clone();
let listen_address = listen_address.clone();
tokio::spawn(async move {
log::info!("New connection to {}", &listen_address);
});
}
}