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-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 {
|
|
|
|
|
pub interface: String,
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-21 15:36:27 -05:00
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
|
pub struct Log {
|
|
|
|
|
pub level: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
|
pub struct Timings {
|
2026-01-23 14:35:28 -05:00
|
|
|
pub wait_between_scans: DurationString,
|
|
|
|
|
pub arp_sender_timeout: DurationString,
|
|
|
|
|
pub arp_scan_duration: DurationString,
|
2026-01-21 15:36:27 -05:00
|
|
|
}
|
|
|
|
|
|
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-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,
|
|
|
|
|
pub timings: Timings,
|
2026-01-23 10:25:45 -05:00
|
|
|
pub notifications: Notifications,
|
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
|
|
|
}
|