Added main loop to run continuously and relevant configuration options

This commit is contained in:
rzuasti
2026-01-23 11:00:24 -05:00
parent 77f996c591
commit ecfc56bc30
5 changed files with 61 additions and 42 deletions
+4
View File
@@ -1,7 +1,11 @@
[networking]
interface = "eno1" # Network interface to use for scans
[log] [log]
level = "info" # off, error, warn, info, debug, trace level = "info" # off, error, warn, info, debug, trace
[timings] [timings]
wait_between_scans="30"
arp_sender_timeout="30" arp_sender_timeout="30"
arp_scan_duration="60" arp_scan_duration="60"
+6 -2
View File
@@ -1,9 +1,13 @@
[networking]
interface = "eno1" # Network interface to use for scans
[log] [log]
level = "info" # off, error, warn, info, debug, trace level = "info" # off, error, warn, info, debug, trace
[timings] [timings]
arp_sender_timeout="30" wait_between_scans="900" # Wait time between scans (in seconds). This does not include the scan time
arp_scan_duration="60" 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] [notifications]
method="pushover" # For now just pushover, you can set this to "none" to avoid sending notifications (it will just log) method="pushover" # For now just pushover, you can set this to "none" to avoid sending notifications (it will just log)
+1 -1
View File
@@ -9,7 +9,7 @@ use pnet::{
}; };
use tokio::time::{Duration, timeout}; use tokio::time::{Duration, timeout};
pub async fn find(interface: &str) -> Result<Vec<Device>, String> { pub async fn find(interface: String) -> Result<Vec<Device>, String> {
debug!("Looking up devices via ARP using interface {}", interface); debug!("Looking up devices via ARP using interface {}", interface);
// Get the network device to use // Get the network device to use
+42 -39
View File
@@ -1,7 +1,7 @@
use crate::settings::CONFIG;
use log::{LevelFilter, debug, info}; use log::{LevelFilter, debug, info};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use tokio::time::{Duration, sleep};
use crate::settings::CONFIG;
mod db; mod db;
mod device_finders; mod device_finders;
@@ -33,48 +33,51 @@ async fn main() -> Result<(), String> {
// Get database connection - thread protected // Get database connection - thread protected
let db_conn = Arc::new(Mutex::new(db::init_db().unwrap())); let db_conn = Arc::new(Mutex::new(db::init_db().unwrap()));
// Find online devices via ARP loop {
let devices = device_finders::arp::find("eno1").await.unwrap(); // Find online devices via ARP
let devices = device_finders::arp::find(CONFIG.networking.interface.to_string()).await?;
info!("Done with ARP probes"); info!("Done with ARP probes");
info!("Found {} online devices", devices.iter().count()); info!("Found {} online devices", devices.iter().count());
// Process found devices // Process found devices
for device in devices.iter() { for device in devices.iter() {
debug!("Online device found {}", device); debug!("Online device found {}", device);
// Using just one connection for now, need to update if moved DB portion to multi-thread // 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 // need to change to a clone if we need multiple threads
let db_conn_clone = Arc::clone(&db_conn); let db_conn_clone = Arc::clone(&db_conn);
// Read device from database // Read device from database
let recorded_device_result = let recorded_device_result =
db::devices::read(db_conn_clone.lock().unwrap(), device.mac_address.clone()); db::devices::read(db_conn_clone.lock().unwrap(), device.mac_address.clone());
match recorded_device_result { match recorded_device_result {
Some(recorded_device) => { Some(recorded_device) => {
// If it exists update its last seen date // If it exists update its last seen date
debug!( debug!(
"Device found in database {}. Updating to {}.", "Device found in database {}. Updating to {}.",
recorded_device, device recorded_device, device
); );
db::devices::update(db_conn_clone.lock().unwrap(), device.clone())?; 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 events::trigger_existing_device(recorded_device, device.clone()).ok(); // Ignoring errors here, do not stop loop if notification delivery fails
} }
None => { None => {
// If it doesn't exist insert it // If it doesn't exist insert it
debug!( debug!(
"Device with MAC address {} not found in database. Inserting it.", "Device with MAC address {} not found in database. Inserting it.",
device.mac_address device.mac_address
); );
db::devices::insert(db_conn_clone.lock().unwrap(), device.clone())?; 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 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(())
} }
+8
View File
@@ -5,6 +5,12 @@ use serde::Deserialize;
// ----------------------------------------------------------- // -----------------------------------------------------------
// Configuration structure // Configuration structure
#[derive(Debug, Deserialize, Clone)]
pub struct Networking {
pub interface: String,
}
#[derive(Debug, Deserialize, Clone)] #[derive(Debug, Deserialize, Clone)]
pub struct Log { pub struct Log {
pub level: String, pub level: String,
@@ -12,6 +18,7 @@ pub struct Log {
#[derive(Debug, Deserialize, Clone)] #[derive(Debug, Deserialize, Clone)]
pub struct Timings { pub struct Timings {
pub wait_between_scans: u64,
pub arp_sender_timeout: u64, pub arp_sender_timeout: u64,
pub arp_scan_duration: u64, pub arp_scan_duration: u64,
} }
@@ -30,6 +37,7 @@ pub struct Notifications {
#[derive(Debug, Deserialize, Clone)] #[derive(Debug, Deserialize, Clone)]
pub struct Settings { pub struct Settings {
pub networking: Networking,
pub log: Log, pub log: Log,
pub timings: Timings, pub timings: Timings,
pub notifications: Notifications, pub notifications: Notifications,