fix: proper deserialization of allowed_ips

This commit is contained in:
2026-02-09 19:25:19 -08:00
parent 15fc72d5c9
commit c4e198de83
3 changed files with 29 additions and 5 deletions

View File

@@ -1,11 +1,13 @@
use std::collections::HashMap; use std::collections::HashMap;
use actix_web::{HttpResponse, web}; use actix_web::{HttpResponse, web};
use ipnetwork::IpNetwork;
use redis::AsyncTypedCommands; use redis::AsyncTypedCommands;
use crate::{ use crate::{
AppState, AppState,
error::{Error, Result}, error::{Error, Result},
utils::Peer,
}; };
pub async fn get_peers(app_state: web::Data<AppState>) -> Result<HttpResponse> { pub async fn get_peers(app_state: web::Data<AppState>) -> Result<HttpResponse> {
@@ -23,14 +25,25 @@ pub async fn get_peers(app_state: web::Data<AppState>) -> Result<HttpResponse> {
pipe.hgetall(format!("peer:{key}")); pipe.hgetall(format!("peer:{key}"));
} }
let mut peers = pipe let peers: Vec<Peer> = pipe
.query_async::<Vec<HashMap<String, String>>>(&mut conn) .query_async::<Vec<HashMap<String, String>>>(&mut conn)
.await .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<IpNetwork> = 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 {
peer.insert("public_key".to_string(), key.clone()); public_key: key.clone(),
public_ip: peer.get("public_ip").unwrap().to_string(),
allowed_ips,
} }
})
.collect();
Ok(HttpResponse::Ok().json(peers)) Ok(HttpResponse::Ok().json(peers))
} }

View File

@@ -1,3 +1,5 @@
mod peer;
mod wg; mod wg;
pub use peer::Peer;
pub use wg::WireguardPublicKey; pub use wg::WireguardPublicKey;

9
src/utils/peer.rs Normal file
View File

@@ -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<IpNetwork>,
}