diff --git a/src/agent.rs b/src/agent.rs index 0df7e2c..359d0a7 100644 --- a/src/agent.rs +++ b/src/agent.rs @@ -57,6 +57,7 @@ enum ServerMessage { allowed_public_keys: Vec, }, Update { + listen: Vec, peers: Vec, allowed_public_keys: Vec, }, @@ -251,19 +252,20 @@ async fn handle_server_message(msg: ServerMessage, ygg_config_path: &str) -> Res } } ServerMessage::Update { + listen, peers, allowed_public_keys, } => { info!("Received configuration update:"); + info!(" Updated listen endpoints: {:?}", listen); info!(" Updated peers: {} configured", peers.len()); for peer in &peers { debug!(" - {}", peer); } info!(" Updated allowed keys: {} configured", allowed_public_keys.len()); - // Apply configuration update to Yggdrasil - // For updates we need to read current config and update only peers/allowed keys - match update_yggdrasil_config(ygg_config_path, &peers, &allowed_public_keys).await { + // Apply full configuration update to Yggdrasil + match update_yggdrasil_config_full(ygg_config_path, &listen, &peers, &allowed_public_keys).await { Ok(_) => info!("Configuration update successfully applied to {}", ygg_config_path), Err(e) => error!("Failed to update Yggdrasil config: {}", e), } @@ -371,4 +373,27 @@ async fn update_yggdrasil_config( info!("Yggdrasil configuration updated in {}", config_path); Ok(()) +} + +async fn update_yggdrasil_config_full( + config_path: &str, + listen: &[String], + peers: &[String], + allowed_public_keys: &[String] +) -> Result<()> { + // Read current config + let current_config = tokio::fs::read_to_string(config_path).await?; + let mut config: serde_json::Value = serde_json::from_str(¤t_config)?; + + // Update listen, peers and allowed public keys + config["Listen"] = serde_json::json!(listen); + config["Peers"] = serde_json::json!(peers); + config["AllowedPublicKeys"] = serde_json::json!(allowed_public_keys); + + // Write updated config back + let updated_config = serde_json::to_string_pretty(&config)?; + tokio::fs::write(config_path, updated_config).await?; + + info!("Yggdrasil configuration fully updated in {}", config_path); + Ok(()) } \ No newline at end of file diff --git a/src/modules/websocket.rs b/src/modules/websocket.rs index b6294ef..864e7b3 100644 --- a/src/modules/websocket.rs +++ b/src/modules/websocket.rs @@ -31,6 +31,7 @@ pub enum ServerMessage { allowed_public_keys: Vec, }, Update { + listen: Vec, peers: Vec, allowed_public_keys: Vec, }, diff --git a/src/websocket_state.rs b/src/websocket_state.rs index 0a27eda..e86627b 100644 --- a/src/websocket_state.rs +++ b/src/websocket_state.rs @@ -35,6 +35,7 @@ pub async fn broadcast_configuration_update(node_manager: &Arc) { for (node_id, tx) in connections.iter() { if let Some(config) = configs.get(node_id) { let update = ServerMessage::Update { + listen: config.listen.clone(), peers: config.peers.clone(), allowed_public_keys: config.allowed_public_keys.clone(), }; @@ -46,6 +47,7 @@ pub async fn broadcast_configuration_update(node_manager: &Arc) { } else { // Node was deleted, send empty configuration to disconnect agent gracefully let update = ServerMessage::Update { + listen: vec![], peers: vec![], allowed_public_keys: vec![], };