# pasarguard/panel architecture

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

- Interactive diagram: https://gitdiagram.com/pasarguard/panel
- Repository: https://github.com/pasarguard/panel

## Overview

##### Project Type and Purpose
- PasarGuard is a full-stack, proxy-management platform combining:
- A Python-based REST API backend (FastAPI or similar)
- A React.js dashboard frontend
- A CLI and TUI client
- Multi-node support via Xray-core agents
- Notification integrations (Telegram, Discord, Webhook)

##### File Structure
- Top-level directories:
- app/             → Core backend service (routers, DB models, business logic, jobs, notification clients)
- dashboard/       → React.js SPA (Vite, Tailwind CSS) for the web UI
- cli/             → Command-line interface for administrative operations
- tui/             → Text-UI client for terminal use
- tests/           → API and functional tests
- config files     → Dockerfile, docker-compose.yml, alembic.ini, pyproject.toml
- scripts          → install_service.sh, start.sh, build_dashboard.sh
- Key backend substructures:
- app/routers      → HTTP endpoints grouping (authentication, admin, host, user, etc.)
- app/operation    → Application services layer (business use-cases)
- app/core         → Low-level integrations (Xray control, host manager)
- app/db           → Database schema (SQLAlchemy), CRUD, Alembic migrations
- app/notification → Clients for Telegram, Discord, Webhook
- app/subscription → Logic to build and share proxy subscription links
- app/jobs         → Periodic tasks (cleanup, notifications, usage recording)
- Frontend structure:
- src/pages, src/components → Visual pages, UI components, charts, dialogs
- src/service/api         → HTTP client to backend
- Deployment:
- Dockerfile & docker-compose.yml
- .env files, Makefile, CI workflows (.github/workflows)

##### README for Additional Insights
- Describes multi-node proxy distribution via Xray-core
- REST API + Web UI + CLI + Telegram Bot + Discord support
- Supports Vmess, VLESS, Trojan, Shadowsocks protocols
- Subscription link generator (Clash, Singbox, Outline) and QR code creation

a. Main Components
- Web Dashboard (React.js SPA)
- REST API Backend (Python service)
- Database (PostgreSQL / TimescaleDB / MySQL / SQLite)
- Xray-core Proxy Nodes (multiple distributed agents)
- CLI & TUI Clients (pasarguard CLI, TUI)
- Notification Services (Telegram Bot, Discord Bot, Webhook endpoint)
b. Relationships & Data Flows
- Dashboard ↔ Backend: HTTPS REST calls (GET/POST)
- Backend ↔ Database: SQLAlchemy ORM queries & migrations
- Backend ↔ Xray-core Nodes: RPC or file-based config deployment
- Backend ↔ Notification Clients: HTTP API calls (Telegram Bot API, Discord webhooks)
- CLI/TUI ↔ Backend: Direct Python modules or REST API calls
- Subscription Generation: Backend reads DB & node data → builds subscription payload → served via API
c. Architectural Patterns & Principles
- Layered architecture: Routers → Operation (services) → Core integrations → DB layer
- Modular plugins: notification clients, subscription formats
- Asynchronous jobs for periodic tasks (cleanup, stats, notifications)
- Infrastructure as code: Docker, Compose, CI pipelines
d. Key Technologies
- Backend: Python 3.x, FastAPI (or similar), SQLAlchemy, Alembic, Xray-core
- Frontend: React.js, Vite, TypeScript, Tailwind CSS
- CLI/TUI: Python click / textual (TUI)
- Database: PostgreSQL / TimescaleDB / MySQL / SQLite
- Notifications: Telegram Bot API, Discord Webhooks
- Deployment: Docker, docker-compose, shell scripts

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "Presentation Layer"
        Dashboard["Web Dashboard (React.js SPA)"]:::frontend
        CLI["CLI Client"]:::frontend
        TUI["TUI Client"]:::frontend
    end

    subgraph "Application Layer"
        API["REST API Backend"]:::api
        subgraph "Core Backend Modules"
            Routers["HTTP Routers"]:::api
            Ops["Business Logic / Service Layer"]:::service
            CoreInt["Core Integrations (Xray & Host Manager)"]:::service
            DBLayer["Database Layer (ORM & CRUD)"]:::database
            Subscription["Subscription Service"]:::service
            subgraph "Notification Clients"
                Telegram["Telegram Client"]:::external
                Discord["Discord Client"]:::external
                Webhook["Webhook Client"]:::external
            end
            Jobs["Background Jobs (Periodic Tasks)"]:::service
        end
    end

    subgraph "Data & Infrastructure Layer"
        DB["Database (PostgreSQL/TimescaleDB)"]:::database
        Models["Data Models (SQLAlchemy)"]:::database
        Migrations["Alembic Migrations"]:::database
        Proxies["Xray-core Proxy Nodes"]:::nodes
        subgraph "Deployment & Config"
            Dockerfile["Dockerfile"]:::infra
            Compose["docker-compose.yml"]:::infra
            StartScript["start.sh"]:::infra
            Install["install_service.sh"]:::infra
        end
        ExternalServices["External Notification Services"]:::external
    end

    %% Connections
    Dashboard -->|"HTTPS REST API"| API
    CLI -->|"REST/API or Direct Modules"| API
    TUI -->|"REST/API or Direct Modules"| API

    API -->|"routes calls"| Routers
    Routers -->|"execute use-cases"| Ops
    Ops -->|"invoke integrations"| CoreInt
    CoreInt -->|"config deployment"| Proxies
    Ops -->|"ORM operations"| DBLayer
    DBLayer -->|"SQL queries"| DB
    DB -->|"schema definitions"| Models
    DB -->|"migrations"| Migrations
    Ops -->|"generate subscriptions"| Subscription
    Subscription -->|"serve via API"| API

    Ops -->|"trigger notifications"| Telegram
    Ops -->|"trigger notifications"| Discord
    Ops -->|"trigger notifications"| Webhook
    Jobs -->|"periodic tasks"| Ops

    Telegram -->|"Telegram Bot API"| ExternalServices
    Discord -->|"Discord Webhook"| ExternalServices
    Webhook -->|"HTTP Webhook"| ExternalServices

    %% Click Events
    click Dashboard "https://github.com/pasarguard/panel/tree/main/dashboard/"
    click CLI "https://github.com/pasarguard/panel/tree/main/cli/"
    click TUI "https://github.com/pasarguard/panel/tree/main/tui/"
    click API "https://github.com/pasarguard/panel/blob/main/main.py"
    click Routers "https://github.com/pasarguard/panel/tree/main/app/routers/"
    click Ops "https://github.com/pasarguard/panel/tree/main/app/operation/"
    click CoreInt "https://github.com/pasarguard/panel/tree/main/app/core/"
    click DBLayer "https://github.com/pasarguard/panel/tree/main/app/db/"
    click Subscription "https://github.com/pasarguard/panel/tree/main/app/subscription/"
    click Telegram "https://github.com/pasarguard/panel/tree/main/app/notification/telegram/"
    click Discord "https://github.com/pasarguard/panel/tree/main/app/notification/discord/"
    click Webhook "https://github.com/pasarguard/panel/tree/main/app/notification/webhook/"
    click Jobs "https://github.com/pasarguard/panel/tree/main/app/jobs/"
    click DB "https://github.com/pasarguard/panel/tree/main/app/db/"
    click Models "https://github.com/pasarguard/panel/blob/main/app/models.py"
    click Migrations "https://github.com/pasarguard/panel/tree/main/app/db/migrations/"
    click Dockerfile "https://github.com/pasarguard/panel/tree/main/Dockerfile"
    click Compose "https://github.com/pasarguard/panel/blob/main/docker-compose.yml"
    click StartScript "https://github.com/pasarguard/panel/blob/main/start.sh"
    click Install "https://github.com/pasarguard/panel/blob/main/install_service.sh"

    %% Styles
    classDef frontend fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef api fill:#90EE90,stroke:#333,stroke-width:1px
    classDef service fill:#98FB98,stroke:#333,stroke-width:1px
    classDef database fill:#D3D3D3,stroke:#333,stroke-width:1px
    classDef nodes fill:#FFA500,stroke:#333,stroke-width:1px
    classDef external fill:#DDA0DD,stroke:#333,stroke-width:1px
    classDef infra fill:#F0E68C,stroke:#333,stroke-width:1px
```
