use lanscope_common::{Event, FlowKey, FlowStats};
use tokio::sync::{mpsc, watch};
use tokio::task::JoinHandle;
use crate::error::Result;
#[cfg(feature = "ebpf")]
pub mod ebpf;
pub mod mock;
#[derive(Clone, Debug)]
pub enum CaptureEvent {
Discovery(Box<Event>),
Flows(Vec<(FlowKey, FlowStats)>),
}
pub trait CaptureBackend: Send {
fn name(&self) -> &'static str;
fn spawn(
self: Box<Self>,
tx: mpsc::Sender<CaptureEvent>,
shutdown: watch::Receiver<bool>,
) -> JoinHandle<Result<()>>;
}
pub fn select_backend(config: &crate::config::Config) -> Result<Box<dyn CaptureBackend>> {
#[cfg(feature = "ebpf")]
{
match ebpf::EbpfBackend::new(config) {
Ok(b) => {
tracing::info!(backend = b.name(), iface = %config.interface, "using eBPF capture backend");
return Ok(Box::new(b));
}
Err(e) => {
tracing::warn!(error = %e, "eBPF backend unavailable, falling back to portable backend");
}
}
}
Ok(Box::new(mock::PortableBackend::new(config)))
}