ntfy/server/visitor.go

146 lines
4 KiB
Go
Raw Normal View History

2021-11-02 08:21:38 +13:00
package server
import (
2021-12-26 03:15:05 +13:00
"errors"
2022-12-03 09:37:48 +13:00
"heckel.io/ntfy/auth"
"net/netip"
2021-11-02 08:21:38 +13:00
"sync"
"time"
"golang.org/x/time/rate"
"heckel.io/ntfy/util"
2021-11-02 08:21:38 +13:00
)
const (
2021-12-24 12:03:04 +13: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-02 08:21:38 +13:00
)
2021-12-26 03:15:05 +13:00
var (
errVisitorLimitReached = errors.New("limit reached")
)
2021-11-02 08:21:38 +13:00
// visitor represents an API user, and its associated rate.Limiter used for rate limiting
type visitor struct {
2022-12-19 08:35:05 +13:00
config *Config
messageCache *messageCache
ip netip.Addr
user *auth.User
requests *util.AtomicCounter[int64]
requestLimiter *rate.Limiter
emails *rate.Limiter
subscriptions util.Limiter
bandwidth util.Limiter
firebase time.Time // Next allowed Firebase message
seen time.Time
mu sync.Mutex
2021-11-02 08:21:38 +13:00
}
2022-04-04 04:39:52 +12:00
type visitorStats struct {
AttachmentFileSizeLimit int64 `json:"attachmentFileSizeLimit"`
VisitorAttachmentBytesTotal int64 `json:"visitorAttachmentBytesTotal"`
VisitorAttachmentBytesUsed int64 `json:"visitorAttachmentBytesUsed"`
VisitorAttachmentBytesRemaining int64 `json:"visitorAttachmentBytesRemaining"`
}
2022-12-03 09:37:48 +13:00
func newVisitor(conf *Config, messageCache *messageCache, ip netip.Addr, user *auth.User) *visitor {
2022-12-19 08:35:05 +13:00
var requestLimiter *rate.Limiter
if user != nil && user.Plan != nil {
2022-12-19 08:35:05 +13:00
requestLimiter = rate.NewLimiter(rate.Limit(user.Plan.RequestLimit)*rate.Every(24*time.Hour), conf.VisitorRequestLimitBurst)
} else {
2022-12-19 08:35:05 +13:00
requestLimiter = rate.NewLimiter(rate.Every(conf.VisitorRequestLimitReplenish), conf.VisitorRequestLimitBurst)
}
2021-11-02 08:21:38 +13:00
return &visitor{
2022-12-19 08:35:05 +13:00
config: conf,
messageCache: messageCache,
ip: ip,
user: user,
requests: util.NewAtomicCounter[int64](0),
requestLimiter: requestLimiter,
emails: rate.NewLimiter(rate.Every(conf.VisitorEmailLimitReplenish), conf.VisitorEmailLimitBurst),
subscriptions: util.NewFixedLimiter(int64(conf.VisitorSubscriptionLimit)),
bandwidth: util.NewBytesLimiter(conf.VisitorAttachmentDailyBandwidthLimit, 24*time.Hour),
firebase: time.Unix(0, 0),
seen: time.Now(),
2021-11-02 08:21:38 +13:00
}
}
func (v *visitor) RequestAllowed() error {
2022-12-19 08:35:05 +13:00
if !v.requestLimiter.Allow() {
2021-12-26 03:15:05 +13:00
return errVisitorLimitReached
2021-12-24 12:03:04 +13:00
}
return nil
}
2022-06-01 12:38:56 +12:00
func (v *visitor) FirebaseAllowed() error {
v.mu.Lock()
defer v.mu.Unlock()
if time.Now().Before(v.firebase) {
return errVisitorLimitReached
}
return nil
}
func (v *visitor) FirebaseTemporarilyDeny() {
v.mu.Lock()
defer v.mu.Unlock()
2022-06-01 15:27:24 +12:00
v.firebase = time.Now().Add(v.config.FirebaseQuotaExceededPenaltyDuration)
2022-06-01 12:38:56 +12:00
}
2021-12-24 12:03:04 +13:00
func (v *visitor) EmailAllowed() error {
if !v.emails.Allow() {
2021-12-26 03:15:05 +13:00
return errVisitorLimitReached
2021-11-02 08:21:38 +13:00
}
return nil
}
2021-12-26 03:15:05 +13:00
func (v *visitor) SubscriptionAllowed() error {
2021-11-02 08:21:38 +13:00
v.mu.Lock()
defer v.mu.Unlock()
if err := v.subscriptions.Allow(1); err != nil {
2021-12-26 03:15:05 +13:00
return errVisitorLimitReached
2021-11-02 08:21:38 +13:00
}
return nil
}
func (v *visitor) RemoveSubscription() {
v.mu.Lock()
defer v.mu.Unlock()
v.subscriptions.Allow(-1)
2021-11-02 08:21:38 +13:00
}
func (v *visitor) Keepalive() {
v.mu.Lock()
defer v.mu.Unlock()
v.seen = time.Now()
}
2022-01-13 15:24:48 +13:00
func (v *visitor) BandwidthLimiter() util.Limiter {
return v.bandwidth
2022-01-13 12:52:07 +13:00
}
2021-11-02 08:21:38 +13:00
func (v *visitor) Stale() bool {
v.mu.Lock()
defer v.mu.Unlock()
return time.Since(v.seen) > visitorExpungeAfter
}
2022-04-04 04:39:52 +12:00
func (v *visitor) Stats() (*visitorStats, error) {
attachmentsBytesUsed, err := v.messageCache.AttachmentBytesUsed(v.ip.String())
2022-04-04 04:39:52 +12:00
if err != nil {
return nil, err
}
attachmentsBytesRemaining := v.config.VisitorAttachmentTotalSizeLimit - attachmentsBytesUsed
if attachmentsBytesRemaining < 0 {
attachmentsBytesRemaining = 0
}
return &visitorStats{
AttachmentFileSizeLimit: v.config.AttachmentFileSizeLimit,
VisitorAttachmentBytesTotal: v.config.VisitorAttachmentTotalSizeLimit,
VisitorAttachmentBytesUsed: attachmentsBytesUsed,
VisitorAttachmentBytesRemaining: attachmentsBytesRemaining,
}, nil
}