How deepfence/flowmeter works
Project Type & Purpose
• FlowMeter is a standalone, command-line utility written in Go.
• It ingests network packets (e.g., pcap files or live capture), extracts header features, groups them into flows, runs ML inference to flag benign vs. malicious traffic, and outputs statistics in CSV form.
• Primary goal: fast pre-filtering to reduce traffic for deeper analysis (e.g., in Deepfence ThreatMapper).
File Structure
• Top-level directories:
• cmd/flowmeter.go – CLI entry point
• pkg/… – core modules: common utilities, packetAnalyzer, fileProcess, ml, flowOutput
• ml/parameters – precomputed ML model parameters (weights, mean, std, intercept)
• assets/ – auxiliary scripts/notebooks for generating or validating ML flowmeter
• docs/ – Docusaurus documentation
• Go modules (go.mod / go.sum) manage dependencies.
• Makefile orchestrates build, test, and possibly parameter generation.
Readme Insights
• Emphasizes two main pipelines:
a) “Flows only” → produce flow statistics
b) “Flows + classification” → apply ML model to label packets/flows
• No external services at runtime; all inference runs locally in Go.
Main Components & Interactions
A. CLI Layer (cmd/flowmeter.go)
• Parses flags/arguments (input source, mode, output path).
• Invokes the Controller (main orchestration).
B. Packet Capture / Input
• Could be a pcap reader or live NIC interface.
• Feeds raw packets to the Packet Analyzer.
C. Packet Analyzer (pkg/packetAnalyzer)
• Extracts header fields (src/dst IP, ports, protocols).
• Emits per-packet feature vectors.
D. Flow Aggregator (pkg/common & pkg/fileProcess)
• Groups packets into flows based on 5-tuple and time windows.
• Computes flow‐level statistics (counts, timing, byte distributions).
E. ML Inference Engine (pkg/ml)
• Loads parameters from ml/parameters (weights.txt, mean/std/intercept).
• Normalizes features, applies logistic regression or ensemble model.
• Outputs classification label (benign/malicious) per flow or packet.
F. Output Module (pkg/flowOutput)
• Formats results as CSV or JSON.
• Writes files (e.g., flow_stats.csv) for downstream consumption.
CLI → Packet Capture → Packet Analyzer → Flow Aggregator → ML Inference → Output
Architectural Patterns & Design Principles
• Modular Go packages: each major concern separated into pkg/…
• Single executable: all modules linked statically, no external inference service
• Configuration via command-line flags + parameter files (no hard-coded paths)
• Dependency injection pattern in Go: Controller wires PacketAnalyzer, FlowAggregator, MLEngine, Output.