-
Notifications
You must be signed in to change notification settings - Fork 1
/
rate_limiter.go
200 lines (176 loc) · 5.16 KB
/
rate_limiter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package middleware
import (
"encoding/base64"
"hash/fnv"
"math/rand"
"net"
"net/http"
"sync"
"time"
"github.com/Nigel2392/router/v3"
"github.com/Nigel2392/router/v3/request"
"golang.org/x/time/rate"
)
// Rate limit types.
type RateLimitType int
// Rate limit types, used to determine what to rate limit with.
const (
RateLimitIP RateLimitType = iota
RateLimitIP_Proxy
RateLimitCookie
)
// Default rate limit options if none are provided.
var defaultConf = &RateLimitOptions{
RequestsPerSecond: 10,
BurstMultiplier: 3,
CleanExpiry: 5 * time.Minute,
CleanInt: 1 * time.Minute,
Type: RateLimitIP,
}
// RateLimitOptions is a struct that holds the options for the rate limiter.
type RateLimitOptions struct {
CookieName string
Type RateLimitType
RequestsPerSecond int
BurstMultiplier int
CleanExpiry time.Duration
CleanInt time.Duration
LimitHandler func(r *request.Request)
}
// Rate returns the rate limit.
func (r *RateLimitOptions) rate() rate.Limit {
return rate.Limit(r.RequestsPerSecond)
}
// Burst returns the burst limit.
func (r *RateLimitOptions) burst() int {
return r.RequestsPerSecond * r.BurstMultiplier
}
// Rate Limiter Middleware
func RateLimiterMiddleware(conf *RateLimitOptions) func(next router.Handler) router.Handler {
// Use default config if none is provided
if conf == nil {
conf = defaultConf
}
// Start goroutine to go through and clean up old visitors
go cleanupVisitors(conf.CleanInt, conf.CleanExpiry)
// Return the middleware function
switch conf.Type {
case RateLimitIP, RateLimitIP_Proxy:
goto rateLimitByIP
case RateLimitCookie:
goto rateLimitByCookie
default:
goto rateLimitByIP
}
// Rate limit by cookie
rateLimitByCookie:
return middlewareFunc(func(next router.Handler, r *request.Request) {
// Get the cookie from the request
var cookie, err = r.Request.Cookie(conf.CookieName)
if err != nil {
// Create a new cookie if it doesn't exist
cookie = &http.Cookie{
Name: conf.CookieName,
// Generate a unique ID for the cookie
Value: generateUniqueID(),
HttpOnly: true,
Path: "/",
}
}
cookie.Expires = time.Now().Add(conf.CleanExpiry)
r.SetCookies(cookie)
makeChoice(cookie.Value, conf, next, r)
})
// Rate limit by IP
rateLimitByIP:
// Return the middleware function
return middlewareFunc(func(next router.Handler, r *request.Request) {
// Get the IP from the request
var ip string
var err error
// If we are rate limiting behind a proxy, get the IP from the headers
if conf.Type == RateLimitIP_Proxy {
if ip = r.GetHeader("X-Forwarded-For"); ip != "" {
r.Request.RemoteAddr = ip
} else if ip = r.GetHeader("X-Real-IP"); ip != "" {
r.Request.RemoteAddr = ip
}
}
// Split the IP from the port
if ip, _, err = net.SplitHostPort(r.Request.RemoteAddr); err != nil {
if DEFAULT_LOGGER != nil {
DEFAULT_LOGGER.Error(FormatMessage(r, "ERROR", "Error getting IP: %s", err.Error()))
}
r.Error(http.StatusInternalServerError, "Internal Server Error")
return
}
// Choose to allow or disallow the request
makeChoice(ip, conf, next, r)
})
}
// Visitor struct, holds the limiter and the last time the visitor was seen.
type visitor struct {
limiter *rate.Limiter
lastSeen time.Time
}
// Map of visitors, and a mutex to lock it.
var visitors = make(map[string]*visitor)
var mu = &sync.Mutex{}
// Get a visitor from the map, or create a new one if it doesn't exist.
func getVisitor(id string, r rate.Limit, b int) *rate.Limiter {
mu.Lock()
defer mu.Unlock()
v, ok := visitors[id]
if !ok {
limiter := rate.NewLimiter(r, b)
visitors[id] = &visitor{limiter, time.Now()}
return limiter
}
v.lastSeen = time.Now()
return v.limiter
}
// Go through and clean up old visitors.
func cleanupVisitors(clean_interval, clean_expiry time.Duration) {
var t = time.NewTicker(clean_interval)
for {
<-t.C
mu.Lock()
for ip, v := range visitors {
if time.Since(v.lastSeen) > clean_expiry {
delete(visitors, ip)
}
}
mu.Unlock()
}
}
// Make a choice, either allow the request or return a 429.
func makeChoice(id string, conf *RateLimitOptions, next router.Handler, r *request.Request) {
limiter := getVisitor(id, conf.rate(), conf.burst())
if !limiter.Allow() {
if conf.LimitHandler != nil {
conf.LimitHandler(r)
return
}
r.Error(http.StatusTooManyRequests, "Too Many Requests")
return
}
next.ServeHTTP(r)
}
// Shorthand for creating a middleware function.
func middlewareFunc(f func(next router.Handler, r *request.Request)) func(next router.Handler) router.Handler {
// Return the middleware function
return func(next router.Handler) router.Handler {
return router.HandleFunc(func(r *request.Request) {
f(next, r)
})
}
}
var seed = rand.NewSource(time.Now().UnixNano())
// Generate a unique ID.
func generateUniqueID() string {
var b = make([]byte, 16)
for i := range b {
b[i] = byte(seed.Int63())
}
return base64.URLEncoding.EncodeToString(fnv.New32a().Sum(b))
}