# eycorsican/leaf architecture

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

- Interactive diagram: https://gitdiagram.com/eycorsican/leaf
- Repository: https://github.com/eycorsican/leaf

## Overview

- Leaf CLI: Represented by the leaf-cli directory (with its own Cargo.toml and main.rs). This component handles user interactions and command-line operations.
- Leaf Plugins: Modules like leaf-plugins/shadowsocks provide plugin support. This demonstrates the framework’s extensibility.
- Core Engine (Leaf): The “leaf” directory is the heart of the system. Within it, show further breakdown:
- Application Layer (“app”): Contains API-related components, inbound/outbound handling, DNS client, NAT management, logger, and dispatcher modules.
- Configuration Module (“config”): Responsible for reading and parsing different configuration formats (INI, JSON, Proto).
- Proxy Layer (“proxy”): Contains support for multiple proxy protocols and transports (e.g., AMux, QUIC, direct, shadowsocks, socks, TLS, trojan, and many more). It is organized in subdirectories that handle protocol-specific logic.
- Platform/Mobile Support (“mobile”): Contains mobile bindings and wrappers for iOS/Android.
- Core Utility Modules (“common”): These provide cross-cutting utilities like crypto, IO, networking, and system calls.
- Testing: The “tests” folder shows various test cases that verify the functionality of different proxy mechanisms.

##### Relationships and Interactions
- How the API server (inside the “app/api” directory) interacts with dispatchers and listeners for incoming traffic.

##### Architectural Patterns
- Modular Design: The project is organized into distinct modules (app, config, common, proxy) that interact through well-defined interfaces.
- Plugin Architecture: The “leaf-plugins” directory signals that the system is designed to allow easy addition or replacement of protocol handlers (e.g., Shadowsocks).
- Layered Approach: Clearly separate the presentation/command-line layer (leaf-cli) from the business logic (core engine) and integration layers (FFI/mobile).

##### Relevant Technologies and Technologies
- Rust/Cargo: Indicate that the entire project is built using Cargo with multiple packages.
- C FFI Integration: Note the usage of cbindgen for generating C-compatible headers.
- Multiplexing Transports: Highlight AMux and QUIC as key strategies for reducing handshake overhead and improving efficiency.
- Transparent Proxying: Include TUN inbound modules for VPN-like behavior, especially used in mobile and gateway modes.
- Testing and CI/CD: Acknowledge the presence of GitHub workflows for continuous integration and release management.

##### Final Touches
- That every component’s internal relations (e.g., the division within the “leaf” directory into application logic, routing, protocol handling) are either explicitly shown or noted.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Entry Points"
        CLI["Leaf CLI"]:::entry
        FFI["Leaf FFI"]:::entry
        PLUGINS["Leaf Plugins (Shadowsocks)"]:::plugin
        MOBILE["Mobile (Support)"]:::entry
    end

    subgraph "Core Engine (Leaf)"
        CORE["Core Engine"]:::core
        APP["Application Layer (App)"]:::core
        API["API Server (app/api)"]:::core
        CONFIG["Configuration Module (Config)"]:::core
        PROXY["Proxy Layer (Proxy)"]:::core
        COMMON["Core Utility Modules (Common)"]:::core
        TESTING["Testing"]:::test
    end

    %% Entry Points to Core Engine
    CLI -->|"triggers"| CORE
    FFI -->|"integrates"| CORE
    MOBILE -->|"supports"| CORE

    %% Core Engine internal relations
    CORE -->|"uses"| APP
    CORE -->|"loads"| CONFIG
    CORE -->|"routes"| PROXY
    CORE -->|"employs"| COMMON
    CORE -->|"verifiedBy"| TESTING

    %% Application Layer interaction
    APP -->|"serves"| API
    API -->|"dispatches"| APP
    APP -->|"routesTraffic"| PROXY

    %% Plugin extends proxy functionalities
    PLUGINS -->|"extends"| PROXY

    %% Click Events
    click CLI "https://github.com/eycorsican/leaf/blob/master/leaf-cli/Cargo.toml"
    click FFI "https://github.com/eycorsican/leaf/blob/master/leaf-ffi/Cargo.toml"
    click PLUGINS "https://github.com/eycorsican/leaf/blob/master/leaf-plugins/shadowsocks/Cargo.toml"
    click CORE "https://github.com/eycorsican/leaf/blob/master/leaf/Cargo.toml"
    click APP "https://github.com/eycorsican/leaf/tree/master/leaf/src/app/"
    click CONFIG "https://github.com/eycorsican/leaf/tree/master/leaf/src/config/"
    click PROXY "https://github.com/eycorsican/leaf/tree/master/leaf/src/proxy/"
    click MOBILE "https://github.com/eycorsican/leaf/tree/master/leaf/src/mobile/"
    click COMMON "https://github.com/eycorsican/leaf/tree/master/leaf/src/common/"
    click TESTING "https://github.com/eycorsican/leaf/tree/master/leaf/tests/"

    %% Styles
    classDef entry fill:#a2d9ce,stroke:#1e8449,stroke-width:2px;
    classDef plugin fill:#f9e79f,stroke:#b7950b,stroke-width:2px;
    classDef core fill:#d6eaf8,stroke:#2874a6,stroke-width:2px;
    classDef test fill:#f5b7b1,stroke:#c0392b,stroke-width:2px;
```
