-
Notifications
You must be signed in to change notification settings - Fork 20
/
ratelimit.go
312 lines (270 loc) · 7.88 KB
/
ratelimit.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package ratelimit
import (
"fmt"
"net"
"net/http"
"net/netip"
"regexp"
"strconv"
"strings"
"time"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"go.uber.org/zap"
)
var (
regexpFullVar = regexp.MustCompile(`^\{http\.request\..+\}$`)
regexpShortVar = regexp.MustCompile(`^\{(\w+)\.(.+)\}$`)
// "host_prefix.<bits>" or "ip_prefix.<bits>"
regexpPrefixVar = regexp.MustCompile(`^(host_prefix|ip_prefix)\.([0-9]+)$`)
regexpRate = regexp.MustCompile(`^(\d+)r/(s|m)$`)
)
func init() {
caddy.RegisterModule(RateLimit{})
}
// RateLimit implements a handler for rate-limiting.
//
// If a client exceeds the rate limit, an HTTP error with status `<reject_status>` will
// be returned. This error can be handled using the conventional error handlers.
// See [handle_errors](https://caddyserver.com/docs/caddyfile/directives/handle_errors)
// for how to set up error handlers.
type RateLimit struct {
// The variable used to differentiate one client from another.
//
// Currently supported variables:
//
// - `{path.<var>}`
// - `{query.<var>}`
// - `{header.<VAR>}`
// - `{cookie.<var>}`
// - `{body.<var>}` (requires the [requestbodyvar](https://github.com/RussellLuo/caddy-ext/tree/master/requestbodyvar) extension)
// - `{remote.host}` (ignores the `X-Forwarded-For` header)
// - `{remote.port}`
// - `{remote.ip}` (prefers the first IP in the `X-Forwarded-For` header)
// - `{remote.host_prefix.<bits>}` (CIDR block version of `{remote.host}`)
// - `{remote.ip_prefix.<bits>}` (CIDR block version of `{remote.ip}`)
Key string `json:"key,omitempty"`
// The request rate limit (per key value) specified in requests
// per second (r/s) or requests per minute (r/m).
Rate string `json:"rate,omitempty"`
// The size (i.e. the number of key values) of the LRU zone that
// keeps states of these key values. Defaults to 10,000.
ZoneSize int `json:"zone_size,omitempty"`
// The HTTP status code of the response when a client exceeds the rate.
// Defaults to 429 (Too Many Requests).
RejectStatusCode int `json:"reject_status,omitempty"`
keyVar *Var
zone *Zone
logger *zap.Logger
}
// CaddyModule returns the Caddy module information.
func (RateLimit) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.rate_limit",
New: func() caddy.Module { return new(RateLimit) },
}
}
// Provision implements caddy.Provisioner.
func (rl *RateLimit) Provision(ctx caddy.Context) (err error) {
rl.logger = ctx.Logger(rl)
return rl.provision()
}
func (rl *RateLimit) provision() (err error) {
rl.keyVar, err = ParseVar(rl.Key)
if err != nil {
return err
}
rateSize, rateLimit, err := parseRate(rl.Rate)
if err != nil {
return err
}
if rl.ZoneSize == 0 {
rl.ZoneSize = 10000 // At most 10,000 keys by default
}
rl.zone, err = NewZone(rl.ZoneSize, rateSize, int64(rateLimit))
if err != nil {
return err
}
if rl.RejectStatusCode == 0 {
rl.RejectStatusCode = http.StatusTooManyRequests
}
return nil
}
// Cleanup cleans up the resources made by rl during provisioning.
func (rl *RateLimit) Cleanup() error {
if rl.zone != nil {
rl.zone.Purge()
}
return nil
}
// Validate implements caddy.Validator.
func (rl *RateLimit) Validate() error {
if rl.keyVar == nil {
return fmt.Errorf("no key variable")
}
if rl.zone == nil {
return fmt.Errorf("no zone created")
}
if http.StatusText(rl.RejectStatusCode) == "" {
return fmt.Errorf("unknown code reject_status: %d", rl.RejectStatusCode)
}
return nil
}
// ServeHTTP implements caddyhttp.MiddlewareHandler.
func (rl *RateLimit) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
keyValue, err := rl.keyVar.Evaluate(r)
if err != nil {
rl.logger.Error("failed to evaluate variable",
zap.String("variable", rl.keyVar.Raw),
zap.Error(err),
)
return next.ServeHTTP(w, r)
}
w.Header().Add("RateLimit-Policy", rl.zone.RateLimitPolicyHeader())
if keyValue != "" && !rl.zone.Allow(keyValue) {
rl.logger.Debug("request is rejected",
zap.String("variable", rl.keyVar.Raw),
zap.String("value", keyValue),
)
w.WriteHeader(rl.RejectStatusCode)
// Return an error to invoke possible error handlers.
return caddyhttp.Error(rl.RejectStatusCode, nil)
}
return next.ServeHTTP(w, r)
}
type Var struct {
Raw string
Name string
Bits int
}
// ParseVar transforms shorthand variables into Caddy-style placeholders.
//
// Examples for shorthand variables:
//
// - `{path.<var>}`
// - `{query.<var>}`
// - `{header.<VAR>}`
// - `{cookie.<var>}`
// - `{body.<var>}`
// - `{remote.host}`
// - `{remote.port}`
// - `{remote.ip}`
// - `{remote.host_prefix.<bits>}`
// - `{remote.ip_prefix.<bits>}`
func ParseVar(s string) (*Var, error) {
v := &Var{Raw: s}
if regexpFullVar.MatchString(s) {
// If the variable is already a fully-qualified Caddy placeholder,
// return it as is.
v.Name = s
return v, nil
}
result := regexpShortVar.FindStringSubmatch(s)
if len(result) != 3 {
return nil, fmt.Errorf("invalid key variable: %q", s)
}
location, name := result[1], result[2]
switch location {
case "path":
v.Name = fmt.Sprintf("{http.request.uri.path.%s}", name)
case "query":
v.Name = fmt.Sprintf("{http.request.uri.query.%s}", name)
case "header":
v.Name = fmt.Sprintf("{http.request.header.%s}", name)
case "cookie":
v.Name = fmt.Sprintf("{http.request.cookie.%s}", name)
case "body":
v.Name = fmt.Sprintf("{http.request.body.%s}", name)
case "remote":
if name == "host" || name == "port" || name == "ip" {
v.Name = fmt.Sprintf("{http.request.remote.%s}", name)
return v, nil
}
r := regexpPrefixVar.FindStringSubmatch(name)
if len(r) != 3 {
return nil, fmt.Errorf("invalid key variable: %q", s)
}
v.Name = fmt.Sprintf("{http.request.remote.%s}", r[1])
if r[2] == "" {
return nil, fmt.Errorf("invalid key variable: %q", s)
}
bits, err := strconv.Atoi(r[2])
if err != nil {
return nil, err
}
v.Bits = bits
default:
return nil, fmt.Errorf("unrecognized key variable: %q", s)
}
return v, nil
}
func (v *Var) Evaluate(r *http.Request) (value string, err error) {
switch v.Name {
case "{http.request.remote.ip}":
ip, err := getClientIP(r, true)
if err != nil {
return "", err
}
return ip.String(), nil
case "{http.request.remote.host_prefix}":
return v.evaluatePrefix(r, false)
case "{http.request.remote.ip_prefix}":
return v.evaluatePrefix(r, true)
default:
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
value = repl.ReplaceAll(v.Name, "")
return value, nil
}
}
func (v *Var) evaluatePrefix(r *http.Request, forwarded bool) (value string, err error) {
ip, err := getClientIP(r, forwarded)
if err != nil {
return "", err
}
prefix, err := ip.Prefix(v.Bits)
if err != nil {
return "", err
}
return prefix.Masked().String(), nil
}
func getClientIP(r *http.Request, forwarded bool) (netip.Addr, error) {
remote := r.RemoteAddr
if forwarded {
if fwdFor := r.Header.Get("X-Forwarded-For"); fwdFor != "" {
remote = strings.TrimSpace(strings.Split(fwdFor, ",")[0])
}
}
ipStr, _, err := net.SplitHostPort(remote)
if err != nil {
ipStr = remote // OK; probably didn't have a port
}
return netip.ParseAddr(ipStr)
}
func parseRate(rate string) (size time.Duration, limit int, err error) {
if rate == "" {
return 0, 0, fmt.Errorf("missing rate")
}
result := regexpRate.FindStringSubmatch(rate)
if len(result) != 3 {
return 0, 0, fmt.Errorf("invalid rate: %s", rate)
}
limitStr, sizeStr := result[1], result[2]
switch sizeStr {
case "s":
size = time.Second
case "m":
size = time.Minute
}
limit, err = strconv.Atoi(limitStr)
if err != nil {
return 0, 0, fmt.Errorf("size-limit must be an integer; invalid: %v", err)
}
return
}
// Interface guards
var (
_ caddy.Provisioner = (*RateLimit)(nil)
_ caddy.CleanerUpper = (*RateLimit)(nil)
_ caddy.Validator = (*RateLimit)(nil)
_ caddyhttp.MiddlewareHandler = (*RateLimit)(nil)
)