-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtollbooth_fasthttp.go
203 lines (173 loc) · 6.44 KB
/
tollbooth_fasthttp.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
package tollbooth_fasthttp
import (
"encoding/base64"
"strings"
"github.com/didip/tollbooth"
"github.com/didip/tollbooth/config"
"github.com/didip/tollbooth/errors"
"github.com/valyala/fasthttp"
)
func LimitHandler(handler fasthttp.RequestHandler, limiter *config.Limiter) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
httpError := LimitByRequest(limiter, ctx)
if httpError != nil {
ctx.Response.Header.Set("Content-Type", limiter.MessageContentType)
ctx.SetStatusCode(httpError.StatusCode)
ctx.SetBody([]byte(httpError.Message))
return
}
handler(ctx)
}
}
func LimitByRequest(limiter *config.Limiter, ctx *fasthttp.RequestCtx) *errors.HTTPError {
sliceKeys := BuildKeys(limiter, ctx)
//Loop sliceKeys and check if one of them has an error.
for _, keys := range sliceKeys {
httpError := tollbooth.LimitByKeys(limiter, keys)
if httpError != nil {
return httpError
}
}
return nil
}
// StringInSlice finds needle in a slice of strings.
func StringInSlice(sliceString []string, needle string) bool {
for _, b := range sliceString {
if b == needle {
return true
}
}
return false
}
func ipAddrFromRemoteAddr(s string) string {
idx := strings.LastIndex(s, ":")
if idx == -1 {
return s
}
return s[:idx]
}
// RemoteIP finds IP Address given http.Request struct.
func RemoteIP(ipLookups []string, ctx *fasthttp.RequestCtx) string {
realIP := string(ctx.Request.Header.Peek("X-Real-IP"))
forwardedFor := string(ctx.Request.Header.Peek("X-Forwarded-For"))
for _, lookup := range ipLookups {
if lookup == "RemoteAddr" {
return ipAddrFromRemoteAddr(ctx.RemoteAddr().String())
}
if lookup == "X-Forwarded-For" && forwardedFor != "" {
// X-Forwarded-For is potentially a list of addresses separated with ","
parts := strings.Split(forwardedFor, ",")
for i, p := range parts {
parts[i] = strings.TrimSpace(p)
}
return parts[0]
}
if lookup == "X-Real-IP" && realIP != "" {
return realIP
}
}
return ""
}
// BuildKeys generates a slice of keys to rate-limit by given config and request structs.
func BuildKeys(limiter *config.Limiter, ctx *fasthttp.RequestCtx) [][]string {
remoteIP := RemoteIP(limiter.IPLookups, ctx)
path := string(ctx.Path())
reqMethod := string(ctx.Method())
sliceKeys := make([][]string, 0)
// Don't BuildKeys if remoteIP is blank.
if remoteIP == "" {
return sliceKeys
}
if limiter.Methods != nil && limiter.Headers != nil && limiter.BasicAuthUsers != nil {
// Limit by HTTP methods and HTTP headers+values and Basic Auth credentials.
if StringInSlice(limiter.Methods, reqMethod) {
for headerKey, headerValues := range limiter.Headers {
headerLen := len(ctx.Request.Header.Peek(headerKey))
if (headerValues == nil || len(headerValues) <= 0) && headerLen != 0 {
// If header values are empty, rate-limit all request with headerKey.
username, _, ok := parseBasicAuth(string(ctx.Request.Header.Peek("Authorization")))
if ok && StringInSlice(limiter.BasicAuthUsers, username) {
sliceKeys = append(sliceKeys, []string{remoteIP, path, reqMethod, headerKey, username})
}
} else if len(headerValues) > 0 && headerLen != 0 {
// If header values are not empty, rate-limit all request with headerKey and headerValues.
for _, headerValue := range headerValues {
username, _, ok := parseBasicAuth(string(ctx.Request.Header.Peek("Authorization")))
if ok && StringInSlice(limiter.BasicAuthUsers, username) {
sliceKeys = append(sliceKeys, []string{remoteIP, path, reqMethod, headerKey, headerValue, username})
}
}
}
}
}
} else if limiter.Methods != nil && limiter.Headers != nil {
// Limit by HTTP methods and HTTP headers+values.
if StringInSlice(limiter.Methods, reqMethod) {
for headerKey, headerValues := range limiter.Headers {
headerLen := len(ctx.Request.Header.Peek(headerKey))
if (headerValues == nil || len(headerValues) <= 0) && headerLen != 0 {
// If header values are empty, rate-limit all request with headerKey.
sliceKeys = append(sliceKeys, []string{remoteIP, path, reqMethod, headerKey})
} else if len(headerValues) > 0 && headerLen != 0 {
// If header values are not empty, rate-limit all request with headerKey and headerValues.
for _, headerValue := range headerValues {
sliceKeys = append(sliceKeys, []string{remoteIP, path, reqMethod, headerKey, headerValue})
}
}
}
}
} else if limiter.Methods != nil && limiter.BasicAuthUsers != nil {
// Limit by HTTP methods and Basic Auth credentials.
if StringInSlice(limiter.Methods, reqMethod) {
username, _, ok := parseBasicAuth(string(ctx.Request.Header.Peek("Authorization")))
if ok && StringInSlice(limiter.BasicAuthUsers, username) {
sliceKeys = append(sliceKeys, []string{remoteIP, path, reqMethod, username})
}
}
} else if limiter.Methods != nil {
// Limit by HTTP methods.
if StringInSlice(limiter.Methods, reqMethod) {
sliceKeys = append(sliceKeys, []string{remoteIP, path, reqMethod})
}
} else if limiter.Headers != nil {
// Limit by HTTP headers+values.
for headerKey, headerValues := range limiter.Headers {
headerLen := len(ctx.Request.Header.Peek(headerKey))
if (headerValues == nil || len(headerValues) <= 0) && headerLen != 0 {
// If header values are empty, rate-limit all request with headerKey.
sliceKeys = append(sliceKeys, []string{remoteIP, path, headerKey})
} else if len(headerValues) > 0 && headerLen != 0 {
// If header values are not empty, rate-limit all request with headerKey and headerValues.
for _, headerValue := range headerValues {
sliceKeys = append(sliceKeys, []string{remoteIP, path, headerKey, headerValue})
}
}
}
} else if limiter.BasicAuthUsers != nil {
// Limit by Basic Auth credentials.
username, _, ok := parseBasicAuth(string(ctx.Request.Header.Peek("Authorization")))
if ok && StringInSlice(limiter.BasicAuthUsers, username) {
sliceKeys = append(sliceKeys, []string{remoteIP, path, username})
}
} else {
// Default: Limit by remoteIP and path.
sliceKeys = append(sliceKeys, []string{remoteIP, path})
}
return sliceKeys
}
func parseBasicAuth(auth string) (string, string, bool) {
const prefix = "Basic "
if !strings.HasPrefix(auth, prefix) {
return "", "", false
}
c, err := base64.StdEncoding.DecodeString(auth[len(prefix):])
if err != nil {
return "", "", false
}
cs := string(c)
s := strings.IndexByte(cs, ':')
if s < 0 {
return "", "", false
}
return cs[:s], cs[s+1:], true
}