Make ports configurable

This commit is contained in:
Pierre HUBERT 2024-08-21 17:17:44 +02:00
parent fa4a5458c2
commit 661e6fdaa6
3 changed files with 76 additions and 2 deletions

53
Cargo.lock generated
View File

@ -393,6 +393,46 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "4.5.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed6719fffa43d0d87e5fd8caeab59be1554fb028cd30edc88fc4369b17971019"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.5.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "216aec2b177652e3846684cbfe25c9964d18ec45234f0f5da5157b207ed1aab6"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.5.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "501d359d5f3dcaf6ecdeee48833ae73ec6e42723a1e52419c79abf9507eec0a0"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97"
[[package]]
name = "colorchoice"
version = "1.0.2"
@ -621,6 +661,12 @@ version = "0.14.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "hermit-abi"
version = "0.3.9"
@ -632,6 +678,7 @@ name = "hidden_server"
version = "0.1.0"
dependencies = [
"actix-web",
"clap",
"env_logger",
"log",
"rand 0.9.0-alpha.2",
@ -1141,6 +1188,12 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "strsim"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "syn"
version = "2.0.75"

View File

@ -7,4 +7,5 @@ edition = "2021"
log = "0.4.22"
env_logger = "0.11.5"
actix-web = "4"
rand = "0.9.0-alpha.2"
rand = "0.9.0-alpha.2"
clap = { version = "4.5.16", features = ["derive"] }

View File

@ -1,5 +1,6 @@
use actix_web::middleware::Logger;
use actix_web::{web, App, HttpResponse, HttpServer};
use clap::Parser;
use log::LevelFilter;
use rand::Rng;
@ -10,6 +11,18 @@ async fn home() -> HttpResponse {
.body(include_str!("../assets/home.html"))
}
/// Server with hidden exposed port
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
/// Minimal port this server will listen to
#[arg(short, long, default_value_t = 80)]
min_port: u16,
/// Maximal port this server will listen to
#[arg(short, long, default_value_t = 1000)]
max_port: u16,
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
env_logger::Builder::new()
@ -17,10 +30,17 @@ async fn main() -> std::io::Result<()> {
.filter(None, LevelFilter::Info)
.init();
let args: Args = Args::parse();
assert!(
args.max_port > args.min_port,
"Max port shall be greater than min port!"
);
log::info!("Choosing a random port to start...");
let mut rng = rand::thread_rng();
let port: u16 = 80 + rng.random::<u16>() % 10000;
let port: u16 = args.min_port + rng.random::<u16>() % (args.max_port - args.min_port);
HttpServer::new(|| {
App::new()