package firewall import ( "strings" "sync" "sync/atomic" "time"…
- User
package firewall
import ( "strings" "sync" "sync/atomic" "time"
"reverse-proxy-ddos/config" "github.com/valyala/fasthttp")
type rpsTracker struct { requestCount int64 lastCheckTime time.Time }
type requestTracker struct { requestCount map[string]*int64 flaggedUACount map[string]*int64 resetTimers map[string]*time.Timer uaResetTimers map[string]*time.Timer previousUserAgent map[string]uaChangeInfo whitelist map[string]time.Time challenges map[string]time.Time blacklist map[string]time.Time blacklistDuration time.Duration challengeFailures map[string]int whitelistDuration time.Duration rpsLimit int64 zonerpstracker *rpsTracker requestCounts []int64 uamActive bool uamExpiration time.Time uamThreshold float64 permUAM int64 flaggedUAs [][]interface{} staticWhitelist map[string]bool mu sync.RWMutex }
type Firewall struct { backendTrackers map[string]*requestTracker cfg *config.Config }
// ---------- // | Helper Functions | // ----------
func (f *Firewall) AddToWhitelist(ctx *fasthttp.RequestCtx) { tracker := f.backendTrackers[string(ctx.Request.Host())] ip := string(ctx.RemoteAddr().String()) if backend, exists := f.cfg.BackendMap[string(ctx.Request.Host())]; exists && backend.Cloudflare { ip = string(ctx.Request.Header.Peek("X-Forwarded-For")) }
tracker.mu.Lock() defer tracker.mu.Unlock() tracker.whitelist[ip] = time.Now().Add(tracker.whitelistDuration)}
func IsChallengeRequest(ctx *fasthttp.RequestCtx) bool { path := string(ctx.Path()) return strings.HasPrefix(path, "/rproxy/") && strings.HasSuffix(path, ".js") }
func (f *Firewall) CleanupExpiredWhitelistEntries() { for _, tracker := range f.backendTrackers { tracker.mu.Lock() for ip, expiration := range tracker.whitelist { if time.Now().After(expiration) { delete(tracker.whitelist, ip) } } tracker.mu.Unlock() } }
func (f *Firewall) CleanupExpiredBlacklistEntries() { for _, tracker := range f.backendTrackers { tracker.mu.Lock()
for ip, expiration := range tracker.blacklist { if time.Now().After(expiration) { delete(tracker.blacklist, ip) } } tracker.mu.Unlock() }}
func NewHandler(cfg *config.Config) *Firewall { backendTrackers := make(map[string]*requestTracker) for host := range cfg.BackendMap { staticWhitelist := make(map[string]bool) for _, ip := range cfg.BackendMap[host].StaticWhitelist { staticWhitelist[ip] = true } backendTrackers[host] = &requestTracker{ requestCount: make(map[string]*int64), flaggedUACount: make(map[string]*int64), resetTimers: make(map[string]*time.Timer), uaResetTimers: make(map[string]*time.Timer), previousUserAgent: make(map[string]uaChangeInfo), whitelist: make(map[string]time.Time), challenges: make(map[string]time.Time), blacklist: make(map[string]time.Time), challengeFailures: make(map[string]int), whitelistDuration: time.Duration(cfg.BackendMap[host].WhitelistDuration) * time.Second, blacklistDuration: time.Duration(cfg.BackendMap[host].BlacklistDuration) * time.Second, rpsLimit: int64(cfg.BackendMap[host].ForceIntrusive), uamThreshold: float64(cfg.BackendMap[host].UAMThreshold), permUAM: int64(cfg.BackendMap[host].PermUAM), flaggedUAs: cfg.BackendMap[host].FlaggedUAs, staticWhitelist: staticWhitelist, } } return &Firewall{ backendTrackers: backendTrackers, cfg: cfg, } }
- Assistant