Created config sample (get real config out of git). Changed

mac_vendor_finder to use lazy_static instead of LazyLock
This commit is contained in:
rzuasti
2026-01-23 08:28:56 -05:00
parent c5f4ffa33c
commit 8e8460afbc
4 changed files with 44 additions and 29 deletions
+1
View File
@@ -1,2 +1,3 @@
/target /target
oott.db oott.db
oott.toml
+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"
-1
View File
@@ -6,7 +6,6 @@ use crate::device_finders::Device;
// Read device from its MAC address // Read device from its MAC address
pub fn read(conn: MutexGuard<Connection>, mac_address: String) -> Option<Device> { pub fn read(conn: MutexGuard<Connection>, mac_address: String) -> Option<Device> {
// TODO: Add last_seen
let result: Result<Device, rusqlite::Error> = conn.query_one( let result: Result<Device, rusqlite::Error> = conn.query_one(
"SELECT mac_address, ipv4_address, vendor, last_seen FROM devices WHERE mac_address=?1", "SELECT mac_address, ipv4_address, vendor, last_seen FROM devices WHERE mac_address=?1",
params![mac_address], params![mac_address],
+37 -28
View File
@@ -1,8 +1,8 @@
use lazy_static::lazy_static;
use log::{debug, error, info}; use log::{debug, error, info};
use serde::Deserialize; use serde::Deserialize;
use std::collections::HashMap; use std::collections::HashMap;
use std::fs; use std::fs;
use std::sync::LazyLock;
#[derive(Deserialize)] #[derive(Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
@@ -14,37 +14,46 @@ struct MacRecord {
// last_update: String, // last_update: String,
} }
// TODO : Change to lazy_load macro (like CONFIG)
// Initialize mac vendors database as a static lazy loaded unit // Initialize mac vendors database as a static lazy loaded unit
static MAC_VENDORS_DATABASE: LazyLock<HashMap<String, String>> = LazyLock::new(|| { lazy_static! {
info!("Loading MAC vendor database into memory"); static ref MAC_VENDORS_DATABASE: HashMap<String, String> = {
info!("Loading MAC vendor database into memory");
let mut database = HashMap::new(); let mut database = HashMap::new();
let data = match fs::read_to_string("data/mac-vendors-export.json") { let data = match fs::read_to_string("data/mac-vendors-export.json") {
Ok(value) => value, Ok(value) => value,
Err(error) => { Err(error) => {
error!("Error reading mac vendors database (data/mac-vendors-export.json): {error}"); error!(
panic!("Error reading mac vendors database (data/mac-vendors-export.json): {error}"); "Error reading mac vendors database (data/mac-vendors-export.json): {error}"
);
panic!(
"Error reading mac vendors database (data/mac-vendors-export.json): {error}"
);
}
};
let json: Vec<MacRecord> = match serde_json::from_str(&data) {
Ok(value) => value,
Err(error) => {
error!(
"Error parsing mac vendors database (data/mac-vendors-export.json): {error}"
);
panic!(
"Error parsing mac vendors database (data/mac-vendors-export.json): {error}"
);
}
};
debug!("Found {} records in the database", json.len());
for el in json {
database.insert(el.mac_prefix.to_uppercase(), el.vendor_name);
} }
info!("MAC vendor database loaded");
database
}; };
}
let json: Vec<MacRecord> = match serde_json::from_str(&data) {
Ok(value) => value,
Err(error) => {
error!("Error parsing mac vendors database (data/mac-vendors-export.json): {error}");
panic!("Error parsing mac vendors database (data/mac-vendors-export.json): {error}");
}
};
debug!("Found {} records in the database", json.len());
for el in json {
database.insert(el.mac_prefix.to_uppercase(), el.vendor_name);
}
info!("MAC vendor database loaded");
database
});
// Find a vendor based on the MAC prefix // Find a vendor based on the MAC prefix
pub fn find(mac_prefix: String) -> String { pub fn find(mac_prefix: String) -> String {