use lanscope_common::{FlowKey, FlowStats};
use crate::alert::Alert;
use crate::registry::{Change, Device};
pub mod heuristics;
#[cfg(feature = "ml")]
pub mod onnx;
pub trait Detector: Send {
fn on_device(&mut self, _device: &Device, _change: Change, _now: i64) -> Vec<Alert> {
Vec::new()
}
fn on_flow(&mut self, _key: &FlowKey, _stats: &FlowStats, _now: i64) -> Vec<Alert> {
Vec::new()
}
}
#[derive(Default)]
pub struct Engine {
detectors: Vec<Box<dyn Detector>>,
}
impl Engine {
pub fn new() -> Self {
Self::default()
}
pub fn with(mut self, detector: Box<dyn Detector>) -> Self {
self.detectors.push(detector);
self
}
pub fn default_stack() -> Self {
use heuristics::{NewDeviceDetector, PortScanDetector, VolumeSpikeDetector};
Self::new()
.with(Box::new(NewDeviceDetector))
.with(Box::new(PortScanDetector::default()))
.with(Box::new(VolumeSpikeDetector::default()))
}
pub fn on_device(&mut self, device: &Device, change: Change, now: i64) -> Vec<Alert> {
self.detectors
.iter_mut()
.flat_map(|d| d.on_device(device, change, now))
.collect()
}
pub fn on_flow(&mut self, key: &FlowKey, stats: &FlowStats, now: i64) -> Vec<Alert> {
self.detectors
.iter_mut()
.flat_map(|d| d.on_flow(key, stats, now))
.collect()
}
}