dg_network_manager 1.0.0

Network Manager DBUS API
Documentation
use super::DeviceClient;
use std::io::{Error, ErrorKind};
use std::sync::Arc;
use zbus::proxy::CacheProperties;
use zbus::{Connection, Result as ZResult, proxy};

#[proxy(
    interface = "org.freedesktop.NetworkManager.Device.WireGuard",
    default_service = "org.freedesktop.NetworkManager"
)]
pub trait WireGuard {
    // Properties
    #[zbus(property)]
    fn public_key(&self) -> ZResult<Vec<u8>>;

    #[zbus(property)]
    fn listen_port(&self) -> ZResult<u16>;

    #[zbus(property)]
    fn fw_mark(&self) -> ZResult<u32>;
}

pub struct WireGuardClient {
    device: DeviceClient,
    proxy: WireGuardProxy<'static>,
}

impl WireGuardClient {
    pub async fn new(connection: Arc<Connection>, service_path: String) -> Result<Self, Error> {
        let device = DeviceClient::new(connection.clone(), service_path.clone()).await?;

        let proxy = WireGuardProxy::builder(&connection)
            .path(service_path)
            .map_err(|e| Error::new(ErrorKind::InvalidInput, e.to_string()))?
            .cache_properties(CacheProperties::Yes)
            .build()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))?;

        // Verify we can access wireguard-specific properties
        proxy
            .listen_port()
            .await
            .map_err(|e| Error::new(ErrorKind::NotFound, e.to_string()))?;

        Ok(Self { device, proxy })
    }

    // Forward common device methods to the base device client
    pub fn service_path(&self) -> &str {
        self.device.service_path()
    }

    pub async fn interface(&self) -> Result<String, Error> {
        self.device.interface().await
    }

    pub async fn disconnect(&self) -> Result<(), Error> {
        self.device.disconnect().await
    }

    // WireGuard-specific methods
    pub async fn public_key(&self) -> Result<Vec<u8>, Error> {
        self.proxy
            .public_key()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

    pub async fn listen_port(&self) -> Result<u16, Error> {
        self.proxy
            .listen_port()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }

    pub async fn fw_mark(&self) -> Result<u32, Error> {
        self.proxy
            .fw_mark()
            .await
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
    }
}