fix: Identity struct must have room for many ports

This commit is contained in:
dr-frmr 2024-05-19 23:50:31 -06:00
parent 6eaa60364e
commit 45b34705cd
No known key found for this signature in database
5 changed files with 37 additions and 53 deletions

View File

@ -1,21 +1,10 @@
use kinode_process_lib::{call_init, net, println, Address, Message, NodeId, Request};
use serde::{Deserialize, Serialize};
use kinode_process_lib::{call_init, net, println, Address, Message, Request};
wit_bindgen::generate!({
path: "target/wit",
world: "process-v0",
});
// types copied from runtime networking core
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Identity {
pub name: NodeId,
pub networking_key: String,
pub ws_routing: Option<(String, u16)>,
pub allowed_routers: Vec<NodeId>,
}
call_init!(init);
fn init(_our: Address) {
let Ok(Ok(Message::Response { body, .. })) = Request::to(("our", "net", "distro", "sys"))

View File

@ -42,8 +42,8 @@ pub async fn networking(
reveal_ip: bool,
) -> Result<()> {
// branch on whether we are a direct or indirect node
match &our.routing {
NodeRouting::Routers(_) => {
match our.ws_routing() {
None => {
// indirect node: run the indirect networking strategy
print_tx
.send(Printout {
@ -64,17 +64,7 @@ pub async fn networking(
)
.await
}
NodeRouting::Direct {
ip,
ws_port,
tcp_port: _,
}
| NodeRouting::Both {
ip,
ws_port,
tcp_port: _,
..
} => {
Some((ip, ws_port)) => {
// direct node: run the direct networking strategy
if &our_ip != ip {
return Err(anyhow!(
@ -987,8 +977,7 @@ async fn handle_local_message(
} else {
NodeRouting::Direct {
ip: log.ips[0].clone(),
ws_port: log.get_protocol_port("ws"),
tcp_port: log.get_protocol_port("tcp"),
ports: log.ports,
}
},
},
@ -1008,8 +997,7 @@ async fn handle_local_message(
} else {
NodeRouting::Direct {
ip: log.ips[0].clone(),
ws_port: log.get_protocol_port("ws"),
tcp_port: log.get_protocol_port("tcp"),
ports: log.ports,
}
},
},

View File

@ -192,7 +192,7 @@ pub async fn create_passthrough(
));
}
let to_id = pki.get(&to_name).ok_or(anyhow!("unknown KNS name"))?;
let NodeRouting::Direct { ip, ws_port, .. } = &to_id.routing else {
let Some((ip, ws_port)) = to_id.ws_routing() else {
// create passthrough to indirect node that we do routing for
//
let target_peer = peers

View File

@ -109,8 +109,7 @@ pub async fn register(
name: "".to_string(),
routing: NodeRouting::Both {
ip: ip.clone(),
ws_port,
tcp_port: 0,
ports: std::collections::BTreeMap::from([("ws".to_string(), ws_port)]),
routers: vec![
"default-router-1.os".into(),
"default-router-2.os".into(),
@ -551,8 +550,7 @@ async fn handle_import_keyfile(
routing: if k.routers.is_empty() {
NodeRouting::Direct {
ip,
ws_port: 9000,
tcp_port: 0,
ports: std::collections::BTreeMap::from([("ws".to_string(), 9000)]),
}
} else {
NodeRouting::Routers(k.routers.clone())
@ -610,8 +608,7 @@ async fn handle_login(
routing: if k.routers.is_empty() {
NodeRouting::Direct {
ip,
ws_port: 9000,
tcp_port: 0,
ports: std::collections::BTreeMap::from([("ws".to_string(), 9000)]),
}
} else {
NodeRouting::Routers(k.routers.clone())
@ -745,8 +742,7 @@ pub async fn assign_ws_routing(
}
our.routing = NodeRouting::Direct {
ip: node_ip,
ws_port: ws,
tcp_port: 0,
ports: std::collections::BTreeMap::from([("ws".to_string(), ws)]),
};
}
Ok(())

View File

@ -1013,14 +1013,12 @@ pub enum NodeRouting {
Routers(Vec<NodeId>),
Direct {
ip: String,
ws_port: u16,
tcp_port: u16,
ports: BTreeMap<String, u16>,
},
/// currently only used for initial registration...
Both {
ip: String,
ws_port: u16,
tcp_port: u16,
ports: BTreeMap<String, u16>,
routers: Vec<NodeId>,
},
}
@ -1032,11 +1030,30 @@ impl Identity {
_ => false,
}
}
pub fn get_protocol_port(&self, protocol: &str) -> Option<u16> {
match &self.routing {
NodeRouting::Routers(_) => None,
NodeRouting::Direct { ports, .. } => ports.get(protocol).cloned(),
NodeRouting::Both { ports, .. } => ports.get(protocol).cloned(),
}
}
pub fn ws_routing(&self) -> Option<(&str, &u16)> {
match &self.routing {
NodeRouting::Routers(_) => None,
NodeRouting::Direct { ip, ws_port, .. } => Some((ip, ws_port)),
NodeRouting::Both { ip, ws_port, .. } => Some((ip, ws_port)),
NodeRouting::Direct { ip, ports } => {
if let Some(port) = ports.get("ws") {
Some((ip, port))
} else {
None
}
}
NodeRouting::Both { ip, ports, .. } => {
if let Some(port) = ports.get("ws") {
Some((ip, port))
} else {
None
}
}
}
}
pub fn routers(&self) -> Option<&Vec<NodeId>> {
@ -1049,23 +1066,17 @@ impl Identity {
pub fn both_to_direct(&mut self) {
if let NodeRouting::Both {
ip,
ws_port,
tcp_port,
ports,
routers: _,
} = self.routing.clone()
{
self.routing = NodeRouting::Direct {
ip,
ws_port,
tcp_port,
};
self.routing = NodeRouting::Direct { ip, ports };
}
}
pub fn both_to_routers(&mut self) {
if let NodeRouting::Both {
ip: _,
ws_port: _,
tcp_port: _,
ports: _,
routers,
} = self.routing.clone()
{