Skip to content

Commit f8be68d

Browse files
committed
Initial code push
1 parent 18f87d8 commit f8be68d

File tree

9 files changed

+1825
-50
lines changed

9 files changed

+1825
-50
lines changed

Cargo.lock

Lines changed: 1098 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "reverse_proxy"
3+
version = "0.2.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
hyper = { version = "1.0", features = ["full"] }
8+
hyper-util = { version = "0.1", features = ["full"] }
9+
http-body-util = "0.1"
10+
tokio = { version = "1", features = ["full"] }
11+
serde = { version = "1.0", features = ["derive"] }
12+
serde_yaml = "0.9"
13+
serde_json = "1.0"
14+
log = "0.4"
15+
env_logger = "0.10"
16+
anyhow = "1.0"
17+
rand = "0.8"
18+
chrono = { version = "0.4", features = ["serde"] }

Dockerfile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
FROM rust:1.75 AS builder
2+
3+
WORKDIR /app
4+
COPY Cargo.toml Cargo.lock ./
5+
COPY src/ src/
6+
7+
RUN cargo build --release
8+
9+
FROM debian:bookworm-slim
10+
11+
RUN apt-get update && \
12+
apt-get install -y ca-certificates && \
13+
rm -rf /var/lib/apt/lists/*
14+
15+
COPY --from=builder /app/target/release/reverse_proxy /usr/local/bin/
16+
COPY config.yaml /etc/reverse-proxy/config.yaml
17+
18+
WORKDIR /etc/reverse-proxy
19+
EXPOSE 3000
20+
21+
CMD ["reverse_proxy"]

README.md

Lines changed: 179 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,180 @@
1-
# rust-reverse-proxy
2-
a lightweight and high performance HTTP and TCP reverse proxy written in Rust
1+
# Rust Reverse Proxy
32

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

cargo.tml/cargo.tml

Lines changed: 0 additions & 8 deletions
This file was deleted.

config.example.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Example configuration for development
2+
server:
3+
host: "127.0.0.1"
4+
port: 8080
5+
6+
backends:
7+
- url: "http://httpbin.org"
8+
weight: 2
9+
health_check_path: "/status/200"
10+
- url: "http://jsonplaceholder.typicode.com"
11+
weight: 1
12+
health_check_path: "/posts/1"
13+
14+
load_balancing:
15+
strategy: "round_robin"
16+
17+
health_checks:
18+
enabled: true
19+
interval_seconds: 15
20+
timeout_seconds: 3
21+
failure_threshold: 2
22+
23+
logging:
24+
level: "debug"
25+
format: "text"
26+
27+
timeouts:
28+
request_timeout_seconds: 20
29+
connect_timeout_seconds: 5

config.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
server:
2+
host: "127.0.0.1"
3+
port: 3000
4+
5+
backends:
6+
- url: "http://httpbin.org"
7+
weight: 1
8+
health_check_path: "/status/200"
9+
- url: "http://example.com"
10+
weight: 1
11+
health_check_path: "/"
12+
13+
load_balancing:
14+
strategy: "round_robin" # round_robin, weighted_round_robin, least_connections
15+
16+
health_checks:
17+
enabled: true
18+
interval_seconds: 30
19+
timeout_seconds: 5
20+
failure_threshold: 3
21+
22+
logging:
23+
level: "info" # debug, info, warn, error
24+
format: "json" # json, text
25+
26+
timeouts:
27+
request_timeout_seconds: 30
28+
connect_timeout_seconds: 10

0 commit comments

Comments
 (0)