use std::collections::BTreeMap;
use serde::Serialize;
use serde_json::Value;
use crate::names::{Sensitivity, sensitivity};
use crate::redact::{self, HIDDEN};
use crate::session::{Error, Session};
pub const PREFIX: &str = "/api/v0/generic/";
pub const MODULES: &[(&str, &str, bool)] = &[
(
"box",
"name, LEDs, night time, UPnP and DHCP lease times",
true,
),
(
"connections",
"internet connections, with IPv4 and IPv6 DNS",
true,
),
("dnscfg", "DNS servers and DNS over TLS", true),
(
"dns_excepted_domains",
"domains excepted from DNS rebind protection",
true,
),
(
"eth_ports",
"LAN ports, their speed and energy saving",
true,
),
("forwardrules", "whether a host is exposed", true),
("ipv6", "IPv6 of the home network, MTU and DHCPv6", true),
("route", "static IPv4 routes", true),
(
"sync_groups",
"IPv4 and IPv6 modes of the internet connection",
true,
),
("time", "time server and time zone", true),
("dect", "DECT and its handsets", false),
(
"filter_profile",
"access profiles of the parental controls",
false,
),
(
"inetstat",
"online counter: traffic by day, week and month",
false,
),
("landevice", "hosts of the home network", false),
("mobiled", "mobile modem", false),
("plc", "powerline energy saving", false),
("tam", "answering machines", false),
("user", "users of the parental controls", false),
("vpn", "VPN connections", false),
("wlan", "wireless networks and their clients", false),
];
#[derive(Debug, Default, Serialize)]
pub struct Settings {
pub modules: BTreeMap<String, Value>,
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
pub unavailable: BTreeMap<String, String>,
}
pub async fn settings(session: &Session, modules: &[String]) -> Settings {
let defaults: Vec<String> = MODULES
.iter()
.filter(|(_, _, default)| *default)
.map(|(name, _, _)| (*name).to_owned())
.collect();
let modules = if modules.is_empty() {
&defaults
} else {
modules
};
let mut settings = Settings::default();
for name in modules {
match module(session, name).await {
Ok(value) => {
settings.modules.insert(name.clone(), value);
}
Err(e) => {
settings.unavailable.insert(name.clone(), e.to_string());
}
}
}
settings
}
pub async fn module(session: &Session, name: &str) -> Result<Value, Error> {
let valid = !name.is_empty()
&& name
.chars()
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_');
if !valid {
return Err(Error::Invalid {
what: "the module name",
reason: format!("{name:?} is not a module"),
});
}
parse(&session.get(&format!("{PREFIX}{name}")).await?)
}
pub fn parse(json: &str) -> Result<Value, Error> {
let value = serde_json::from_str(json).map_err(|e| Error::Invalid {
what: "the settings API",
reason: e.to_string(),
})?;
Ok(hide(value))
}
pub fn hide(value: Value) -> Value {
match value {
Value::Object(object) => Value::Object(
object
.into_iter()
.map(|(name, value)| {
let value = if secret(&name) {
Value::String(HIDDEN.to_owned())
} else {
hide(value)
};
(name, value)
})
.collect(),
),
Value::Array(items) => Value::Array(items.into_iter().map(hide).collect()),
Value::String(text) if PRIVATE.iter().any(|p| text.contains(p)) => {
Value::String(HIDDEN.to_owned())
}
Value::String(text) => Value::String(redact::value("", &text)),
other => other,
}
}
const SECRET_PARTS: &[&str] = &["psk", "passw", "secret", "privatekey", "private_key"];
const PRIVATE: &[&str] = &["PrivateKey", "PRIVATE KEY"];
fn secret(name: &str) -> bool {
let lower = name.to_ascii_lowercase();
sensitivity(name) != Sensitivity::Plain
|| SECRET_PARTS.iter().any(|part| lower.contains(part))
|| (lower.starts_with("wg_") && lower.ends_with("config"))
}
#[cfg(test)]
mod tests {
use serde_json::json;
use super::*;
#[test]
fn hides_secrets_at_any_depth() {
let value = json!({
"name": "box",
"password": "secret",
"accounts": [{"username": "user", "psk": {"value": "key"}, "pin_enabled": "1"}],
"url": "/x.lua?sid=0123",
"mtu": 1492,
"pskvalue": "wlan key",
"guest_pskvalue": "guest key",
"wg_slave_config": "[Interface]",
"dns_domains": "[Interface]\nPrivateKey = abc",
"ip4_is_private": "1",
});
assert_eq!(
hide(value),
json!({
"name": "box",
"password": "***",
"accounts": [{"username": "***", "psk": "***", "pin_enabled": "1"}],
"url": "/x.lua?sid=***",
"mtu": 1492,
"pskvalue": "***",
"guest_pskvalue": "***",
"wg_slave_config": "***",
"dns_domains": "***",
"ip4_is_private": "1",
})
);
}
#[test]
fn rejects_invalid_json() {
assert!(matches!(parse("<html>"), Err(Error::Invalid { .. })));
}
}