90 lines
2.9 KiB
Rust
90 lines
2.9 KiB
Rust
// shell.exec("{networksetup,airport}") goes brrrr
|
|
|
|
use crate::connectors::types::{Connector, Network, NetworkInterface};
|
|
use std::process::Command;
|
|
extern crate plist;
|
|
use serde::{Deserialize};
|
|
use regex::{Regex};
|
|
|
|
#[derive(Deserialize)]
|
|
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
|
pub struct AirportNetwork {
|
|
#[serde(rename = "SSID_STR")]
|
|
ssid: String,
|
|
bssid: String,
|
|
channel: u8,
|
|
rssi: i8,
|
|
}
|
|
|
|
impl From<AirportNetwork> for Network {
|
|
fn from(net: AirportNetwork) -> Network {
|
|
Network {
|
|
ssid: net.ssid.clone(),
|
|
bssid: Some(net.bssid.clone()),
|
|
channel: Some(net.channel.clone()),
|
|
rssi: Some(net.rssi.clone()),
|
|
..Default::default()
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct MacOSConnector {}
|
|
|
|
impl MacOSConnector {
|
|
fn call_airport(&self, args: Vec<&str>) -> Result<Vec<u8>, String> {
|
|
let output = Command::new("/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport")
|
|
.args(&args)
|
|
.arg("--xml")
|
|
.output()
|
|
.expect("calling airport failed");
|
|
if !output.status.success() {
|
|
panic!("airport returned {:} status", output.status);
|
|
}
|
|
Ok(output.stdout)
|
|
}
|
|
|
|
fn call_networksetup(&self, command: &str) -> Result<Vec<u8>, String> {
|
|
let output = Command::new("/usr/sbin/networksetup")
|
|
.arg(&command)
|
|
.output()
|
|
.expect("calling networksetup failed");
|
|
if !output.status.success() {
|
|
panic!("networksetup returned {:} status", output.status);
|
|
}
|
|
Ok(output.stdout)
|
|
}
|
|
}
|
|
|
|
impl Connector for MacOSConnector {
|
|
fn list_networks(&self) -> Result<Vec<Network>, String> {
|
|
let output = self.call_airport(vec!["-s"])
|
|
.expect("airport idk what actually");
|
|
let res: Vec<AirportNetwork> = plist::from_bytes(&output).expect("airport returned shit");
|
|
let networks = res.into_iter().map(|net| Network::from(net)).collect();
|
|
Ok(networks)
|
|
}
|
|
|
|
fn list_network_interfaces(&self) -> Result<Vec<NetworkInterface>, String> {
|
|
// order returns both names
|
|
let output = String::from_utf8(
|
|
self.call_networksetup("-listnetworkserviceorder")
|
|
.expect("networksetup idk what actually"))
|
|
.expect("networksetup returned non-utf8 chars???");
|
|
lazy_static! {
|
|
static ref RE: Regex = Regex::new(r"(?x)
|
|
\((?P<order>\d+|\*)\)\ (?P<human>[^\n]+)\n
|
|
\(Hardware\ Port:\ .+?,\ Device:\ (?P<unix>[a-z]+\d+)\)
|
|
").unwrap();
|
|
}
|
|
let mut ifaces: Vec<NetworkInterface> = vec![];
|
|
for iface in RE.captures_iter(&output) {
|
|
ifaces.push(NetworkInterface {
|
|
enabled: &iface["order"] != "*",
|
|
machine_name: iface["unix"].to_string(),
|
|
human_name: Some(iface["human"].to_string()),
|
|
});
|
|
}
|
|
Ok(ifaces)
|
|
}
|
|
}
|