|
1 | | -# rust-reverse-proxy |
2 | | -a lightweight and high performance HTTP and TCP reverse proxy written in Rust |
| 1 | +# Rust Reverse Proxy |
3 | 2 |
|
4 | | -Prioritizing security and memory concurrency. Ultra fast performance. currently trying to update to improve. |
| 3 | +A lightweight, high-performance HTTP reverse proxy written in Rust, prioritizing security, memory safety, and concurrency. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **High Performance**: Built with Tokio and Hyper for maximum throughput |
| 8 | +- **Load Balancing**: Round-robin load balancing across multiple backend servers |
| 9 | +- **Health Checks**: Automatic health monitoring of backend servers |
| 10 | +- **Configuration-Driven**: YAML-based configuration for easy management |
| 11 | +- **Structured Logging**: Comprehensive request/response logging with configurable levels |
| 12 | +- **Fault Tolerance**: Automatic failover and error handling |
| 13 | +- **Request Timeouts**: Configurable timeouts for reliability |
| 14 | +- **Header Forwarding**: Proper X-Forwarded-* headers for backend services |
| 15 | + |
| 16 | +## Quick Start |
| 17 | + |
| 18 | +1. **Build the project**: |
| 19 | + ```bash |
| 20 | + cargo build --release |
| 21 | + ``` |
| 22 | + |
| 23 | +2. **Configure your backends** in `config.yaml`: |
| 24 | + ```yaml |
| 25 | + server: |
| 26 | + host: "127.0.0.1" |
| 27 | + port: 3000 |
| 28 | + |
| 29 | + backends: |
| 30 | + - url: "http://backend1.example.com" |
| 31 | + weight: 1 |
| 32 | + health_check_path: "/health" |
| 33 | + - url: "http://backend2.example.com" |
| 34 | + weight: 1 |
| 35 | + health_check_path: "/health" |
| 36 | + ``` |
| 37 | +
|
| 38 | +3. **Run the proxy**: |
| 39 | + ```bash |
| 40 | + cargo run --release |
| 41 | + ``` |
| 42 | + |
| 43 | +## Configuration |
| 44 | + |
| 45 | +The proxy is configured using a `config.yaml` file: |
| 46 | + |
| 47 | +```yaml |
| 48 | +server: |
| 49 | + host: "127.0.0.1" # Interface to bind to |
| 50 | + port: 3000 # Port to listen on |
| 51 | + |
| 52 | +backends: |
| 53 | + - url: "http://httpbin.org" # Backend server URL |
| 54 | + weight: 1 # Load balancing weight |
| 55 | + health_check_path: "/status/200" # Health check endpoint |
| 56 | + - url: "http://example.com" |
| 57 | + weight: 1 |
| 58 | + health_check_path: "/" |
| 59 | + |
| 60 | +load_balancing: |
| 61 | + strategy: "round_robin" # Load balancing strategy |
| 62 | + |
| 63 | +health_checks: |
| 64 | + enabled: true # Enable/disable health checks |
| 65 | + interval_seconds: 30 # Health check interval |
| 66 | + timeout_seconds: 5 # Health check timeout |
| 67 | + failure_threshold: 3 # Failures before marking unhealthy |
| 68 | + |
| 69 | +logging: |
| 70 | + level: "info" # Log level (debug, info, warn, error) |
| 71 | + format: "json" # Log format (json, text) |
| 72 | + |
| 73 | +timeouts: |
| 74 | + request_timeout_seconds: 30 # Backend request timeout |
| 75 | + connect_timeout_seconds: 10 # Connection timeout |
| 76 | +``` |
| 77 | +
|
| 78 | +## Architecture |
| 79 | +
|
| 80 | +The proxy consists of several key components: |
| 81 | +
|
| 82 | +- **ProxyService**: Main request handling service |
| 83 | +- **LoadBalancer**: Manages backend selection and health status |
| 84 | +- **HealthChecker**: Monitors backend server health |
| 85 | +- **Configuration**: YAML-based configuration management |
| 86 | +
|
| 87 | +## Load Balancing |
| 88 | +
|
| 89 | +Currently supports round-robin load balancing: |
| 90 | +- Requests are distributed evenly across healthy backends |
| 91 | +- Unhealthy backends are automatically excluded |
| 92 | +- Backends are marked unhealthy after consecutive failures |
| 93 | +
|
| 94 | +## Health Checks |
| 95 | +
|
| 96 | +- Periodic health checks to all configured backends |
| 97 | +- Configurable health check paths and intervals |
| 98 | +- Automatic failover when backends become unhealthy |
| 99 | +- Recovery detection when backends come back online |
| 100 | +
|
| 101 | +## Logging |
| 102 | +
|
| 103 | +Structured logging with configurable levels: |
| 104 | +- Request/response logging with timing information |
| 105 | +- Health check status updates |
| 106 | +- Error tracking and debugging information |
| 107 | +- JSON or plain text output formats |
| 108 | +
|
| 109 | +## Error Handling |
| 110 | +
|
| 111 | +Robust error handling with appropriate HTTP status codes: |
| 112 | +- `503 Service Unavailable`: No healthy backends |
| 113 | +- `502 Bad Gateway`: Backend request failures |
| 114 | +- `504 Gateway Timeout`: Backend request timeouts |
| 115 | +- `400 Bad Request`: Client request issues |
| 116 | + |
| 117 | +## Performance |
| 118 | + |
| 119 | +Optimized for high performance: |
| 120 | +- Async/await with Tokio runtime |
| 121 | +- Connection pooling with Hyper client |
| 122 | +- Zero-copy request/response forwarding where possible |
| 123 | +- Minimal memory allocations |
| 124 | + |
| 125 | +## Development |
| 126 | + |
| 127 | +### Building |
| 128 | +```bash |
| 129 | +cargo build |
| 130 | +``` |
| 131 | + |
| 132 | +### Testing |
| 133 | +```bash |
| 134 | +cargo test |
| 135 | +``` |
| 136 | + |
| 137 | +### Running with Debug Logging |
| 138 | +```bash |
| 139 | +RUST_LOG=debug cargo run |
| 140 | +``` |
| 141 | + |
| 142 | +## Docker Support |
| 143 | + |
| 144 | +Create a `Dockerfile`: |
| 145 | +```dockerfile |
| 146 | +FROM rust:1.70 AS builder |
| 147 | +WORKDIR /app |
| 148 | +COPY . . |
| 149 | +RUN cargo build --release |
| 150 | +
|
| 151 | +FROM debian:bookworm-slim |
| 152 | +RUN apt-get update && apt-get install -y ca-certificates && rm -rf /var/lib/apt/lists/* |
| 153 | +COPY --from=builder /app/target/release/reverse_proxy /usr/local/bin/ |
| 154 | +COPY config.yaml /etc/reverse-proxy/ |
| 155 | +WORKDIR /etc/reverse-proxy |
| 156 | +EXPOSE 3000 |
| 157 | +CMD ["reverse_proxy"] |
| 158 | +``` |
| 159 | + |
| 160 | +## Contributing |
| 161 | + |
| 162 | +1. Fork the repository |
| 163 | +2. Create a feature branch |
| 164 | +3. Make your changes |
| 165 | +4. Add tests if applicable |
| 166 | +5. Submit a pull request |
| 167 | + |
| 168 | +## License |
| 169 | + |
| 170 | +This project is licensed under the MIT License - see the LICENSE file for details. |
| 171 | + |
| 172 | +## Roadmap |
| 173 | + |
| 174 | +- [ ] HTTPS/TLS termination support |
| 175 | +- [ ] WebSocket proxying |
| 176 | +- [ ] Metrics and monitoring endpoints |
| 177 | +- [ ] Rate limiting |
| 178 | +- [ ] Path-based routing |
| 179 | +- [ ] Circuit breaker pattern |
| 180 | +- [ ] Admin API for runtime configuration |
0 commit comments