diff --git a/src/connectivity/providers/windows.rs b/src/connectivity/providers/windows.rs index 783b5a4..af18d04 100644 --- a/src/connectivity/providers/windows.rs +++ b/src/connectivity/providers/windows.rs @@ -13,37 +13,44 @@ pub(crate) struct Windows { impl Windows { pub fn new(name: &str) -> Result { - let profile_file_name = format!("netsh wlan add profile filename=\"{}\"", name); - - Command::new("cmd") - .args(&["/C", &profile_file_name[..]]) - .output()?; - Ok(Windows { name: String::from(name), output_xml_path: OUTPUT_XML_FILE_PATH.into(), }) } + + pub(crate) fn add_profile(&self, password: &str) -> bool { + let mut handler = NetworkXmlProfileHandler::new(); + handler.content = handler + .content + .replace("{SSID}", &self.name) + .replace("{password}", password); + + // Write details to new xml file + if let Err(_) = handler.to_file(&self.output_xml_path).map_err(|_err| false) { + return false; + } + + // Add the network profile + if let Err(_) = Command::new("netsh") + .args(&["wlan", "add", "profile", &format!("filename={}", self.output_xml_path)]) + .output() + { + return false; + } + + true + } } impl Network for Windows { fn connect(&self, password: &str) -> bool { - { - let mut handler = NetworkXmlProfileHandler::new(); - - handler.content = handler - .content - .replace("{SSID}", &self.name) - .replace("{password}", password); - - // Write details to new xml file - if let Err(err) = handler.to_file(&self.output_xml_path).map_err(|_err| false) { - return err; - } + if self.add_profile(password) == false { + return false; } - + let output = Command::new("netsh") - .args(&["wlan", "connect", &format!("name = \"{}\"", self.name)]) + .args(&["wlan", "connect", &format!("name={}", self.name)]) .output(); match output { diff --git a/src/connectivity/stubs/windows_wifi_profile.rs b/src/connectivity/stubs/windows_wifi_profile.rs index f39eb15..196420e 100644 --- a/src/connectivity/stubs/windows_wifi_profile.rs +++ b/src/connectivity/stubs/windows_wifi_profile.rs @@ -1,8 +1,6 @@ pub fn get_wifi_profile() -> String { - String::from( - r#" - - + String::from(r#" + {SSID} @@ -25,11 +23,9 @@ pub fn get_wifi_profile() -> String { - + false - - "#, +"#, ) }