# byjoey/cfnew architecture

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

- Interactive diagram: https://gitdiagram.com/byjoey/cfnew
- Repository: https://github.com/byjoey/cfnew

## Overview

1. This repository is a Cloudflare edge application, not a traditional server app.  
   Its core runtime is a Cloudflare Worker/Pages deployment configured by `wrangler.toml`, with the worker acting as both a control plane and a traffic relay.

2. The project has two equivalent source variants: `明文源吗` and the obfuscated `少年你相信光吗`.  
   The README says they implement the same logic, so the architecture is the same regardless of which file is deployed.

3. The main external entrypoint is a UUID-based or custom-path HTTP surface.  
   That single edge endpoint serves several roles: a graphical admin UI, subscription generation, client auto-detection, and REST API management.

4. Configuration is split between immutable deployment settings and mutable runtime state.  
   Immutable defaults come from environment variables in Worker/Pages, while mutable settings are stored in Cloudflare KV bound as `C`.  
   The README emphasizes that KV changes take effect immediately without redeploying.

5. The control plane centers on the browser UI.  
   An operator opens the configured path, edits protocol and routing settings, toggles features like ECH/ALPN/proxy selection, and saves them back to KV.  
   Local browser state is also used for UI preferences such as language and visual effects.

6. There is a small management API layered onto the same path namespace.  
   Under `/api/preferred-ips`, the worker accepts GET/POST/DELETE to query, add, or clear preferred IPs.  
   This API is gated by an explicit `ae` flag and is meant for dynamic IP list management without code changes.

7. The data plane is the tunnel/proxy path.  
   The worker accepts VLESS, Trojan, and xhttp connections, then forwards traffic through Cloudflare edge logic toward selected ProxyIP targets or an optional SOCKS5 fallback.  
   Path parameters can override connection-level routing such as `p`, `wk`, `rm`, and `s`, with `p` and `wk` treated as mutually exclusive.

8. Subscription generation is a major subsystem.  
   The worker directly produces Clash, Stash, Sing-box, Surge, Loon, Quantumult X, V2Ray, Shadowrocket, Nekoray, and V2RayNG outputs.  
   According to the README, it no longer depends on an external sub-converter; rule sets are assembled inside the Worker, with remote rule sources used only where those client formats require them.

9. Preferred IP selection is another distinct subsystem.  
   It can use manually configured endpoints, default GitHub-derived lists, or custom source URLs, then filter by region, latency, and top-N selection.  
   This logic feeds both the proxy/tunnel path and the generated subscription nodes.

10. ECH support adds a runtime dependency on external DNS-over-HTTPS endpoints.  
    On refresh, the worker fetches ECH configuration from DoH providers, preferring Google DNS and falling back to Cloudflare DNS, then injects the resulting config into generated TLS-capable clients.

11. The repository includes CI/CD support through GitHub Actions.  
    `.github/workflows/test.yml` suggests validation/testing, and `.github/workflows/obfuscate.yml` supports producing the obfuscated variant.  
    So the repo’s build pipeline is oriented around packaging and publishing edge code, not compiling a multi-service backend.

12. The important architectural boundary is Cloudflare itself.  
    All request handling, configuration storage, and most caching happen at the edge; there is no separate database, app server, or message queue in the repo.  
    External dependencies are limited to KV storage, SOCKS5/proxy endpoints, DoH services, and rule/subscription source URLs.

13. For an architecture diagram, the key flow is: client browser or VPN client → Cloudflare Worker endpoint → KV/env/config resolution → either UI/subscription response or tunneling/proxy forwarding.  
    A separate side flow is GitHub Actions producing or validating the obfuscated/plaintext deployable code.

Main entry points: [`明文源吗`](https://github.com/byjoey/cfnew/tree/main/明文源吗) (Plain source), [`wrangler.toml`](https://github.com/byjoey/cfnew/blob/main/wrangler.toml) (Wrangler), [`少年你相信光吗`](https://github.com/byjoey/cfnew/tree/main/少年你相信光吗) (Obfuscated source), [`.github/workflows/test.yml`](https://github.com/byjoey/cfnew/blob/main/.github/workflows/test.yml) (GitHub Actions)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_edge["Edge runtime"]
  node_wrangler["Wrangler<br/>deployment config<br/>[wrangler.toml]"]
  node_plain_src["Plain source<br/>worker source"]
  node_obf_src["Obfuscated source<br/>worker source"]
end

subgraph group_control["Control plane"]
  node_ui["Admin UI<br/>browser control plane"]
  node_runtime_kv[("Runtime KV<br/>mutable config store")]
  node_env_defaults["Env defaults<br/>deploy-time config"]
  node_preferred_api["IP API<br/>management api"]
end

subgraph group_data["Data plane"]
  node_relay{{"Tunnel relay<br/>traffic plane"}}
  node_protocols["Protocols<br/>connection adapters"]
  node_route_rules["Route rules<br/>routing controls"]
  node_preferred_ips[("Preferred IPs<br/>ip selection")]
  node_socks_fallback(("SOCKS5<br/>proxy fallback"))
  node_ech["ECH fetcher<br/>tls enhancement"]
  node_doh(("DoH providers<br/>external dns"))
end

subgraph group_subs["Subscriptions"]
  node_subs_engine["Subscription engine<br/>client profile generator"]
end

subgraph group_ci["CI/CD"]
  node_github_actions["GitHub Actions<br/>ci pipeline<br/>[test.yml]"]
  node_obfuscate_wf["Obfuscate<br/>ci workflow<br/>[obfuscate.yml]"]
end

node_wrangler -->|"deploys"| node_plain_src
node_wrangler -->|"deploys"| node_obf_src
node_wrangler -->|"binds"| node_env_defaults
node_plain_src -->|"serves"| node_ui
node_plain_src -->|"exposes"| node_preferred_api
node_plain_src -->|"generates"| node_subs_engine
node_plain_src -->|"handles"| node_relay
node_plain_src -->|"refreshes"| node_ech
node_ui -->|"writes"| node_runtime_kv
node_preferred_api -->|"reads/writes"| node_runtime_kv
node_runtime_kv -->|"feeds"| node_preferred_ips
node_env_defaults -->|"defaults"| node_route_rules
node_relay -->|"terminates"| node_protocols
node_protocols -->|"applies"| node_route_rules
node_route_rules -->|"selects"| node_preferred_ips
node_preferred_ips -->|"falls back"| node_socks_fallback
node_subs_engine -->|"uses"| node_preferred_ips
node_ech -->|"queries"| node_doh
node_github_actions -->|"validates"| node_plain_src
node_obfuscate_wf -->|"builds"| node_obf_src

click node_wrangler "https://github.com/byjoey/cfnew/blob/main/wrangler.toml"
click node_plain_src "https://github.com/byjoey/cfnew/tree/main/明文源吗"
click node_obf_src "https://github.com/byjoey/cfnew/tree/main/少年你相信光吗"
click node_github_actions "https://github.com/byjoey/cfnew/blob/main/.github/workflows/test.yml"
click node_obfuscate_wf "https://github.com/byjoey/cfnew/blob/main/.github/workflows/obfuscate.yml"

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_wrangler,node_plain_src,node_obf_src toneBlue
class node_ui,node_runtime_kv,node_env_defaults,node_preferred_api toneAmber
class node_relay,node_protocols,node_route_rules,node_preferred_ips,node_socks_fallback,node_ech,node_doh toneMint
class node_subs_engine toneRose
class node_github_actions,node_obfuscate_wf toneIndigo
```

## Components

### Edge runtime

Cloudflare Worker/Pages deployment and request handling

- [Wrangler](https://github.com/byjoey/cfnew/blob/main/wrangler.toml): deployment config · Cloudflare edge deployment settings and bindings · `wrangler.toml`
- [Plain source](https://github.com/byjoey/cfnew/tree/main/明文源吗): worker source · Readable implementation of the edge app · `明文源吗`
- [Obfuscated source](https://github.com/byjoey/cfnew/tree/main/少年你相信光吗): worker source · Equivalent obfuscated implementation used for publishing · `少年你相信光吗`

### Control plane

Browser UI and runtime configuration management

- **Admin UI**: browser control plane · Edge-served configuration UI for operators
- **Runtime KV**: mutable config store · Cloudflare KV-backed settings that take effect immediately
- **Env defaults**: deploy-time config · Immutable worker/Pages environment settings
- **IP API**: management api · GET/POST/DELETE surface for preferred IP management

### Data plane

Protocol handling, routing, and proxy forwarding

- **Tunnel relay**: traffic plane · Accepts client connections and forwards traffic at the edge
- **Protocols**: connection adapters · VLESS, Trojan, and xhttp handling
- **Route rules**: routing controls · Path parameters and flags that steer connection behavior
- **Preferred IPs**: ip selection · Manual, default, or custom IP sources with filtering and top-N selection
- **SOCKS5**: proxy fallback · Optional downstream proxy used when selected
- **ECH fetcher**: tls enhancement · Refreshes ECH config from DoH providers for TLS-capable clients
- **DoH providers**: external dns · External DNS-over-HTTPS endpoints used for ECH retrieval

### Subscriptions

Client profile generation and format-specific outputs

- **Subscription engine**: client profile generator · Builds subscriptions for multiple client formats inside the worker

### CI/CD

Workflow automation for validation and obfuscation

- [GitHub Actions](https://github.com/byjoey/cfnew/blob/main/.github/workflows/test.yml): ci pipeline · Workflow automation for validation and release packaging · `.github/workflows/test.yml`
- [Obfuscate](https://github.com/byjoey/cfnew/blob/main/.github/workflows/obfuscate.yml): ci workflow · Produces the obfuscated deployable source variant · `.github/workflows/obfuscate.yml`

## Connections

- Wrangler → Plain source: deploys · Worker runtime is configured to run the readable source
- Wrangler → Obfuscated source: deploys · Workflow can publish the obfuscated equivalent
- Wrangler → Env defaults: binds · Deployment environment supplies immutable defaults
- Plain source → Admin UI: serves · Same edge endpoint exposes the browser control surface
- Plain source → IP API: exposes · The worker mounts the preferred IP management API
- Plain source → Subscription engine: generates · Client subscriptions are produced directly in the worker
- Plain source → Tunnel relay: handles · Tunnel/proxy traffic is accepted on the same endpoint
- Plain source → ECH fetcher: refreshes · Runtime can fetch and inject ECH config
- Admin UI → Runtime KV: writes · Operator changes are persisted to KV
- IP API → Runtime KV: reads/writes · Preferred IP state is managed dynamically without redeploying
- Runtime KV → Preferred IPs: feeds · Mutable runtime settings drive IP selection
- Env defaults → Route rules: defaults · Immutable settings provide baseline routing behavior
- Tunnel relay → Protocols: terminates · VLESS, Trojan, and xhttp connections are parsed here
- Protocols → Route rules: applies · Connection overrides such as p, wk, rm, and s steer routing
- Route rules → Preferred IPs: selects · Chosen targets determine which edge IPs or proxies are used
- Preferred IPs → SOCKS5: falls back · Optional SOCKS5 proxy can be used as an alternate path
- Subscription engine → Preferred IPs: uses · Generated client nodes incorporate selected preferred IPs
- ECH fetcher → DoH providers: queries · ECH config is retrieved from external DoH services
- GitHub Actions → Plain source: validates · Testing workflow targets the worker codebase
- Obfuscate → Obfuscated source: builds · Obfuscation workflow produces the publishable obfuscated variant
