Skip to content

Commit

Permalink
fix(dgw): rework network interface DTO definition (#871)
Browse files Browse the repository at this point in the history
Issue: DGW-133
  • Loading branch information
irvingoujAtDevolution authored Jun 17, 2024
1 parent 0e77ada commit bc2cb96
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
11 changes: 9 additions & 2 deletions crates/network-scanner/src/interfaces/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use netlink_packet_route::link::{LinkAttribute, LinkFlag};
use netlink_packet_route::route::{RouteAddress, RouteAttribute, RouteMessage};
use rtnetlink::{new_connection, Handle};

use super::InterfaceAddress;

pub async fn get_network_interfaces() -> anyhow::Result<Vec<NetworkInterface>> {
let (connection, handle, _) = new_connection()?;
tokio::spawn(connection);
Expand Down Expand Up @@ -138,8 +140,13 @@ fn convert_link_info_to_network_interface(link_info: &LinkInfo) -> anyhow::Resul
name: link_info.name.clone(),
description: None,
mac_address: link_info.mac.as_slice().try_into().ok(),
ip_addresses,
prefixes,
addresses: prefixes
.into_iter()
.map(|(addr, prefix)| InterfaceAddress {
ip: addr,
prefixlen: prefix,
})
.collect(),
operational_status: link_info.flags.contains(&LinkFlag::Up),
gateways,
dns_servers: link_info.dns_servers.clone(),
Expand Down
9 changes: 7 additions & 2 deletions crates/network-scanner/src/interfaces/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,18 @@ impl serde::Serialize for MacAddr {
}
}

#[derive(Debug, Clone)]
pub struct InterfaceAddress {
pub ip: IpAddr,
pub prefixlen: u32,
}

#[derive(Debug, Clone)]
pub struct NetworkInterface {
pub name: String,
pub description: Option<String>,
pub mac_address: Option<MacAddr>,
pub ip_addresses: Vec<IpAddr>,
pub prefixes: Vec<(IpAddr, u32)>,
pub addresses: Vec<InterfaceAddress>,
pub operational_status: bool,
pub gateways: Vec<IpAddr>,
pub dns_servers: Vec<IpAddr>,
Expand Down
10 changes: 8 additions & 2 deletions crates/network-scanner/src/interfaces/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ impl From<ipconfig::Adapter> for NetworkInterface {
name: adapter.adapter_name().to_string(),
description: Some(adapter.description().to_string()),
mac_address,
ip_addresses: adapter.ip_addresses().to_vec(),
prefixes: adapter.prefixes().to_vec(),
addresses: adapter
.prefixes()
.iter()
.map(|(ip, prefix)| super::InterfaceAddress {
ip: ip.clone(),
prefixlen: *prefix,
})
.collect(),
operational_status: adapter.oper_status() == ipconfig::OperStatus::IfOperStatusUp,
gateways: adapter.gateways().to_vec(),
dns_servers: adapter.dns_servers().to_vec(),
Expand Down
32 changes: 22 additions & 10 deletions devolutions-gateway/src/api/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,13 @@ pub async fn get_net_config(_token: crate::extract::NetScanToken) -> Result<Json
Ok(Json(interfaces))
}

#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[derive(Debug, Clone, Serialize)]
pub struct InterfaceAddress {
pub ip: IpAddr,
pub prefixlen: u32,
}

/// Network interface configuration
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[derive(Debug, Clone, Serialize)]
Expand All @@ -224,15 +231,14 @@ pub struct NetworkInterface {
#[cfg_attr(feature = "openapi", schema(value_type = Option<String>))]
#[serde(skip_serializing_if = "Option::is_none")]
pub mac_address: Option<MacAddr>,
#[cfg_attr(feature = "openapi", schema(value_type = Vec<String>))]
pub ip_addresses: Vec<IpAddr>,
#[cfg_attr(feature = "openapi", schema(value_type = Vec<(String, u32)>))]
pub prefixes: Vec<(IpAddr, u32)>,
pub operational_status: bool,
#[cfg_attr(feature = "openapi", schema(value_type = Vec<InterfaceAddress>))]
pub addresses: Vec<InterfaceAddress>,
#[cfg_attr(feature = "openapi", schema(value_type = bool))]
pub is_up: bool,
#[cfg_attr(feature = "openapi", schema(value_type = Vec<String>))]
pub gateways: Vec<IpAddr>,
#[cfg_attr(feature = "openapi", schema(value_type = Vec<String>))]
pub dns_servers: Vec<IpAddr>,
pub nameservers: Vec<IpAddr>,
}

impl From<interfaces::NetworkInterface> for NetworkInterface {
Expand All @@ -241,11 +247,17 @@ impl From<interfaces::NetworkInterface> for NetworkInterface {
name: iface.name,
description: iface.description,
mac_address: iface.mac_address,
ip_addresses: iface.ip_addresses,
prefixes: iface.prefixes,
operational_status: iface.operational_status,
addresses: iface
.addresses
.into_iter()
.map(|addr| InterfaceAddress {
ip: addr.ip,
prefixlen: addr.prefixlen,
})
.collect(),
is_up: iface.operational_status,
gateways: iface.gateways,
dns_servers: iface.dns_servers,
nameservers: iface.dns_servers,
}
}
}

0 comments on commit bc2cb96

Please sign in to comment.