Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions plugins/topbar/src/fly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,21 +609,26 @@ fn esc(s: &str) -> String {
s.replace('&', "&amp;").replace('<', "&lt;").replace('>', "&gt;").replace('"', "&quot;")
}

/// Saves a profile for a new network and connects. shortcut: only open and WPA/WPA2/WPA3 personal (password) networks; enterprise ones go to Windows settings.
unsafe fn wifi_join(n: &Net, pass: &str) {
let (auth, key) = match n.auth {
1 => ("open", String::new()),
4 => ("WPAPSK", pass.into()),
7 => ("WPA2PSK", pass.into()),
_ => ("WPA3SAE", pass.into()),
/// Builds the WLAN profile XML for `wifi_join`. The Windows WLAN schema requires `passPhrase` (case-sensitive).
fn wifi_profile_xml(ssid: &str, auth: i32, pass: &str) -> String {
let auth = match auth {
1 => "open",
4 => "WPAPSK",
7 => "WPA2PSK",
_ => "WPA3SAE",
};
let sec = if auth == "open" {
"<authEncryption><authentication>open</authentication><encryption>none</encryption><useOneX>false</useOneX></authEncryption>".to_string()
} else {
format!("<authEncryption><authentication>{auth}</authentication><encryption>AES</encryption><useOneX>false</useOneX></authEncryption><sharedKey><keyType>passphrase</keyType><protected>false</protected><keyMaterial>{}</keyMaterial></sharedKey>", esc(&key))
format!("<authEncryption><authentication>{auth}</authentication><encryption>AES</encryption><useOneX>false</useOneX></authEncryption><sharedKey><keyType>passPhrase</keyType><protected>false</protected><keyMaterial>{}</keyMaterial></sharedKey>", esc(pass))
};
let name = esc(&n.ssid);
let xml = format!("<?xml version=\"1.0\"?><WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\"><name>{name}</name><SSIDConfig><SSID><name>{name}</name></SSID></SSIDConfig><connectionType>ESS</connectionType><connectionMode>auto</connectionMode><MSM><security>{sec}</security></MSM></WLANProfile>");
let name = esc(ssid);
format!("<?xml version=\"1.0\"?><WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\"><name>{name}</name><SSIDConfig><SSID><name>{name}</name></SSID></SSIDConfig><connectionType>ESS</connectionType><connectionMode>auto</connectionMode><MSM><security>{sec}</security></MSM></WLANProfile>")
}

/// Saves a profile for a new network and connects. shortcut: only open and WPA/WPA2/WPA3 personal (password) networks; enterprise ones go to Windows settings.
unsafe fn wifi_join(n: &Net, pass: &str) {
let xml = wifi_profile_xml(&n.ssid, n.auth, pass);
let x = wide(&xml);
let mut reason = 0u32;
let ok = with_iface(&n.g, |h, g| WlanSetProfile(h, g, 0, x.as_ptr(), null(), 1, null_mut(), &mut reason) == 0).unwrap_or(false);
Expand Down Expand Up @@ -2263,7 +2268,7 @@ mod tile_glide_tests {

#[cfg(test)]
mod tests {
use super::{flip_layout, score, init, is_open, toggle, close_now, Kind, FLY, FH, GetModuleHandleW, SetProcessDPIAware, null, null_mut, DestroyWindow, IsWindow};
use super::{flip_layout, score, wifi_profile_xml, init, is_open, toggle, close_now, Kind, FLY, FH, GetModuleHandleW, SetProcessDPIAware, null, null_mut, DestroyWindow, IsWindow};

#[test]
fn layout_flip_both_ways() {
Expand Down Expand Up @@ -2308,6 +2313,21 @@ mod tests {
assert_eq!(score("calculator", ""), None);
assert_eq!(score("v", "v"), Some(0)); // one-letter initials do not count, the prefix does
}

#[test]
fn wifi_profile_uses_passphrase_key_type() {
for (auth, expected) in [(4, "WPAPSK"), (7, "WPA2PSK"), (9, "WPA3SAE")] {
let xml = wifi_profile_xml("a<b", auth, "p&ss\"1234");
assert!(xml.contains(&format!("<authentication>{expected}</authentication>")));
assert!(xml.contains("<keyType>passPhrase</keyType>"));
assert!(!xml.contains("<keyType>passphrase</keyType>"));
assert!(xml.contains("<name>a&lt;b</name>"));
assert!(xml.contains("<keyMaterial>p&amp;ss&quot;1234</keyMaterial>"));
}
let open = wifi_profile_xml("Cafe", 1, "");
assert!(open.contains("<authentication>open</authentication><encryption>none</encryption>"));
assert!(!open.contains("sharedKey"));
}
}

#[cfg(test)]
Expand Down