Skip to content

Navigation Menu

Sign in
Sign up

Repository files navigation

๐Ÿ›ก๏ธ SurgeShield (surgeshield)

A high-performance, asynchronous, lock-free rate limiter library, Redis distributed cluster store, dynamic IP blacklisting engine ("The Jail"), zero-downtime TOML hot-reloader, Prometheus exporter, and Tower/Axum middleware built in Rust.

Rust 2021 License: MIT CI & Quality Docker Image Platform: Cross-Platform


๐Ÿ“Œ Overview

SurgeShield is an enterprise-grade rate limiting engine, distributed cluster store, automated DDoS defense system, and real-time observability stack designed for high-concurrency web services, microservices, and API gateways in Rust. It protects web applications against denial-of-service (DoS) attacks, brute-force exploits, and sudden traffic spikes without introducing garbage collection delays or global locks.

By combining DashMap lock-free in-memory storage, Redis atomic Lua distributed storage, Dynamic IP Blacklisting ("The Jail"), Zero-Downtime TOML Hot-Reloading, Prometheus OpenMetrics telemetry, an embedded live visual Web Dashboard, and Tokio's async runtime, SurgeShield delivers sub-millisecond rate limit decisions and real-time cluster observability.


โœจ Key Features

  • ๐Ÿš€ High Throughput & Lock-Free State: Built on DashMap for concurrent, non-blocking evaluation across all CPU cores with sub-millisecond latency.
  • ๐Ÿšซ Dynamic IP Blacklisting ("The Jail"): Automatically detects clients with repeated 429 Too Many Requests violations and jails them with instant 403 Forbidden responses (< 0.01ms overhead, bypassing token bucket evaluations).
  • ๐Ÿ”„ Zero-Downtime Hot-Reloading (surgeshield.toml): Background file watcher reloads rate limits and jail rules live at runtime without restarting the server.
  • ๐ŸŒ Distributed Redis Storage (RedisStore): Share atomic rate limit quotas across multiple cloud server nodes or Kubernetes pods using Redis and atomic Lua scripting.
  • ๐Ÿงฎ Dual Rate Limiting Engines:
    • Token Bucket Algorithm: Perfect for bursty traffic with smooth floating-point token replenishment.
    • Sliding Window Counter: Eliminates window-boundary burst exploits using weighted window estimation.
  • ๐Ÿ–ฅ๏ธ Embedded Live Telemetry Dashboard (GET /dashboard): Zero-dependency, single-page dark-mode Web UI featuring real-time Chart.js graphs showing live RPS, allowed (200), blocked (429), and jailed (403) traffic breakdown, with an interactive 60 FPS Cyber Matrix background grid.
  • ๐Ÿ“Š Prometheus Metrics Exporter (GET /metrics): Exposes standard OpenMetrics format:
    • surgeshield_requests_allowed_total (counter)
    • surgeshield_requests_blocked_total (counter for 429 status)
    • surgeshield_requests_jailed_total (counter for 403 status)
    • surgeshield_active_keys (gauge)
    • surgeshield_evaluation_duration_seconds (histogram)
  • ๐Ÿณ Docker Ready (ghcr.io): Lightweight multi-stage container deployment published to GitHub Container Registry.
  • ๐Ÿค– Automated GitHub Actions CI/CD: Matrix builds across Linux, Windows, and macOS with inline Clippy PR code review annotations and daily RustSec dependency security audits.

๐Ÿ—๏ธ System Architecture

graph TD
 Client[๐Ÿ“ฑ Client Request] --> MW[๐Ÿ›ก๏ธ SurgeShield Middleware Layer]
 MW --> JailCheck{๐Ÿšซ Check Jail Status}
 
 JailCheck -->|Jailed / Banned| FastReject[โ›” Instant 403 Forbidden - < 0.01ms]
 FastReject --> Client
 
 JailCheck -->|Not Jailed| KeyExt[๐Ÿ”‘ Key Extractor]
 KeyExt --> Metrics[๐Ÿ“Š Record Prometheus Telemetry]
 
 KeyExt -->|Single-Instance Node| MemStore[๐Ÿ’พ MemoryStore / DashMap]
 KeyExt -->|Multi-Node Cluster| RedisStore[โšก RedisStore / Atomic Lua]
 
 MemStore --> Engine{๐Ÿงฎ Rate Limit Engine}
 RedisStore --> Engine
 
 Engine -->|Quota Available| Pass[โœ… Allow Request - 200 OK]
 Engine -->|Quota Exceeded| Reject[โ›” Block Request - 429 Rate Limited]
 Reject --> JailMgr[๐Ÿšซ Increment Violation Counter]
 JailMgr -->|โ‰ฅ 5 Violations| JailTrigger[๐Ÿ”’ Place Client in Jail - 60s]
 Pass --> AppRoute[๐Ÿš€ Application Route Handler]
 Reject --> Client
 Prometheus[๐Ÿ”ฅ Prometheus Server] -->|Scrapes GET /metrics| MetricsEndpoint[๐Ÿ“ˆ Prometheus Endpoint /metrics]
 Browser[๐ŸŒ Web Browser] -->|Views GET /dashboard| UI[๐Ÿ–ฅ๏ธ Embedded Telemetry Web UI]
 classDef clientStyle fill:#2563eb,stroke:#1d4ed8,stroke-width:2px,color:#ffffff;
 classDef mwStyle fill:#7c3aed,stroke:#6d28d9,stroke-width:2px,color:#ffffff;
 classDef storeStyle fill:#0284c7,stroke:#0369a1,stroke-width:2px,color:#ffffff;
 classDef engineStyle fill:#d97706,stroke:#b45309,stroke-width:2px,color:#ffffff;
 classDef passStyle fill:#059669,stroke:#047857,stroke-width:2px,color:#ffffff;
 classDef rejectStyle fill:#dc2626,stroke:#b91c1c,stroke-width:2px,color:#ffffff;
 classDef jailStyle fill:#d97706,stroke:#b45309,stroke-width:2px,color:#ffffff;
 classDef obsStyle fill:#0891b2,stroke:#0e7490,stroke-width:2px,color:#ffffff;
 class Client,Browser clientStyle;
 class MW,KeyExt mwStyle;
 class MemStore,RedisStore storeStyle;
 class Engine engineStyle;
 class Pass,AppRoute passStyle;
 class Reject,FastReject rejectStyle;
 class JailCheck,JailMgr,JailTrigger jailStyle;
 class Metrics,Prometheus,MetricsEndpoint,UI obsStyle;
Loading

๐Ÿš€ Quick Start

1. Basic In-Memory Rate Limiting with Jail Protection

use axum::{routing::get, Json, Router};
use rust_rate_limiter::{
 config::KeyExtractor,
 init_prometheus,
 middleware::RateLimiterLayer,
 render_dashboard,
 store::MemoryStore,
};
use serde_json::{json, Value};
#[tokio::main]
async fn main() {
 let prometheus_handle = init_prometheus().unwrap();
 // 5 request burst capacity, refills 1 token/sec + default Jail (5 429s -> 60s ban)
 let store = MemoryStore::new_token_bucket(5, 1.0);
 let rate_limiter = RateLimiterLayer::new(store, KeyExtractor::ClientIp).with_default_jail();
 let app = Router::new()
 .route("/api/data", get(|| async { Json(json!({"status": "success"})) }))
 .layer(rate_limiter)
 .route("/metrics", get(move || async move { prometheus_handle.render() }))
 .route("/dashboard", get(render_dashboard));
 let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await.unwrap();
 axum::serve(listener, app).await.unwrap();
}

2. Multi-Node Distributed Cluster (RedisStore)

For Kubernetes pods or load-balanced cloud nodes, use RedisStore to share the exact same rate limit quota across all instances:

use rust_rate_limiter::{RedisStore, RateLimiterLayer, KeyExtractor};
use axum::{Router, routing::get};
#[tokio::main]
async fn main() {
 // Distributed Redis Store: 100 requests burst capacity, refills 10 tokens/sec
 let redis_store = RedisStore::new("redis://127.0.0.1:6379/", 100, 10.0)
 .await
 .expect("Failed to connect to Redis cluster");
 let rate_limiter = RateLimiterLayer::new(redis_store, KeyExtractor::ClientIp).with_default_jail();
 let app = Router::new()
 .route("/api/v1/resource", get(|| async { "Cluster protected resource" }))
 .layer(rate_limiter);
 let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
 axum::serve(listener, app).await.unwrap();
}

๐Ÿณ Docker Deployment

Run SurgeShield instantly using Docker:

docker run -d --name surgeshield -p 3000:3000 ghcr.io/8ernity/surgeshield:latest

Or build locally:

docker build -t surgeshield .
docker run -p 3000:3000 surgeshield

๐Ÿ”„ Zero-Downtime Hot-Reloading (surgeshield.toml)

Edit surgeshield.toml while the server is running to update limits live without server restarts:

[rate_limiter]
capacity = 10
refill_rate_per_sec = 2.0
[jail]
enabled = true
max_violations = 5
ban_duration_secs = 60

๐Ÿ–ฅ๏ธ Live Telemetry Dashboard (GET /dashboard)

Open http://localhost:3000/dashboard in your browser:

  • Live Request Counters: Real-time counter for Allowed (200), Blocked (429), and Jailed (403) requests.
  • Refractive Liquid Glass Panels: 100% crystal-clear backdrop refraction with specular 3D edge lighting.
  • Interactive Cyber Matrix Grid: High-density 60 FPS matrix background responding to cursor proximity.
  • Throughput Charts: Chart.js lines plotting allowed, blocked, and jailed traffic per second.

๐Ÿ“ Repository Structure

surgeshield/
โ”œโ”€โ”€ Cargo.toml # Project manifest & dependencies
โ”œโ”€โ”€ Dockerfile # Multi-stage container build
โ”œโ”€โ”€ .dockerignore # Docker build exclusions
โ”œโ”€โ”€ surgeshield.toml # Hot-reloadable runtime configuration
โ”œโ”€โ”€ README.md # Project documentation
โ”œโ”€โ”€ .github/
โ”‚ โ””โ”€โ”€ workflows/
โ”‚ โ”œโ”€โ”€ ci.yml # Multi-OS matrix CI with inline Clippy annotations
โ”‚ โ”œโ”€โ”€ audit.yml # Daily RustSec dependency security audit
โ”‚ โ””โ”€โ”€ docker.yml # Automated GitHub Container Registry publisher
โ”œโ”€โ”€ dashboards/
โ”‚ โ””โ”€โ”€ grafana_surgeshield.json # 1-Click Grafana dashboard template
โ”œโ”€โ”€ src/
โ”‚ โ”œโ”€โ”€ lib.rs # Main library entry point & exports
โ”‚ โ”œโ”€โ”€ error.rs # Custom Error types and Result alias
โ”‚ โ”œโ”€โ”€ config.rs # KeyExtractor, SurgeShieldConfig & ConfigHandle watcher
โ”‚ โ”œโ”€โ”€ jail.rs # Thread-safe lock-free JailManager & IP blacklisting
โ”‚ โ”œโ”€โ”€ metrics/ # Prometheus telemetry exporter & counters
โ”‚ โ”‚ โ””โ”€โ”€ mod.rs
โ”‚ โ”œโ”€โ”€ dashboard/ # Embedded HTML/JS visual web dashboard
โ”‚ โ”‚ โ””โ”€โ”€ mod.rs
โ”‚ โ”œโ”€โ”€ engine/ # Core rate limiter algorithm implementations
โ”‚ โ”‚ โ”œโ”€โ”€ mod.rs # Engine traits & decision outcome types
โ”‚ โ”‚ โ”œโ”€โ”€ token_bucket.rs # Token Bucket algorithm
โ”‚ โ”‚ โ””โ”€โ”€ sliding_window.rs# Sliding Window Counter algorithm
โ”‚ โ”œโ”€โ”€ store/ # Storage layer abstractions
โ”‚ โ”‚ โ”œโ”€โ”€ mod.rs # RateLimitStore async trait
โ”‚ โ”‚ โ”œโ”€โ”€ memory.rs # DashMap in-memory store + background TTL worker
โ”‚ โ”‚ โ””โ”€โ”€ redis.rs # Redis atomic Lua distributed cluster store
โ”‚ โ””โ”€โ”€ middleware/ # Tower & Axum middleware integration
โ”‚ โ”œโ”€โ”€ mod.rs # Tower Layer & Service implementations
โ”‚ โ””โ”€โ”€ headers.rs # HTTP header injection logic
โ”œโ”€โ”€ examples/
โ”‚ โ””โ”€โ”€ axum_server.rs # Complete runnable server with Redis, Jail & Hot-Reloading
โ””โ”€โ”€ tests/
 โ”œโ”€โ”€ engine_tests.rs # Unit tests for algorithms & refill logic
 โ”œโ”€โ”€ concurrency_tests.rs # 100-thread multi-threaded stress tests
 โ””โ”€โ”€ middleware_tests.rs # Axum HTTP integration & header tests

๐Ÿ“„ License

Distributed under the MIT License. See LICENSE for more information.

Crafted with โค๏ธ and ๐Ÿฆ€ Rust by 8ernity

About

High-performance async Rust rate limiter & DDoS defense middleware featuring Redis cluster support, automatic IP blacklisting (The Jail), hot-reloading TOML configs, and a real-time liquid glass telemetry dashboard.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

AltStyle ใซใ‚ˆใฃใฆๅค‰ๆ›ใ•ใ‚ŒใŸใƒšใƒผใ‚ธ (->ใ‚ชใƒชใ‚ธใƒŠใƒซ) /