2021-11-01 20:21:38 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2021-12-25 15:15:05 +01:00
|
|
|
"errors"
|
2021-11-01 20:21:38 +01:00
|
|
|
"golang.org/x/time/rate"
|
2021-11-01 21:39:40 +01:00
|
|
|
"heckel.io/ntfy/util"
|
2021-11-01 20:21:38 +01:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-12-24 00:03:04 +01:00
|
|
|
// visitorExpungeAfter defines how long a visitor is active before it is removed from memory. This number
|
|
|
|
// has to be very high to prevent e-mail abuse, but it doesn't really affect the other limits anyway, since
|
|
|
|
// they are replenished faster (typically).
|
|
|
|
visitorExpungeAfter = 24 * time.Hour
|
2021-11-01 20:21:38 +01:00
|
|
|
)
|
|
|
|
|
2021-12-25 15:15:05 +01:00
|
|
|
var (
|
|
|
|
errVisitorLimitReached = errors.New("limit reached")
|
|
|
|
)
|
|
|
|
|
2021-11-01 20:21:38 +01:00
|
|
|
// visitor represents an API user, and its associated rate.Limiter used for rate limiting
|
|
|
|
type visitor struct {
|
2021-12-19 04:02:36 +01:00
|
|
|
config *Config
|
2021-12-24 15:01:29 +01:00
|
|
|
ip string
|
2021-12-24 00:03:04 +01:00
|
|
|
requests *rate.Limiter
|
|
|
|
emails *rate.Limiter
|
2021-11-01 21:39:40 +01:00
|
|
|
subscriptions *util.Limiter
|
2021-11-01 20:21:38 +01:00
|
|
|
seen time.Time
|
|
|
|
mu sync.Mutex
|
|
|
|
}
|
|
|
|
|
2021-12-24 15:01:29 +01:00
|
|
|
func newVisitor(conf *Config, ip string) *visitor {
|
2021-11-01 20:21:38 +01:00
|
|
|
return &visitor{
|
2021-11-01 21:39:40 +01:00
|
|
|
config: conf,
|
2021-12-24 15:01:29 +01:00
|
|
|
ip: ip,
|
2021-12-24 00:03:04 +01:00
|
|
|
requests: rate.NewLimiter(rate.Every(conf.VisitorRequestLimitReplenish), conf.VisitorRequestLimitBurst),
|
|
|
|
emails: rate.NewLimiter(rate.Every(conf.VisitorEmailLimitReplenish), conf.VisitorEmailLimitBurst),
|
2021-11-01 21:39:40 +01:00
|
|
|
subscriptions: util.NewLimiter(int64(conf.VisitorSubscriptionLimit)),
|
|
|
|
seen: time.Now(),
|
2021-11-01 20:21:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-24 15:01:29 +01:00
|
|
|
func (v *visitor) IP() string {
|
|
|
|
return v.ip
|
|
|
|
}
|
|
|
|
|
2021-11-01 20:21:38 +01:00
|
|
|
func (v *visitor) RequestAllowed() error {
|
2021-12-24 00:03:04 +01:00
|
|
|
if !v.requests.Allow() {
|
2021-12-25 15:15:05 +01:00
|
|
|
return errVisitorLimitReached
|
2021-12-24 00:03:04 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitor) EmailAllowed() error {
|
|
|
|
if !v.emails.Allow() {
|
2021-12-25 15:15:05 +01:00
|
|
|
return errVisitorLimitReached
|
2021-11-01 20:21:38 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-25 15:15:05 +01:00
|
|
|
func (v *visitor) SubscriptionAllowed() error {
|
2021-11-01 20:21:38 +01:00
|
|
|
v.mu.Lock()
|
|
|
|
defer v.mu.Unlock()
|
2021-11-01 21:39:40 +01:00
|
|
|
if err := v.subscriptions.Add(1); err != nil {
|
2021-12-25 15:15:05 +01:00
|
|
|
return errVisitorLimitReached
|
2021-11-01 20:21:38 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitor) RemoveSubscription() {
|
|
|
|
v.mu.Lock()
|
|
|
|
defer v.mu.Unlock()
|
2021-11-01 21:39:40 +01:00
|
|
|
v.subscriptions.Sub(1)
|
2021-11-01 20:21:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitor) Keepalive() {
|
|
|
|
v.mu.Lock()
|
|
|
|
defer v.mu.Unlock()
|
|
|
|
v.seen = time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitor) Stale() bool {
|
|
|
|
v.mu.Lock()
|
|
|
|
defer v.mu.Unlock()
|
|
|
|
return time.Since(v.seen) > visitorExpungeAfter
|
|
|
|
}
|