How bearice/redproxy-rs works
Project Type and Purpose
• This project is a high-performance proxy routing tool written in Rust. It is designed to translate protocols (e.g., HTTP CONNECT with mTLS, QUIC-based connections, SOCKS versions) and to enable policy-based selection of destination proxies.
• It also features an embedded web console, Prometheus metrics integration, access logs in JSON format, and supports dynamic reconfigurable rules via a RESTful API.
Analyzing the File Structure
• Top-level files (Cargo.toml, build.rs, config.yaml, etc.) and directories (.github, LICENSE, README.md) indicate standard Rust project conventions with CI/CD integration.
• There’s a “src” directory housing the primary application logic:
– Modules like “access_log.rs” and “metrics.rs” are dedicated to logging and monitoring.
– The “common” folder contains various networking utility modules (HTTP, QUIC, TLS, DNS, etc.), which are likely foundational libraries for protocol-specific handling.
– The “connectors” folder is segmented into different types (direct, HTTP, loadbalance, quic, socks), representing the code paths for outbound connections and protocol translation.
– The “listeners” folder includes files for handling incoming traffic (HTTP, QUIC, reverse proxy, socks, TPROXY), representing how the proxy receives requests.
– “rules” and “script” modules indicate policy configuration and scripting/extension support for dynamic decision-making.
• A separate “milu” directory exists that appears to function as a library or module (possibly for parsing and scripting) – with its own Cargo manifest and source files (parser, script, stdlib) – which likely provides the dynamic/scriptable policy engine embedded within the main system.
• The “ui” folder contains the web interface assets (HTML, JavaScript, CSS) for the embedded web console.
a. Main Components:
– Client Layer: External clients initiating connections using various protocols.
– Listener Modules: Distinct entry points for each protocol (HTTP CONNECT, QUIC, SOCKS, TPROXY, Reverse Proxy), represented by modules in “src/listeners/*”.
– Core Routing Engine: The heart of the proxy, processing incoming connections, maintaining context (see src/context.rs), and interfacing with rules and scripts.
– Connectors: Different outbound connection handlers (direct, HTTP, loadbalanced, etc. from “src/connectors/*”) that translate protocols or route traffic to the correct destination proxy.
– Policy Engine & Rules: Handling dynamic reconfiguration through RESTful API calls with business logic encapsulated in “src/rules” and “src/script”. The “milu” subproject with its parser and script features likely supports this layer.
– Monitoring and Logging: Access logs, Prometheus metrics (from “src/metrics.rs” and “src/access_log.rs”) for observability.
– Embedded Web Console & REST API: The UI directory provides a web-based interface, and REST endpoints likely exist within the main or configuration modules.
– External Dependencies: Any integrations with mTLS setups, Prometheus, or LDAP/other authentication methods visible in “src/common/auth.rs”.
b. Component Relationships and Data Flow:
– The client connects to one of the Listener modules.
– The Listener hands the request off to the Core Routing Engine.
– The Core Routing Engine interprets the connection using the policy/rules engine (which might dynamically load scripts via modules in “milu”) to decide which Connector to use.
– Data flows from the Connector outwards (or possibly vice versa, for reverse proxy scenarios).
– Observability data (logs, metrics) flow continuously to the monitoring components.
– External control is provided via the REST API and embedded web console, enabling live rule adjustments.
– Underpinning network commonalities are implemented in shared utility modules (“src/common/*”).
c. Architectural Patterns & Design Principles:
– Modular Design: Separation into listeners, connectors, and common utility modules.
– Extensibility: Use of a scripting/parser library (in the “milu” module) to achieve dynamic reconfigurability.
– Clean Separation of Concerns: Distinct components handling network I/O, business logic, configuration, and monitoring.
– Potential use of reactive or event-driven patterns to manage concurrent network connections.
d. Relevant Technologies & Frameworks:
– Rust as the primary programming language, emphasizing performance and safety.
– CI/CD integration via GitHub workflows.
– Prometheus for monitoring.
– Embedded web technologies for the UI (Vue.js is hinted by the inclusion of vue.global.prod.js).
– TLS/mTLS and QUIC protocols are supported, indicating reliance on modern networking libraries.