Compare commits

...

4 Commits

Author SHA1 Message Date
ae754241ce Handle restricted ports 2024-08-22 09:42:10 +02:00
b52fbca4fd Fix listen host 2024-08-22 08:59:31 +02:00
7dccdf58fa Fix clap configuration 2024-08-21 17:18:49 +02:00
661e6fdaa6 Make ports configurable 2024-08-21 17:17:44 +02:00
5 changed files with 183 additions and 3 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

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

1
src/lib.rs Normal file
View File

@ -0,0 +1 @@
pub mod restricted_port;

View File

@ -1,5 +1,7 @@
use actix_web::middleware::Logger;
use actix_web::{web, App, HttpResponse, HttpServer};
use clap::Parser;
use hidden_server::restricted_port::is_restricted_port;
use log::LevelFilter;
use rand::Rng;
@ -10,6 +12,21 @@ 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('m'), long, default_value_t = 80)]
min_port: u16,
/// Maximal port this server will listen to
#[arg(short('M'), long, default_value_t = 1000)]
max_port: u16,
/// Host this server will listen to
#[arg(short, long, default_value = "0.0.0.0")]
listen_host: String,
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
env_logger::Builder::new()
@ -17,17 +34,36 @@ 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 mut port: u16;
loop {
port = args.min_port + rng.random::<u16>() % (args.max_port - args.min_port);
if !is_restricted_port(port) {
break;
}
log::info!("I chose a restricted port, I have to choose another one...");
}
log::info!("Can now start server...");
HttpServer::new(|| {
App::new()
.wrap(Logger::default())
.route("/", web::get().to(home))
})
.bind(("127.0.0.1", port))?
.bind((args.listen_host, port))?
.run()
.await
}

89
src/restricted_port.rs Normal file
View File

@ -0,0 +1,89 @@
// Source : https://chromium.googlesource.com/chromium/src.git/+/refs/heads/master/net/base/port_util.cc
const RESTRICTED_PORTS: [u16; 80] = [
1, // tcpmux
7, // echo
9, // discard
11, // systat
13, // daytime
15, // netstat
17, // qotd
19, // chargen
20, // ftp data
21, // ftp access
22, // ssh
23, // telnet
25, // smtp
37, // time
42, // name
43, // nicname
53, // domain
69, // tftp
77, // priv-rjs
79, // finger
87, // ttylink
95, // supdup
101, // hostriame
102, // iso-tsap
103, // gppitnp
104, // acr-nema
109, // pop2
110, // pop3
111, // sunrpc
113, // auth
115, // sftp
117, // uucp-path
119, // nntp
123, // NTP
135, // loc-srv /epmap
137, // netbios
139, // netbios
143, // imap2
161, // snmp
179, // BGP
389, // ldap
427, // SLP (Also used by Apple Filing Protocol)
465, // smtp+ssl
512, // print / exec
513, // login
514, // shell
515, // printer
526, // tempo
530, // courier
531, // chat
532, // netnews
540, // uucp
548, // AFP (Apple Filing Protocol)
554, // rtsp
556, // remotefs
563, // nntp+ssl
587, // smtp (rfc6409)
601, // syslog-conn (rfc3195)
636, // ldap+ssl
989, // ftps-data
990, // ftps
993, // ldap+ssl
995, // pop3+ssl
1719, // h323gatestat
1720, // h323hostcall
1723, // pptp
2049, // nfs
3659, // apple-sasl / PasswordServer
4045, // lockd
5060, // sip
5061, // sips
6000, // X11
6566, // sane-port
6665, // Alternate IRC [Apple addition]
6666, // Alternate IRC [Apple addition]
6667, // Standard IRC [Apple addition]
6668, // Alternate IRC [Apple addition]
6669, // Alternate IRC [Apple addition]
6697, // IRC + TLS
10080, // Amanda
];
/// Check out wether a port is a restricted port for major browsers
pub fn is_restricted_port(port: u16) -> bool {
RESTRICTED_PORTS.contains(&port)
}