connecting to network, minor improvements
parent
5ccbc5e144
commit
c3732ba4bc
|
@ -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
|
||||
|
|
|
@ -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>;
|
||||
}
|
||||
|
||||
|
|
16
src/main.rs
16
src/main.rs
|
@ -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(())
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue