connecting to network, minor improvements

master
Lauren Liberda 2022-01-02 22:15:23 +01:00
parent 5ccbc5e144
commit c3732ba4bc
No known key found for this signature in database
GPG Key ID: 734C629FD04BD319
3 changed files with 39 additions and 12 deletions

View File

@ -43,20 +43,35 @@ impl MacOSConnector {
Ok(output.stdout)
}
fn call_networksetup(&self, command: &str) -> Result<Vec<u8>, String> {
fn call_networksetup(&self, args: Vec<&str>) -> Result<Vec<u8>, String> {
let output = Command::new("/usr/sbin/networksetup")
.arg(&command)
.args(&args)
.output()
.expect("calling networksetup failed");
if !output.status.success() {
panic!("networksetup returned {:} status", output.status);
match output.status.success() {
true => Ok(output.stdout),
false => Err(match output.status.code() {
Some(code) => format!("networksetup exited with {:}: {:?}", code, output.stdout),
None => format!("networksetup exited by signal"),
}),
}
Ok(output.stdout)
}
}
impl Connector for MacOSConnector {
fn list_networks(&self) -> Result<Vec<Network>, String> {
fn connect_to_network(&self, iface: &NetworkInterface, net: &Network, psk: Option<&str>) -> Result<bool, String> {
let mut args = vec!["-setairportnetwork", &iface.machine_name, &net.ssid];
if let Some(p) = psk {
args.push(p);
}
match self.call_networksetup(args) {
Ok(_) => Ok(true),
Err(e) => Err(e),
}
}
fn list_networks(&self, _iface: &NetworkInterface) -> Result<Vec<Network>, String> {
// this should use the specific network interface, but airport won't let us specify it
let output = self.call_airport(vec!["-s"])
.expect("airport idk what actually");
let res: Vec<AirportNetwork> = plist::from_bytes(&output).expect("airport returned shit");
@ -67,9 +82,12 @@ impl Connector for MacOSConnector {
fn list_network_interfaces(&self) -> Result<Vec<NetworkInterface>, String> {
// order returns both names
let output = String::from_utf8(
self.call_networksetup("-listnetworkserviceorder")
self.call_networksetup(vec!["-listnetworkserviceorder"])
.expect("networksetup idk what actually"))
.expect("networksetup returned non-utf8 chars???");
if output.len() == 0 {
return Err("networksetup returned nothing. Is the Wi-Fi turned on?".to_string());
}
lazy_static! {
static ref RE: Regex = Regex::new(r"(?x)
\((?P<order>\d+|\*)\)\ (?P<human>[^\n]+)\n

View File

@ -1,5 +1,6 @@
pub trait Connector {
fn list_networks(&self) -> Result<Vec<Network>, String>;
fn connect_to_network(&self, iface: &NetworkInterface, net: &Network, psk: Option<&str>) -> Result<bool, String>;
fn list_networks(&self, iface: &NetworkInterface) -> Result<Vec<Network>, String>;
fn list_network_interfaces(&self) -> Result<Vec<NetworkInterface>, String>;
}

View File

@ -13,15 +13,23 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let ifaces = connector.list_network_interfaces().expect("network interface listing errored");
for iface in ifaces {
for iface in &ifaces {
println!("{:?}", iface);
}
let networks = connector.list_networks().expect("network listing errored");
for net in networks {
let iface = ifaces.into_iter().find(|f| f.human_name == Some("Wi-Fi".to_string()))
.expect("Wi-Fi interface not found");
let networks = connector.list_networks(&iface).expect("network listing errored");
for net in &networks {
println!("{:?}", net);
}
let network = networks.into_iter().find(|net| net.ssid == "test")
.expect("network not found");
connector.connect_to_network(&iface, &network, None).expect("could not connect to network");
Ok(())
}