Added REST route to get the list of bridges
This commit is contained in:
@ -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::{
|
||||
|
Reference in New Issue
Block a user