Deadcode-verified removals (RTA reachability from main, cross-checked with -test and textual xrefs): Go server: - whole files: middleware/transaction.go, middleware/error_handler.go, middleware/password_validator.go (live registration machinery kept), handlers/sync.go, sync/offline.go - unrouted endpoints: auth UpdateUsername/UpdateEmail, timezone settings handler, OPDS RegisterOPDS, Kobo LibrarySync handler, ReaderHandler.ShowReader, JSON-data twins on collections/library - duplicate book-matching handlers (book_matching.go Handler twins, matching.go MatchingHandler twins), superseded helpers (router.parseUUID, handlers.ValidateDeviceToken, normalizePath, userExists, generateLockID) Templ/client: - never-routed pages: api_explorer.templ, queue.templ and their client modules api-explorer.ts, api-explorer-docs.ts, queue.ts - fully orphaned wrapper modules events.ts, dom.ts - broken clearSyncQueue (called a nonexistent endpoint) and the hidden, never-populated sync-queue section on the devices page - 166+ unused exports demoted to private (knip-verified); ~85 truly dead declarations removed via tsc noUnusedLocals fixpoint - stale 'interactive API Explorer' claims dropped from docs The docs drift-gate package (internal/docs), templ-bound modules (dashboard, stats, header, index, login, profile*, bookshelf, collections, toast-error), sqlc-generated code, and web/static are intentionally untouched.
115 lines
2.4 KiB
Go
115 lines
2.4 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/labstack/echo/v5"
|
|
)
|
|
|
|
// RateLimiterConfig defines rate limiting configuration
|
|
type RateLimiterConfig struct {
|
|
Enabled bool
|
|
RequestsPerMinute int
|
|
CleanupInterval time.Duration
|
|
}
|
|
|
|
// RateLimiter tracks request counts per IP
|
|
type RateLimiter struct {
|
|
mu sync.RWMutex
|
|
requests map[string][]time.Time
|
|
config RateLimiterConfig
|
|
}
|
|
|
|
// NewRateLimiter creates a new rate limiter
|
|
func NewRateLimiter(config RateLimiterConfig) *RateLimiter {
|
|
rl := &RateLimiter{
|
|
requests: make(map[string][]time.Time),
|
|
config: config,
|
|
}
|
|
|
|
// Start cleanup goroutine
|
|
go rl.cleanup()
|
|
|
|
return rl
|
|
}
|
|
|
|
// cleanup removes old request records
|
|
func (rl *RateLimiter) cleanup() {
|
|
ticker := time.NewTicker(rl.config.CleanupInterval)
|
|
defer ticker.Stop()
|
|
|
|
for range ticker.C {
|
|
rl.mu.Lock()
|
|
now := time.Now()
|
|
for ip, requests := range rl.requests {
|
|
// Remove requests older than 1 minute
|
|
var valid []time.Time
|
|
for _, reqTime := range requests {
|
|
if now.Sub(reqTime) < time.Minute {
|
|
valid = append(valid, reqTime)
|
|
}
|
|
}
|
|
if len(valid) == 0 {
|
|
delete(rl.requests, ip)
|
|
} else {
|
|
rl.requests[ip] = valid
|
|
}
|
|
}
|
|
rl.mu.Unlock()
|
|
}
|
|
}
|
|
|
|
// Allow checks if a request from the given IP should be allowed
|
|
func (rl *RateLimiter) Allow(ip string) bool {
|
|
rl.mu.Lock()
|
|
defer rl.mu.Unlock()
|
|
|
|
now := time.Now()
|
|
|
|
// Clean old requests for this IP
|
|
var valid []time.Time
|
|
for _, reqTime := range rl.requests[ip] {
|
|
if now.Sub(reqTime) < time.Minute {
|
|
valid = append(valid, reqTime)
|
|
}
|
|
}
|
|
rl.requests[ip] = valid
|
|
|
|
// Check if limit exceeded
|
|
if len(rl.requests[ip]) >= rl.config.RequestsPerMinute {
|
|
return false
|
|
}
|
|
|
|
// Add this request
|
|
rl.requests[ip] = append(rl.requests[ip], now)
|
|
return true
|
|
}
|
|
|
|
// RateLimiterMiddleware returns echo middleware for rate limiting
|
|
func RateLimiterMiddleware(rl *RateLimiter) echo.MiddlewareFunc {
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
return func(c *echo.Context) error {
|
|
// If rate limiting is disabled, skip checks
|
|
if !rl.config.Enabled {
|
|
return next(c)
|
|
}
|
|
|
|
// Get client IP
|
|
ip := c.RealIP()
|
|
if ip == "" {
|
|
ip = c.Request().RemoteAddr
|
|
}
|
|
|
|
if !rl.Allow(ip) {
|
|
return c.JSON(http.StatusTooManyRequests, map[string]string{
|
|
"error": "too many requests, please try again later",
|
|
})
|
|
}
|
|
|
|
return next(c)
|
|
}
|
|
}
|
|
}
|