diff --git a/kinode/src/main.rs b/kinode/src/main.rs index 2c8c1435..7b2e8360 100644 --- a/kinode/src/main.rs +++ b/kinode/src/main.rs @@ -499,19 +499,24 @@ async fn set_http_server_port(set_port: Option<&u16>) -> u16 { /// If a specific port is provided, it attempts to bind to it directly. /// If no port is provided, it searches for the first available port between 9000 and 65535. /// Returns a tuple containing the TcpListener and a boolean indicating if a specific port was used. -async fn setup_networking(networking_port: Option<&u16>) -> (tokio::net::TcpListener, bool) { +async fn setup_networking( + networking_port: Option<&u16>, +) -> (Option, bool) { + if let Some(0) = networking_port { + return (None, true); + } match networking_port { Some(port) => { let listener = http::utils::find_open_port(*port, port + 1) .await .expect("port selected with flag could not be bound"); - (listener, true) + (Some(listener), true) } None => { let listener = http::utils::find_open_port(9000, 65535) .await .expect("no ports found in range 9000-65535 for kinode networking"); - (listener, false) + (Some(listener), false) } } } @@ -709,8 +714,8 @@ async fn find_public_ip() -> std::net::Ipv4Addr { async fn serve_register_fe( home_directory_path: &str, our_ip: String, - ws_networking: (tokio::net::TcpListener, bool), - tcp_networking: (tokio::net::TcpListener, bool), + ws_networking: (Option, bool), + tcp_networking: (Option, bool), http_server_port: u16, maybe_rpc: Option, ) -> (Identity, Vec, Keyfile) { @@ -754,8 +759,8 @@ async fn serve_register_fe( async fn login_with_password( home_directory_path: &str, our_ip: String, - ws_networking: (tokio::net::TcpListener, bool), - tcp_networking: (tokio::net::TcpListener, bool), + ws_networking: (Option, bool), + tcp_networking: (Option, bool), maybe_rpc: Option, password: &str, ) -> (Identity, Vec, Keyfile) { @@ -795,14 +800,14 @@ async fn login_with_password( &mut our, kns_address, provider, - ( - ws_networking.0.local_addr().unwrap().port(), - ws_networking.1, - ), - ( - tcp_networking.0.local_addr().unwrap().port(), - tcp_networking.1, - ), + match ws_networking.0 { + Some(listener) => (listener.local_addr().unwrap().port(), ws_networking.1), + None => (0, ws_networking.1), + }, + match tcp_networking.0 { + Some(listener) => (listener.local_addr().unwrap().port(), tcp_networking.1), + None => (0, tcp_networking.1), + }, ) .await .expect("information used to boot does not match information onchain"); diff --git a/kinode/src/register.rs b/kinode/src/register.rs index 9ae8ba6d..a1e96431 100644 --- a/kinode/src/register.rs +++ b/kinode/src/register.rs @@ -43,8 +43,8 @@ pub async fn register( tx: RegistrationSender, kill_rx: oneshot::Receiver, ip: String, - ws_networking: (tokio::net::TcpListener, bool), - tcp_networking: (tokio::net::TcpListener, bool), + ws_networking: (Option, bool), + tcp_networking: (Option, bool), http_port: u16, keyfile: Option>, maybe_rpc: Option, @@ -54,11 +54,25 @@ pub async fn register( let net_keypair = Arc::new(serialized_networking_keypair.as_ref().to_vec()); let tx = Arc::new(tx); - let ws_port = ws_networking.0.local_addr().unwrap().port(); + let ws_port = match ws_networking.0 { + Some(listener) => listener.local_addr().unwrap().port(), + None => 0, + }; let ws_flag_used = ws_networking.1; - let tcp_port = tcp_networking.0.local_addr().unwrap().port(); + let tcp_port = match tcp_networking.0 { + Some(listener) => listener.local_addr().unwrap().port(), + None => 0, + }; let tcp_flag_used = tcp_networking.1; + let mut ports_map = std::collections::BTreeMap::new(); + if ws_port != 0 { + ports_map.insert("ws".to_string(), ws_port); + } + if tcp_port != 0 { + ports_map.insert("tcp".to_string(), tcp_port); + } + // 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. @@ -67,10 +81,7 @@ pub async fn register( name: "".to_string(), routing: NodeRouting::Both { ip: ip.clone(), - ports: std::collections::BTreeMap::from([ - ("ws".to_string(), ws_port), - ("tcp".to_string(), tcp_port), - ]), + ports: ports_map, routers: vec![ "default-router-1.os".into(), "default-router-2.os".into(),