Feat(OSX): Add OSX Support
This commit is contained in:
@@ -25,9 +25,9 @@ Note that only **open**, **WEP** and **WPA-PSK** networks are supported at the m
|
||||
|
||||
```RUST
|
||||
extern crate wifi_rs;
|
||||
use wifi_rs::{WiFi, Config, NetworkError};
|
||||
use wifi_rs::{WiFi, Config, WifiConnectionError};
|
||||
|
||||
fn main() -> Result<(), NetworkError> {
|
||||
fn main() -> Result<(), WifiConnectionError> {
|
||||
let config = Some(Config {
|
||||
interface: Some("wlo1"), // interface : None would default to `wlan0`.
|
||||
});
|
||||
|
||||
@@ -1,3 +1,2 @@
|
||||
mod xml_profile_handler;
|
||||
|
||||
pub(crate) use self::xml_profile_handler::NetworkXmlProfileHandler;
|
||||
|
||||
+11
-7
@@ -1,15 +1,20 @@
|
||||
mod handlers;
|
||||
pub mod profile_network;
|
||||
mod providers;
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
mod handlers;
|
||||
#[cfg(target_os = "windows")]
|
||||
mod stubs;
|
||||
|
||||
use std::{fmt, io};
|
||||
|
||||
pub trait Network: fmt::Debug {
|
||||
/// Makes an attempt to connect to a selected wireless network with password specified.
|
||||
fn connect(&self, password: &str) -> Result<bool, NetworkError>;
|
||||
fn disconnect(&self) -> Result<bool, NetworkError>;
|
||||
fn connect(&self, password: &str) -> Result<bool, WifiConnectionError>;
|
||||
fn disconnect(&self) -> Result<bool, WifiConnectionError>;
|
||||
fn is_wifi_enabled(&self) -> bool;
|
||||
fn connnection_up(&self) -> bool;
|
||||
fn connnection_down(&self) -> bool;
|
||||
}
|
||||
|
||||
// #[derive(Debug)]
|
||||
@@ -25,8 +30,7 @@ pub struct Config<'a> {
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum NetworkError {
|
||||
// FromUtf8Error(FromUtf8Error),
|
||||
pub enum WifiConnectionError {
|
||||
SsidNotFound,
|
||||
OsNotSupported,
|
||||
IpAssignFailed,
|
||||
@@ -37,8 +41,8 @@ pub enum NetworkError {
|
||||
WiFiInterfaceDisabled,
|
||||
}
|
||||
|
||||
impl From<io::Error> for NetworkError {
|
||||
impl From<io::Error> for WifiConnectionError {
|
||||
fn from(error: io::Error) -> Self {
|
||||
NetworkError::IoError(error)
|
||||
WifiConnectionError::IoError(error)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use connectivity::{providers::Machine, Config, Network, NetworkError};
|
||||
use connectivity::{providers::Machine, Config, Network, WifiConnectionError};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ProfileNetwork {
|
||||
@@ -7,9 +7,9 @@ pub struct ProfileNetwork {
|
||||
|
||||
/// Profile Network handler responsible to connect to a wireless network.
|
||||
impl ProfileNetwork {
|
||||
pub fn new(name: &str, config: Option<Config>) -> Result<Self, NetworkError> {
|
||||
pub fn new(name: &str, config: Option<Config>) -> Result<Self, WifiConnectionError> {
|
||||
if !(cfg!(target_os = "linux") || cfg!(target_os = "windows")) {
|
||||
return Err(NetworkError::OsNotSupported);
|
||||
return Err(WifiConnectionError::OsNotSupported);
|
||||
}
|
||||
|
||||
let handler = Machine::new(
|
||||
@@ -22,11 +22,15 @@ impl ProfileNetwork {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn connect(&self, password: &str) -> Result<bool, NetworkError> {
|
||||
pub fn connect(&self, password: &str) -> Result<bool, WifiConnectionError> {
|
||||
if !self.handler.is_wifi_enabled() {
|
||||
return Err(NetworkError::WiFiInterfaceDisabled);
|
||||
return Err(WifiConnectionError::WiFiInterfaceDisabled);
|
||||
}
|
||||
|
||||
self.handler.connect(password)
|
||||
}
|
||||
|
||||
pub fn connection_up(&self) -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use connectivity::{Network, NetworkError};
|
||||
use connectivity::{Network, WifiConnectionError};
|
||||
use std::process::Command;
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -17,7 +17,7 @@ impl Linux {
|
||||
}
|
||||
|
||||
impl Network for Linux {
|
||||
fn connect(&self, password: &str) -> Result<bool, NetworkError> {
|
||||
fn connect(&self, password: &str) -> Result<bool, WifiConnectionError> {
|
||||
let output = Command::new("nmcli")
|
||||
.args(&[
|
||||
"d",
|
||||
@@ -30,18 +30,18 @@ impl Network for Linux {
|
||||
&self.interface,
|
||||
])
|
||||
.output()
|
||||
.map_err(|err| NetworkError::FailedToConnect(format!("{}", err)))?;
|
||||
.map_err(|err| WifiConnectionError::FailedToConnect(format!("{}", err)))?;
|
||||
|
||||
Ok(String::from_utf8_lossy(&output.stdout)
|
||||
.as_ref()
|
||||
.contains("successfully activated"))
|
||||
}
|
||||
|
||||
fn disconnect(&self) -> Result<bool, NetworkError> {
|
||||
fn disconnect(&self) -> Result<bool, WifiConnectionError> {
|
||||
let output = Command::new("nmcli")
|
||||
.args(&["d", "disconnect", "ifname", &self.interface])
|
||||
.output()
|
||||
.map_err(|err| NetworkError::FailedToDisconnect(format!("{}", err)))?;
|
||||
.map_err(|err| WifiConnectionError::FailedToDisconnect(format!("{}", err)))?;
|
||||
|
||||
Ok(String::from_utf8_lossy(&output.stdout)
|
||||
.as_ref()
|
||||
@@ -60,4 +60,28 @@ impl Network for Linux {
|
||||
.replace("\n", "")
|
||||
.contains("enabled")
|
||||
}
|
||||
|
||||
fn connnection_up(&self) -> bool {
|
||||
let output = Command::new("nmcli")
|
||||
.args(&["radio", "wifi", "on"])
|
||||
.output();
|
||||
|
||||
if let Err(_) = output {
|
||||
return false;
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
fn connnection_down(&self) -> bool {
|
||||
let output = Command::new("nmcli")
|
||||
.args(&["radio", "wifi", "off"])
|
||||
.output();
|
||||
|
||||
if let Err(_) = output {
|
||||
return false;
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,14 @@
|
||||
mod linux;
|
||||
#[cfg(target_os = "windows")]
|
||||
mod windows;
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
pub(crate) use self::windows::Windows as Machine;
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
mod linux;
|
||||
#[cfg(target_os = "linux")]
|
||||
pub(crate) use self::linux::Linux as Machine;
|
||||
|
||||
#[cfg(target_os = "osx")]
|
||||
mod osx;
|
||||
#[cfg(target_os = "osx")]
|
||||
pub(crate) use self::osx::OSX as Machine;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use connectivity::handlers::NetworkXmlProfileHandler;
|
||||
use connectivity::{Network, NetworkError};
|
||||
use connectivity::{Network, WifiConnectionError};
|
||||
use std::process::Command;
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -15,7 +15,7 @@ impl Windows {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn add_profile(&self, password: &str) -> Result<(), NetworkError> {
|
||||
pub(crate) fn add_profile(&self, password: &str) -> Result<(), WifiConnectionError> {
|
||||
let mut handler = NetworkXmlProfileHandler::new();
|
||||
handler.content = handler
|
||||
.content
|
||||
@@ -33,31 +33,31 @@ impl Windows {
|
||||
&format!("filename={}", temp_file.path().to_str().unwrap()),
|
||||
])
|
||||
.output()
|
||||
.map_err(|_| NetworkError::AddNetworkProfileFailed)?;
|
||||
.map_err(|_| WifiConnectionError::AddNetworkProfileFailed)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Network for Windows {
|
||||
fn connect(&self, password: &str) -> Result<bool, NetworkError> {
|
||||
fn connect(&self, password: &str) -> Result<bool, WifiConnectionError> {
|
||||
self.add_profile(password)?;
|
||||
|
||||
let output = Command::new("netsh")
|
||||
.args(&["wlan", "connect", &format!("name={}", self.name)])
|
||||
.output()
|
||||
.map_err(|err| NetworkError::FailedToConnect(format!("{}", err)))?;
|
||||
.map_err(|err| WifiConnectionError::FailedToConnect(format!("{}", err)))?;
|
||||
|
||||
Ok(String::from_utf8_lossy(&output.stdout)
|
||||
.as_ref()
|
||||
.contains("successfully activated"))
|
||||
}
|
||||
|
||||
fn disconnect(&self) -> Result<bool, NetworkError> {
|
||||
fn disconnect(&self) -> Result<bool, WifiConnectionError> {
|
||||
let output = Command::new("netsh")
|
||||
.args(&["wlan", "disconnect"])
|
||||
.output()
|
||||
.map_err(|err| NetworkError::FailedToDisconnect(format!("{}", err)))?;
|
||||
.map_err(|err| WifiConnectionError::FailedToDisconnect(format!("{}", err)))?;
|
||||
|
||||
Ok(String::from_utf8_lossy(&output.stdout)
|
||||
.as_ref()
|
||||
@@ -65,16 +65,14 @@ impl Network for Windows {
|
||||
}
|
||||
|
||||
fn is_wifi_enabled(&self) -> bool {
|
||||
// let output = Command::new("nmcli").args(&["radio", "wifi"]).output();
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
// if let Err(_) = output {
|
||||
// return false;
|
||||
// }
|
||||
fn connnection_up(&self) -> bool {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
// match String::from_utf8_lossy(&output.unwrap().stdout).as_ref() {
|
||||
// "enabled" => true,
|
||||
// _ => false,
|
||||
// }
|
||||
false
|
||||
fn connnection_down(&self) -> bool {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
mod connectivity;
|
||||
pub use connectivity::{profile_network::ProfileNetwork as WiFi, Config, NetworkError};
|
||||
pub use connectivity::{profile_network::ProfileNetwork as WiFi, Config, WifiConnectionError};
|
||||
|
||||
fn main() -> Result<(), NetworkError> {
|
||||
fn main() -> Result<(), WifiConnectionError> {
|
||||
let config = Some(Config {
|
||||
interface: Some("wlo1"),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user