hippox-drivers 0.3.5

🦛All indivisible atomic driver units in Hippox.
//! WiFi MAC address set skill - set/spoof MAC address
use crate::{
    DriverCallback, DriverCategory, DriverContext, DriverError, DriverResult,
    types::{Driver, DriverParameter},
};
use serde_json::{Value, json};
use std::collections::HashMap;
use std::process::Command;
use tracing::{debug, info};
/// Driver for setting/spoofing WiFi MAC address
#[derive(Debug)]
pub struct WifiMacAddressSetDriver;
#[async_trait::async_trait]
impl Driver for WifiMacAddressSetDriver {
    fn name(&self) -> &str {
        return "wifi_mac_address_set";
    }
    fn description(&self) -> &str {
        return "Set or spoof the WiFi adapter MAC address";
    }
    fn usage_hint(&self) -> &str {
        return "Use this skill to change your MAC address for privacy or to bypass MAC filters. May require administrator privileges.";
    }
    fn parameters(&self) -> Vec<DriverParameter> {
        return vec![
            DriverParameter {
                name: "mac".to_string(),
                param_type: "string".to_string(),
                description: "New MAC address (format: XX:XX:XX:XX:XX:XX) or 'random' for random MAC".to_string(),
                required: true,
                default: None,
                example: Some(Value::String("00:11:22:33:44:55".to_string())),
                enum_values: None,
            },
            DriverParameter {
                name: "interface".to_string(),
                param_type: "string".to_string(),
                description: "Interface name (default: auto-detect)".to_string(),
                required: false,
                default: None,
                example: Some(Value::String("wlan0".to_string())),
                enum_values: None,
            },
        ];
    }
    fn example_call(&self) -> DriverResult<Value> {
        return Ok(json!({
            "action": "wifi_mac_address_set",
            "parameters": {
                "mac": "random"
            }
        }));
    }
    fn example_output(&self) -> String {
        return "MAC address set to random: 3a:2f:8e:1c:4b:7d".to_string();
    }
    fn category(&self) -> DriverCategory {
        return DriverCategory::Wifi;
    }
    async fn execute(
        &self,
        parameters: &HashMap<String, Value>,
        _callback: Option<&dyn DriverCallback>,
        _context: Option<&DriverContext>,
    ) -> DriverResult<String> {
        debug!("Executing wifi_mac_address_set driver");
        let mac_input = parameters.get("mac").and_then(|v| v.as_str()).ok_or_else(|| {
            debug!("Missing 'mac' parameter");
            return DriverError::missing_parameter("mac");
        })?;
        let interface = parameters.get("interface").and_then(|v| v.as_str()).unwrap_or("wlan0");
        let new_mac = if mac_input == "random" {
            // Generate random MAC (locally administered, unicast)
            format!(
                "02:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
                rand::random::<u8>(),
                rand::random::<u8>(),
                rand::random::<u8>(),
                rand::random::<u8>(),
                rand::random::<u8>()
            )
        } else {
            mac_input.to_string()
        };
        #[cfg(target_os = "linux")]
        {
            // Bring interface down, change MAC, bring up
            crate::common::hidden_cmd("sudo").args(["ip", "link", "set", interface, "down"]).output().map_err(|e| {
                debug!("Failed to bring interface down: {}", e);
                return DriverError::execution(format!("Failed to bring interface down: {}", e));
            })?;
            crate::common::hidden_cmd("sudo").args(["ip", "link", "set", interface, "address", &new_mac]).output().map_err(|e| {
                debug!("Failed to set MAC address: {}", e);
                return DriverError::execution(format!("Failed to set MAC address: {}", e));
            })?;
            crate::common::hidden_cmd("sudo").args(["ip", "link", "set", interface, "up"]).output().map_err(|e| {
                debug!("Failed to bring interface up: {}", e);
                return DriverError::execution(format!("Failed to bring interface up: {}", e));
            })?;
        }
        #[cfg(target_os = "windows")]
        {
            debug!("MAC address change on Windows requires device manager manipulation");
            return Err(DriverError::execution("MAC address change on Windows requires device manager manipulation"));
        }
        #[cfg(target_os = "macos")]
        {
            crate::common::hidden_cmd("sudo").args(["ifconfig", interface, "ether", &new_mac]).output().map_err(|e| {
                debug!("Failed to set MAC address: {}", e);
                return DriverError::execution(format!("Failed to set MAC address: {}", e));
            })?;
        }
        info!("MAC address set to: {}", new_mac);
        return Ok(format!("MAC address set to: {}", new_mac));
    }
}