Open ports on client

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

50
Cargo.lock generated
View File

@ -540,6 +540,21 @@ dependencies = [
"percent-encoding", "percent-encoding",
] ]
[[package]]
name = "futures"
version = "0.3.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f21eda599937fba36daeb58a22e8f5cee2d14c4a17b5b7739c7c8e5e3b8230c"
dependencies = [
"futures-channel",
"futures-core",
"futures-executor",
"futures-io",
"futures-sink",
"futures-task",
"futures-util",
]
[[package]] [[package]]
name = "futures-channel" name = "futures-channel"
version = "0.3.24" version = "0.3.24"
@ -547,6 +562,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30bdd20c28fadd505d0fd6712cdfcb0d4b5648baf45faef7f852afb2399bb050" checksum = "30bdd20c28fadd505d0fd6712cdfcb0d4b5648baf45faef7f852afb2399bb050"
dependencies = [ dependencies = [
"futures-core", "futures-core",
"futures-sink",
] ]
[[package]] [[package]]
@ -555,6 +571,34 @@ version = "0.3.24"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e5aa3de05362c3fb88de6531e6296e85cde7739cccad4b9dfeeb7f6ebce56bf" checksum = "4e5aa3de05362c3fb88de6531e6296e85cde7739cccad4b9dfeeb7f6ebce56bf"
[[package]]
name = "futures-executor"
version = "0.3.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ff63c23854bee61b6e9cd331d523909f238fc7636290b96826e9cfa5faa00ab"
dependencies = [
"futures-core",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-io"
version = "0.3.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbf4d2a7a308fd4578637c0b17c7e1c7ba127b8f6ba00b29f717e9655d85eb68"
[[package]]
name = "futures-macro"
version = "0.3.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42cd15d1c7456c04dbdf7e88bcd69760d74f3a798d6444e16974b505b0e62f17"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "futures-sink" name = "futures-sink"
version = "0.3.24" version = "0.3.24"
@ -573,8 +617,13 @@ version = "0.3.24"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44fb6cb1be61cc1d2e43b262516aafcf63b241cffdb1d3fa115f91d9c7b09c90" checksum = "44fb6cb1be61cc1d2e43b262516aafcf63b241cffdb1d3fa115f91d9c7b09c90"
dependencies = [ dependencies = [
"futures-channel",
"futures-core", "futures-core",
"futures-io",
"futures-macro",
"futures-sink",
"futures-task", "futures-task",
"memchr",
"pin-project-lite", "pin-project-lite",
"pin-utils", "pin-utils",
"slab", "slab",
@ -1341,6 +1390,7 @@ dependencies = [
"base", "base",
"clap", "clap",
"env_logger", "env_logger",
"futures",
"log", "log",
"reqwest", "reqwest",
"tokio", "tokio",

View File

@ -10,3 +10,4 @@ log = "0.4.17"
env_logger = "0.9.0" env_logger = "0.9.0"
reqwest = { version = "0.11", features = ["json"] } 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 std::sync::Arc;
use clap::Parser; use clap::Parser;
use futures::future::join_all;
use base::RemoteConfig; use base::RemoteConfig;
use tcp_relay_client::relay_client::relay_client; 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 { for port in conf {
let listen_address = format!("{}:{}", args.listen_address, port.port); let listen_address = format!("{}:{}", args.listen_address, port.port);
let args_clone = args.clone(); let h = tokio::spawn(relay_client(
let handle = std::thread::spawn(move || { args.token.clone(),
relay_client(&args_clone.token, port.id, port.id,
&args_clone.listen_address, &listen_address); args.listen_address.clone(),
}); listen_address,
handles.push(handle); ));
handles.push(h);
} }
for h in handles { join_all(handles).await;
h.join().unwrap();
}
Ok(()) Ok(())
} }

View File

@ -1,9 +1,11 @@
use std::future::Future; 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); 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, Ok(l) => l,
Err(e) => { Err(e) => {
log::error!("Failed to start to listen on {}!", listen_address); 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);
});
}
} }