-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlimiter.go
More file actions
78 lines (63 loc) · 1.54 KB
/
limiter.go
File metadata and controls
78 lines (63 loc) · 1.54 KB
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
package main
import (
"log"
"net/http"
"strings"
"sync"
"time"
"golang.org/x/time/rate"
)
const NUM_REQUESTS = 2
const NUM_SECONDS = 20
type RateLimiter struct {
limiters map[string]*rate.Limiter
mu sync.Mutex
}
var rateLimiter = NewRateLimiter()
func RateLimited(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
ip := getIP(r)
limiter := rateLimiter.GetLimiter(ip)
if limiter.Allow() {
log.Println("Request allowed for IP:", ip)
next.ServeHTTP(w, r)
} else {
log.Println("Request rate limited for IP:", ip)
http.Error(w, "Too Many Requests", http.StatusTooManyRequests)
}
}
}
func NewRateLimiter() *RateLimiter {
return &RateLimiter{
limiters: make(map[string]*rate.Limiter),
}
}
func (r *RateLimiter) GetLimiter(ip string) *rate.Limiter {
r.mu.Lock()
defer r.mu.Unlock()
limiter, exists := r.limiters[ip]
if !exists {
limiter = rate.NewLimiter(rate.Every(NUM_SECONDS*time.Second), NUM_REQUESTS) // 2 requests every 20 seconds
r.limiters[ip] = limiter
}
return limiter
}
func getIP(r *http.Request) string {
ip := r.Header.Get("X-Forwarded-For")
if ip == "" {
ip = r.RemoteAddr
}
// Extract the IP address without the port
if strings.Contains(ip, ":") {
ip = ip[:strings.LastIndex(ip, ":")]
if strings.Count(ip, ":") > 1 { // IPv6 address
ip = strings.Trim(ip, "[]")
}
}
log.Println("Extracted IP address:", ip)
return ip
}