use std::collections::HashSet;
use std::hash::{Hash, Hasher};
use std::thread;
use std::time::Duration;
use crate::interface::{Interface, InterfaceStats};
use crate::Result;
impl Hash for Interface {
fn hash<H: Hasher>(&self, state: &mut H) {
self.name.hash(state);
}
}
impl PartialEq for Interface {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
}
}
impl Eq for Interface {}
#[derive(Debug, Clone)]
pub enum InterfaceEvent {
Added(Interface),
Removed(Interface),
IpChanged(Interface),
StatsChanged(Interface, InterfaceStats),
VpnConnected(Interface),
VpnDisconnected(Interface),
}
pub type InterfaceCallback = Box<dyn Fn(InterfaceEvent) + Send + 'static>;
pub struct InterfaceMonitor {
callbacks: Vec<InterfaceCallback>,
interval: Duration,
running: bool,
}
impl InterfaceMonitor {
pub fn new() -> Result<Self> {
Ok(Self {
callbacks: Vec::new(),
interval: Duration::from_secs(1),
running: false,
})
}
#[must_use]
pub const fn interval(mut self, interval: Duration) -> Self {
self.interval = interval;
self
}
pub fn on_interface_change<F>(&mut self, callback: F) -> Result<()>
where
F: Fn(InterfaceEvent) + Send + 'static,
{
self.callbacks.push(Box::new(callback));
Ok(())
}
pub fn start(&mut self) -> Result<()> {
if self.running {
return Ok(());
}
self.running = true;
let interval = self.interval;
let callbacks = std::mem::take(&mut self.callbacks);
thread::spawn(move || {
let mut previous_interfaces: HashSet<Interface> = HashSet::new();
while let Ok(interfaces) = Interface::list() {
let current_interfaces: HashSet<Interface> = interfaces.into_iter().collect();
for interface in ¤t_interfaces {
if !previous_interfaces.contains(interface) {
let event = InterfaceEvent::Added(interface.clone());
for callback in &callbacks {
callback(event.clone());
}
}
}
for interface in &previous_interfaces {
if !current_interfaces.contains(interface) {
let event = InterfaceEvent::Removed(interface.clone());
for callback in &callbacks {
callback(event.clone());
}
}
}
for interface in ¤t_interfaces {
if let Some(prev_interface) = previous_interfaces.get(interface) {
if interface.ipv4 != prev_interface.ipv4
|| interface.ipv6 != prev_interface.ipv6
{
let event = InterfaceEvent::IpChanged(interface.clone());
for callback in &callbacks {
callback(event.clone());
}
}
}
}
previous_interfaces = current_interfaces;
thread::sleep(interval);
}
});
Ok(())
}
pub const fn stop(&mut self) {
self.running = false;
}
}
impl Drop for InterfaceMonitor {
fn drop(&mut self) {
self.stop();
}
}
pub struct NetworkChangeDetector {
monitor: InterfaceMonitor,
}
impl NetworkChangeDetector {
#[allow(dead_code)]
pub fn new() -> Result<Self> {
Ok(Self {
monitor: InterfaceMonitor::new()?,
})
}
#[allow(dead_code)]
pub const fn interval(&mut self, interval: Duration) {
self.monitor.interval = interval;
}
#[allow(dead_code)]
pub fn on_interface_change<F>(&mut self, callback: F) -> Result<()>
where
F: Fn(InterfaceEvent) + Send + 'static,
{
self.monitor.on_interface_change(callback)
}
#[allow(dead_code)]
pub fn start(&mut self, interval: Duration) -> Result<()> {
self.monitor.interval = interval;
self.monitor.start()
}
pub const fn stop(&mut self) {
self.monitor.stop();
}
}
impl Drop for NetworkChangeDetector {
fn drop(&mut self) {
self.stop();
}
}