|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +ClashRS is a custom protocol, rule-based network proxy software written in Rust. It's a high-performance proxy with support for multiple protocols (Shadowsocks, Trojan, Vmess, Wireguard, Tor, Tuic, Socks5), flexible routing rules, DNS anti-spoofing, and cross-platform support. |
| 8 | + |
| 9 | +## Workspace Structure |
| 10 | + |
| 11 | +This is a Rust workspace with four main crates: |
| 12 | +- `clash/` - Main binary executable |
| 13 | +- `clash_lib/` - Core library containing all proxy logic |
| 14 | +- `clash_doc/` - Documentation generation |
| 15 | +- `clash_ffi/` - FFI bindings for mobile platforms |
| 16 | + |
| 17 | +## Common Commands |
| 18 | + |
| 19 | +### Build |
| 20 | +```bash |
| 21 | +cargo build # Debug build |
| 22 | +cargo build --release # Release build |
| 23 | +cargo build --features plus # Build with all features including Tor |
| 24 | +``` |
| 25 | + |
| 26 | +### Testing |
| 27 | +```bash |
| 28 | +cargo test --all --all-features # Run all tests |
| 29 | +CLASH_RS_CI=true cargo test --all --all-features # Run tests in CI mode |
| 30 | +make test-no-docker # Run tests without Docker |
| 31 | +``` |
| 32 | + |
| 33 | +### Documentation |
| 34 | +```bash |
| 35 | +make docs # Generate documentation |
| 36 | +cargo doc -p clash_doc --no-deps # Generate config docs only |
| 37 | +``` |
| 38 | + |
| 39 | +### Running |
| 40 | +```bash |
| 41 | +./target/debug/clash-rs -c config.yaml # Run with config file |
| 42 | +./target/debug/clash-rs -t # Test configuration |
| 43 | +./target/debug/clash-rs -h # Show help |
| 44 | +``` |
| 45 | + |
| 46 | +## Architecture |
| 47 | + |
| 48 | +### Core Components |
| 49 | + |
| 50 | +**clash_lib/src/app/** - Main application modules: |
| 51 | +- `api/` - REST API handlers for web dashboard |
| 52 | +- `dispatcher/` - Traffic routing and connection management |
| 53 | +- `dns/` - DNS resolution with anti-spoofing |
| 54 | +- `inbound/` - Inbound connection handling |
| 55 | +- `outbound/` - Outbound proxy connections |
| 56 | +- `router/` - Rule-based routing logic |
| 57 | + |
| 58 | +**clash_lib/src/proxy/** - Protocol implementations: |
| 59 | +- `shadowsocks/`, `trojan/`, `vmess/`, `socks/` - Proxy protocols |
| 60 | +- `group/` - Proxy group types (selector, fallback, load balance) |
| 61 | +- `transport/` - Underlying transports (TLS, WebSocket, gRPC, H2) |
| 62 | +- `tun/` - TUN device support for transparent proxy |
| 63 | + |
| 64 | +**clash_lib/src/config/** - Configuration parsing and validation |
| 65 | + |
| 66 | +### Key Design Patterns |
| 67 | + |
| 68 | +- **Async/await**: Heavy use of Tokio for async networking |
| 69 | +- **Trait-based**: Extensible proxy system using traits |
| 70 | +- **Error handling**: Comprehensive error types with `thiserror` |
| 71 | +- **Feature flags**: Conditional compilation for different protocols |
| 72 | +- **Zero-copy**: Optimized data paths where possible |
| 73 | + |
| 74 | +## Testing |
| 75 | + |
| 76 | +Tests are located in `clash_lib/tests/` and include: |
| 77 | +- `smoke_tests.rs` - Basic functionality tests |
| 78 | +- `api_tests.rs` - API endpoint tests |
| 79 | +- Integration tests with Docker containers for various proxy protocols |
| 80 | + |
| 81 | +Set `CLASH_RS_CI=true` environment variable when running tests to enable CI-specific behavior. |
| 82 | + |
| 83 | +## Platform-Specific Notes |
| 84 | + |
| 85 | +- **iOS/macOS**: Use `scripts/build_apple.sh` to build XCFramework |
| 86 | +- **Windows**: Requires `wintun.dll` in same directory as executable |
| 87 | +- **Linux**: Enhanced with platform-specific UDP socket optimizations |
| 88 | + |
| 89 | +## Feature Flags |
| 90 | + |
| 91 | +Key features that can be enabled: |
| 92 | +- `shadowsocks` - Shadowsocks protocol support |
| 93 | +- `tuic` - TUIC protocol support |
| 94 | +- `ssh` - SSH tunnel support |
| 95 | +- `onion` - Tor support |
| 96 | +- `shadowquic` - ShadowQUIC protocol support |
| 97 | +- `tokio-console` - Tokio console debugging |
| 98 | +- `bench` - Benchmarking tools |
| 99 | + |
| 100 | +## Configuration |
| 101 | + |
| 102 | +The project uses YAML configuration files. Sample configs are in `clash/tests/data/config/`. |
| 103 | + |
| 104 | +Main configuration sections: |
| 105 | +- `port` - HTTP/SOCKS proxy port |
| 106 | +- `dns` - DNS server settings |
| 107 | +- `rules` - Traffic routing rules |
| 108 | +- `proxies` - Outbound proxy definitions |
| 109 | +- `proxy-groups` - Proxy group configurations |
0 commit comments