2026-01-21 15:36:27 -05:00
|
|
|
use config::{Config, ConfigError, File};
|
2026-01-23 14:35:28 -05:00
|
|
|
use duration_string::DurationString;
|
2026-02-18 11:23:20 -05:00
|
|
|
use once_cell::sync::OnceCell;
|
2026-01-21 15:36:27 -05:00
|
|
|
use serde::Deserialize;
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------
|
|
|
|
|
// Configuration structure
|
2026-01-23 11:00:24 -05:00
|
|
|
|
2026-06-01 19:00:56 -04:00
|
|
|
fn default_true() -> bool {
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-27 13:13:02 -05:00
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
|
pub struct Database {
|
|
|
|
|
pub path: String,
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-23 11:00:24 -05:00
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
|
pub struct Networking {
|
2026-05-29 07:35:25 -04:00
|
|
|
pub interface: Option<String>,
|
2026-01-23 11:00:24 -05:00
|
|
|
}
|
|
|
|
|
|
2026-01-21 15:36:27 -05:00
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
|
pub struct Log {
|
|
|
|
|
pub level: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
2026-05-28 17:57:26 -04:00
|
|
|
pub struct ArpScanner {
|
2026-06-01 19:00:56 -04:00
|
|
|
#[serde(default = "default_true")]
|
|
|
|
|
pub enabled: bool,
|
2026-01-23 14:35:28 -05:00
|
|
|
pub wait_between_scans: DurationString,
|
2026-05-28 17:57:26 -04:00
|
|
|
pub sender_timeout: DurationString,
|
|
|
|
|
pub scan_duration: DurationString,
|
2026-01-21 15:36:27 -05:00
|
|
|
}
|
|
|
|
|
|
2026-05-29 09:37:33 -04:00
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
|
pub struct MdnsScanner {
|
2026-06-01 19:00:56 -04:00
|
|
|
#[serde(default = "default_true")]
|
|
|
|
|
pub enabled: bool,
|
2026-05-29 09:37:33 -04:00
|
|
|
pub probe_timeout: DurationString,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for MdnsScanner {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
MdnsScanner {
|
2026-06-01 19:00:56 -04:00
|
|
|
enabled: true,
|
2026-05-29 09:37:33 -04:00
|
|
|
probe_timeout: DurationString::try_from("2s".to_string()).unwrap(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-01 17:55:33 -04:00
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
|
pub struct SsdpScanner {
|
2026-06-01 19:00:56 -04:00
|
|
|
#[serde(default = "default_true")]
|
|
|
|
|
pub enabled: bool,
|
2026-06-01 17:55:33 -04:00
|
|
|
pub probe_timeout: DurationString,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for SsdpScanner {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
SsdpScanner {
|
2026-06-01 19:00:56 -04:00
|
|
|
enabled: true,
|
2026-06-01 17:55:33 -04:00
|
|
|
probe_timeout: DurationString::try_from("2s".to_string()).unwrap(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-01 19:00:56 -04:00
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
|
pub struct DhcpScanner {
|
|
|
|
|
#[serde(default = "default_true")]
|
|
|
|
|
pub enabled: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for DhcpScanner {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
DhcpScanner { enabled: true }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-23 10:25:45 -05:00
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
|
pub struct Pushover {
|
|
|
|
|
pub token: String,
|
|
|
|
|
pub user_key: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
|
pub struct Notifications {
|
|
|
|
|
pub method: String,
|
|
|
|
|
pub pushover: Pushover,
|
2026-01-23 15:08:58 -05:00
|
|
|
pub notify_when_not_seen_for: DurationString,
|
2026-01-23 10:25:45 -05:00
|
|
|
}
|
|
|
|
|
|
2026-02-26 16:30:51 -05:00
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
|
pub struct WebServer {
|
|
|
|
|
pub ip_address: String,
|
|
|
|
|
pub port: u16,
|
|
|
|
|
pub api_key: String,
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-28 09:00:36 -04:00
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
|
pub struct Retention {
|
|
|
|
|
pub window: DurationString,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for Retention {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Retention {
|
|
|
|
|
window: DurationString::try_from("365d".to_string()).unwrap(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-21 15:36:27 -05:00
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
|
pub struct Settings {
|
2026-01-27 13:13:02 -05:00
|
|
|
pub database: Database,
|
2026-01-23 11:00:24 -05:00
|
|
|
pub networking: Networking,
|
2026-01-21 15:36:27 -05:00
|
|
|
pub log: Log,
|
2026-05-28 17:57:26 -04:00
|
|
|
pub arp_scanner: ArpScanner,
|
2026-01-23 10:25:45 -05:00
|
|
|
pub notifications: Notifications,
|
2026-02-26 16:30:51 -05:00
|
|
|
pub web_server: WebServer,
|
2026-05-28 09:00:36 -04:00
|
|
|
#[serde(default)]
|
|
|
|
|
pub retention: Retention,
|
2026-05-29 09:37:33 -04:00
|
|
|
#[serde(default)]
|
|
|
|
|
pub mdns_scanner: MdnsScanner,
|
2026-06-01 17:55:33 -04:00
|
|
|
#[serde(default)]
|
|
|
|
|
pub ssdp_scanner: SsdpScanner,
|
2026-06-01 19:00:56 -04:00
|
|
|
#[serde(default)]
|
|
|
|
|
pub dhcp_scanner: DhcpScanner,
|
2026-01-21 15:36:27 -05:00
|
|
|
}
|
|
|
|
|
// End configuration structure
|
|
|
|
|
// -----------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
impl Settings {
|
2026-02-18 11:23:20 -05:00
|
|
|
pub fn new(config_path: String) -> Result<Self, ConfigError> {
|
|
|
|
|
println!("Reading configuration from {}", config_path);
|
2026-01-27 09:21:00 -05:00
|
|
|
|
2026-01-21 15:36:27 -05:00
|
|
|
let local_settings = Config::builder()
|
2026-01-27 09:21:00 -05:00
|
|
|
.add_source(File::with_name(config_path.as_str()))
|
2026-01-21 15:36:27 -05:00
|
|
|
.build()?;
|
|
|
|
|
|
|
|
|
|
local_settings.try_deserialize()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-18 11:23:20 -05:00
|
|
|
pub const DEFAULT_CONFIG_FILE_PATH: &str = "./oott.toml";
|
|
|
|
|
static SETTINGS: OnceCell<Settings> = OnceCell::new();
|
|
|
|
|
|
|
|
|
|
pub fn get_settings() -> &'static Settings {
|
|
|
|
|
match SETTINGS.get() {
|
|
|
|
|
Some(value) => value,
|
|
|
|
|
None => {
|
|
|
|
|
println!(
|
|
|
|
|
"Configuration was not initialized, reading from default path ({})",
|
|
|
|
|
DEFAULT_CONFIG_FILE_PATH
|
|
|
|
|
);
|
|
|
|
|
let settings = Settings::new(DEFAULT_CONFIG_FILE_PATH.to_string()).unwrap();
|
|
|
|
|
let _ = SETTINGS.set(settings);
|
|
|
|
|
SETTINGS.get().unwrap()
|
2026-01-21 15:36:27 -05:00
|
|
|
}
|
2026-02-18 11:23:20 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn init(config_path: String) {
|
|
|
|
|
let _ = SETTINGS.set(Settings::new(config_path).unwrap());
|
2026-01-21 15:36:27 -05:00
|
|
|
}
|
2026-06-01 19:00:56 -04:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
use config::{Config, FileFormat};
|
|
|
|
|
|
|
|
|
|
fn parse(toml: &str) -> Settings {
|
|
|
|
|
Config::builder()
|
|
|
|
|
.add_source(config::File::from_str(toml, FileFormat::Toml))
|
|
|
|
|
.build()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.try_deserialize()
|
|
|
|
|
.unwrap()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const BASE_CONFIG: &str = r#"
|
|
|
|
|
[database]
|
|
|
|
|
path = "./oott.db"
|
|
|
|
|
[networking]
|
|
|
|
|
[log]
|
|
|
|
|
level = "info"
|
|
|
|
|
[arp_scanner]
|
|
|
|
|
wait_between_scans = "1m"
|
|
|
|
|
sender_timeout = "1m"
|
|
|
|
|
scan_duration = "2m"
|
|
|
|
|
[notifications]
|
|
|
|
|
method = "none"
|
|
|
|
|
notify_when_not_seen_for = "1w"
|
|
|
|
|
[notifications.pushover]
|
|
|
|
|
token = ""
|
|
|
|
|
user_key = ""
|
|
|
|
|
[web_server]
|
|
|
|
|
ip_address = "0.0.0.0"
|
|
|
|
|
port = 3000
|
|
|
|
|
api_key = "test"
|
|
|
|
|
"#;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn scanners_enabled_by_default_when_flag_omitted() {
|
|
|
|
|
let settings = parse(BASE_CONFIG);
|
|
|
|
|
assert!(settings.arp_scanner.enabled);
|
|
|
|
|
assert!(settings.mdns_scanner.enabled);
|
|
|
|
|
assert!(settings.ssdp_scanner.enabled);
|
|
|
|
|
assert!(settings.dhcp_scanner.enabled);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn scanners_can_be_disabled() {
|
|
|
|
|
let toml = format!(
|
|
|
|
|
"{BASE_CONFIG}
|
|
|
|
|
[mdns_scanner]
|
|
|
|
|
enabled = false
|
|
|
|
|
probe_timeout = \"2s\"
|
|
|
|
|
[ssdp_scanner]
|
|
|
|
|
enabled = false
|
|
|
|
|
probe_timeout = \"2s\"
|
|
|
|
|
[dhcp_scanner]
|
|
|
|
|
enabled = false
|
|
|
|
|
"
|
|
|
|
|
);
|
|
|
|
|
// Disable the ARP scanner via its existing section too.
|
|
|
|
|
let toml = toml.replace(
|
|
|
|
|
"wait_between_scans = \"1m\"",
|
|
|
|
|
"enabled = false\n wait_between_scans = \"1m\"",
|
|
|
|
|
);
|
|
|
|
|
let settings = parse(&toml);
|
|
|
|
|
assert!(!settings.arp_scanner.enabled);
|
|
|
|
|
assert!(!settings.mdns_scanner.enabled);
|
|
|
|
|
assert!(!settings.ssdp_scanner.enabled);
|
|
|
|
|
assert!(!settings.dhcp_scanner.enabled);
|
|
|
|
|
}
|
|
|
|
|
}
|