Chore(Stage): Some Refactoring
This commit is contained in:
@@ -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"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user