use flowscope::extract::FiveTuple;
use flowscope::pcap::PcapFlowSource;
use flowscope::{AccumulatingSessionParser, FlowSessionDriver, SessionEvent};
fn parse_one(buf: &[u8]) -> Option<(String, usize)> {
let nl = buf.iter().position(|&b| b == b'\n')?;
let line = String::from_utf8_lossy(&buf[..nl]).into_owned();
Some((line, nl + 1))
}
fn main() -> flowscope::Result<()> {
let path = std::env::args()
.nth(1)
.unwrap_or_else(|| "tests/data/http_session.pcap".to_string());
let parser = AccumulatingSessionParser::new("line", parse_one);
let mut driver = FlowSessionDriver::new(FiveTuple::bidirectional(), parser);
let mut lines = 0usize;
let mut bytes = 0usize;
for owned in PcapFlowSource::open(&path)?.views() {
let owned = owned?;
for ev in driver.track(&owned) {
if let SessionEvent::Application {
key, message: line, ..
} = ev
{
lines += 1;
bytes += line.len();
if lines <= 10 {
println!(" {:<32} {line}", format!("{}", key.a.ip()));
}
}
}
}
if lines > 10 {
println!(" … plus {} more lines …", lines - 10);
}
println!();
println!("decoded {lines} lines ({bytes} bytes total)");
Ok(())
}