middleware

package
v2.8.0 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2026 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RequestsPerMinute = 100 // Simple limit - 100 requests per minute per IP
	BurstSize         = 20  // Allow burst of 20 requests
)

Variables

This section is empty.

Functions

func HTTPIPFilterMiddleware added in v2.7.0

func HTTPIPFilterMiddleware(filter *IPFilter) func(http.Handler) http.Handler

HTTPIPFilterMiddleware creates an HTTP middleware that filters requests by IP. This middleware applies to both regular HTTP requests and WebSocket upgrade requests.

func HTTPRateLimitMiddleware

func HTTPRateLimitMiddleware(limiter *IPRateLimiter) func(http.Handler) http.Handler

HTTPRateLimitMiddleware creates an HTTP rate limiting middleware

func WebSocketRateLimitHandler

func WebSocketRateLimitHandler(
	limiter *IPRateLimiter,
	handler func(*melody.Session, []byte),
) func(*melody.Session, []byte)

WebSocketRateLimitHandler wraps a WebSocket message handler with rate limiting

Types

type IPFilter added in v2.7.0

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

IPFilter manages IP allowlist filtering for both HTTP and WebSocket connections

func NewIPFilter added in v2.7.0

func NewIPFilter(allowedIPs []string) *IPFilter

NewIPFilter creates a new IP filter from a list of allowed IPs/CIDRs. Empty list means all IPs are allowed (no filtering).

func (*IPFilter) IsAllowed added in v2.7.0

func (f *IPFilter) IsAllowed(remoteAddr string) bool

IsAllowed checks if an IP address is allowed. Returns true if the allowlist is empty (no filtering) or if the IP matches an allowed entry.

type IPRateLimiter

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

IPRateLimiter manages rate limiters per IP address for both HTTP and WebSocket

func NewIPRateLimiter

func NewIPRateLimiter() *IPRateLimiter

NewIPRateLimiter creates a new IP-based rate limiter with hardcoded limits

func (*IPRateLimiter) Cleanup

func (rl *IPRateLimiter) Cleanup()

Cleanup removes old entries that haven't been seen recently

func (*IPRateLimiter) GetLimiter

func (rl *IPRateLimiter) GetLimiter(ip string) *rate.Limiter

GetLimiter returns the rate limiter for the given IP

func (*IPRateLimiter) StartCleanup

func (rl *IPRateLimiter) StartCleanup(ctx context.Context)

StartCleanup starts a goroutine to periodically clean up old rate limiters. The cleanup goroutine will stop when the provided context is cancelled.

Jump to

Keyboard shortcuts

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