use std::fmt;
use serde::Serialize;
use crate::generic::Error;
use crate::read::{Reader, Values};
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
pub struct Details {
pub security: Security,
pub radio: Radio,
pub wps: Wps,
pub night: Night,
pub statistics: Statistics,
pub hints: Vec<Hint>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
pub struct Security {
pub beacon_type: Option<String>,
pub encryption: Option<String>,
pub possible_beacon_types: Option<Vec<String>>,
pub basic_encryption: Option<String>,
pub basic_authentication: Option<String>,
pub hidden: Option<bool>,
pub mac_filter: Option<bool>,
pub user_isolation: Option<bool>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
pub struct Radio {
pub channel: Option<u32>,
pub auto_channel: Option<bool>,
pub possible_channels: Option<Vec<u32>>,
pub bssid: Option<String>,
pub max_bit_rate: Option<String>,
pub iptv_optimized: Option<bool>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
pub struct Wps {
pub enabled: Option<bool>,
pub mode: Option<String>,
pub status: Option<String>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
pub struct Night {
pub schedule: Option<String>,
pub no_forced_off: Option<bool>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
pub struct Statistics {
pub packets_sent: Option<u64>,
pub packets_received: Option<u64>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Hint {
OpenNetwork,
Wep,
TkipAllowed,
WpsEnabled,
HiddenSsid,
}
impl fmt::Display for Hint {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::OpenNetwork => "open network",
Self::Wep => "WEP",
Self::TkipAllowed => "TKIP allowed",
Self::WpsEnabled => "WPS enabled",
Self::HiddenSsid => "hidden SSID",
})
}
}
pub(super) async fn read(
reader: &mut Reader,
service: &str,
info: &Values,
ext: Option<&Values>,
) -> Result<Details, Error> {
let channel = reader.try_read(service, "GetChannelInfo", &[]).await?;
let advertised = reader
.try_read(service, "GetBeaconAdvertisement", &[])
.await?
.and_then(|v| v.flag("NewBeaconAdvertisementEnabled"));
let wps = reader.try_read(service, "X_AVM-DE_GetWPSInfo", &[]).await?;
let night = reader
.try_read(service, "X_AVM-DE_GetNightControl", &[])
.await?;
let mut statistics = reader.try_read(service, "GetStatistics", &[]).await?;
if statistics.is_none() {
statistics = reader.try_read(service, "GetPacketStatistics", &[]).await?;
}
let iptv = reader
.try_read(service, "X_AVM-DE_GetIPTVOptimized", &[])
.await?
.and_then(|v| v.flag("NewX_AVM-DE_IPTVoptimize"));
let beacon_type = info.text("NewBeaconType");
let basic_encryption = info.text("NewBasicEncryptionModes");
let security = Security {
encryption: beacon_type
.as_deref()
.and_then(|b| encryption(b, basic_encryption.as_deref()))
.map(str::to_owned),
possible_beacon_types: info
.text("NewX_AVM-DE_PossibleBeaconTypes")
.map(|t| list(&t)),
basic_authentication: info.text("NewBasicAuthenticationMode"),
hidden: advertised.map(|a| !a),
mac_filter: info.flag("NewMACAddressControlEnabled"),
user_isolation: ext.and_then(|e| e.flag("NewX_AVM-DE_UserIsolation")),
beacon_type,
basic_encryption,
};
let radio = Radio {
channel: channel
.as_ref()
.and_then(|c| c.number("NewChannel"))
.or_else(|| info.number("NewChannel")),
auto_channel: channel
.as_ref()
.and_then(|c| c.flag("NewX_AVM-DE_AutoChannelEnabled")),
possible_channels: channel
.as_ref()
.and_then(|c| c.text("NewPossibleChannels"))
.map(|c| list(&c).iter().filter_map(|n| n.parse().ok()).collect()),
bssid: info.text("NewBSSID"),
max_bit_rate: info.text("NewMaxBitRate"),
iptv_optimized: iptv,
};
let wps_status = wps.as_ref().and_then(|w| w.text("NewX_AVM-DE_WPSStatus"));
let wps = Wps {
enabled: wps_status.as_deref().and_then(wps_enabled),
mode: wps.as_ref().and_then(|w| w.text("NewX_AVM-DE_WPSMode")),
status: wps_status,
};
let night = Night {
schedule: night.as_ref().and_then(|n| n.text("NewNightControl")),
no_forced_off: night
.as_ref()
.and_then(|n| n.flag("NewNightTimeControlNoForcedOff")),
};
let statistics = Statistics {
packets_sent: statistics
.as_ref()
.and_then(|s| s.number("NewTotalPacketsSent")),
packets_received: statistics
.as_ref()
.and_then(|s| s.number("NewTotalPacketsReceived")),
};
let mut details = Details {
security,
radio,
wps,
night,
statistics,
hints: Vec::new(),
};
details.hints = hints(&details);
Ok(details)
}
fn encryption(beacon_type: &str, basic: Option<&str>) -> Option<&'static str> {
Some(match beacon_type {
"Basic" if basic.is_some_and(|b| b.contains("WEP")) => "WEP",
"None" | "Basic" => "none",
"WPA" => "WPA, TKIP",
"11i" => "WPA2, AES",
"WPAand11i" => "WPA/WPA2 mixed, TKIP and AES",
"11iandWPA3" => "WPA2/WPA3 mixed, AES",
"WPA3" => "WPA3, AES",
"OWE" => "Enhanced Open (OWE)",
"OWETrans" => "Enhanced Open (OWE), transition mode",
_ => return None,
})
}
fn wps_enabled(status: &str) -> Option<bool> {
match status {
"off" => Some(false),
"inactive" | "active" | "success" => Some(true),
_ => None,
}
}
fn hints(details: &Details) -> Vec<Hint> {
let security = &details.security;
let mut hints = Vec::new();
match security.encryption.as_deref() {
Some("none") => hints.push(Hint::OpenNetwork),
Some("WEP") => hints.push(Hint::Wep),
_ => {}
}
if matches!(security.beacon_type.as_deref(), Some("WPA" | "WPAand11i")) {
hints.push(Hint::TkipAllowed);
}
if details.wps.enabled == Some(true) {
hints.push(Hint::WpsEnabled);
}
if security.hidden == Some(true) {
hints.push(Hint::HiddenSsid);
}
hints
}
fn list(text: &str) -> Vec<String> {
text.split(',')
.map(str::trim)
.filter(|i| !i.is_empty())
.map(str::to_owned)
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn names_the_encryption() {
assert_eq!(encryption("None", None), Some("none"));
assert_eq!(encryption("Basic", Some("WEPEncryption")), Some("WEP"));
assert_eq!(encryption("Basic", Some("None")), Some("none"));
assert_eq!(encryption("WPA", None), Some("WPA, TKIP"));
assert_eq!(encryption("11i", None), Some("WPA2, AES"));
assert_eq!(
encryption("WPAand11i", None),
Some("WPA/WPA2 mixed, TKIP and AES")
);
assert_eq!(encryption("11iandWPA3", None), Some("WPA2/WPA3 mixed, AES"));
assert_eq!(encryption("WPA3", None), Some("WPA3, AES"));
assert_eq!(encryption("OWE", None), Some("Enhanced Open (OWE)"));
assert_eq!(
encryption("OWETrans", None),
Some("Enhanced Open (OWE), transition mode")
);
assert_eq!(encryption("Other", None), None);
}
#[test]
fn tells_whether_wps_is_on() {
assert_eq!(wps_enabled("off"), Some(false));
assert_eq!(wps_enabled("inactive"), Some(true));
assert_eq!(wps_enabled("active"), Some(true));
assert_eq!(wps_enabled("success"), Some(true));
assert_eq!(wps_enabled("err_common"), None);
}
#[test]
fn finds_weak_settings() {
let details = |beacon: &str, basic: Option<&str>, wps: Option<bool>, hidden| Details {
security: Security {
beacon_type: Some(beacon.to_owned()),
encryption: encryption(beacon, basic).map(str::to_owned),
hidden,
..Security::default()
},
wps: Wps {
enabled: wps,
..Wps::default()
},
..Details::default()
};
assert_eq!(
hints(&details("11iandWPA3", None, Some(false), Some(false))),
[]
);
assert_eq!(hints(&details("11i", None, None, None)), []);
assert_eq!(
hints(&details("None", None, Some(true), Some(true))),
[Hint::OpenNetwork, Hint::WpsEnabled, Hint::HiddenSsid]
);
assert_eq!(
hints(&details("Basic", Some("WEPEncryption"), None, None)),
[Hint::Wep]
);
assert_eq!(
hints(&details("WPA", None, None, None)),
[Hint::TkipAllowed]
);
assert_eq!(
hints(&details("WPAand11i", None, None, None)),
[Hint::TkipAllowed]
);
let text: Vec<String> = [
Hint::OpenNetwork,
Hint::Wep,
Hint::TkipAllowed,
Hint::WpsEnabled,
Hint::HiddenSsid,
]
.iter()
.map(ToString::to_string)
.collect();
assert_eq!(
text,
[
"open network",
"WEP",
"TKIP allowed",
"WPS enabled",
"hidden SSID"
]
);
}
#[test]
fn splits_lists() {
assert_eq!(list("None, 11i,,WPA3"), ["None", "11i", "WPA3"]);
assert_eq!(list(""), [] as [String; 0]);
}
}