Skip to content

PerishCode/RustProxyCore

Repository files navigation

Proxy Core

A self-hosted, automated proxy core written in Rust. Designed for "seamless proxying" with automatic speed testing, clustering, switching, and self-healing.

Features

  • Multi-protocol Support: Shadowsocks (AEAD, AEAD-2022)
  • Smart Routing: Domain/IP-based rules with external ruleset support
  • Auto Failover: Automatic node switching on failure with health tracking
  • Auto Clustering: Latency-based node grouping for optimal selection
  • Happy Eyeballs: Concurrent connection attempts for faster establishment
  • DNS Integration: Built-in DNS server with domain-IP mapping

Quick Start

# Build
cargo build --release

# Run with config
./target/release/proxy-core -c config.yml

Documentation

Full documentation available at: https://perishcode.github.io/RustProxyCore/

Configuration

listen: "127.0.0.1:7890"
mode: rule  # direct | global | rule | auto

dns:
  listen: "127.0.0.1:5353"
  upstream: system

subscriptions:
  - key: airport-1
    url: https://airport.com/sub

rules:
  - [DOMAIN-SUFFIX, google.com, PROXY]
  - [DOMAIN-SUFFIX, baidu.com, DIRECT]
  - [FINAL, "", PROXY]

Proxy Modes

Mode Description
direct All traffic connects directly
global All traffic through proxy
rule Route based on rules (default)
auto Smart mode with clustering and learning

Architecture

src/
├── main.rs                 # CLI entry point
├── lib.rs                  # Module exports
│
├── core/                   # Foundation layer (no business deps)
│   ├── error.rs            # Unified error types
│   ├── config/             # Configuration parsing
│   │   ├── mod.rs          # Config, Settings
│   │   ├── mode.rs         # ProxyMode
│   │   ├── outbound.rs     # OutboundConfig
│   │   ├── health.rs       # HealthConfig
│   │   └── pool.rs         # PoolConfig
│   └── types/              # Data types
│       ├── address.rs      # Address
│       ├── proxy.rs        # Proxy
│       └── rule.rs         # MatchRule, MatchContext
│
├── protocol/               # Protocol layer
│   ├── subscription.rs     # SS subscription parsing
│   └── sniffer/            # Protocol sniffing
│       ├── tls.rs          # TLS SNI extraction
│       └── http.rs         # HTTP Host extraction
│
├── dns/                    # DNS layer
│   ├── server.rs           # UDP DNS server
│   ├── mapping.rs          # IP-domain mapping
│   └── resolver.rs         # Domain resolver
│
├── node/                   # Node layer
│   ├── registry.rs         # Global node registry
│   └── health.rs           # Node health cache
│
├── rule/                   # Rule layer
│   ├── engine.rs           # Rule matching engine
│   ├── loader.rs           # Ruleset loader
│   └── learned.rs          # Dynamic learned rules
│
├── outbound/               # Outbound layer (pure implementations)
│   ├── traits.rs           # Outbound trait, BoxedStream
│   ├── direct.rs           # DirectOutbound
│   ├── shadowsocks.rs      # ShadowsocksOutbound
│   ├── group.rs            # GroupOutbound
│   └── reject.rs           # RejectOutbound
│
├── relay/                  # Relay layer
│   └── mod.rs              # relay(), relay_with_validation()
│
├── routing/                # Routing layer
│   ├── manager.rs          # OutboundManager
│   └── happy_eyeballs.rs   # Concurrent connection
│
├── learning/               # Learning layer (auto mode)
│   ├── event.rs            # Event definitions + EventEmitter
│   ├── state.rs            # Learning state structures
│   ├── reader.rs           # StateReader (read-only)
│   ├── manager.rs          # StateManager, LearningSystem
│   ├── fingerprint.rs      # Node stable fingerprint
│   ├── persistence.rs      # SQLite persistence
│   ├── exception.rs        # Service exception definitions
│   ├── health.rs           # System health detection
│   └── probe.rs            # Recovery probe runner
│
├── inbound/                # Inbound layer
│   └── socks5.rs           # SOCKS5 protocol handler
│
└── runtime/                # Runtime configuration
    └── mod.rs

Learning System (Auto Mode)

┌─────────────────────────────────────────────────────────────────┐
│                    Business Layer (emit events)                 │
│        Socks5Handler / OutboundManager / ProbeTask              │
└─────────────────────────────────────────────────────────────────┘
                          │ emit()        │ read()
                          ▼               ▼
┌─────────────────────────────────────────────────────────────────┐
│    EventEmitter  │  StateReader  │  StateManager                │
│   (send events)  │ (read-only)   │ (single writer)              │
└─────────────────────────────────────────────────────────────────┘
                          │
                          ▼
┌─────────────────────────────────────────────────────────────────┐
│                     LearnedState                                │
│      (domain_node_map, node_health, failure_correlations)       │
└─────────────────────────────────────────────────────────────────┘
                          │
                          ▼
┌─────────────────────────────────────────────────────────────────┐
│                   SQLite Persistence                            │
│         (domain_mapping, node_health, correlation)              │
└─────────────────────────────────────────────────────────────────┘

Key Features:

  • Event-driven learning from actual connection results
  • Failure correlation analysis (skip related failing nodes)
  • SQLite persistence (survive restarts)
  • Recovery probe for cooldown nodes
  • Service exception feedback (no healthy nodes, subscription stale, etc.)

Data Flow

┌─────────────────┐     ┌──────────────┐     ┌────────────────┐
│  SOCKS5 Client  │────▶│  Socks5 In   │────▶│  RuleEngine    │
└─────────────────┘     └──────────────┘     └────────────────┘
                                                     │
                        ┌────────────────────────────┼────────────────────────────┐
                        ▼                            ▼                            ▼
                 ┌─────────────┐            ┌──────────────┐            ┌──────────────┐
                 │   DIRECT    │            │    GROUP     │            │   REJECT     │
                 └─────────────┘            └──────────────┘            └──────────────┘
                        │                            │
                        │                   ┌────────┴────────┐
                        ▼                   ▼                 ▼
                 ┌─────────────┐    ┌─────────────┐   ┌─────────────┐
                 │   Target    │    │  SS Node 1  │   │  SS Node 2  │
                 └─────────────┘    └─────────────┘   └─────────────┘

Development

Tech Stack

Component Technology
Core Rust + Tokio
Protocol shadowsocks-rust
DNS hickory-dns
Storage rusqlite
E2E Test pnpm + vitest

Prerequisites

  • Rust 1.75+
  • pnpm 10+
  • Node.js 20+ (for E2E tests)

Commands

# Build
cargo build

# Run tests
cargo test

# Run with debug logging
RUST_LOG=debug cargo run -- -c config.yml

# Format code
cargo fmt

# Lint
cargo clippy

# E2E tests
pnpm test

# E2E tests with real network
PROXY_E2E_REAL=1 pnpm test

Code Style

  • Rust Edition: 2021
  • Max Line Width: 100 characters
  • Indentation: 4 spaces
  • Lints: clippy pedantic + nursery enabled
  • Error Handling: Typed errors (ProxyError, ConnectionError, etc.)
  • Async Runtime: Tokio

Lint Configuration

[lints.rust]
unsafe_code = "forbid"

[lints.clippy]
all = { level = "warn", priority = -1 }
pedantic = { level = "warn", priority = -1 }
nursery = { level = "warn", priority = -1 }

Project Conventions

  1. Module Organization

    • core/ - Foundation layer (error, config, types)
    • protocol/ - Protocol parsing (subscription, sniffer)
    • inbound/ - Incoming connection handlers
    • outbound/ - Outgoing connection handlers
    • relay/ - Bidirectional data relay
    • routing/ - Route decision (OutboundManager, Happy Eyeballs)
    • learning/ - Smart learning system (auto mode)
    • node/ - Node management and health tracking
    • rule/ - Rule matching engine
    • dns/ - DNS server and resolution
  2. Error Handling

    • Use typed errors from src/core/error.rs
    • Avoid anyhow::bail! in library code
    • Let callers decide presentation
  3. Configuration

    • All runtime constants should be configurable
    • Use Arc<Runtime> for shared configuration
    • Defaults defined in types/config/
  4. Testing

    • Unit tests: cargo test
    • E2E tests: pnpm test (vitest)
    • Real network tests require PROXY_E2E_REAL=1

License

MIT

About

Let's Rust !

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •