Module Structure
xrat follows a modular architecture with clear separation of concerns across CLI parsing, command handlers, config parsing, database access, and engine integration.
Component Diagram
Arrows show dependency direction (A โ B means A depends on B).
graph TB
classDef entry fill:#1a2744,stroke:#4a9eff,color:#e6edf3
classDef iface fill:#1a3a2a,stroke:#5bdf8a,color:#e6edf3
classDef app fill:#2a1a3a,stroke:#b070df,color:#e6edf3
classDef domain fill:#2e2a1a,stroke:#dfba5b,color:#e6edf3
classDef store fill:#1a2e2e,stroke:#5bcfdf,color:#e6edf3
classDef engine fill:#2e1a1a,stroke:#df6060,color:#e6edf3
classDef probe fill:#2a2a1a,stroke:#c0df5b,color:#e6edf3
main["main.rs"]:::entry
subgraph ui["User Interfaces"]
cli["cli/"]:::iface
server["server/"]:::iface
tui["tui/"]:::iface
end
subgraph app_layer["Application Layer"]
cmds["commands/"]:::app
daemon["daemon/"]:::app
rtsvc["runtime_service/"]:::app
end
subgraph domain_layer["Domain & Config"]
model["model/"]:::domain
config["config/"]:::domain
support["support/"]:::domain
end
db["db/"]:::store
subgraph engine_layer["Proxy Engines"]
xray["xray/"]:::engine
singbox["singbox/"]:::engine
end
prober["prober/"]:::probe
main --> cli
main --> cmds
cmds --> rtsvc
cmds --> daemon
daemon --> rtsvc
rtsvc --> xray
config --> model
support --> config
support --> db
db --> xray
db --> prober
prober --> xray
prober --> model
server --> db
server --> model
tui --> cmds
tui --> db
Module Responsibilities
| Module | Responsibility |
|---|---|
cli/ | Define CLI interface with Clap. Parse args and flags. Test parsing. |
app/ | Orchestrate command execution. Manage app lifecycle (context, config, daemon). |
model/ | Shared domain types (Node, Protocol, NodeDedupKey). No dependencies on other modules. |
config/ | Parse proxy URIs. Normalize nodes. Detect import formats. |
db/ | Database connection, migrations, queries, repositories. |
xray/ | Generate Xray JSON configs. Parse Xray JSON. Manage Xray processes. |
singbox/ | Generate sing-box JSON configs. Manage sing-box processes. |
prober/ | Connection testing probes: ICMP, TCP, HTTP real-delay, download, upload. |
server/ | HTTP API server using Axum. Auth, routes, response types. |
support/ | Shared utilities: base64 decode, GeoIP, network helpers. |
Data Flows
Import Flow
flowchart LR
classDef io fill:#1a2e1a,stroke:#5bdf8a,color:#e6edf3
classDef cfg fill:#2e2a1a,stroke:#dfba5b,color:#e6edf3
classDef db fill:#1a2e2e,stroke:#5bcfdf,color:#e6edf3
SRC["Input\n(URL / File / Stdin)"]:::io
APP_IN["app/input/"]:::io
DETECT["config/import/detect"]:::cfg
PARSE_FMT["config/import/parsers/"]:::cfg
PROTO["config/protocols/"]:::cfg
NORM["config/normalize/"]:::cfg
DEDUP["model/node_dedup_key/"]:::cfg
PERSIST["db/repository/configs/"]:::db
SUB["db/repository/subscriptions/"]:::db
SRC --> APP_IN --> DETECT --> PARSE_FMT --> PROTO --> NORM --> DEDUP --> PERSIST --> SUB
Test Flow
flowchart TD
classDef cli fill:#1a2744,stroke:#4a9eff,color:#e6edf3
classDef app fill:#2a1a3a,stroke:#b070df,color:#e6edf3
classDef engine fill:#2e1a1a,stroke:#df6060,color:#e6edf3
classDef probe fill:#2a2a1a,stroke:#c0df5b,color:#e6edf3
classDef store fill:#1a2e2e,stroke:#5bcfdf,color:#e6edf3
CLI["CLI args"]:::cli
SET["resolve settings\napp/commands/test/"]:::app
LOAD["load configs\ndb/repository/configs/"]:::store
LOOP{"For each config"}
GEN["generate probe config\nxray/config/generator/"]:::engine
SPAWN["spawn Xray\nxray/process/"]:::engine
ICMP["prober/icmp/"]:::probe
TCP["prober/tcp/"]:::probe
DELAY["prober/real_delay/"]:::probe
DL["prober/download/"]:::probe
UL["prober/upload/"]:::probe
KILL["kill probe\nxray/process/"]:::engine
SAVE["persist results\ndb/repository/connection_tests/"]:::store
OUT["format & print\napp/commands/test/output/"]:::app
CLI --> SET --> LOAD --> LOOP
LOOP --> GEN --> SPAWN --> ICMP --> TCP --> DELAY --> DL --> UL --> KILL --> LOOP
LOOP --> SAVE --> OUT
Connect Flow
flowchart LR
classDef cli fill:#1a2744,stroke:#4a9eff,color:#e6edf3
classDef app fill:#2a1a3a,stroke:#b070df,color:#e6edf3
classDef engine fill:#2e1a1a,stroke:#df6060,color:#e6edf3
classDef store fill:#1a2e2e,stroke:#5bcfdf,color:#e6edf3
CLI["CLI args"]:::cli
LOAD["load config\napp/commands/connect/"]:::app
RTSVC["start session\napp/runtime_service/connect/"]:::app
XGEN["build runtime config\nxray/config/generator/"]:::engine
XSPAWN["spawn detached\nxray/process_mgmt/"]:::engine
SAVE["persist session\ndb/repository/runtime_sessions/"]:::store
CLI --> LOAD --> RTSVC --> XGEN --> XSPAWN --> SAVE
Daemon Flow
flowchart TD
classDef cli fill:#1a2744,stroke:#4a9eff,color:#e6edf3
classDef app fill:#2a1a3a,stroke:#b070df,color:#e6edf3
classDef event fill:#1a2e1a,stroke:#5bdf8a,color:#e6edf3
START["xrat daemon start"]:::cli
FORK["fork child process"]:::app
SUP["event loop\napp/daemon/supervisor/"]:::app
REATTACH["reconcile stale sessions\napp/runtime_service/reattach/"]:::app
SELECT{"tokio::select!"}:::app
HEALTH["health check\n(every 15s)"]:::event
IPC["IPC events\n(Unix socket)"]:::event
ROTATE["rotation timer"]:::event
START --> FORK --> SUP --> REATTACH --> SELECT
SELECT --> HEALTH
SELECT --> IPC
SELECT --> ROTATE
Dependency Graph
Modules ordered from most foundational (left) to most dependent (right). An arrow means the target depends on the source.
graph LR
classDef entry fill:#1a2744,stroke:#4a9eff,color:#e6edf3
classDef domain fill:#2e2a1a,stroke:#dfba5b,color:#e6edf3
classDef store fill:#1a2e2e,stroke:#5bcfdf,color:#e6edf3
classDef engine fill:#2e1a1a,stroke:#df6060,color:#e6edf3
classDef probe fill:#2a2a1a,stroke:#c0df5b,color:#e6edf3
classDef app fill:#2a1a3a,stroke:#b070df,color:#e6edf3
classDef iface fill:#1a3a2a,stroke:#5bdf8a,color:#e6edf3
support["support/"]:::domain
model["model/"]:::domain
config["config/"]:::domain
db["db/"]:::store
xray["xray/"]:::engine
singbox["singbox/"]:::engine
prober["prober/"]:::probe
app["app/"]:::app
cli["cli/"]:::iface
server["server/"]:::iface
main["main.rs"]:::entry
support --> model
model --> config
config --> db
config --> xray
xray --> prober
xray --> singbox
prober --> app
db --> app
app --> cli
cli --> main
support --> server
db --> server
Source Tree
src/
โโโ main.rs # Entrypoint: parse CLI, init tracing, dispatch command
โโโ lib.rs # Re-exports all public modules
โ
โโโ cli/ # Clap command/flag definitions
โ โโโ mod.rs # Module root, pub re-exports
โ โโโ root.rs # Cli struct with global flags
โ โโโ command.rs # Command enum (all subcommands)
โ โโโ add.rs # AddArgs
โ โโโ connect.rs # ConnectArgs
โ โโโ daemon.rs # DaemonArgs + DaemonAction
โ โโโ disconnect.rs # DisconnectArgs
โ โโโ import.rs # ImportArgs
โ โโโ lifecycle.rs # select / enable / disable / delete / restore
โ โโโ list.rs # ListArgs + ListTarget
โ โโโ parse.rs # ParseArgs + ParseEngine
โ โโโ proxy.rs # ProxyArgs + ProxyAction
โ โโโ scan.rs # ScanArgs
โ โโโ serve.rs # ServeArgs
โ โโโ status.rs # StatusArgs
โ โโโ tui.rs # TuiArgs
โ โโโ test_cmd/ # TestArgs + TestFormat/TestSortBy
โ โโโ tests/ # CLI parsing tests (cases/test_command, cases/runtime_parse, ...)
โ
โโโ app/ # Application layer
โ โโโ mod.rs
โ โโโ app_paths.rs # Filesystem layout resolution
โ โโโ context.rs # AppContext: DB + config + runtime paths
โ โโโ context/
โ โ โโโ paths.rs # Runtime path resolution
โ โ โโโ tests/ # Context tests (binary, database resolution)
โ โโโ config/ # AppConfig TOML deserialization (proxy + testing)
โ โโโ daemon.rs # Daemon CLI dispatch glue
โ โโโ error.rs # AppError enum
โ โโโ import.rs # Top-level import orchestration
โ โโโ input/ # Input source reading (read_input, fetch_url)
โ โโโ runtime_service.rs # RuntimeService public re-exports
โ โโโ commands/ # Command handlers
โ โ โโโ mod.rs
โ โ โโโ add.rs
โ โ โโโ connect.rs
โ โ โโโ daemon.rs
โ โ โโโ disconnect.rs
โ โ โโโ import.rs
โ โ โโโ lifecycle.rs
โ โ โโโ list.rs
โ โ โโโ parse.rs
โ โ โโโ proxy.rs
โ โ โโโ runtime_output.rs
โ โ โโโ scan.rs
โ โ โโโ serve.rs
โ โ โโโ status/ # display + json + tests submodules
โ โ โโโ test.rs
โ โ โโโ test/
โ โ โ โโโ bulk/ # bulk executor
โ โ โ โ โโโ bulk_executor/
โ โ โ โโโ execution/ # per-config probe loop
โ โ โ โโโ handlers/ # CLI arg handling helpers
โ โ โ โโโ output/ # table / TSV / CSV / JSON output
โ โ โ โโโ output_types/
โ โ โ โโโ settings/ # resolve / rows / validation
โ โ โ โโโ stages/ # endpoint / progress / throughput
โ โ โ โโโ tests/ # focused tests
โ โ โโโ tui.rs
โ โโโ runtime_service/ # Proxy process lifecycle
โ โ โโโ connect/ # Connect flow
โ โ โโโ replace_flow/ # Atomic disconnect + connect (candidate, ports, stage)
โ โ โโโ reattach/ # Stale session recovery (process inspector)
โ โ โโโ session_state/# State transitions + inbound health
โ โ โโโ types.rs
โ โ โโโ tests/ # Integration tests
โ โโโ daemon/ # Daemon supervisor
โ โโโ ipc/ # Unix socket IPC protocol
โ โ โโโ types.rs # Request/response types (DaemonRequest, RotationTrigger, ...)
โ โ โโโ handler/ # dispatch.rs + io.rs
โ โ โโโ client/ # unix_impl.rs + unsupported_impl.rs
โ โ โโโ transport/ # ping_shutdown.rs, proxy.rs, runtime.rs
โ โ โโโ tests/ # IPC integration tests
โ โโโ supervisor/ # Event loop
โ โโโ mod.rs
โ โโโ types.rs
โ โโโ health.rs
โ โโโ runtime.rs
โ โโโ test_support.rs
โ โโโ tests.rs
โ โโโ handlers/ # Health check, rotation, runtime
โ โโโ health.rs
โ โโโ mod.rs
โ โโโ runtime/ # runtime_lifecycle/, runtime_status_connect/
โ โโโ tests/ # tests_replace/
โ
โโโ model/ # Shared domain types
โ โโโ node.rs # Node struct
โ โโโ protocol.rs # Protocol enum
โ โโโ node_dedup_key.rs # Dedup key generation
โ
โโโ config/ # Config parsing and normalization
โ โโโ protocols/ # Protocol-specific parsers
โ โ โโโ vless.rs # vless:// parser
โ โ โโโ vmess.rs # vmess:// parser
โ โ โโโ ss.rs # ss:// parser
โ โ โโโ trojan.rs # trojan:// parser
โ โ โโโ http.rs # http:// parser
โ โ โโโ socks5.rs # socks5:// parser
โ โ โโโ hy2.rs # hysteria2:// parser
โ โ โโโ tests/ # Parser tests
โ โโโ line.rs # Line-by-line text parsing
โ โโโ normalize.rs # Node normalization defaults
โ โโโ parse_service.rs # Engine-aware parsing
โ โโโ import/ # Import format detection
โ โ โโโ detect.rs # Format detection heuristics
โ โ โโโ error.rs
โ โ โโโ mod.rs # ImportMode / ImportResult / parse_import
โ โ โโโ subscription.rs # URL fetch + metadata
โ โ โโโ parsers/ # single_link, plain_list, base64, sip008, xray
โ โโโ parsing_helpers.rs # Shared URI helpers
โ
โโโ db/ # Database layer
โ โโโ connection.rs # Connection pool management
โ โโโ schema.rs # Migration runner
โ โโโ error.rs # DbError enum
โ โโโ mod.rs # DbPool + facade re-exports
โ โโโ database/ # Database query methods
โ โโโ repository/ # SQL implementations
โ โ โโโ api/ # API-specific queries
โ โ โโโ cf_scan_results.rs
โ โ โโโ configs/
โ โ โ โโโ import_ops/ # Upsert on dedup_key
โ โ โ โโโ state_ops/ # enable/disable/select/delete
โ โ โ โโโ server_ops.rs
โ โ โโโ connection_tests.rs
โ โ โโโ row/ # Shared row helpers
โ โ โโโ runtime_sessions.rs
โ โโโ record/ # Record types (DTOs)
โ โโโ cf_scan_results.rs
โ โโโ configs.rs
โ โโโ connection_tests.rs
โ โโโ import.rs # ImportSource, SubscriptionRecord, ...
โ โโโ mod.rs
โ โโโ runtime_sessions.rs
โ
โโโ xray/ # Xray-core integration
โ โโโ config/ # Config generation
โ โ โโโ generator/ # Probe + runtime config builders
โ โ โโโ outbound.rs # Protocol-to-outbound mapping
โ โ โโโ stream.rs # Stream settings (TLS, WS, gRPC, TCP)
โ โ โโโ types.rs # XrayConfig, Inbound, Outbound structs
โ โโโ parsing/ # Xray JSON config parsing
โ โ โโโ core/ # Top-level config structure
โ โ โโโ protocols/ # Inbound/outbound protocol parsers
โ โ โ โโโ inbound_settings/
โ โ โ โโโ outbound_settings/
โ โ โโโ transports/ # Transport settings parsers
โ โ โ โโโ security/
โ โ โโโ shared/ # Shared types (enums, strings)
โ โโโ process/ # Low-level process spawn + lifecycle
โ โ โโโ errors.rs
โ โ โโโ spawn.rs
โ โ โโโ tests.rs
โ โโโ process_mgmt/ # High-level process management + signals
โ โโโ mod.rs
โ โโโ process.rs
โ โโโ signals.rs
โ โโโ tests.rs
โ
โโโ singbox/ # sing-box integration
โ โโโ mod.rs
โ โโโ config/ # sing-box config generation + process_mgmt helper
โ โโโ mod.rs
โ โโโ process_mgmt.rs
โ
โโโ prober/ # Connection testing probes
โ โโโ mod.rs # FailureKind + combined TestResult
โ โโโ icmp/ # ICMP ping (parse system ping output)
โ โ โโโ mod.rs # icmp_ping, ping_with_system_command
โ โ โโโ parsing.rs # parse_ping_latency, classify_ping_failure
โ โ โโโ tests.rs
โ โโโ tcp/ # TCP connectivity check + failure classification
โ โ โโโ check.rs # tcp_check
โ โ โโโ classify.rs
โ โ โโโ errors.rs
โ โ โโโ model.rs # TcpResult
โ โ โโโ mod.rs
โ โ โโโ tests.rs
โ โโโ real_delay/ # HTTP round-trip latency via proxy
โ โ โโโ check/ # execute, model, port, request, mod
โ โ โโโ classify.rs
โ โ โโโ mod.rs
โ โโโ download/ # Download speed measurement
โ โ โโโ check/ # proxied, result, mod
โ โ โโโ classify.rs
โ โ โโโ mod.rs
โ โโโ upload/ # Upload speed measurement
โ โโโ classify.rs
โ โโโ mod.rs
โ โโโ request.rs
โ
โโโ server/ # Axum HTTP API
โ โโโ mod.rs
โ โโโ routes/ # b64, configs, health, json
โ โโโ auth.rs # API key authentication
โ โโโ response.rs # Response types
โ โโโ state.rs # ServerState
โ โโโ error.rs # Server error types
โ
โโโ tui/ # Ratatui TUI
โ โโโ mod.rs
โ โโโ run.rs # Terminal lifecycle + main loop
โ โโโ keymap.rs
โ โโโ task.rs # Background task primitives
โ โโโ theme.rs
โ โโโ app/ # App state, reducers, navigation
โ โโโ data/ # Data loading + tests
โ โโโ view/ # chrome, configs, sources, runtime, tests, modals
โ
โโโ support/ # Shared utilities
โโโ decode.rs # Base64 decoding
โโโ geoip.rs # MaxMind GeoIP lookups
โโโ net.rs # Network utilities
โโโ time.rs # Timestamp helpers
โโโ url.rs # URL detection helpers
File Conventions
mod.rs: Module root, pub re-exports- Names: Snake_case for files/modules, PascalCase for types, snake_case for functions
- Tests:
#[cfg(test)] mod tests { ... }in same file ortests/submodule - Records/DTOs: In
db/record/โ thin structs matching DB rows - Repository: In
db/repository/โ SQL query functions separated by entity