# fscarmen/sing-box architecture

> Architecture diagram of the GitHub repository fscarmen/sing-box, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/fscarmen/sing-box
- Repository: https://github.com/fscarmen/sing-box

## Overview

1. **Repository type and purpose**
This is not an application server in the usual sense; it is a **deployment and lifecycle automation repo** for a “sing-box full bundle” on VPS or inside Docker. The core job is to install, configure, start, update, and expose a multi-protocol sing-box setup, plus generate client-facing node/subscription outputs.

2. **Primary entrypoint**
The main control surface is **`sing-box.sh`**, which acts as the interactive CLI and non-interactive installer/manager. The README shows it being invoked directly from GitHub raw content, and later reused as the local `sb` command. It is the central orchestrator for install, modify, export, stop/start, upgrade, and uninstall flows.

3. **Configuration input paths**
There are three supported configuration paths:
- interactive menu-driven setup,
- KV-style command-line flags,
- `config.conf` for file-based unattended install.
In Docker mode, the same parameters are supplied via environment variables. Architecturally, this means the repo has a **single logical configuration model** with multiple input adapters.

4. **Runtime target and service boundary**
The deployed system runs on a VPS or inside a container and manages the real runtime under **`/etc/sing-box/`**. That directory is the main boundary between “installer” and “running system.” It contains:
- sing-box server config fragments,
- certificates,
- logs,
- generated node lists,
- subscription artifacts,
- tunnel metadata,
- helper binaries and launch scripts.

5. **Core runtime components**
The runtime is composed of three main processes:
- **sing-box** itself as the proxy core,
- **nginx** for subscription delivery when enabled,
- **cloudflared** for Argo Tunnel when external ingress is needed without opening ports or when websocket-based protocols need CDN-style exposure.
The installer decides which of these are present and how they connect.

6. **Configuration assembly model**
The `conf/` directory is split into many numbered JSON fragments such as logging, outbounds, DNS, route, experimental settings, NTP, HTTP clients, and protocol-specific inbound definitions. This indicates a **modular config assembly pipeline**: the script composes final sing-box behavior from reusable fragments instead of keeping one monolithic config file.

7. **Protocol family as pluggable subsystems**
The bundle supports many protocol modules, each mapped to its own inbound config:
- Reality / XTLS
- Hysteria2
- Tuic
- ShadowTLS
- Shadowsocks
- Trojan
- VMess over WebSocket
- VLESS over WebSocket + TLS
- H2 Reality
- gRPC Reality
- AnyTLS
- NaiveProxy
These are treated as **feature switches**, not separate apps. The installer allocates ports, certificates, and client outputs per selected protocol set.

8. **Subscription and client export pipeline**
The `subscribe/` directory is the artifact layer for client consumption. It contains multiple output formats for different client families such as:
- Clash / Mihomo
- V2rayN
- NekoBox / Nekoray
- Shadowrocket
- sing-box mobile/desktop variants
- QR and proxy-provider formats
This shows a second major data flow: **server-side config → client-specific subscription/link exports**.

9. **CDN / Argo / origin exposure layer**
The repo supports three exposure strategies:
- direct origin access,
- Cloudflare Argo Tunnel temporary mode,
- Argo Tunnel with JSON/Token/API-backed fixed domains.
For websocket-oriented protocols, it can also use CDN/origin-rule style routing. This boundary is important because the deployment can run **with or without an exposed inbound port**, and the chosen exposure mode affects subscription URLs and connection parameters.

10. **Network and security plumbing**
The installer manages system-level networking concerns such as UFW vs iptables selection, certificate generation/usage, and WARP-assisted routing for certain outbound or hole-punch scenarios. It also supports certificate fingerprint/pin behavior that differs by client family, which affects how generated client configs are validated and trusted.

11. **Docker packaging path**
The **`Dockerfile`** and **`docker_init.sh`** provide a containerized variant of the same bundle. In Docker, host networking is used, and the runtime is driven by environment variables instead of shell prompts. The architecture remains the same, but the service manager boundary shifts from host init systems to container startup scripts.

12. **Version pinning and release control**
The **`force_version`** file implies explicit sing-box version pinning to avoid breaking upgrades. That fits the repo’s role as a compatibility layer over a fast-moving upstream core. The installer therefore manages not only deployment but also **upstream version selection and upgrade safety**.

13. **CI/CD and distribution**
The **GitHub Actions workflows** (`build.yml`, `mirror.yml`) indicate automated build and mirroring of the Docker image or release artifacts. This repo is therefore split into:
- source-of-truth scripts and templates,
- generated runtime artifacts,
- CI-produced distribution assets.

14. **External dependencies and operating-system adapters**
The implementation is designed to adapt across Linux distributions and service managers, including Ubuntu/Debian/CentOS/Alpine/Arch/Armbian, with Alpine using OpenRC where needed. External services that materially affect behavior include Cloudflare APIs/Argo, GitHub raw/CDN hosting, and whatever package manager or init system is present on the host.

15. **High-level data flow**
A useful architecture diagram would show:
User input or env vars → `sing-box.sh` / `docker_init.sh` → generated `/etc/sing-box` configs and secrets → sing-box + optional nginx/cloudflared services → exported subscriptions/QR/link files → client apps.  
A parallel path is CI → Docker image publish / mirrored artifacts → deployment.

Main entry points: [`sing-box.sh`](https://github.com/fscarmen/sing-box/blob/main/sing-box.sh) (sing-box.sh), [`docker_init.sh`](https://github.com/fscarmen/sing-box/blob/main/docker_init.sh) (docker_init.sh), [`.github/workflows/build.yml`](https://github.com/fscarmen/sing-box/blob/main/.github/workflows/build.yml) (build.yml), [`Dockerfile`](https://github.com/fscarmen/sing-box/tree/main/Dockerfile) (Dockerfile)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_cli["Deployment Control"]
  node_sing_box_sh["sing-box.sh<br/>installer/manager<br/>[sing-box.sh]"]
  node_docker_init_sh["docker_init.sh<br/>container entrypoint<br/>[docker_init.sh]"]
  node_config_conf["config.conf<br/>unattended config<br/>[config.conf]"]
  node_force_version["force_version<br/>version pin"]
end

subgraph group_group_runtime["Runtime System"]
  node_runtime_root[("/etc/sing-box<br/>host runtime")]
  node_config_assembly["Config assembly<br/>modular config pipeline"]
  node_protocol_modules{{"Protocol modules<br/>inbound presets"}}
  node_sb_core["sing-box core<br/>proxy engine"]
  node_nginx["nginx<br/>subscription web"]
  node_cloudflared["cloudflared<br/>tunnel ingress"]
  node_exposure_mode{{"Exposure mode<br/>origin/cdn routing"}}
  node_security_plumbing{{"Net and TLS<br/>host plumbing"}}
end

subgraph group_group_outputs["Client Artifacts"]
  node_subscribe_dir[("export artifacts<br/>client exports")]
  node_client_apps["Client apps<br/>consumer clients"]
end

subgraph group_group_ci["Build and Release"]
  node_build_workflow["build.yml<br/>ci workflow<br/>[build.yml]"]
  node_mirror_workflow["mirror.yml<br/>mirror workflow<br/>[mirror.yml]"]
  node_dockerfile["Dockerfile<br/>image spec"]
end

node_config_conf -->|"file input"| node_sing_box_sh
node_force_version -->|"pins"| node_sing_box_sh
node_sing_box_sh -->|"installs"| node_runtime_root
node_docker_init_sh -->|"starts"| node_runtime_root
node_sing_box_sh -->|"generates"| node_config_assembly
node_docker_init_sh -->|"feeds env"| node_config_assembly
node_config_assembly -->|"selects"| node_protocol_modules
node_config_assembly -->|"launches"| node_sb_core
node_sing_box_sh -->|"configures"| node_security_plumbing
node_security_plumbing -->|"conditions"| node_exposure_mode
node_exposure_mode -->|"optional"| node_nginx
node_exposure_mode -->|"optional"| node_cloudflared
node_nginx -->|"serves"| node_subscribe_dir
node_cloudflared -->|"fronts"| node_sb_core
node_sb_core -->|"exports"| node_subscribe_dir
node_subscribe_dir -->|"consumed by"| node_client_apps
node_build_workflow -->|"builds"| node_dockerfile
node_mirror_workflow -->|"mirrors"| node_dockerfile
node_build_workflow -->|"packages"| node_docker_init_sh
node_sing_box_sh -.->|"publishes"| node_build_workflow

click node_sing_box_sh "https://github.com/fscarmen/sing-box/blob/main/sing-box.sh"
click node_docker_init_sh "https://github.com/fscarmen/sing-box/blob/main/docker_init.sh"
click node_config_conf "https://github.com/fscarmen/sing-box/blob/main/config.conf"
click node_force_version "https://github.com/fscarmen/sing-box/tree/main/force_version"
click node_build_workflow "https://github.com/fscarmen/sing-box/blob/main/.github/workflows/build.yml"
click node_mirror_workflow "https://github.com/fscarmen/sing-box/blob/main/.github/workflows/mirror.yml"
click node_dockerfile "https://github.com/fscarmen/sing-box/tree/main/Dockerfile"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_sing_box_sh,node_docker_init_sh,node_config_conf,node_force_version toneBlue
class node_runtime_root,node_config_assembly,node_protocol_modules,node_sb_core,node_nginx,node_cloudflared,node_exposure_mode,node_security_plumbing toneAmber
class node_subscribe_dir,node_client_apps toneMint
class node_build_workflow,node_mirror_workflow,node_dockerfile toneRose
```

## Components

### Deployment Control

Installer and container entrypoints that orchestrate setup, updates, and lifecycle management.

- [sing-box.sh](https://github.com/fscarmen/sing-box/blob/main/sing-box.sh): installer/manager · Main interactive and non-interactive CLI for install, configure, modify, export, start, stop, upgrade, and uninstall flows. · `sing-box.sh`
- [docker_init.sh](https://github.com/fscarmen/sing-box/blob/main/docker_init.sh): container entrypoint · Container startup script that adapts the same logical bundle to Docker runtime and env-based configuration. · `docker_init.sh`
- [config.conf](https://github.com/fscarmen/sing-box/blob/main/config.conf): unattended config · File-based configuration input for scripted installation and repeatable deployment. · `config.conf`
- [force_version](https://github.com/fscarmen/sing-box/tree/main/force_version): version pin · Pinned upstream sing-box version selection used to control upgrade compatibility. · `force_version`

### Runtime System

The installed sing-box bundle and its supporting services under the target host boundary.

- **/etc/sing-box**: host runtime · Installed system boundary holding generated configs, secrets, logs, helpers, and service metadata.
- **Config assembly**: modular config pipeline · Composes final sing-box behavior from reusable fragments rather than a monolithic config.
- **Protocol modules**: inbound presets · Feature-selected inbound definitions for Reality, Hysteria2, Tuic, ShadowTLS, Shadowsocks, Trojan, VMess/VLESS variants, AnyTLS, and NaiveProxy.
- **sing-box core**: proxy engine · Primary proxy process that serves the deployed bundle.
- **nginx**: subscription web · Optional HTTP delivery layer for subscriptions and related client-facing artifacts.
- **cloudflared**: tunnel ingress · Optional Argo Tunnel process used for external exposure without open inbound ports.
- **Exposure mode**: origin/cdn routing · Direct origin, Argo tunnel, or CDN/websocket-oriented exposure strategy chosen during deployment.
- **Net and TLS**: host plumbing · Firewall selection, certificate handling, fingerprint behavior, and WARP-assisted routing decisions.

### Client Artifacts

Generated subscription, link, and QR outputs for downstream clients.

- **export artifacts**: client exports · Artifact layer for client-specific subscriptions, links, QR codes, and proxy-provider formats.
- **Client apps**: consumer clients · Downstream clients such as Clash/Mihomo, V2rayN, NekoBox/Nekoray, Shadowrocket, and sing-box variants.

### Build and Release

Automation used to build, mirror, and distribute the bundle.

- [build.yml](https://github.com/fscarmen/sing-box/blob/main/.github/workflows/build.yml): ci workflow · Automation for building distributable artifacts and images. · `.github/workflows/build.yml`
- [mirror.yml](https://github.com/fscarmen/sing-box/blob/main/.github/workflows/mirror.yml): mirror workflow · Automation for mirroring releases or images to alternate distribution targets. · `.github/workflows/mirror.yml`
- [Dockerfile](https://github.com/fscarmen/sing-box/tree/main/Dockerfile): image spec · Container build definition for the packaged deployment path. · `Dockerfile`

## Connections

- config.conf → sing-box.sh: file input · Provides unattended configuration to the installer.
- force_version → sing-box.sh: pins · Constrains upstream version selection during install and upgrade flows.
- sing-box.sh → /etc/sing-box: installs · Creates and manages the host runtime boundary and its contents.
- docker_init.sh → /etc/sing-box: starts · Initializes the same runtime model inside a container.
- sing-box.sh → Config assembly: generates · Builds runtime configuration from modular fragments.
- docker_init.sh → Config assembly: feeds env · Maps container environment variables into the same logical config model.
- Config assembly → Protocol modules: selects · Enables protocol-specific inbound presets as feature switches.
- Config assembly → sing-box core: launches · Produces the final core configuration consumed by sing-box.
- sing-box.sh → Net and TLS: configures · Handles firewall, certificate, and routing-related deployment choices.
- Net and TLS → Exposure mode: conditions · Influences which ingress strategy is valid for the deployment.
- Exposure mode → nginx: optional · Uses nginx when HTTP subscription delivery is enabled.
- Exposure mode → cloudflared: optional · Uses cloudflared when tunnel-based exposure is selected.
- nginx → export artifacts: serves · Publishes generated client artifacts over HTTP.
- cloudflared → sing-box core: fronts · Exposes sing-box traffic through tunnel ingress.
- sing-box core → export artifacts: exports · Produces client-facing links, subscriptions, and QR artifacts.
- export artifacts → Client apps: consumed by · Feeds multiple downstream client families and formats.
- build.yml → Dockerfile: builds · Creates distributable container artifacts from the repo source.
- mirror.yml → Dockerfile: mirrors · Replicates built image or release assets for distribution.
- build.yml → docker_init.sh: packages · Bundles the container entrypoint into the release path.
- sing-box.sh → build.yml: publishes · Source scripts and templates are the basis for CI-produced artifacts.
