2020-12-21 00:32:39 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-12-24 14:59:11 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"fmt"
|
2020-12-21 00:32:39 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2021-01-24 00:46:40 +00:00
|
|
|
type timerType uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
timerTypePermit timerType = iota
|
|
|
|
timerTypeCooldown
|
|
|
|
)
|
|
|
|
|
2020-12-21 00:32:39 +00:00
|
|
|
var timerStore = newTimer()
|
|
|
|
|
2021-01-24 00:46:40 +00:00
|
|
|
type timerEntry struct {
|
|
|
|
kind timerType
|
|
|
|
time time.Time
|
|
|
|
}
|
|
|
|
|
2020-12-21 00:32:39 +00:00
|
|
|
type timer struct {
|
2021-01-24 00:46:40 +00:00
|
|
|
timers map[string]timerEntry
|
2020-12-21 00:32:39 +00:00
|
|
|
lock *sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTimer() *timer {
|
|
|
|
return &timer{
|
2021-01-24 00:46:40 +00:00
|
|
|
timers: map[string]timerEntry{},
|
2020-12-21 00:32:39 +00:00
|
|
|
lock: new(sync.RWMutex),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-24 00:46:40 +00:00
|
|
|
// Cooldown timer
|
2020-12-21 00:32:39 +00:00
|
|
|
|
2021-06-07 20:20:19 +00:00
|
|
|
func (t *timer) AddCooldown(tt timerType, limiter, ruleID string) {
|
|
|
|
t.add(timerTypeCooldown, t.getCooldownTimerKey(tt, limiter, ruleID))
|
2020-12-21 00:32:39 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 20:20:19 +00:00
|
|
|
func (t *timer) InCooldown(tt timerType, limiter, ruleID string, cooldown time.Duration) bool {
|
|
|
|
return t.has(t.getCooldownTimerKey(tt, limiter, ruleID), cooldown)
|
2020-12-24 14:59:11 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 20:20:19 +00:00
|
|
|
func (t timer) getCooldownTimerKey(tt timerType, limiter, ruleID string) string {
|
2021-01-24 00:46:40 +00:00
|
|
|
h := sha256.New()
|
2021-06-07 20:20:19 +00:00
|
|
|
fmt.Fprintf(h, "%d:%s:%s", tt, limiter, ruleID)
|
2021-01-24 00:46:40 +00:00
|
|
|
return fmt.Sprintf("sha256:%x", h.Sum(nil))
|
2020-12-21 00:32:39 +00:00
|
|
|
}
|
|
|
|
|
2021-01-24 00:46:40 +00:00
|
|
|
// Permit timer
|
|
|
|
|
|
|
|
func (t *timer) AddPermit(channel, username string) {
|
|
|
|
t.add(timerTypePermit, t.getPermitTimerKey(channel, username))
|
2020-12-21 00:32:39 +00:00
|
|
|
}
|
|
|
|
|
2021-01-24 00:46:40 +00:00
|
|
|
func (t *timer) HasPermit(channel, username string) bool {
|
|
|
|
return t.has(t.getPermitTimerKey(channel, username), config.PermitTimeout)
|
2020-12-21 00:32:39 +00:00
|
|
|
}
|
2020-12-24 14:59:11 +00:00
|
|
|
|
|
|
|
func (t timer) getPermitTimerKey(channel, username string) string {
|
|
|
|
h := sha256.New()
|
2021-01-24 00:46:40 +00:00
|
|
|
fmt.Fprintf(h, "%d:%s:%s", timerTypePermit, channel, strings.ToLower(strings.TrimLeft(username, "@")))
|
2020-12-24 14:59:11 +00:00
|
|
|
return fmt.Sprintf("sha256:%x", h.Sum(nil))
|
|
|
|
}
|
2021-01-24 00:46:40 +00:00
|
|
|
|
|
|
|
// Generic
|
|
|
|
|
|
|
|
func (t *timer) add(kind timerType, id string) {
|
|
|
|
t.lock.Lock()
|
|
|
|
defer t.lock.Unlock()
|
|
|
|
|
|
|
|
t.timers[id] = timerEntry{kind: kind, time: time.Now()}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *timer) has(id string, validity time.Duration) bool {
|
|
|
|
t.lock.RLock()
|
|
|
|
defer t.lock.RUnlock()
|
|
|
|
|
|
|
|
return time.Since(t.timers[id].time) < validity
|
|
|
|
}
|