Null-DRM-Official/apps/streamd/internal/auth/ratelimit.go
404errordeveloper 066e018749
All checks were successful
release / build-and-upload (push) Successful in 6m18s
modules-release / build-and-upload (push) Successful in 5m18s
streamd: preview quality picker, dashboard polish, C4 wrap keys
- Preview player can select FHD/HD/SD (or ABR master) from publish links
- Embed dashboard screenshots in README; Linux streamd+agent setup guide
- C4 Widevine wrap license mode + require --wvd; prefer Widevine PSSH
- Channel 4 and My5 modules; streamd auth, variants, schedules, HLS gate
2026-10-09 20:57:43 +02:00

140 lines
2.7 KiB
Go

package auth
import (
"sync"
"time"
)
// Limiter is a keyed token bucket. Keys are usually a client IP, or an
// IP+username pair for login attempts.
type Limiter struct {
// Burst is the bucket capacity.
Burst float64
// PerSecond is the refill rate.
PerSecond float64
mu sync.Mutex
buckets map[string]*bucket
lastGC time.Time
}
type bucket struct {
tokens float64
seen time.Time
}
func NewLimiter(burst, perSecond float64) *Limiter {
if burst <= 0 {
burst = 1
}
if perSecond <= 0 {
perSecond = 1
}
return &Limiter{
Burst: burst,
PerSecond: perSecond,
buckets: map[string]*bucket{},
lastGC: time.Now(),
}
}
// Allow consumes a token for key, reporting whether the request may proceed.
func (l *Limiter) Allow(key string) bool {
return l.AllowN(key, 1)
}
// AllowN consumes n tokens, for operations that should cost more than one.
func (l *Limiter) AllowN(key string, n float64) bool {
if l == nil || key == "" {
return true
}
nowT := time.Now()
l.mu.Lock()
defer l.mu.Unlock()
l.gcLocked(nowT)
b, ok := l.buckets[key]
if !ok {
b = &bucket{tokens: l.Burst, seen: nowT}
l.buckets[key] = b
}
// Refill for the elapsed time, capped at Burst.
elapsed := nowT.Sub(b.seen).Seconds()
if elapsed > 0 {
b.tokens += elapsed * l.PerSecond
if b.tokens > l.Burst {
b.tokens = l.Burst
}
}
b.seen = nowT
if b.tokens < n {
return false
}
b.tokens -= n
return true
}
// Penalise drains a bucket so repeated failures cost more than one attempt.
func (l *Limiter) Penalise(key string, tokens float64) {
if l == nil || key == "" {
return
}
l.mu.Lock()
defer l.mu.Unlock()
b, ok := l.buckets[key]
if !ok {
b = &bucket{tokens: l.Burst, seen: time.Now()}
l.buckets[key] = b
}
b.tokens -= tokens
if b.tokens < -l.Burst {
b.tokens = -l.Burst
}
}
// Reset clears a key, used after a successful login.
func (l *Limiter) Reset(key string) {
if l == nil || key == "" {
return
}
l.mu.Lock()
delete(l.buckets, key)
l.mu.Unlock()
}
// RetryAfter estimates the seconds until key has a token again.
func (l *Limiter) RetryAfter(key string) int {
if l == nil {
return 0
}
l.mu.Lock()
defer l.mu.Unlock()
b, ok := l.buckets[key]
if !ok || b.tokens >= 1 {
return 0
}
need := 1 - b.tokens
secs := int(need/l.PerSecond) + 1
if secs < 1 {
secs = 1
}
return secs
}
// gcLocked drops idle buckets so the map cannot grow without bound under a
// spray of distinct source addresses.
func (l *Limiter) gcLocked(nowT time.Time) {
if nowT.Sub(l.lastGC) < time.Minute {
return
}
l.lastGC = nowT
idle := time.Duration(float64(time.Second) * (2 * l.Burst / l.PerSecond))
if idle < 5*time.Minute {
idle = 5 * time.Minute
}
for k, b := range l.buckets {
if nowT.Sub(b.seen) > idle {
delete(l.buckets, k)
}
}
}