fix: allow for registration without a networking mode

This commit is contained in:
dr-frmr 2024-06-10 13:57:28 -06:00
parent d2db967502
commit 7628293871
No known key found for this signature in database
2 changed files with 39 additions and 23 deletions

View File

@ -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<tokio::net::TcpListener>, 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<tokio::net::TcpListener>, bool),
tcp_networking: (Option<tokio::net::TcpListener>, bool),
http_server_port: u16,
maybe_rpc: Option<String>,
) -> (Identity, Vec<u8>, 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<tokio::net::TcpListener>, bool),
tcp_networking: (Option<tokio::net::TcpListener>, bool),
maybe_rpc: Option<String>,
password: &str,
) -> (Identity, Vec<u8>, 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");

View File

@ -43,8 +43,8 @@ pub async fn register(
tx: RegistrationSender,
kill_rx: oneshot::Receiver<bool>,
ip: String,
ws_networking: (tokio::net::TcpListener, bool),
tcp_networking: (tokio::net::TcpListener, bool),
ws_networking: (Option<tokio::net::TcpListener>, bool),
tcp_networking: (Option<tokio::net::TcpListener>, bool),
http_port: u16,
keyfile: Option<Vec<u8>>,
maybe_rpc: Option<String>,
@ -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(),