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