| 1 |
use anyhow::{Ok, Result}; |
| 2 |
use nmrs::{ConnectionError, Device, Network, SavedConnection, WifiSecurity}; |
| 3 |
|
| 4 |
use crate::app::App; |
| 5 |
|
| 6 |
#[derive()] |
| 7 |
pub struct NetworkManager { |
| 8 |
pub network_manager: nmrs::NetworkManager, |
| 9 |
pub current_connection: Option<Network>, |
| 10 |
pub devices: Vec<Device>, |
| 11 |
pub enabled: bool, |
| 12 |
} |
| 13 |
|
| 14 |
impl NetworkManager { |
| 15 |
pub async fn new() -> anyhow::Result<Self> { |
| 16 |
let network_manager = nmrs::NetworkManager::new().await?; |
| 17 |
let current_connection = network_manager.current_network().await?; |
| 18 |
|
| 19 |
let devices = network_manager.list_devices().await?; |
| 20 |
|
| 21 |
let enabled = network_manager.wifi_state().await?.enabled; |
| 22 |
|
| 23 |
Ok(Self { |
| 24 |
network_manager, |
| 25 |
current_connection, |
| 26 |
devices, |
| 27 |
enabled, |
| 28 |
}) |
| 29 |
} |
| 30 |
|
| 31 |
pub async fn get_devices(&mut self) -> Result<Vec<Device>> { |
| 32 |
let devices = self.network_manager.list_devices().await?; |
| 33 |
let wireless_devices: Vec<Device> = devices |
| 34 |
.into_iter() |
| 35 |
.filter(|dev| dev.is_wireless()) |
| 36 |
.collect(); |
| 37 |
if wireless_devices.is_empty() { |
| 38 |
anyhow::bail!("No wireless device found") |
| 39 |
} else { |
| 40 |
Ok(wireless_devices) |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
pub async fn get_wifi_scan(&mut self) -> Result<Vec<Network>> { |
| 45 |
self.network_manager.scan_networks(None).await?; |
| 46 |
self.current_connection = self.network_manager.current_network().await?; |
| 47 |
|
| 48 |
let mut networks = self.network_manager.list_networks(None).await?; |
| 49 |
networks.sort_by(|a, b| b.strength.cmp(&a.strength)); |
| 50 |
Ok(networks) |
| 51 |
} |
| 52 |
|
| 53 |
pub async fn scan_networks(&mut self) -> anyhow::Result<(Vec<Network>, Vec<Network>)> { |
| 54 |
let scan_list = self.get_wifi_scan().await.unwrap_or_default(); |
| 55 |
|
| 56 |
let mut known_final = Vec::new(); |
| 57 |
let mut new_final = Vec::new(); |
| 58 |
|
| 59 |
for net in &scan_list { |
| 60 |
if self.has_saved_connection(&net.ssid).await? { |
| 61 |
known_final.push(net.clone()); |
| 62 |
} else { |
| 63 |
new_final.push(net.clone()); |
| 64 |
} |
| 65 |
} |
| 66 |
Ok((known_final, new_final)) |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
pub async fn forget(&mut self, ssid: &str) -> anyhow::Result<()> { |
| 77 |
self.network_manager.forget(ssid).await?; |
| 78 |
Ok(()) |
| 79 |
} |
| 80 |
|
| 81 |
pub async fn connect( |
| 82 |
&mut self, |
| 83 |
ssid: &str, |
| 84 |
interface: Option<&str>, |
| 85 |
credentials: WifiSecurity, |
| 86 |
) -> Result<()> { |
| 87 |
match self |
| 88 |
.network_manager |
| 89 |
.connect(ssid, interface, credentials) |
| 90 |
.await |
| 91 |
{ |
| 92 |
std::result::Result::Ok(_) => { |
| 93 |
println!("Connected!"); |
| 94 |
Ok(()) |
| 95 |
} |
| 96 |
Err(ConnectionError::NotFound) => { |
| 97 |
warn!("Network not visible — is it in range?"); |
| 98 |
Ok(()) |
| 99 |
} |
| 100 |
Err(ConnectionError::AuthFailed) => { |
| 101 |
warn!("Wrong password"); |
| 102 |
Ok(()) |
| 103 |
} |
| 104 |
Err(ConnectionError::Timeout) => { |
| 105 |
error!("Connection timed out — try increasing the timeout"); |
| 106 |
Ok(()) |
| 107 |
} |
| 108 |
Err(ConnectionError::DhcpFailed) => { |
| 109 |
eprintln!("Failed to get an IP address"); |
| 110 |
Ok(()) |
| 111 |
} |
| 112 |
Err(e) => { |
| 113 |
eprintln!("Connection failed: {}", e); |
| 114 |
Ok(()) |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
pub async fn is_connected(&mut self, ssid: &String) -> Result<bool> { |
| 120 |
Ok(self.network_manager.is_connected(ssid).await?) |
| 121 |
} |
| 122 |
|
| 123 |
pub fn is_connected_cached(&mut self, ssid: &String) -> bool { |
| 124 |
self.current_connection.is_some() && self.current_connection.clone().unwrap().ssid == *ssid |
| 125 |
} |
| 126 |
|
| 127 |
pub async fn has_saved_connection(&mut self, ssid: &str) -> Result<bool> { |
| 128 |
Ok(self.network_manager.has_saved_connection(ssid).await?) |
| 129 |
} |
| 130 |
} |