Chore(Stage): Some Refactoring
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
use connectivity::{Network, WifiConnectionError};
|
||||
use std::process::Command;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct OSX {
|
||||
pub name: String,
|
||||
interface: String,
|
||||
}
|
||||
|
||||
impl OSX {
|
||||
#[cfg(target_os = "windows")]
|
||||
pub fn new(name: &str, interface: Option<&str>) -> Self {
|
||||
OSX {
|
||||
name: name.into(),
|
||||
interface: interface.unwrap_or("en0").into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Network for OSX {
|
||||
fn connect(&self, password: &str) -> Result<bool, WifiConnectionError> {
|
||||
let output = Command::new("networksetup")
|
||||
.args(&["-setairportnetwork", &self.interface, &self.name, &password])
|
||||
.output()
|
||||
.map_err(|err| WifiConnectionError::FailedToConnect(format!("{}", err)))?;
|
||||
|
||||
Ok(String::from_utf8_lossy(&output.stdout)
|
||||
.as_ref()
|
||||
.contains("successfully activated"))
|
||||
}
|
||||
|
||||
fn disconnect(&self) -> Result<bool, WifiConnectionError> {
|
||||
let output = Command::new("networksetup")
|
||||
.args(&[
|
||||
"-removepreferredwirelessnetwork",
|
||||
&self.interface,
|
||||
&self.name,
|
||||
])
|
||||
.output()
|
||||
.map_err(|err| WifiConnectionError::FailedToDisconnect(format!("{}", err)))?;
|
||||
|
||||
Ok(String::from_utf8_lossy(&output.stdout)
|
||||
.as_ref()
|
||||
.contains("disconnect"))
|
||||
}
|
||||
|
||||
fn is_wifi_enabled(&self) -> bool {
|
||||
let output = Command::new("networksetup")
|
||||
.args(&["radio", "wifi"])
|
||||
.output();
|
||||
|
||||
if let Err(_) = output {
|
||||
return false;
|
||||
}
|
||||
|
||||
String::from_utf8_lossy(&output.unwrap().stdout)
|
||||
.replace(" ", "")
|
||||
.replace("\n", "")
|
||||
.contains("enabled")
|
||||
}
|
||||
|
||||
fn connnection_up(&self) -> bool {
|
||||
let output = Command::new("networksetup")
|
||||
.args(&["-setairportpower", &self.interface, "on"])
|
||||
.output();
|
||||
|
||||
if let Err(_) = output {
|
||||
return false;
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
fn connnection_down(&self) -> bool {
|
||||
let output = Command::new("networksetup")
|
||||
.args(&["-setairportpower", &self.interface, "off"])
|
||||
.output();
|
||||
|
||||
if let Err(_) = output {
|
||||
return false;
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
mod providers;
|
||||
|
||||
// pub use self::providers::Machine;
|
||||
use platforms::{WifiError, WifiInterface};
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum WifiHotspotError {
|
||||
CreationFailed,
|
||||
Other { kind: WifiError },
|
||||
}
|
||||
|
||||
/// Adds support for wifi hotspot functionality
|
||||
pub trait WifiHotspot: fmt::Debug + WifiInterface {
|
||||
/// Creates wifi hotspot service for host machine. This only creats the wifi network,
|
||||
/// and isn't responsible for initiating the serving of the wifi network process.
|
||||
/// To begin serving the hotspot, use ```start_hotspot()```.
|
||||
fn create_hotspot(ssid: &str, password: &str) -> Result<bool, WifiHotspotError>;
|
||||
|
||||
/// Start serving publicly an already created wifi hotspot.
|
||||
fn start_hotspot() -> Result<bool, WifiHotspotError>;
|
||||
|
||||
/// Stop serving a wifi network.
|
||||
///
|
||||
/// > All users connected will automatically be disconnected.
|
||||
fn stop_hotspot() -> Result<bool, WifiHotspotError>;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
use hotspot::{WifiHotspot, WifiHotspotError};
|
||||
use platforms::Linux;
|
||||
|
||||
// #[derive(Debug)]
|
||||
// pub struct Linux {
|
||||
// pub name: String,
|
||||
// interface: String,
|
||||
// }
|
||||
|
||||
// impl Linux {
|
||||
// pub fn new(name: &str, interface: Option<&str>) -> Self {
|
||||
// Linux {
|
||||
// name: name.into(),
|
||||
// interface: interface.unwrap_or("wlan0").into(),
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
impl WifiHotspot for Linux {
|
||||
fn create_hotspot(ssid: &str, password: &str) -> Result<bool, WifiHotspotError> {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
fn start_hotspot() -> Result<bool, WifiHotspotError> {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
fn stop_hotspot() -> Result<bool, WifiHotspotError> {
|
||||
unimplemented!();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// #[cfg(target_os = "windows")]
|
||||
// mod windows;
|
||||
// #[cfg(target_os = "windows")]
|
||||
// pub use self::windows::Windows as Machine;
|
||||
|
||||
// #[cfg(target_os = "linux")]
|
||||
// mod linux;
|
||||
// #[cfg(target_os = "linux")]
|
||||
// pub use self::linux as Machine;
|
||||
|
||||
// #[cfg(target_os = "osx")]
|
||||
// mod osx;
|
||||
// #[cfg(target_os = "osx")]
|
||||
// pub use self::osx::OSX as Machine;
|
||||
@@ -0,0 +1,18 @@
|
||||
use connectivity::{Network, WifiConnectionError};
|
||||
use std::process::Command;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct OSX {
|
||||
pub name: String,
|
||||
interface: String,
|
||||
}
|
||||
|
||||
impl OSX {
|
||||
#[cfg(target_os = "windows")]
|
||||
pub fn new(name: &str, interface: Option<&str>) -> Self {
|
||||
OSX {
|
||||
name: name.into(),
|
||||
interface: interface.unwrap_or("en0").into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
use connectivity::handlers::NetworkXmlProfileHandler;
|
||||
use connectivity::{
|
||||
Network, WifiConnectionError, WifiError, WifiHotspot, WifiHotspotError, WifiInterface,
|
||||
};
|
||||
use platforms::Windows;
|
||||
use std::process::Command;
|
||||
|
||||
// #[derive(Debug)]
|
||||
// pub(crate) struct Windows {
|
||||
// name: String,
|
||||
// }
|
||||
|
||||
impl WifiHotspot for Windows {
|
||||
fn create_hotspot(ssid: &str, password: &str) -> Result<bool, WifiHotspotError> {
|
||||
let output = Command::new("netsh")
|
||||
.args(&[
|
||||
"wlan",
|
||||
"set",
|
||||
"hostednetwork",
|
||||
"mode = allow",
|
||||
&format!("ssid={}", ssid),
|
||||
&format!("key={}", password),
|
||||
])
|
||||
.output()
|
||||
.map_err(|err| WifiHotspotError::CreationFailed)?;
|
||||
|
||||
Ok(String::from_utf8_lossy(&output.stdout)
|
||||
.as_ref()
|
||||
.contains("successfully changed"))
|
||||
}
|
||||
|
||||
fn start_hotspot() -> Result<bool, WifiHotspotError> {
|
||||
if !Self::is_wifi_enabled() {
|
||||
if !Self::turn_on().map_err(|err| WifiHotspotError::Other { kind: err })? {
|
||||
return Err(WifiHotspotError::Other {
|
||||
kind: WifiError::InterfaceFailedToOn,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
let output = Command::new("netsh")
|
||||
.args(&["wlan", "start", "hostednetwork"])
|
||||
.output()
|
||||
.map_err(|err| WifiHotspotError::CreationFailed)?;
|
||||
|
||||
Ok(String::from_utf8_lossy(&output.stdout)
|
||||
.as_ref()
|
||||
.contains("hosted network started"))
|
||||
}
|
||||
|
||||
fn stop_hotspot() -> Result<bool, WifiHotspotError> {
|
||||
let output = Command::new("netsh")
|
||||
.args(&["wlan", "stop", "hostednetwork"])
|
||||
.output()
|
||||
.map_err(|err| WifiHotspotError::CreationFailed)?;
|
||||
|
||||
return Ok(String::from_utf8_lossy(&output.stdout)
|
||||
.as_ref()
|
||||
.contains("hosted network stopped"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
use platforms::{WifiError, WifiInterface};
|
||||
use std::collections::HashMap;
|
||||
use std::process::Command;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct Connection {
|
||||
pub ssid: String,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct Linux {
|
||||
hotspot: Option<HashMap<String, String>>,
|
||||
pub connection: Option<Connection>,
|
||||
pub interface: String,
|
||||
}
|
||||
|
||||
impl Linux {
|
||||
pub fn new(name: &str, interface: Option<&str>) -> Self {
|
||||
Linux {
|
||||
hotspot: None,
|
||||
connection: None,
|
||||
interface: String::from("wlan0"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl WifiInterface for Linux {
|
||||
fn is_wifi_enabled() -> bool {
|
||||
let output = Command::new("nmcli").args(&["radio", "wifi"]).output();
|
||||
|
||||
if let Err(_) = output {
|
||||
return false;
|
||||
}
|
||||
|
||||
String::from_utf8_lossy(&output.unwrap().stdout)
|
||||
.replace(" ", "")
|
||||
.replace("\n", "")
|
||||
.contains("enabled")
|
||||
}
|
||||
|
||||
fn turn_on() -> Result<bool, WifiError> {
|
||||
let _output = Command::new("nmcli")
|
||||
.args(&["radio", "wifi", "on"])
|
||||
.output()
|
||||
.map_err(|err| WifiError::IoError(err))?;
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
fn turn_off() -> Result<bool, WifiError> {
|
||||
let _output = Command::new("nmcli")
|
||||
.args(&["radio", "wifi", "off"])
|
||||
.output()
|
||||
.map_err(|err| WifiError::IoError(err))?;
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
#[cfg(target_os = "linux")]
|
||||
mod linux;
|
||||
|
||||
#[cfg(target_os = "osx")]
|
||||
mod osx;
|
||||
|
||||
// #[cfg(target_os = "windows")]
|
||||
mod windows;
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
pub(crate) use self::linux::{Connection, Linux};
|
||||
|
||||
#[cfg(target_os = "osx")]
|
||||
pub use self::osx::OSX;
|
||||
|
||||
// #[cfg(target_os = "windows")]
|
||||
pub use self::windows::Windows;
|
||||
|
||||
use std::{fmt, io};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum WifiError {
|
||||
OsNotSupported,
|
||||
InterfaceDisabled,
|
||||
InterfaceFailedToOn,
|
||||
IoError(io::Error),
|
||||
}
|
||||
|
||||
pub trait WifiInterface: fmt::Debug {
|
||||
/// Checks if the wifi interface on host machine is enables.
|
||||
fn is_wifi_enabled() -> bool;
|
||||
|
||||
/// Turns on the wifi interface of host machine.
|
||||
fn turn_on() -> Result<bool, WifiError>;
|
||||
|
||||
// Turns off the wifi interface of host machine.
|
||||
fn turn_off() -> Result<bool, WifiError>;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Osx {
|
||||
hotspot: Option<HashMap>,
|
||||
connectivity: Option<HashMap>,
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
use std::collections::HashMap;
|
||||
use
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Windows {
|
||||
hotspot: Option<HashMap>,
|
||||
connectivity: Option<HashMap>,
|
||||
}
|
||||
|
||||
impl WifiInterface for Windows {
|
||||
fn is_wifi_enabled() -> bool {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn turn_on() -> Result<bool, WifiError> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn turn_off() -> Result<bool, WifiError> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user