Moved config to project file and lazy loaded it

This commit is contained in:
rzuasti
2026-01-21 15:36:27 -05:00
parent 5fbd63df5f
commit 37ba7abc30
8 changed files with 2033 additions and 56 deletions
-4
View File
@@ -1,4 +0,0 @@
[env]
RUST_LOG="info"
OOTT_ARP_SENDER_TIMEOUT="30"
OOTT_ARP_SCAN_DURATION="60"
Generated
+1953 -22
View File
File diff suppressed because it is too large Load Diff
+3
View File
@@ -14,3 +14,6 @@ rusqlite = { version = "0.38.0", features = ["bundled", "chrono"] }
rusqlite_migration = { version = "2.4.0", features = ["from-directory"] } rusqlite_migration = { version = "2.4.0", features = ["from-directory"] }
include_dir = "0.7.4" include_dir = "0.7.4"
chrono = "0.4.43" chrono = "0.4.43"
pushover = "0.4.0"
config = "0.15.19"
lazy_static = "1.5.0"
+6
View File
@@ -0,0 +1,6 @@
[log]
level = "info" # off, error, warn, info, debug, trace
[timings]
arp_sender_timeout="30"
arp_scan_duration="60"
-19
View File
@@ -1,19 +0,0 @@
use log::error;
use std::env;
use std::fmt::Display;
use std::str::FromStr;
pub fn parse_env<T>(variable: &str) -> Option<T>
where
T: FromStr,
<T as FromStr>::Err: Display,
{
env::var(variable)
.map_err(|error| error!("{error}: {variable}"))
.ok()
.and_then(|raw| {
raw.parse::<T>()
.map_err(|error| error!("{error}: {raw}"))
.ok()
})
}
+3 -8
View File
@@ -1,6 +1,6 @@
mod packet_send_receive; mod packet_send_receive;
use crate::{config, device_finders::Device}; use crate::{device_finders::Device, settings::CONFIG};
use log::{debug, error, info, warn}; use log::{debug, error, info, warn};
use packet_send_receive::{listen_for_packets, send_packet}; use packet_send_receive::{listen_for_packets, send_packet};
use pnet::{ use pnet::{
@@ -9,9 +9,6 @@ use pnet::{
}; };
use tokio::time::{Duration, timeout}; use tokio::time::{Duration, timeout};
const DEFAULT_SENDER_TIMEOUT: u64 = 60; // 1 minute to send all packets - good for a class C network
const DEFAULT_SCAN_DURATION: u64 = 300; // 5 minutes to wait per round to receive responses
pub async fn find(interface: &str) -> Result<Vec<Device>, String> { pub async fn find(interface: &str) -> Result<Vec<Device>, String> {
debug!("Looking up devices via ARP using interface {}", interface); debug!("Looking up devices via ARP using interface {}", interface);
@@ -84,11 +81,9 @@ pub async fn find(interface: &str) -> Result<Vec<Device>, String> {
let send_interface = network_interface.clone(); let send_interface = network_interface.clone();
// Get timeouts // Get timeouts
let sender_timeout = let sender_timeout = CONFIG.timings.arp_sender_timeout;
config::parse_env("OOTT_ARP_SENDER_TIMEOUT").unwrap_or(DEFAULT_SENDER_TIMEOUT);
info!("Sender timeout set to {} seconds", sender_timeout); info!("Sender timeout set to {} seconds", sender_timeout);
let scan_duration = let scan_duration = CONFIG.timings.arp_scan_duration;
config::parse_env("OOTT_ARP_SCAN_DURATION").unwrap_or(DEFAULT_SCAN_DURATION);
let receiver_timeout = scan_duration * 2; let receiver_timeout = scan_duration * 2;
info!("Scan duration set to {} seconds", scan_duration); info!("Scan duration set to {} seconds", scan_duration);
info!("Receiver timeout set to {} seconds", receiver_timeout); info!("Receiver timeout set to {} seconds", receiver_timeout);
+21 -3
View File
@@ -1,15 +1,33 @@
use log::{debug, info}; use log::{LevelFilter, debug, info};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
mod config; use crate::settings::CONFIG;
mod db; mod db;
mod device_finders; mod device_finders;
mod events; mod events;
mod mac_vendor_finder; mod mac_vendor_finder;
mod settings;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), String> { async fn main() -> Result<(), String> {
env_logger::init(); // Initialize logging
let log_level = match CONFIG.log.level.as_str() {
"off" => LevelFilter::Off,
"error" => LevelFilter::Error,
"warn" => LevelFilter::Warn,
"info" => LevelFilter::Info,
"debug" => LevelFilter::Debug,
"trace" => LevelFilter::Trace,
_ => LevelFilter::Error,
};
env_logger::Builder::new()
.filter(None, log_level)
.write_style(env_logger::WriteStyle::Always)
.init();
// Now onto the important stuff
info!("Starting up oott"); info!("Starting up oott");
// Get database connection - thread protected // Get database connection - thread protected
+47
View File
@@ -0,0 +1,47 @@
use config::{Config, ConfigError, File};
use lazy_static::lazy_static;
use log::error;
use serde::Deserialize;
// -----------------------------------------------------------
// Configuration structure
#[derive(Debug, Deserialize, Clone)]
pub struct Log {
pub level: String,
}
#[derive(Debug, Deserialize, Clone)]
pub struct Timings {
pub arp_sender_timeout: u64,
pub arp_scan_duration: u64,
}
#[derive(Debug, Deserialize, Clone)]
pub struct Settings {
pub log: Log,
pub timings: Timings,
}
// End configuration structure
// -----------------------------------------------------------
const CONFIG_FILE_PATH: &str = "./oott.toml";
impl Settings {
pub fn new() -> Result<Self, ConfigError> {
let local_settings = Config::builder()
.add_source(File::with_name(CONFIG_FILE_PATH))
.build()?;
local_settings.try_deserialize()
}
}
lazy_static! {
pub static ref CONFIG: Settings = match Settings::new() {
Ok(value) => value,
Err(error) => {
error!("Error reading configuration file ({CONFIG_FILE_PATH}): {error}");
panic!("Error reading configuration file ({CONFIG_FILE_PATH}): {error}");
}
};
}