Added pushover notification sending

This commit is contained in:
rzuasti
2026-01-23 10:25:45 -05:00
parent 8e8460afbc
commit 77f996c591
6 changed files with 111 additions and 19 deletions
+7
View File
@@ -4,3 +4,10 @@ level = "info" # off, error, warn, info, debug, trace
[timings] [timings]
arp_sender_timeout="30" arp_sender_timeout="30"
arp_scan_duration="60" arp_scan_duration="60"
[notifications]
method="pushover" # For now just pushover
[notifications.pushover]
token="acxa8ouwj5iraswe8pupnpp12kmsm7" # Your pushover token goes here, just copy&paste from their website after creating the app
user_key="u872udy8xc8ceys9ee6iur229xs3fj" # User key goes here, this is the account wide code for pushover
+7
View File
@@ -4,3 +4,10 @@ level = "info" # off, error, warn, info, debug, trace
[timings] [timings]
arp_sender_timeout="30" arp_sender_timeout="30"
arp_scan_duration="60" arp_scan_duration="60"
[notifications]
method="pushover" # For now just pushover, you can set this to "none" to avoid sending notifications (it will just log)
[notifications.pushover]
token="" # Your pushover token goes here, just copy&paste from their website after creating the app
user_key="" # User key goes here, this is the account wide code for pushover
+55 -17
View File
@@ -1,21 +1,59 @@
use crate::device_finders::Device; mod pushover;
use log::info;
pub fn trigger_new_device(device: Device) { use crate::{device_finders::Device, settings::CONFIG};
info!("Trigger - New device found: {}.", device); use log::{debug, info, warn};
// Private helper function to deliver messages
fn send_message(body: String) -> Result<(), String> {
debug!("About to send notification ({body}).");
match CONFIG.notifications.method.as_str() {
"pushover" => {
pushover::send_message(body)?;
}
other => {
warn!("Notification method set to '{other}'. Set logs to 'info' to see notifications.");
info!("Notification: {body}");
}
};
Ok(())
} }
pub fn trigger_existing_device(existing_device: Device, new_device: Device) { pub fn trigger_new_device(device: Device) -> Result<(), String> {
if existing_device.ipv4_address != new_device.ipv4_address { send_message(format!("New device found in your network: {device}"))?;
info!( Ok(())
"Trigger - Device with MAC {} changed IPv4 address from {} to {}.", }
existing_device.mac_address, existing_device.ipv4_address, new_device.ipv4_address
); pub fn trigger_existing_device(existing_device: Device, new_device: Device) -> Result<(), String> {
} if (existing_device.ipv4_address != new_device.ipv4_address)
if existing_device.vendor != new_device.vendor { && (existing_device.vendor != new_device.vendor)
info!( {
"Trigger - Device with MAC {} changed vendor from {} to {}.", send_message(format!(
existing_device.mac_address, existing_device.vendor, new_device.vendor "Device with MAC {} changed IP address from {} to {} and vendor from {} to {}.",
); existing_device.mac_address,
} existing_device.ipv4_address,
new_device.ipv4_address,
existing_device.vendor,
new_device.vendor
))?;
} else if existing_device.ipv4_address != new_device.ipv4_address {
send_message(format!(
"Device with MAC {} ({}) changed IP address from {} to {}.",
existing_device.mac_address,
new_device.vendor,
existing_device.ipv4_address,
new_device.ipv4_address
))?;
} else if existing_device.vendor != new_device.vendor {
send_message(format!(
"Device with MAC {} ({}) changed vendor from {} to {}.",
existing_device.mac_address,
existing_device.ipv4_address,
existing_device.vendor,
new_device.vendor
))?;
};
Ok(())
} }
+27
View File
@@ -0,0 +1,27 @@
use log::{debug, error};
use pushover::API;
use pushover::requests::message::SendMessage;
use crate::settings::CONFIG;
pub fn send_message(body: String) -> Result<(), String> {
debug!("About to send message via pushover ({body})");
let api = API::new();
let msg = SendMessage::new(
CONFIG.notifications.pushover.token.as_str(),
CONFIG.notifications.pushover.user_key.as_str(),
body,
);
match api.send(&msg) {
Ok(_) => {
debug!("Message sent successfully");
Ok(())
}
Err(error) => {
error!("Error sending message via pushover: {error}");
Err(format!("Error sending message via pushover: {error}"))
}
}
}
+2 -2
View File
@@ -59,7 +59,7 @@ async fn main() -> Result<(), String> {
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()); 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
@@ -69,7 +69,7 @@ async fn main() -> Result<(), String> {
); );
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()); events::trigger_new_device(device.clone()).ok(); // Ignoring errors here, do not stop loop if notification delivery fails
} }
}; };
} }
+13
View File
@@ -16,10 +16,23 @@ pub struct Timings {
pub arp_scan_duration: u64, pub arp_scan_duration: u64,
} }
#[derive(Debug, Deserialize, Clone)]
pub struct Pushover {
pub token: String,
pub user_key: String,
}
#[derive(Debug, Deserialize, Clone)]
pub struct Notifications {
pub method: String,
pub pushover: Pushover,
}
#[derive(Debug, Deserialize, Clone)] #[derive(Debug, Deserialize, Clone)]
pub struct Settings { pub struct Settings {
pub log: Log, pub log: Log,
pub timings: Timings, pub timings: Timings,
pub notifications: Notifications,
} }
// End configuration structure // End configuration structure
// ----------------------------------------------------------- // -----------------------------------------------------------