rackattack

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 29, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package rackattack provides a Redis-based rate limiting and IP blocking system.

RedisRackAttack implements rate limiting functionality using Redis as a backend store. It supports: - IP-based throttling with configurable rules - IP safelisting and blocklisting - CIDR block filtering - Path pattern matching - HTTP method filtering

Rate limiting rules can be configured using ThrottleRule structs that specify: - Path patterns (with wildcard support) - HTTP methods to match - Rate limit key templates - Request limits within time periods

Example usage:

client := redis.NewClient(&redis.Options{...})
ra := rackattack.New(client)

// Add a throttle rule
ra.AddThrottleRule(ThrottleRule{
    PathPattern: "/api/*",
    Method:      "POST",
    Key:         "ratelimit:%{ip}:%{path}",
    Limit:       100,
    Period:      time.Hour,
})

// Safelist IPs
ra.SafelistIP("127.0.0.1")

// Block IPs or CIDR ranges
ra.BlocklistIP("10.0.0.1")
ra.BlocklistCIDR("10.0.0.0/24")

// Check if request is throttled
isThrottled, err := ra.IsThrottled(request)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RedisRackAttack

type RedisRackAttack struct {
	// contains filtered or unexported fields
}

RedisRackAttack provides rate limiting and IP blocking functionality using Redis.

func New

func New(redisClient *redis.Client) *RedisRackAttack

New creates a new RedisRackAttack instance with the provided Redis client.

func (*RedisRackAttack) AddThrottleRule

func (ra *RedisRackAttack) AddThrottleRule(rule ThrottleRule)

To add a throttle rule to the RedisRackAttack instance.

func (*RedisRackAttack) BlocklistCIDR

func (ra *RedisRackAttack) BlocklistCIDR(cidr string) error

BlocklistCIDR adds a CIDR range to the blocklist to deny access to address subnets.

func (*RedisRackAttack) BlocklistIP

func (ra *RedisRackAttack) BlocklistIP(ip string)

blocklistIP adds an IP address to the blocklist to deny access

func (*RedisRackAttack) IsBlocked

func (ra *RedisRackAttack) IsBlocked(ip string) bool

IsBlocked checks if an IP address is blocked by the blocklist or CIDR ranges

func (*RedisRackAttack) IsThrottled

func (ra *RedisRackAttack) IsThrottled(req *http.Request) (bool, error)

IsThrottled checks if a request is throttled based on the configured rules.

func (*RedisRackAttack) SafelistIP

func (ra *RedisRackAttack) SafelistIP(ip string)

safelistIP adds an IP address to the safelist to bypass rate limiting.

type ThrottleRule

type ThrottleRule struct {
	PathPattern string
	Method      string
	Key         string
	Limit       int
	Period      time.Duration
}

ThrottleRule represents a rate limiting rule for a specific path and HTTP method.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL