From ecfc56bc303a1ed613e1d2f0ffa5532720a7b162 Mon Sep 17 00:00:00 2001 From: rzuasti Date: Fri, 23 Jan 2026 11:00:24 -0500 Subject: [PATCH] Added main loop to run continuously and relevant configuration options --- oott.toml | 4 ++ sample_oott.toml | 8 +++- src/device_finders/arp.rs | 2 +- src/main.rs | 81 ++++++++++++++++++++------------------- src/settings.rs | 8 ++++ 5 files changed, 61 insertions(+), 42 deletions(-) diff --git a/oott.toml b/oott.toml index 697eedd..4dcd6d8 100644 --- a/oott.toml +++ b/oott.toml @@ -1,7 +1,11 @@ +[networking] +interface = "eno1" # Network interface to use for scans + [log] level = "info" # off, error, warn, info, debug, trace [timings] +wait_between_scans="30" arp_sender_timeout="30" arp_scan_duration="60" diff --git a/sample_oott.toml b/sample_oott.toml index b10c8a4..12561d5 100644 --- a/sample_oott.toml +++ b/sample_oott.toml @@ -1,9 +1,13 @@ +[networking] +interface = "eno1" # Network interface to use for scans + [log] level = "info" # off, error, warn, info, debug, trace [timings] -arp_sender_timeout="30" -arp_scan_duration="60" +wait_between_scans="900" # Wait time between scans (in seconds). This does not include the scan time +arp_sender_timeout="60" # If the ARP sender process takes longer than this (in seconds) it will be stopped (for a class C network - 254 IPs - it should take less than a minute) +arp_scan_duration="300" # How long to wait (in seconds) for response packets on each scan (300 to 600 seconds is a good timeframe for a class B or C network) [notifications] method="pushover" # For now just pushover, you can set this to "none" to avoid sending notifications (it will just log) diff --git a/src/device_finders/arp.rs b/src/device_finders/arp.rs index 8a9b6b2..d963483 100644 --- a/src/device_finders/arp.rs +++ b/src/device_finders/arp.rs @@ -9,7 +9,7 @@ use pnet::{ }; use tokio::time::{Duration, timeout}; -pub async fn find(interface: &str) -> Result, String> { +pub async fn find(interface: String) -> Result, String> { debug!("Looking up devices via ARP using interface {}", interface); // Get the network device to use diff --git a/src/main.rs b/src/main.rs index 11a640f..3a759ce 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ +use crate::settings::CONFIG; use log::{LevelFilter, debug, info}; use std::sync::{Arc, Mutex}; - -use crate::settings::CONFIG; +use tokio::time::{Duration, sleep}; mod db; mod device_finders; @@ -33,48 +33,51 @@ async fn main() -> Result<(), String> { // Get database connection - thread protected let db_conn = Arc::new(Mutex::new(db::init_db().unwrap())); - // Find online devices via ARP - let devices = device_finders::arp::find("eno1").await.unwrap(); + loop { + // Find online devices via ARP + let devices = device_finders::arp::find(CONFIG.networking.interface.to_string()).await?; - info!("Done with ARP probes"); - info!("Found {} online devices", devices.iter().count()); + info!("Done with ARP probes"); + info!("Found {} online devices", devices.iter().count()); - // Process found devices - for device in devices.iter() { - debug!("Online device found {}", device); + // Process found devices + for device in devices.iter() { + debug!("Online device found {}", device); - // Using just one connection for now, need to update if moved DB portion to multi-thread - // need to change to a clone if we need multiple threads - let db_conn_clone = Arc::clone(&db_conn); + // Using just one connection for now, need to update if moved DB portion to multi-thread + // need to change to a clone if we need multiple threads + let db_conn_clone = Arc::clone(&db_conn); - // Read device from database - let recorded_device_result = - db::devices::read(db_conn_clone.lock().unwrap(), device.mac_address.clone()); + // Read device from database + let recorded_device_result = + db::devices::read(db_conn_clone.lock().unwrap(), device.mac_address.clone()); - match recorded_device_result { - Some(recorded_device) => { - // If it exists update its last seen date - debug!( - "Device found in database {}. Updating to {}.", - recorded_device, device - ); - db::devices::update(db_conn_clone.lock().unwrap(), device.clone())?; - events::trigger_existing_device(recorded_device, device.clone()).ok(); // Ignoring errors here, do not stop loop if notification delivery fails - } - None => { - // If it doesn't exist insert it - debug!( - "Device with MAC address {} not found in database. Inserting it.", - device.mac_address - ); + match recorded_device_result { + Some(recorded_device) => { + // If it exists update its last seen date + debug!( + "Device found in database {}. Updating to {}.", + recorded_device, device + ); + db::devices::update(db_conn_clone.lock().unwrap(), device.clone())?; + events::trigger_existing_device(recorded_device, device.clone()).ok(); // Ignoring errors here, do not stop loop if notification delivery fails + } + None => { + // If it doesn't exist insert it + debug!( + "Device with MAC address {} not found in database. Inserting it.", + device.mac_address + ); - db::devices::insert(db_conn_clone.lock().unwrap(), device.clone())?; - events::trigger_new_device(device.clone()).ok(); // Ignoring errors here, do not stop loop if notification delivery fails - } - }; + db::devices::insert(db_conn_clone.lock().unwrap(), device.clone())?; + events::trigger_new_device(device.clone()).ok(); // Ignoring errors here, do not stop loop if notification delivery fails + } + }; + } + info!( + "Scan finished. Sleeping for {} seconds", + CONFIG.timings.wait_between_scans + ); + sleep(Duration::from_secs(CONFIG.timings.wait_between_scans)).await; } - - info!("Exiting"); - - Ok(()) } diff --git a/src/settings.rs b/src/settings.rs index 71a90e3..5f7b061 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -5,6 +5,12 @@ use serde::Deserialize; // ----------------------------------------------------------- // Configuration structure + +#[derive(Debug, Deserialize, Clone)] +pub struct Networking { + pub interface: String, +} + #[derive(Debug, Deserialize, Clone)] pub struct Log { pub level: String, @@ -12,6 +18,7 @@ pub struct Log { #[derive(Debug, Deserialize, Clone)] pub struct Timings { + pub wait_between_scans: u64, pub arp_sender_timeout: u64, pub arp_scan_duration: u64, } @@ -30,6 +37,7 @@ pub struct Notifications { #[derive(Debug, Deserialize, Clone)] pub struct Settings { + pub networking: Networking, pub log: Log, pub timings: Timings, pub notifications: Notifications,