Added agent

This commit is contained in:
Ultradesu
2025-08-26 14:09:32 +03:00
parent a2eabe2c69
commit 35ec6995a0
3 changed files with 31 additions and 3 deletions

View File

@@ -57,6 +57,7 @@ enum ServerMessage {
allowed_public_keys: Vec<String>,
},
Update {
listen: Vec<String>,
peers: Vec<String>,
allowed_public_keys: Vec<String>,
},
@@ -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(&current_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(())
}

View File

@@ -31,6 +31,7 @@ pub enum ServerMessage {
allowed_public_keys: Vec<String>,
},
Update {
listen: Vec<String>,
peers: Vec<String>,
allowed_public_keys: Vec<String>,
},

View File

@@ -35,6 +35,7 @@ pub async fn broadcast_configuration_update(node_manager: &Arc<NodeManager>) {
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<NodeManager>) {
} else {
// Node was deleted, send empty configuration to disconnect agent gracefully
let update = ServerMessage::Update {
listen: vec![],
peers: vec![],
allowed_public_keys: vec![],
};