2026-01-23 11:00:24 -05:00
|
|
|
use crate::settings::CONFIG;
|
2026-02-12 16:45:04 -05:00
|
|
|
use axum::{Router, routing::get};
|
2026-01-21 15:36:27 -05:00
|
|
|
use log::{LevelFilter, debug, info};
|
2026-01-21 10:30:37 -05:00
|
|
|
use std::sync::{Arc, Mutex};
|
2026-02-13 11:35:36 -05:00
|
|
|
use tokio::time::{Duration, sleep};
|
2026-02-12 21:46:50 -05:00
|
|
|
use tower_http::services::ServeDir;
|
2026-01-21 15:36:27 -05:00
|
|
|
|
2026-01-19 19:03:08 -05:00
|
|
|
mod db;
|
2026-01-13 07:57:47 -05:00
|
|
|
mod device_finders;
|
2026-01-21 11:09:58 -05:00
|
|
|
mod events;
|
2026-01-19 12:45:52 -05:00
|
|
|
mod mac_vendor_finder;
|
2026-01-21 15:36:27 -05:00
|
|
|
mod settings;
|
2026-01-13 07:57:47 -05:00
|
|
|
|
2026-01-13 15:48:17 -05:00
|
|
|
#[tokio::main]
|
2026-01-21 10:30:37 -05:00
|
|
|
async fn main() -> Result<(), String> {
|
2026-01-21 15:36:27 -05:00
|
|
|
// 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
|
2026-01-14 09:53:01 -05:00
|
|
|
info!("Starting up oott");
|
2026-01-13 07:57:47 -05:00
|
|
|
|
2026-02-13 11:35:36 -05:00
|
|
|
tokio::join!(scanner(), web_server()).0
|
2026-02-12 16:45:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn web_server() -> Result<(), String> {
|
|
|
|
|
info!("Starting web server");
|
2026-02-12 21:46:50 -05:00
|
|
|
let static_files = ServeDir::new("./web");
|
|
|
|
|
|
|
|
|
|
let router = Router::new()
|
|
|
|
|
.route("/", get(|| async { "hello" }))
|
|
|
|
|
.nest_service("/web", static_files);
|
2026-02-12 16:45:04 -05:00
|
|
|
info!("Server running at http://0.0.0.0:3000");
|
|
|
|
|
// Start the server
|
|
|
|
|
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
|
2026-02-12 21:46:50 -05:00
|
|
|
axum::serve(listener, router).await.unwrap();
|
2026-02-12 16:45:04 -05:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn scanner() -> Result<(), String> {
|
2026-01-21 10:30:37 -05:00
|
|
|
// Get database connection - thread protected
|
|
|
|
|
let db_conn = Arc::new(Mutex::new(db::init_db().unwrap()));
|
2026-01-19 12:45:52 -05:00
|
|
|
|
2026-01-23 11:00:24 -05:00
|
|
|
loop {
|
|
|
|
|
// Find online devices via ARP
|
|
|
|
|
let devices = device_finders::arp::find(CONFIG.networking.interface.to_string()).await?;
|
2026-01-14 09:53:01 -05:00
|
|
|
|
2026-01-23 11:00:24 -05:00
|
|
|
info!("Done with ARP probes");
|
|
|
|
|
info!("Found {} online devices", devices.iter().count());
|
2026-01-20 14:28:31 -05:00
|
|
|
|
2026-01-23 11:00:24 -05:00
|
|
|
// Process found devices
|
|
|
|
|
for device in devices.iter() {
|
|
|
|
|
debug!("Online device found {}", device);
|
2026-01-21 10:30:37 -05:00
|
|
|
|
2026-01-23 11:00:24 -05:00
|
|
|
// 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);
|
2026-01-21 10:30:37 -05:00
|
|
|
|
2026-01-23 11:00:24 -05:00
|
|
|
// Read device from database
|
|
|
|
|
let recorded_device_result =
|
|
|
|
|
db::devices::read(db_conn_clone.lock().unwrap(), device.mac_address.clone());
|
2026-01-21 10:30:37 -05:00
|
|
|
|
2026-01-23 11:00:24 -05:00
|
|
|
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
|
|
|
|
|
);
|
2026-01-21 10:30:37 -05:00
|
|
|
|
2026-01-23 11:00:24 -05:00
|
|
|
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
|
|
|
|
|
);
|
2026-01-23 14:35:28 -05:00
|
|
|
sleep(Duration::from(CONFIG.timings.wait_between_scans)).await;
|
2026-01-20 14:28:31 -05:00
|
|
|
}
|
2026-01-13 07:57:47 -05:00
|
|
|
}
|