visitor.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package server
  2. import (
  3. "errors"
  4. "golang.org/x/time/rate"
  5. "heckel.io/ntfy/util"
  6. "sync"
  7. "time"
  8. )
  9. const (
  10. // visitorExpungeAfter defines how long a visitor is active before it is removed from memory. This number
  11. // has to be very high to prevent e-mail abuse, but it doesn't really affect the other limits anyway, since
  12. // they are replenished faster (typically).
  13. visitorExpungeAfter = 24 * time.Hour
  14. )
  15. var (
  16. errVisitorLimitReached = errors.New("limit reached")
  17. )
  18. // visitor represents an API user, and its associated rate.Limiter used for rate limiting
  19. type visitor struct {
  20. config *Config
  21. messageCache *messageCache
  22. ip string
  23. requests *rate.Limiter
  24. emails *rate.Limiter
  25. subscriptions util.Limiter
  26. bandwidth util.Limiter
  27. seen time.Time
  28. mu sync.Mutex
  29. }
  30. type visitorStats struct {
  31. AttachmentFileSizeLimit int64 `json:"attachmentFileSizeLimit"`
  32. VisitorAttachmentBytesTotal int64 `json:"visitorAttachmentBytesTotal"`
  33. VisitorAttachmentBytesUsed int64 `json:"visitorAttachmentBytesUsed"`
  34. VisitorAttachmentBytesRemaining int64 `json:"visitorAttachmentBytesRemaining"`
  35. }
  36. func newVisitor(conf *Config, messageCache *messageCache, ip string) *visitor {
  37. return &visitor{
  38. config: conf,
  39. messageCache: messageCache,
  40. ip: ip,
  41. requests: rate.NewLimiter(rate.Every(conf.VisitorRequestLimitReplenish), conf.VisitorRequestLimitBurst),
  42. emails: rate.NewLimiter(rate.Every(conf.VisitorEmailLimitReplenish), conf.VisitorEmailLimitBurst),
  43. subscriptions: util.NewFixedLimiter(int64(conf.VisitorSubscriptionLimit)),
  44. bandwidth: util.NewBytesLimiter(conf.VisitorAttachmentDailyBandwidthLimit, 24*time.Hour),
  45. seen: time.Now(),
  46. }
  47. }
  48. func (v *visitor) IP() string {
  49. return v.ip
  50. }
  51. func (v *visitor) RequestAllowed() error {
  52. if !v.requests.Allow() {
  53. return errVisitorLimitReached
  54. }
  55. return nil
  56. }
  57. func (v *visitor) EmailAllowed() error {
  58. if !v.emails.Allow() {
  59. return errVisitorLimitReached
  60. }
  61. return nil
  62. }
  63. func (v *visitor) SubscriptionAllowed() error {
  64. v.mu.Lock()
  65. defer v.mu.Unlock()
  66. if err := v.subscriptions.Allow(1); err != nil {
  67. return errVisitorLimitReached
  68. }
  69. return nil
  70. }
  71. func (v *visitor) RemoveSubscription() {
  72. v.mu.Lock()
  73. defer v.mu.Unlock()
  74. v.subscriptions.Allow(-1)
  75. }
  76. func (v *visitor) Keepalive() {
  77. v.mu.Lock()
  78. defer v.mu.Unlock()
  79. v.seen = time.Now()
  80. }
  81. func (v *visitor) BandwidthLimiter() util.Limiter {
  82. return v.bandwidth
  83. }
  84. func (v *visitor) Stale() bool {
  85. v.mu.Lock()
  86. defer v.mu.Unlock()
  87. return time.Since(v.seen) > visitorExpungeAfter
  88. }
  89. func (v *visitor) Stats() (*visitorStats, error) {
  90. attachmentsBytesUsed, err := v.messageCache.AttachmentBytesUsed(v.ip)
  91. if err != nil {
  92. return nil, err
  93. }
  94. attachmentsBytesRemaining := v.config.VisitorAttachmentTotalSizeLimit - attachmentsBytesUsed
  95. if attachmentsBytesRemaining < 0 {
  96. attachmentsBytesRemaining = 0
  97. }
  98. return &visitorStats{
  99. AttachmentFileSizeLimit: v.config.AttachmentFileSizeLimit,
  100. VisitorAttachmentBytesTotal: v.config.VisitorAttachmentTotalSizeLimit,
  101. VisitorAttachmentBytesUsed: attachmentsBytesUsed,
  102. VisitorAttachmentBytesRemaining: attachmentsBytesRemaining,
  103. }, nil
  104. }