2019-03-12 09:13:57 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ErrBadRateLimiter = errors.New("That rate limiter doesn't exist")
|
|
|
|
var ErrExceededRateLimit = errors.New("You're exceeding a rate limit. Please wait a while before trying again.")
|
|
|
|
|
|
|
|
// TODO: Persist rate limits to disk
|
|
|
|
type RateLimiter interface {
|
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
|
|
|
LimitIP(limit, ip string) error
|
2019-03-12 09:13:57 +00:00
|
|
|
LimitUser(limit string, user int) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type RateData struct {
|
|
|
|
value int
|
|
|
|
floorTime int
|
|
|
|
}
|
|
|
|
|
|
|
|
type RateFence struct {
|
|
|
|
duration int
|
|
|
|
max int
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Optimise this by using something other than a string when possible
|
|
|
|
type RateLimit struct {
|
|
|
|
data map[string][]RateData
|
|
|
|
fences []RateFence
|
|
|
|
|
|
|
|
sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRateLimit(fences []RateFence) *RateLimit {
|
|
|
|
for i, fence := range fences {
|
|
|
|
fences[i].duration = fence.duration * 1000 * 1000 * 1000
|
|
|
|
}
|
|
|
|
return &RateLimit{data: make(map[string][]RateData), fences: fences}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *RateLimit) Limit(name string, ltype int) error {
|
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
|
|
|
|
|
|
|
data, ok := l.data[name]
|
|
|
|
if !ok {
|
|
|
|
data = make([]RateData, len(l.fences))
|
|
|
|
for i, _ := range data {
|
|
|
|
data[i] = RateData{0, int(time.Now().Unix())}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, field := range data {
|
|
|
|
fence := l.fences[i]
|
|
|
|
diff := int(time.Now().Unix()) - field.floorTime
|
|
|
|
|
|
|
|
if diff >= fence.duration {
|
|
|
|
field = RateData{0, int(time.Now().Unix())}
|
|
|
|
data[i] = field
|
|
|
|
}
|
|
|
|
|
|
|
|
if field.value > fence.max {
|
|
|
|
return ErrExceededRateLimit
|
|
|
|
}
|
|
|
|
|
|
|
|
field.value++
|
|
|
|
data[i] = field
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type DefaultRateLimiter struct {
|
|
|
|
limits map[string]*RateLimit
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDefaultRateLimiter() *DefaultRateLimiter {
|
|
|
|
return &DefaultRateLimiter{map[string]*RateLimit{
|
2021-01-06 06:41:08 +00:00
|
|
|
"register": NewRateLimit([]RateFence{{int(time.Hour / 2), 1}}),
|
2019-03-12 09:13:57 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
|
|
|
func (l *DefaultRateLimiter) LimitIP(limit, ip string) error {
|
2019-03-12 09:13:57 +00:00
|
|
|
limiter, ok := l.limits[limit]
|
|
|
|
if !ok {
|
|
|
|
return ErrBadRateLimiter
|
|
|
|
}
|
|
|
|
return limiter.Limit(ip, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *DefaultRateLimiter) LimitUser(limit string, user int) error {
|
|
|
|
limiter, ok := l.limits[limit]
|
|
|
|
if !ok {
|
|
|
|
return ErrBadRateLimiter
|
|
|
|
}
|
|
|
|
return limiter.Limit(strconv.Itoa(user), 1)
|
|
|
|
}
|