mirror of
https://github.com/rzuasti/oott.git
synced 2026-07-08 19:21:54 +02:00
Moved config to project file and lazy loaded it
This commit is contained in:
@@ -1,4 +0,0 @@
|
||||
[env]
|
||||
RUST_LOG="info"
|
||||
OOTT_ARP_SENDER_TIMEOUT="30"
|
||||
OOTT_ARP_SCAN_DURATION="60"
|
||||
Generated
+1953
-22
File diff suppressed because it is too large
Load Diff
@@ -14,3 +14,6 @@ rusqlite = { version = "0.38.0", features = ["bundled", "chrono"] }
|
||||
rusqlite_migration = { version = "2.4.0", features = ["from-directory"] }
|
||||
include_dir = "0.7.4"
|
||||
chrono = "0.4.43"
|
||||
pushover = "0.4.0"
|
||||
config = "0.15.19"
|
||||
lazy_static = "1.5.0"
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
[log]
|
||||
level = "info" # off, error, warn, info, debug, trace
|
||||
|
||||
[timings]
|
||||
arp_sender_timeout="30"
|
||||
arp_scan_duration="60"
|
||||
@@ -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()
|
||||
})
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
mod packet_send_receive;
|
||||
|
||||
use crate::{config, device_finders::Device};
|
||||
use crate::{device_finders::Device, settings::CONFIG};
|
||||
use log::{debug, error, info, warn};
|
||||
use packet_send_receive::{listen_for_packets, send_packet};
|
||||
use pnet::{
|
||||
@@ -9,9 +9,6 @@ use pnet::{
|
||||
};
|
||||
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> {
|
||||
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();
|
||||
|
||||
// Get timeouts
|
||||
let sender_timeout =
|
||||
config::parse_env("OOTT_ARP_SENDER_TIMEOUT").unwrap_or(DEFAULT_SENDER_TIMEOUT);
|
||||
let sender_timeout = CONFIG.timings.arp_sender_timeout;
|
||||
info!("Sender timeout set to {} seconds", sender_timeout);
|
||||
let scan_duration =
|
||||
config::parse_env("OOTT_ARP_SCAN_DURATION").unwrap_or(DEFAULT_SCAN_DURATION);
|
||||
let scan_duration = CONFIG.timings.arp_scan_duration;
|
||||
let receiver_timeout = scan_duration * 2;
|
||||
info!("Scan duration set to {} seconds", scan_duration);
|
||||
info!("Receiver timeout set to {} seconds", receiver_timeout);
|
||||
|
||||
+21
-3
@@ -1,15 +1,33 @@
|
||||
use log::{debug, info};
|
||||
use log::{LevelFilter, debug, info};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
mod config;
|
||||
use crate::settings::CONFIG;
|
||||
|
||||
mod db;
|
||||
mod device_finders;
|
||||
mod events;
|
||||
mod mac_vendor_finder;
|
||||
mod settings;
|
||||
|
||||
#[tokio::main]
|
||||
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");
|
||||
|
||||
// Get database connection - thread protected
|
||||
|
||||
@@ -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}");
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user