# ieooo/clash architecture

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

- Interactive diagram: https://gitdiagram.com/ieooo/clash
- Repository: https://github.com/ieooo/clash

## Overview

Clash is a rule-based Go tunnel: configuration initializes inbound listeners, traffic enters as connection or packet contexts, routing selects a rule and proxy, and the selected outbound carries traffic onward. The architecture also includes DNS/fake-IP resolution, proxy groups and remote providers, transparent proxy modes, and a REST API for live control. The README documents several capabilities whose specific implementations are not sampled; those branches are included at subsystem level. Dashed edges mark documented but unverified runtime wiring.

Main entry points: [`tunnel/tunnel.go`](https://github.com/ieooo/clash/blob/master/tunnel/tunnel.go) (Traffic tunnel), [`adapter/outbound`](https://github.com/ieooo/clash/tree/master/adapter/outbound) (Outbound protocols), [`hub/route/server.go`](https://github.com/ieooo/clash/blob/master/hub/route/server.go) (REST controller), [`hub/executor/executor.go`](https://github.com/ieooo/clash/blob/master/hub/executor/executor.go) (Config executor)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime control"]
  node_main["Process lifecycle<br/>[main.go]"]
  node_hub["Hub orchestration<br/>[hub.go]"]
  node_executor["Config executor<br/>[executor.go]"]
end

subgraph group_ingress["Traffic ingress"]
  node_listeners["Listener manager<br/>[listener.go]"]
  node_listenertypes["HTTP / SOCKS / TUN"]
  node_inbound["Inbound adapters"]
end

subgraph group_routing["Routing and DNS"]
  node_tunnel["Traffic tunnel<br/>[tunnel.go]"]
  node_rules["Rule matching"]
  node_resolver["Resolver / fake IP"]
  node_dnsserver["DNS service<br/>[server.go]"]
  node_dnsclient["Upstream DNS<br/>[client.go]"]
end

subgraph group_egress["Proxy egress"]
  node_proxyparse["Proxy construction<br/>[parser.go]"]
  node_proxytypes["Outbound protocols"]
  node_groups["Proxy groups<br/>[parser.go]"]
  node_providers["Remote providers"]
  node_transport["Protocol transports"]
end

subgraph group_control["Management API"]
  node_api["REST controller<br/>[server.go]"]
  node_api_routes["Live config / proxy routes"]
  node_traffic_stats[("Traffic statistics<br/>[manager.go]")]
end

node_user(("Operator / client"))

node_user -->|"starts process"| node_main
node_main -->|"loads config"| node_hub
node_hub -->|"parses config"| node_executor
node_executor -->|"configures listeners"| node_listeners
node_listeners -->|"creates listeners"| node_listenertypes
node_listenertypes -.->|"adapts connections"| node_inbound
node_inbound -->|"submits traffic"| node_tunnel
node_tunnel -->|"matches metadata"| node_rules
node_tunnel -->|"resolves metadata"| node_resolver
node_dnsserver -->|"uses resolver"| node_resolver
node_dnsclient -->|"looks up host"| node_resolver
node_tunnel -.->|"dials selected proxy"| node_proxytypes
node_proxyparse -->|"constructs protocol"| node_proxytypes
node_groups -->|"groups proxies"| node_proxytypes
node_groups -->|"uses providers"| node_providers
node_proxytypes -.->|"uses transport"| node_transport
node_user -.->|"connects inbound"| node_listeners
node_user -.->|"queries DNS"| node_dnsserver
node_user -.->|"controls runtime"| node_api
node_api -->|"routes requests"| node_api_routes
node_api_routes -.->|"updates config"| node_executor
node_api -->|"reads traffic"| node_traffic_stats

click node_main "https://github.com/ieooo/clash/blob/master/main.go"
click node_hub "https://github.com/ieooo/clash/blob/master/hub/hub.go"
click node_executor "https://github.com/ieooo/clash/blob/master/hub/executor/executor.go"
click node_listeners "https://github.com/ieooo/clash/blob/master/listener/listener.go"
click node_listenertypes "https://github.com/ieooo/clash/tree/master/listener"
click node_inbound "https://github.com/ieooo/clash/tree/master/adapter/inbound"
click node_tunnel "https://github.com/ieooo/clash/blob/master/tunnel/tunnel.go"
click node_rules "https://github.com/ieooo/clash/tree/master/rule"
click node_resolver "https://github.com/ieooo/clash/tree/master/component/resolver"
click node_dnsserver "https://github.com/ieooo/clash/blob/master/dns/server.go"
click node_dnsclient "https://github.com/ieooo/clash/blob/master/dns/client.go"
click node_proxyparse "https://github.com/ieooo/clash/blob/master/adapter/parser.go"
click node_proxytypes "https://github.com/ieooo/clash/tree/master/adapter/outbound"
click node_groups "https://github.com/ieooo/clash/blob/master/adapter/outboundgroup/parser.go"
click node_providers "https://github.com/ieooo/clash/tree/master/adapter/provider"
click node_transport "https://github.com/ieooo/clash/tree/master/transport"
click node_api "https://github.com/ieooo/clash/blob/master/hub/route/server.go"
click node_api_routes "https://github.com/ieooo/clash/tree/master/hub/route"
click node_traffic_stats "https://github.com/ieooo/clash/blob/master/tunnel/statistic/manager.go"

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_main,node_hub,node_executor,node_user toneBlue
class node_listeners,node_listenertypes,node_inbound toneAmber
class node_tunnel,node_rules,node_resolver,node_dnsserver,node_dnsclient toneMint
class node_proxyparse,node_proxytypes,node_groups,node_providers,node_transport toneRose
class node_api,node_api_routes,node_traffic_stats toneIndigo
```

## Components

### Runtime control

- [Process lifecycle](https://github.com/ieooo/clash/blob/master/main.go): `main.go`
- [Hub orchestration](https://github.com/ieooo/clash/blob/master/hub/hub.go): `hub/hub.go`
- [Config executor](https://github.com/ieooo/clash/blob/master/hub/executor/executor.go): `hub/executor/executor.go`

### Traffic ingress

- [Listener manager](https://github.com/ieooo/clash/blob/master/listener/listener.go): `listener/listener.go`
- [HTTP / SOCKS / TUN](https://github.com/ieooo/clash/tree/master/listener): `listener`
- [Inbound adapters](https://github.com/ieooo/clash/tree/master/adapter/inbound): `adapter/inbound`

### Routing and DNS

- [Traffic tunnel](https://github.com/ieooo/clash/blob/master/tunnel/tunnel.go): `tunnel/tunnel.go`
- [Rule matching](https://github.com/ieooo/clash/tree/master/rule): `rule`
- [Resolver / fake IP](https://github.com/ieooo/clash/tree/master/component/resolver): `component/resolver`
- [DNS service](https://github.com/ieooo/clash/blob/master/dns/server.go): `dns/server.go`
- [Upstream DNS](https://github.com/ieooo/clash/blob/master/dns/client.go): `dns/client.go`

### Proxy egress

- [Proxy construction](https://github.com/ieooo/clash/blob/master/adapter/parser.go): `adapter/parser.go`
- [Outbound protocols](https://github.com/ieooo/clash/tree/master/adapter/outbound): `adapter/outbound`
- [Proxy groups](https://github.com/ieooo/clash/blob/master/adapter/outboundgroup/parser.go): `adapter/outboundgroup/parser.go`
- [Remote providers](https://github.com/ieooo/clash/tree/master/adapter/provider): `adapter/provider`
- [Protocol transports](https://github.com/ieooo/clash/tree/master/transport): `transport`

### Management API

- [REST controller](https://github.com/ieooo/clash/blob/master/hub/route/server.go): `hub/route/server.go`
- [Live config / proxy routes](https://github.com/ieooo/clash/tree/master/hub/route): `hub/route`
- [Traffic statistics](https://github.com/ieooo/clash/blob/master/tunnel/statistic/manager.go): `tunnel/statistic/manager.go`

### Other components

- **Operator / client**

## Connections

- Operator / client → Process lifecycle: starts process (evidence: `main.go`)
- Process lifecycle → Hub orchestration: loads config (evidence: `main.go`)
- Hub orchestration → Config executor: parses config (evidence: `hub/hub.go`)
- Config executor → Listener manager: configures listeners (evidence: `hub/executor/executor.go`)
- Listener manager → HTTP / SOCKS / TUN: creates listeners (evidence: `listener/listener.go`)
- HTTP / SOCKS / TUN → Inbound adapters: adapts connections (evidence: `listener/listener.go`)
- Inbound adapters → Traffic tunnel: submits traffic (evidence: `tunnel/tunnel.go`)
- Traffic tunnel → Rule matching: matches metadata (evidence: `tunnel/tunnel.go`)
- Traffic tunnel → Resolver / fake IP: resolves metadata (evidence: `tunnel/tunnel.go`)
- DNS service → Resolver / fake IP: uses resolver (evidence: `dns/server.go`)
- Upstream DNS → Resolver / fake IP: looks up host (evidence: `dns/client.go`)
- Traffic tunnel → Outbound protocols: dials selected proxy
- Proxy construction → Outbound protocols: constructs protocol (evidence: `adapter/parser.go`)
- Proxy groups → Outbound protocols: groups proxies (evidence: `adapter/outboundgroup/parser.go`)
- Proxy groups → Remote providers: uses providers (evidence: `adapter/outboundgroup/parser.go`)
- Outbound protocols → Protocol transports: uses transport (evidence: `adapter/outbound/vmess.go`)
- Operator / client → Listener manager: connects inbound
- Operator / client → DNS service: queries DNS
- Operator / client → REST controller: controls runtime
- REST controller → Live config / proxy routes: routes requests (evidence: `hub/route/server.go`)
- Live config / proxy routes → Config executor: updates config
- REST controller → Traffic statistics: reads traffic (evidence: `hub/route/server.go`)
