-
Notifications
You must be signed in to change notification settings - Fork 1
/
pandaria_limiter.go
226 lines (185 loc) · 4.81 KB
/
pandaria_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
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
package publicapi
import (
"fmt"
"math"
"net"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
"github.com/sirupsen/logrus"
"golang.org/x/time/rate"
)
var (
ipLookups = []string{"X-Forwarded-For", "X-Real-IP", "RemoteAddr"}
)
const (
defaultRequestsPerSecond = 1.0
defaultCooldownSeconds = 20 * time.Second
defaultFailureTimes = 3
loginCooldownEnv = "PANDARIA_LOGIN_COOLDOWN"
loginRateLimitEnv = "PANDARIA_LOGIN_RATELIMIT"
responseFailureHeader = "X-Pandaria-Login-Failure"
responseCooldownHeader = "X-Pandaria-Login-CoolDown"
)
// AuthLimiter can only work for local auth provider
type AuthLimiter struct {
df map[string]int
dt map[string]time.Time
mu *sync.RWMutex
period time.Duration
}
func newAuthLimiter() *AuthLimiter {
var period time.Duration
cooldown := os.Getenv(loginCooldownEnv)
if cooldown != "" {
period, _ = time.ParseDuration(cooldown)
}
if period <= 0 {
period = defaultCooldownSeconds
}
logrus.Infof("Auth limit cooldown period for login http request: %s", period.String())
return &AuthLimiter{
df: make(map[string]int),
dt: make(map[string]time.Time),
mu: &sync.RWMutex{},
period: period,
}
}
func (al *AuthLimiter) MarkFailure(key string, request *types.APIContext) int {
if os.Getenv(loginCooldownEnv) == "" {
return 0
}
al.mu.Lock()
defer al.mu.Unlock()
count, ok := al.df[key]
if ok {
if count < defaultFailureTimes {
al.df[key] = count + 1
}
if al.df[key] >= defaultFailureTimes {
al.dt[key] = time.Now().Add(al.period)
}
} else {
al.df[key] = 1
}
request.Response.Header().Add(responseFailureHeader, strconv.Itoa(al.df[key]))
return al.df[key]
}
func (al *AuthLimiter) getCooldownTime(key string) (time.Time, bool) {
al.mu.Lock()
defer al.mu.Unlock()
t, ok := al.dt[key]
return t, ok
}
func (al *AuthLimiter) reset(key string) {
al.mu.Lock()
defer al.mu.Unlock()
delete(al.df, key)
delete(al.dt, key)
}
func (al *AuthLimiter) LimitByUser(key string, request *types.APIContext) error {
if os.Getenv(loginCooldownEnv) != "" {
t, exist := al.getCooldownTime(key)
if exist {
cd := t.Sub(time.Now())
if cd > 0 {
displayCD := fmt.Sprintf("%0.2fs", cd.Seconds())
request.Response.Header().Add(responseCooldownHeader, displayCD)
return httperror.NewAPIError(httperror.PermissionDenied, fmt.Sprintf("You should try after %s.", displayCD))
}
al.reset(key)
}
}
return nil
}
// IPRateLimiter .
type IPRateLimiter struct {
ips map[string]*rate.Limiter
mu *sync.RWMutex
r rate.Limit
b int
}
func newIPRateLimiter() *IPRateLimiter {
var max float64
rateLimitEnv := os.Getenv("PANDARIA_LOGIN_RATELIMIT")
if rateLimitEnv != "" {
max, _ = strconv.ParseFloat(rateLimitEnv, 64)
}
if max <= 0 {
max = defaultRequestsPerSecond
}
logrus.Infof("rate limit for login http request: %f/s", max)
i := &IPRateLimiter{
ips: make(map[string]*rate.Limiter),
mu: &sync.RWMutex{},
r: rate.Limit(max),
b: int(math.Max(1, max)),
}
return i
}
// addIP creates a new rate limiter and adds it to the ips map,
// using the IP address as the key
func (i *IPRateLimiter) addIP(ip string) *rate.Limiter {
i.mu.Lock()
defer i.mu.Unlock()
limiter := rate.NewLimiter(i.r, i.b)
i.ips[ip] = limiter
return limiter
}
// getLimiter returns the rate limiter for the provided IP address if it exists.
// Otherwise calls addIP to add IP address to the map
func (i *IPRateLimiter) getLimiter(ip string) *rate.Limiter {
i.mu.Lock()
limiter, exists := i.ips[ip]
if !exists {
i.mu.Unlock()
return i.addIP(ip)
}
i.mu.Unlock()
return limiter
}
func (i *IPRateLimiter) LimitByRequest(request *types.APIContext) error {
if os.Getenv(loginRateLimitEnv) != "" {
limiter := i.getLimiter(lookupRemoteIP(request.Request))
if !limiter.Allow() {
return httperror.NewAPIError(httperror.MaxLimitExceeded, "You have reached maximum request limit.")
}
}
return nil
}
func lookupRemoteIP(r *http.Request) string {
realIP := r.Header.Get("X-Real-IP")
forwardedFor := r.Header.Get("X-Forwarded-For")
for _, lookup := range ipLookups {
if lookup == "RemoteAddr" {
// 1. Cover the basic use cases for both ipv4 and ipv6
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
// 2. Upon error, just return the remote addr.
return r.RemoteAddr
}
return ip
}
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)
}
partIndex := len(parts) - 1
if partIndex < 0 {
partIndex = 0
}
return parts[partIndex]
}
if lookup == "X-Real-IP" && realIP != "" {
return realIP
}
}
return ""
}