Merge pull request #168 from uqbar-dao/dr/port-flag-enforce-only

Dr/port flag enforce only
This commit is contained in:
dr-frmr 2024-01-17 18:05:44 -03:00 committed by GitHub
commit 8cfe5819ef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 43 additions and 21 deletions

View File

@ -99,8 +99,8 @@ pub fn deserialize_headers(hashmap: HashMap<String, String>) -> HeaderMap {
header_map header_map
} }
pub async fn find_open_port(start_at: u16) -> Option<u16> { pub async fn find_open_port(start_at: u16, end_at: u16) -> Option<u16> {
for port in start_at..(start_at + 1000) { for port in start_at..end_at {
let bind_addr = format!("0.0.0.0:{}", port); let bind_addr = format!("0.0.0.0:{}", port);
if is_port_available(&bind_addr).await { if is_port_available(&bind_addr).await {
return Some(port); return Some(port);

View File

@ -97,11 +97,7 @@ async fn main() {
.author("Uqbar DAO: https://github.com/uqbar-dao") .author("Uqbar DAO: https://github.com/uqbar-dao")
.about("A General Purpose Sovereign Cloud Computing Platform") .about("A General Purpose Sovereign Cloud Computing Platform")
.arg(arg!([home] "Path to home directory").required(true)) .arg(arg!([home] "Path to home directory").required(true))
.arg( .arg(arg!(--port <PORT> "First port to try binding").value_parser(value_parser!(u16)))
arg!(--port <PORT> "First port to try binding")
.default_value("8080")
.value_parser(value_parser!(u16)),
)
.arg( .arg(
arg!(--testnet "Use Sepolia testnet") arg!(--testnet "Use Sepolia testnet")
.default_value("false") .default_value("false")
@ -129,7 +125,10 @@ async fn main() {
let matches = app.get_matches(); let matches = app.get_matches();
let home_directory_path = matches.get_one::<String>("home").unwrap(); let home_directory_path = matches.get_one::<String>("home").unwrap();
let port = *matches.get_one::<u16>("port").unwrap(); let (port, port_flag_used) = match matches.get_one::<u16>("port") {
Some(port) => (*port, true),
None => (8080, false),
};
let on_testnet = *matches.get_one::<bool>("testnet").unwrap(); let on_testnet = *matches.get_one::<bool>("testnet").unwrap();
let contract_address = if on_testnet { let contract_address = if on_testnet {
register::KNS_SEPOLIA_ADDRESS register::KNS_SEPOLIA_ADDRESS
@ -211,16 +210,35 @@ async fn main() {
} }
}; };
let http_server_port = http::utils::find_open_port(port).await.unwrap(); let http_server_port = if port_flag_used {
if http_server_port != port { match http::utils::find_open_port(port, port + 1).await {
let error_message = format!( Some(port) => port,
"error: couldn't bind {}; first available port found {}. Set an available port with `--port` and try again.", None => {
port, println!(
http_server_port, "error: couldn't bind {}; first available port found was {}. \
); Set an available port with `--port` and try again.",
println!("{error_message}"); port,
panic!("{error_message}"); http::utils::find_open_port(port, port + 1000)
} .await
.expect("no ports found in range"),
);
panic!();
}
}
} else {
match http::utils::find_open_port(port, port + 1000).await {
Some(port) => port,
None => {
println!(
"error: couldn't bind any ports between {port} and {}. \
Set an available port with `--port` and try again.",
port + 1000,
);
panic!();
}
}
};
println!( println!(
"login or register at http://localhost:{}\r", "login or register at http://localhost:{}\r",
http_server_port http_server_port

View File

@ -110,7 +110,11 @@ pub async fn register(
let tx = Arc::new(tx); let tx = Arc::new(tx);
// TODO: if IP is localhost, don't allow registration as direct // TODO: if IP is localhost, don't allow registration as direct
let ws_port = crate::http::utils::find_open_port(9000).await.unwrap(); let ws_port = crate::http::utils::find_open_port(9000, 65535)
.await
.expect(
"Unable to find free port between 9000 and 65535 for a new websocket, are you kidding?",
);
// This is a temporary identity, passed to the UI. If it is confirmed through a /boot or /confirm-change-network-keys, then it will be used to replace the current identity // This is a temporary identity, passed to the UI. If it is confirmed through a /boot or /confirm-change-network-keys, then it will be used to replace the current identity
let our_temp_id = Arc::new(Identity { let our_temp_id = Arc::new(Identity {
@ -355,7 +359,7 @@ async fn handle_import_keyfile(
} }
}; };
let Some(ws_port) = crate::http::utils::find_open_port(9000).await else { let Some(ws_port) = crate::http::utils::find_open_port(9000, 9999).await else {
return Ok(warp::reply::with_status( return Ok(warp::reply::with_status(
warp::reply::json(&"Unable to find free port"), warp::reply::json(&"Unable to find free port"),
StatusCode::INTERNAL_SERVER_ERROR, StatusCode::INTERNAL_SERVER_ERROR,
@ -417,7 +421,7 @@ async fn handle_login(
} }
let encoded_keyfile = encoded_keyfile.unwrap(); let encoded_keyfile = encoded_keyfile.unwrap();
let Some(ws_port) = crate::http::utils::find_open_port(9000).await else { let Some(ws_port) = crate::http::utils::find_open_port(9000, 65535).await else {
return Ok(warp::reply::with_status( return Ok(warp::reply::with_status(
warp::reply::json(&"Unable to find free port"), warp::reply::json(&"Unable to find free port"),
StatusCode::INTERNAL_SERVER_ERROR, StatusCode::INTERNAL_SERVER_ERROR,