# liudf0716/xfrpc architecture

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

- Interactive diagram: https://gitdiagram.com/liudf0716/xfrpc
- Repository: https://github.com/liudf0716/xfrpc

## Overview

Here are our detailed instructions tailored for xfrpc:

##### Project Type and Purpose
- xfrpc is an open‐source C implementation of the FRP client, intended for resource‐constrained environments (e.g., OpenWrt/IoT devices).
- Its main purpose is to forward traffic from frps to local services using different proxy types (TCP, HTTP, HTTPS, SOCKS5, etc.) while keeping the binary footprint minimal.

##### Main Architectural Components
- Client Core: This is the heart of xfrpc. It manages reading configuration files (e.g., xfrpc.ini), starting the main event loop, and coordinating connection and protocol handling.
- Connection Manager: Responsible for establishing and maintaining the connection with the frps server, sending the “TypeLogin”, “TypeNewWorkConn” and heartbeat (ping/pong) messages.
- Message/Protocol Modules: These implement message types (login, login response, new work connection, start work connection, new proxy, heartbeat, etc.). You may indicate that this module handles encryption (aes-128-cfb) as seen in the sequence.
- Plugin Modules: The “plugins” folder contains files for additional functionalities (such as HTTP, instaloader, telnetd, youtubedl). Represent these as extendable sub-modules that add proxy support.
- Third-party Libraries Integration: These dependencies include libevent (for networking events), OpenSSL (for security/authentication and encrypted data), json-c (for configuration and data exchange), and zlib (compression in thirdparty). They are built either from system libraries or statically via the included thirdparty directories.
- Build and Deployment Support: This covers the CMake-based build scripts (CMakeLists.txt and cmake/Modules) as well as Docker, CI/CD directories, and systemd unit files.
- Cross-Compilation/Platform Specific Components: Indicate that xfrpc is cross-compiled for ARM (OpenWrt/IoT), MIPS, and running as a static binary in Alpine containers.
- User-facing Configuration: Use of configuration files (“xfrpc.ini”, “xfrpc_min.ini”) that define connection parameters to the frps server and proxy rules for local services.

– The initial login handshake (xfrpc sends a “TypeLogin Message” and receives “TypeLoginResp Message”).
– The server’s request to create work connections based on a pool value (illustrated in a loop).
– The cycle of sending and receiving “ping/pong” messages for heartbeat.
– Finally, the forwarding flow from the remote user (through frps) to xfrpc and then to the local service and vice‐versa.

##### System Design Elements
- Acknowledge architectural design patterns: Notice that the client implicitly uses an event-driven design (supported by libevent) and “plugin” extension points.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Systems
    extUser["Remote Access User"]:::external
    frps["frps Server"]:::external
    local["Local Services"]:::external

    %% xfrpc Client Subgraph
    subgraph "xfrpc Client"
        direction TB
        CC["Client Core"]:::client
        CM["Connection Manager"]:::cm
        MP["Message/Protocol Modules"]:::msgproto
        PM["Plugin Modules"]:::plugin
        UC["User-facing Configuration"]:::config
    end

    %% Support Layers
    TPL["Third-party Libraries"]:::thirdparty
    BD["Build & Deployment"]:::build

    %% External Interactions and Data Flows
    extUser -->|"sendsRequest"| frps
    frps -->|"forwardsRequest"| CM
    CM -->|"sendsLoginMessage"| CC
    CC -->|"loadsConfig"| UC
    CC -->|"invokesProtocol"| MP
    MP -->|"handlesHandshake&HBeat"| CM
    CC -->|"delegatesTo"| PM
    TPL --- CC
    TPL --- CM
    TPL --- MP
    BD --- CC

    frps -->|"sendsWorkConn"| CC
    CC -->|"forwardsTraffic"| local
    local -->|"responseTo"| CC
    CC -->|"returnsResponseVia"| frps
    frps -->|"sendsResponse"| extUser

    %% Heartbeat Cycle
    CM -->|"ping"| frps
    frps -->|"pong"| CM

    %% Work Connection Callback
    PM -->|"Based_on_PoolCount"| CC

    %% Click Events
    click CC "https://github.com/liudf0716/xfrpc/blob/master/xfrpc.c"
    click CM "https://github.com/liudf0716/xfrpc/blob/master/login.c"
    click MP "https://github.com/liudf0716/xfrpc/blob/master/msg.c"
    click PM "https://github.com/liudf0716/xfrpc/tree/master/plugins"
    click TPL "https://github.com/liudf0716/xfrpc/tree/master/thirdparty"
    click BD "https://github.com/liudf0716/xfrpc/blob/master/CMakeLists.txt"
    click UC "https://github.com/liudf0716/xfrpc/blob/master/xfrpc.ini"

    %% Styles
    classDef external fill:#ffddcc,stroke:#aa3333,stroke-width:2px;
    classDef client fill:#cce5ff,stroke:#003366,stroke-width:2px;
    classDef cm fill:#ffcccc,stroke:#990000,stroke-width:2px;
    classDef msgproto fill:#fff2cc,stroke:#996600,stroke-width:2px;
    classDef plugin fill:#d5f5e3,stroke:#1e8449,stroke-width:2px;
    classDef config fill:#fcf3cf,stroke:#b7950b,stroke-width:2px;
    classDef thirdparty fill:#fadbd8,stroke:#922b21,stroke-width:2px;
    classDef build fill:#e8eaf6,stroke:#3f51b5,stroke-width:2px;
```
