# deepfence/flowmeter architecture

> Architecture diagram of the GitHub repository deepfence/flowmeter, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-10-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/deepfence/flowmeter
- Repository: https://github.com/deepfence/flowmeter

## Overview

##### 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.

## Diagram (Mermaid)

```mermaid
flowchart TD
    CLI["CLI Layer\n(cmd/flowmeter.go)"]:::cli
    PacketCapture["Packet Capture\n(networkCommon.go)"]:::dataIngest
    PacketAnalyzer["Packet Analyzer\n(packetAnalyzer.go)"]:::processing

    subgraph "Flow Aggregator"
        CommonUtils["Common Utils\n(common.go)"]:::processing
        FlowFields["Flow Fields\n(flowFields.go)"]:::processing
        FileProcess["File Processor\n(fileProcess.go)"]:::processing
    end

    MLEngine["ML Inference Engine\n(mlCommon.go)"]:::ml

    subgraph "ML Parameters"
        intercept["intercept.txt"]:::params
        mean["mean.txt"]:::params
        std["std.txt"]:::params
        weights["weights.txt"]:::params
    end

    Output["Output Module\n(pkg/flowOutput)"]:::output
    External["Deepfence ThreatMapper"]:::external

    %% Connections
    CLI --> PacketCapture
    PacketCapture --> PacketAnalyzer
    PacketAnalyzer --> CommonUtils
    CommonUtils --> FlowFields
    FlowFields --> FileProcess
    FileProcess --> MLEngine
    intercept --> MLEngine
    mean --> MLEngine
    std --> MLEngine
    weights --> MLEngine
    MLEngine --> Output
    Output --> External

    %% Click Events
    click CLI "https://github.com/deepfence/flowmeter/blob/main/cmd/flowmeter.go"
    click PacketCapture "https://github.com/deepfence/flowmeter/blob/main/pkg/packetAnalyzer/networkCommon.go"
    click PacketAnalyzer "https://github.com/deepfence/flowmeter/blob/main/pkg/packetAnalyzer/packetAnalyzer.go"
    click CommonUtils "https://github.com/deepfence/flowmeter/blob/main/pkg/common/common.go"
    click FlowFields "https://github.com/deepfence/flowmeter/blob/main/pkg/common/flowFields.go"
    click FileProcess "https://github.com/deepfence/flowmeter/blob/main/pkg/fileProcess/fileProcess.go"
    click MLEngine "https://github.com/deepfence/flowmeter/blob/main/pkg/ml/mlCommon.go"
    click intercept "https://github.com/deepfence/flowmeter/blob/main/ml/parameters/intercept.txt"
    click mean "https://github.com/deepfence/flowmeter/blob/main/ml/parameters/mean.txt"
    click std "https://github.com/deepfence/flowmeter/blob/main/ml/parameters/std.txt"
    click weights "https://github.com/deepfence/flowmeter/blob/main/ml/parameters/weights.txt"
    click Output "https://github.com/deepfence/flowmeter/tree/main/pkg/flowOutput/"

    %% Styles
    classDef cli fill:#cccccc,stroke:#333,stroke-width:1px;
    classDef dataIngest fill:#aaddff,stroke:#333,stroke-width:1px;
    classDef processing fill:#aaffaa,stroke:#333,stroke-width:1px;
    classDef ml fill:#ffcc99,stroke:#333,stroke-width:1px;
    classDef params fill:#ffd699,stroke:#333,stroke-width:1px,stroke-dasharray: 5 5;
    classDef output fill:#ffff7f,stroke:#333,stroke-width:1px;
    classDef external fill:#eeeeee,stroke:#333,stroke-width:1px,stroke-dasharray: 2 2;
```
