RouteShield

A powerful Phoenix/Elixir plug that provides route discovery, rule-based request filtering, and a beautiful LiveView dashboard for managing route access controls.

Overview

RouteShield is a comprehensive solution for managing and protecting your Phoenix application routes. It automatically discovers all routes at compile-time, stores them efficiently in ETS, and provides a real-time dashboard for configuring access rules. The plug enforces these rules before authentication, making it perfect for rate limiting, IP filtering, and advanced access control.

Implemented Features Summary

Route Discovery - Automatic compile-time route discovery with ETS storage
Rate Limiting - Per-IP token bucket algorithm with configurable windows
IP Filtering - Per-route whitelist/blacklist + global blacklist with CIDR support
Concurrent Limits - Maximum simultaneous connections per IP
Time Restrictions - Time windows and day-of-week restrictions
Custom Responses - Customizable HTTP status codes and error messages
LiveView Dashboard - Beautiful Tailwind CSS interface for rule management
Mix Tasks - Route discovery and migration generation utilities
High Performance - ETS-based caching with PostgreSQL persistence

Core Architecture

1. Route Discovery (Compile-Time)

2. Storage Strategy

3. Dashboard Integration

4. Plug Pipeline

Request → RouteShield Plug (checks rules) → Auth Plug → Controller

Features

Implemented Features

1. Route Discovery

2. Rate Limiting

3. IP Filtering (Whitelist & Blacklist)

4. Dashboard

5. Concurrent Request Limits

6. Custom Blocked Responses

7. Time-Based Restrictions

8. Storage & Caching

Future Features

9. Geographic Restrictions

10. Request Pattern Matching

11. Advanced Logging & Analytics

12. Additional Security Features

Technical Decisions

Rate Limiting

Route Matching

Database Schema

Performance

Installation

# mix.exs
defp deps do
  [
    {:route_shield, "~> 0.1.0"}
  ]
end

Setup

1. Add to Router

defmodule MyApp.Router do
  use MyApp, :router
  use RouteShield.Plug  # Add this

  # ... your routes ...

  # Add dashboard route
  live "/route_shield", RouteShield.DashboardLive
end

2. Install Migrations

mix route_shield.install
mix ecto.migrate

The mix route_shield.install command generates a migration file in your project's priv/repo/migrations/ directory with all RouteShield tables.

3. Configure

# config/config.exs
config :route_shield,
  repo: MyApp.Repo,
  dashboard_route: "/route_shield"  # Optional, defaults to "/route_shield"

Usage

In Router

defmodule MyApp.Router do
  use MyApp, :router
  use RouteShield.Plug

  pipeline :api do
    plug RouteShield.Plug  # Add before authentication
    plug :accepts, ["json"]
    # ... other plugs including auth ...
  end
end

Discover Routes

Routes can be discovered automatically at compile-time or manually via mix task:

# Manual route discovery
mix route_shield.discover YourApp.Router

Or programmatically:

# In your application startup
RouteShield.discover_routes(YourApp.Router, YourApp.Repo)

Dashboard

Navigate to /route_shield (or your configured path) to:

Programmatic Usage

# Discover routes
RouteShield.discover_routes(YourApp.Router, YourApp.Repo)

# Refresh cache after rule changes (usually automatic via dashboard)
RouteShield.refresh_cache(YourApp.Repo)

Project Structure

route_shield/
├── lib/
│   ├── route_shield/
│   │   ├── plug.ex                  # Main plug for enforcement
│   │   ├── router.ex                # Compile-time route discovery
│   │   ├── route_discovery.ex       # Route discovery logic
│   │   ├── dashboard_live.ex        # LiveView dashboard
│   │   ├── application.ex           # Application startup
│   │   ├── rules/
│   │   │   ├── rate_limit.ex        # Rate limiting logic
│   │   │   ├── ip_filter.ex         # IP whitelist/blacklist
│   │   │   ├── time_restriction.ex  # Time-based restrictions
│   │   │   └── concurrent_limit.ex  # Concurrent request limits
│   │   ├── storage/
│   │   │   ├── ets.ex               # ETS operations
│   │   │   └── cache.ex             # Cache refresh logic
│   │   ├── schema/
│   │   │   ├── route.ex
│   │   │   ├── rule.ex
│   │   │   ├── rate_limit.ex
│   │   │   ├── ip_filter.ex
│   │   │   ├── global_ip_blacklist.ex
│   │   │   ├── time_restriction.ex
│   │   │   ├── concurrent_limit.ex
│   │   │   └── custom_response.ex
│   │   └── mix/
│   │       └── tasks/
│   │           ├── route_shield.install.ex    # Migration generator
│   │           └── route_shield.discover.ex   # Route discovery task
│   └── route_shield.ex
├── priv/
│   └── repo/
│       └── migrations/              # Ecto migrations
└── mix.exs

License

MIT