# gojue/ecapture architecture

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

- Interactive diagram: https://gitdiagram.com/gojue/ecapture
- Repository: https://github.com/gojue/ecapture

## Overview

##### Main Components
- CLI/Command Interface – Represented by the “cli” directory (with subfolders like “cmd”, “http”, and “cobrautl”). This is where command parsing, user flags (e.g., mode selection: pcap, keylog, text, gotls, etc.), and the HTTP server (for configuration and monitoring) are initiated.
- eBPF Engine & Kernel Integration – The “kern” folder houses many C source files and header files for different libraries (e.g., OpenSSL, GnuTLS, etc.) as well as sub-folders for BPF bytecode (found inside “kern/bpf”). This component loads and attaches eBPF programs to the target functions in shared libraries.
- Event Processing Pipeline – Located in “pkg/event_processor” and “pkg/proc”. This layer handles the processing, transformation, and testing (via accompanying test files) of captured plaintext and protocol metadata.
- Upgrade and Utility Modules – In “pkg/upgrade” and “pkg/util”, where upgrade checking and various utility functions (like ebpf helper functions, ethernet operations, HKDF, and kernel version parsing) are implemented.
- User Configuration & Custom Modules – Found in the “user” directory (with subdirectories “config”, “event”, and “module”) which indicate pluggable modules that determine what events to capture, how to parse configuration parameters, and how to process the captured events.
- Build & Deployment – Although not part of runtime, the “builder” directory (with Dockerfile, Makefiles, and initialization scripts) and CI configurations (in “.github”) denote the automation and release pipelines for the project.

##### Relationships and Interactions
- The CLI component (initiated via main.go) first parses command-line arguments. Depending on the selected subcommand (e.g., tls, gotls, bash, mysqld), it then loads the appropriate configuration from the “user/config” folder.
- The CLI triggers the loading of eBPF programs (from pre-compiled bytecode files positioned in “user/bytecode” and referenced in the “utils” and “kern” directories). These are then attached to target functions in the shared libraries found on the host system.
- Once the kernel hooks are in place, the kernel sends captured event data back to userspace. The “pkg/event_processor” processes this data and passes it to different output handlers: either writing to a pcap file (if in pcap mode), logging key material (in keylog mode), or directly outputting plaintext to the console (text mode).
- Additional auxiliary components (like the HTTP server in “cli/http”) provide a GUI-like configuration interface and real-time monitoring.
- Utility packages and upgrade modules ensure sustainability, version checks, and help in adapting the tool for different environments (for instance, handling differences between Android and Linux kernels).

##### Architectural Patterns and Technologies
- Modular Design – Each function (e.g., capturing for a specific library, processing events, handling configuration) is separated into its own module (e.g., “pkg/event_processor”, “user/module”), following a loosely coupled design.
- Client-Server Pattern – The CLI component doubles as both a command-line tool and a server (via an embedded HTTP server) that listens on a configurable port.
- eBPF and Kernel Space Interaction – The project leverages eBPF’s capability to run custom programs in kernel space, which is central to its functionality. The architecture clearly delineates user-space components (CLI, event processors) from kernel-space components (eBPF programs and probes).
- Deployment & Build Automation – Presence of CI workflows, Docker support, and Makefiles indicate that the project follows modern build and deployment practices.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User-Space Components Subgraph
    subgraph "CLI & HTTP Interface"
        CLI_Main["\"CLI Main\""]:::userSpace
        CLI_Cmd["\"CLI Commands\""]:::userSpace
        CLI_HTTP["\"HTTP Server\""]:::userSpace
        CLI_Main --> CLI_Cmd
        CLI_Cmd --> CLI_HTTP
    end

    subgraph "User Configuration & Custom Modules"
        UC_Config["\"User Config\""]:::userSpace
        UC_Event["\"User Event\""]:::userSpace
        UC_Module["\"User Module\""]:::userSpace
        UC_Bytecode["\"User Bytecode\""]:::userSpace
        UC_Config --- UC_Event
        UC_Event --- UC_Module
        UC_Module --- UC_Bytecode
    end

    subgraph "Event Processing Pipeline"
        EP_Processor["\"Event Processor\""]:::userSpace
        EP_Proc["\"Proc Module\""]:::userSpace
        EP_Processor --> EP_Proc
    end

    subgraph "Output Handling"
        OH_PCAP["\"PCAP Mode\""]:::userSpace
        OH_Keylog["\"Keylog Mode\""]:::userSpace
        OH_Text["\"Text Mode\""]:::userSpace
        OH_PCAP --> OH_Keylog
        OH_Keylog --> OH_Text
    end

    %% Kernel-Space Components Subgraph
    subgraph "eBPF Engine & Kernel Probes"
        EBP_Bash["\"Bash Probe\""]:::kernelSpace
        EBP_OpenSSL["\"OpenSSL Probe\""]:::kernelSpace
        EBP_BPF["\"BPF Bytecode\""]:::kernelSpace
        EBP_Bash --> EBP_OpenSSL
        EBP_OpenSSL --> EBP_BPF
    end

    %% Build & Deployment Infrastructure Subgraph
    subgraph "Build & Deployment Infrastructure"
        BD["\"Builder\""]:::buildInfra
        GH["\"CI & Workflows\""]:::buildInfra
        BD --> GH
    end

    %% Main Data Flows
    CLI_HTTP -->|"initiate"| EBP_Bash
    EBP_BPF -->|"capture event data"| EP_Processor
    EP_Proc -->|"dispatch output"| OH_PCAP
    UC_Config ---|"injects config"| CLI_Main
    UC_Bytecode ---|"influences"| EP_Processor

    %% Deployment influence (dotted connection)
    BD ---|"build automation"| CLI_HTTP
    GH ---|"CI integration"| BD

    %% Call-out Note for Root Privileges
    Note1["\"Requires Root Privileges for eBPF probes\""]:::note
    EBP_BPF --- Note1

    %% Legend (informational)
    Legend["\"Legend: Blue = User-Space Modules; Yellow = Kernel-Space Modules; Purple = Build/Deployment\""]:::note

    %% Click Events for CLI & HTTP Interface
    click CLI_Main "https://github.com/gojue/ecapture/blob/master/cli/main.go"
    click CLI_Cmd "https://github.com/gojue/ecapture/tree/master/cli/cmd"
    click CLI_HTTP "https://github.com/gojue/ecapture/blob/master/cli/http/server.go"

    %% Click Events for eBPF Engine & Kernel Probes
    click EBP_Bash "https://github.com/gojue/ecapture/blob/master/kern/bash_kern.c"
    click EBP_OpenSSL "https://github.com/gojue/ecapture/blob/master/kern/openssl_*_kern.c"
    click EBP_BPF "https://github.com/gojue/ecapture/tree/master/kern/bpf"

    %% Click Events for Event Processing Pipeline
    click EP_Processor "https://github.com/gojue/ecapture/blob/master/pkg/event_processor/processor.go"
    click EP_Proc "https://github.com/gojue/ecapture/blob/master/pkg/proc/proc.go"

    %% Click Events for Build & Deployment Infrastructure
    click BD "https://github.com/gojue/ecapture/tree/master/builder"
    click GH "https://github.com/gojue/ecapture/blob/master/.github"

    %% Click Events for User Configuration & Custom Modules
    click UC_Config "https://github.com/gojue/ecapture/tree/master/user/config"
    click UC_Event "https://github.com/gojue/ecapture/tree/master/user/event"
    click UC_Module "https://github.com/gojue/ecapture/tree/master/user/module"
    click UC_Bytecode "https://github.com/gojue/ecapture/tree/master/user/bytecode"

    %% Styles
    classDef userSpace fill:#AED6F1,stroke:#1B4F72,stroke-width:2px,rx:5,ry:5;
    classDef kernelSpace fill:#F9E79F,stroke:#B9770E,stroke-width:2px;
    classDef buildInfra fill:#D7BDE2,stroke:#6C3483,stroke-width:2px,rx:5,ry:5;
    classDef note fill:#FAD7A0,stroke:#D68910,stroke-width:1px,stroke-dasharray: 5 5, font-style: italic;
    
    %% Place Legend at the bottom
    Legend --- CLI_Main
    Legend --- EBP_Bash
    Legend --- BD
```
