From c4e198de834953823d5b88f1d69098728baca5b2 Mon Sep 17 00:00:00 2001 From: lucalise Date: Mon, 9 Feb 2026 19:25:19 -0800 Subject: [PATCH] fix: proper deserialization of allowed_ips --- src/endpoints/peers.rs | 23 ++++++++++++++++++----- src/utils/mod.rs | 2 ++ src/utils/peer.rs | 9 +++++++++ 3 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 src/utils/peer.rs diff --git a/src/endpoints/peers.rs b/src/endpoints/peers.rs index 5b82b8b..43b8a6c 100644 --- a/src/endpoints/peers.rs +++ b/src/endpoints/peers.rs @@ -1,11 +1,13 @@ use std::collections::HashMap; use actix_web::{HttpResponse, web}; +use ipnetwork::IpNetwork; use redis::AsyncTypedCommands; use crate::{ AppState, error::{Error, Result}, + utils::Peer, }; pub async fn get_peers(app_state: web::Data) -> Result { @@ -23,14 +25,25 @@ pub async fn get_peers(app_state: web::Data) -> Result { pipe.hgetall(format!("peer:{key}")); } - let mut peers = pipe + let peers: Vec = pipe .query_async::>>(&mut conn) .await - .map_err(|e| Error::get_peer(e))?; + .map_err(|e| Error::get_peer(e))? + .into_iter() + .zip(keys.iter()) + .map(|(peer, key)| { + let allowed_ips: Vec = peer + .get("allowed_ips") + .map(|s| serde_json::from_str(s).unwrap_or_default()) + .unwrap_or_default(); - for (key, peer) in keys.iter().zip(peers.iter_mut()) { - peer.insert("public_key".to_string(), key.clone()); - } + Peer { + public_key: key.clone(), + public_ip: peer.get("public_ip").unwrap().to_string(), + allowed_ips, + } + }) + .collect(); Ok(HttpResponse::Ok().json(peers)) } diff --git a/src/utils/mod.rs b/src/utils/mod.rs index c0cdc11..00a4845 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,3 +1,5 @@ +mod peer; mod wg; +pub use peer::Peer; pub use wg::WireguardPublicKey; diff --git a/src/utils/peer.rs b/src/utils/peer.rs new file mode 100644 index 0000000..b21bb10 --- /dev/null +++ b/src/utils/peer.rs @@ -0,0 +1,9 @@ +use ipnetwork::IpNetwork; +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize)] +pub struct Peer { + pub public_key: String, + pub public_ip: String, + pub allowed_ips: Vec, +}