Added REST route to get the list of bridges

This commit is contained in:
2025-05-26 20:43:19 +02:00
parent de33c7d521
commit a8171375a8
5 changed files with 46 additions and 1 deletions

View File

@ -1,6 +1,8 @@
use crate::constants;
use nix::sys::socket::{AddressFamily, SockaddrLike};
use std::collections::HashMap;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::process::Command;
use std::str::FromStr;
use sysinfo::Networks;
@ -68,7 +70,7 @@ pub fn net_list() -> Vec<String> {
/// Get the list of available network interfaces associated with their IP address
pub fn net_list_and_ips() -> anyhow::Result<HashMap<String, Vec<IpAddr>>> {
let addrs = nix::ifaddrs::getifaddrs().unwrap();
let addrs = nix::ifaddrs::getifaddrs()?;
let mut res = HashMap::new();
@ -136,6 +138,31 @@ pub fn net_list_and_ips() -> anyhow::Result<HashMap<String, Vec<IpAddr>>> {
Ok(res)
}
#[derive(serde::Deserialize)]
struct IPBridgeInfo {
ifname: String,
}
/// Get the list of bridge interfaces
pub fn bridges_list() -> anyhow::Result<Vec<String>> {
let mut cmd = Command::new(constants::IP_PROGRAM);
cmd.args(["-json", "link", "show", "type", "bridge"]);
let output = cmd.output()?;
if !output.status.success() {
anyhow::bail!(
"{} failed, status: {}, stderr: {}",
constants::IP_PROGRAM,
output.status,
String::from_utf8_lossy(&output.stderr)
);
}
// Parse JSON result
let res: Vec<IPBridgeInfo> = serde_json::from_str(&String::from_utf8_lossy(&output.stdout))?;
Ok(res.iter().map(|ip| ip.ifname.clone()).collect())
}
#[cfg(test)]
mod tests {
use crate::utils::net_utils::{