2021-11-01 19:21:38 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2021-12-25 14:15:05 +00:00
|
|
|
"errors"
|
2022-12-02 20:37:48 +00:00
|
|
|
"heckel.io/ntfy/auth"
|
2022-10-05 20:42:07 +00:00
|
|
|
"net/netip"
|
2021-11-01 19:21:38 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
2022-10-05 20:42:07 +00:00
|
|
|
|
|
|
|
"golang.org/x/time/rate"
|
|
|
|
"heckel.io/ntfy/util"
|
2021-11-01 19:21:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-12-23 23:03:04 +00: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 19:21:38 +00:00
|
|
|
)
|
|
|
|
|
2021-12-25 14:15:05 +00:00
|
|
|
var (
|
|
|
|
errVisitorLimitReached = errors.New("limit reached")
|
|
|
|
)
|
|
|
|
|
2021-11-01 19:21:38 +00:00
|
|
|
// visitor represents an API user, and its associated rate.Limiter used for rate limiting
|
|
|
|
type visitor struct {
|
2022-12-19 14:59:32 +00:00
|
|
|
config *Config
|
|
|
|
messageCache *messageCache
|
|
|
|
ip netip.Addr
|
|
|
|
user *auth.User
|
|
|
|
messages int64
|
|
|
|
emails int64
|
|
|
|
requestLimiter *rate.Limiter
|
|
|
|
emailsLimiter *rate.Limiter
|
|
|
|
subscriptionLimiter util.Limiter
|
|
|
|
bandwidthLimiter util.Limiter
|
|
|
|
firebase time.Time // Next allowed Firebase message
|
|
|
|
seen time.Time
|
|
|
|
mu sync.Mutex
|
2021-11-01 19:21:38 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 16:39:52 +00:00
|
|
|
type visitorStats struct {
|
2022-12-19 14:59:32 +00:00
|
|
|
Messages int64
|
|
|
|
Emails int64
|
|
|
|
AttachmentBytes int64
|
2022-04-03 16:39:52 +00:00
|
|
|
}
|
|
|
|
|
2022-12-02 20:37:48 +00:00
|
|
|
func newVisitor(conf *Config, messageCache *messageCache, ip netip.Addr, user *auth.User) *visitor {
|
2022-12-18 19:35:05 +00:00
|
|
|
var requestLimiter *rate.Limiter
|
2022-12-18 04:54:19 +00:00
|
|
|
if user != nil && user.Plan != nil {
|
2022-12-19 14:59:32 +00:00
|
|
|
requestLimiter = rate.NewLimiter(rate.Limit(user.Plan.MessageLimit)*rate.Every(24*time.Hour), conf.VisitorRequestLimitBurst)
|
2022-12-18 04:54:19 +00:00
|
|
|
} else {
|
2022-12-18 19:35:05 +00:00
|
|
|
requestLimiter = rate.NewLimiter(rate.Every(conf.VisitorRequestLimitReplenish), conf.VisitorRequestLimitBurst)
|
2022-12-18 04:54:19 +00:00
|
|
|
}
|
2021-11-01 19:21:38 +00:00
|
|
|
return &visitor{
|
2022-12-19 14:59:32 +00:00
|
|
|
config: conf,
|
|
|
|
messageCache: messageCache,
|
|
|
|
ip: ip,
|
|
|
|
user: user,
|
|
|
|
messages: 0, // TODO
|
|
|
|
emails: 0, // TODO
|
|
|
|
requestLimiter: requestLimiter,
|
|
|
|
emailsLimiter: rate.NewLimiter(rate.Every(conf.VisitorEmailLimitReplenish), conf.VisitorEmailLimitBurst),
|
|
|
|
subscriptionLimiter: util.NewFixedLimiter(int64(conf.VisitorSubscriptionLimit)),
|
|
|
|
bandwidthLimiter: util.NewBytesLimiter(conf.VisitorAttachmentDailyBandwidthLimit, 24*time.Hour),
|
|
|
|
firebase: time.Unix(0, 0),
|
|
|
|
seen: time.Now(),
|
2021-11-01 19:21:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitor) RequestAllowed() error {
|
2022-12-18 19:35:05 +00:00
|
|
|
if !v.requestLimiter.Allow() {
|
2021-12-25 14:15:05 +00:00
|
|
|
return errVisitorLimitReached
|
2021-12-23 23:03:04 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-01 00:38:56 +00: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 03:27:24 +00:00
|
|
|
v.firebase = time.Now().Add(v.config.FirebaseQuotaExceededPenaltyDuration)
|
2022-06-01 00:38:56 +00:00
|
|
|
}
|
|
|
|
|
2021-12-23 23:03:04 +00:00
|
|
|
func (v *visitor) EmailAllowed() error {
|
2022-12-19 14:59:32 +00:00
|
|
|
if !v.emailsLimiter.Allow() {
|
2021-12-25 14:15:05 +00:00
|
|
|
return errVisitorLimitReached
|
2021-11-01 19:21:38 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-25 14:15:05 +00:00
|
|
|
func (v *visitor) SubscriptionAllowed() error {
|
2021-11-01 19:21:38 +00:00
|
|
|
v.mu.Lock()
|
|
|
|
defer v.mu.Unlock()
|
2022-12-19 14:59:32 +00:00
|
|
|
if err := v.subscriptionLimiter.Allow(1); err != nil {
|
2021-12-25 14:15:05 +00:00
|
|
|
return errVisitorLimitReached
|
2021-11-01 19:21:38 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitor) RemoveSubscription() {
|
|
|
|
v.mu.Lock()
|
|
|
|
defer v.mu.Unlock()
|
2022-12-19 14:59:32 +00:00
|
|
|
v.subscriptionLimiter.Allow(-1)
|
2021-11-01 19:21:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitor) Keepalive() {
|
|
|
|
v.mu.Lock()
|
|
|
|
defer v.mu.Unlock()
|
|
|
|
v.seen = time.Now()
|
|
|
|
}
|
|
|
|
|
2022-01-13 02:24:48 +00:00
|
|
|
func (v *visitor) BandwidthLimiter() util.Limiter {
|
2022-12-19 14:59:32 +00:00
|
|
|
return v.bandwidthLimiter
|
2022-01-12 23:52:07 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 19:21:38 +00:00
|
|
|
func (v *visitor) Stale() bool {
|
|
|
|
v.mu.Lock()
|
|
|
|
defer v.mu.Unlock()
|
|
|
|
return time.Since(v.seen) > visitorExpungeAfter
|
|
|
|
}
|
2022-04-03 16:39:52 +00:00
|
|
|
|
2022-12-19 14:59:32 +00:00
|
|
|
func (v *visitor) IncrMessages() {
|
|
|
|
v.mu.Lock()
|
|
|
|
defer v.mu.Unlock()
|
|
|
|
v.messages++
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitor) IncrEmails() {
|
|
|
|
v.mu.Lock()
|
|
|
|
defer v.mu.Unlock()
|
|
|
|
v.emails++
|
|
|
|
}
|
|
|
|
|
2022-04-03 16:39:52 +00:00
|
|
|
func (v *visitor) Stats() (*visitorStats, error) {
|
2022-10-05 20:42:07 +00:00
|
|
|
attachmentsBytesUsed, err := v.messageCache.AttachmentBytesUsed(v.ip.String())
|
2022-04-03 16:39:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-19 14:59:32 +00:00
|
|
|
v.mu.Lock()
|
|
|
|
defer v.mu.Unlock()
|
2022-04-03 16:39:52 +00:00
|
|
|
return &visitorStats{
|
2022-12-19 14:59:32 +00:00
|
|
|
Messages: v.messages,
|
|
|
|
Emails: v.emails,
|
|
|
|
AttachmentBytes: attachmentsBytesUsed,
|
2022-04-03 16:39:52 +00:00
|
|
|
}, nil
|
|
|
|
}
|