use async_trait::async_trait;
use tokio::sync::mpsc;
use crate::core::models::port::Protocol;
use crate::core::models::target::Target;
use crate::core::session::{ScanContext, ScannerKind};
use crate::scanner::PortScanner;
pub struct CompositePortScanner {
scanners: Vec<Box<dyn PortScanner>>,
}
impl CompositePortScanner {
pub fn new(scanners: Vec<Box<dyn PortScanner>>) -> Self {
Self { scanners }
}
}
#[async_trait]
impl PortScanner for CompositePortScanner {
fn kind(&self) -> ScannerKind {
ScannerKind::Composite
}
fn supported_protocols(&self) -> Vec<Protocol> {
let mut protocols = Vec::new();
for scanner in &self.scanners {
for proto in scanner.supported_protocols() {
if !protocols.contains(&proto) {
protocols.push(proto);
}
}
}
protocols
}
async fn scan(&mut self, mut targets: mpsc::Receiver<Target>) -> anyhow::Result<()> {
struct Route {
supported_protocols: Vec<Protocol>,
tx: mpsc::Sender<Target>,
}
let mut routes = Vec::new();
let mut handles = Vec::new();
for mut scanner in self.scanners.drain(..) {
let (tx, rx) = mpsc::channel(1024);
let supported_protocols = scanner.supported_protocols();
let handle = tokio::spawn(async move {
let res = scanner.scan(rx).await;
(scanner, res)
});
handles.push(handle);
routes.push(Route {
supported_protocols,
tx,
});
}
while let Some(target) = targets.recv().await {
for route in &routes {
if route.supported_protocols.contains(&target.protocol) {
let _ = route.tx.send(target).await;
break;
}
}
}
drop(routes);
for handle in handles {
if let Ok((scanner, res)) = handle.await {
self.scanners.push(scanner);
res?;
}
}
Ok(())
}
async fn detect_services(&mut self, ctx: &ScanContext) {
for scanner in &mut self.scanners {
scanner.detect_services(ctx).await;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::net::{IpAddr, Ipv4Addr};
use std::sync::{Arc, Mutex};
struct MockPortScanner {
supported: Vec<Protocol>,
received: Arc<Mutex<Vec<Target>>>,
}
impl MockPortScanner {
fn new(supported: Vec<Protocol>) -> (Self, Arc<Mutex<Vec<Target>>>) {
let received = Arc::new(Mutex::new(Vec::new()));
(
Self {
supported,
received: received.clone(),
},
received,
)
}
}
#[async_trait]
impl PortScanner for MockPortScanner {
fn kind(&self) -> ScannerKind {
ScannerKind::Composite
}
fn supported_protocols(&self) -> Vec<Protocol> {
self.supported.clone()
}
async fn scan(&mut self, mut targets: mpsc::Receiver<Target>) -> anyhow::Result<()> {
while let Some(t) = targets.recv().await {
self.received.lock().unwrap().push(t);
}
Ok(())
}
}
#[tokio::test]
async fn composite_routes_by_protocol() {
let (tcp_scanner, tcp_rx) = MockPortScanner::new(vec![Protocol::Tcp]);
let (udp_scanner, udp_rx) = MockPortScanner::new(vec![Protocol::Udp]);
let mut composite =
CompositePortScanner::new(vec![Box::new(tcp_scanner), Box::new(udp_scanner)]);
let (tx, rx) = mpsc::channel(10);
let target_tcp = Target {
ip: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
port: 80,
protocol: Protocol::Tcp,
};
let target_udp = Target {
ip: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
port: 53,
protocol: Protocol::Udp,
};
tx.send(target_tcp).await.unwrap();
tx.send(target_udp).await.unwrap();
drop(tx);
composite.scan(rx).await.unwrap();
let tcp_received = tcp_rx.lock().unwrap();
assert_eq!(tcp_received.len(), 1);
assert_eq!(tcp_received[0].protocol, Protocol::Tcp);
let udp_received = udp_rx.lock().unwrap();
assert_eq!(udp_received.len(), 1);
assert_eq!(udp_received[0].protocol, Protocol::Udp);
}
}