Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var LIMITER = func(next http.Handler) http.Handler { var limiter = rate.NewLimiter(1, LIMITER_TOKENS) return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { v, ok := banned.Load(r.RemoteAddr) if ok { if time.Since(v.(time.Time)) > LIMITER_TIMEOUT { banned.Delete(r.RemoteAddr) } else { w.WriteHeader(http.StatusTooManyRequests) w.Write([]byte("<h1>YOU DID TOO MANY REQUEST, YOU HAVE BEEN BANNED FOR 5 MINUTES </h1>")) banned.Store(r.RemoteAddr, time.Now()) return } } if !limiter.Allow() { w.WriteHeader(http.StatusTooManyRequests) w.Write([]byte("<h1>YOU DID TOO MANY REQUEST, YOU HAVE BEEN BANNED FOR 5 MINUTES </h1>")) banned.Store(r.RemoteAddr, time.Now()) return } next.ServeHTTP(w, r) }) }
View Source
var LIMITER_TIMEOUT = 5 * time.Minute
View Source
var LIMITER_TOKENS = 50
Functions ¶
This section is empty.
Types ¶
type IPRateLimiter ¶
type IPRateLimiter struct {
// contains filtered or unexported fields
}
func NewIPRateLimiter ¶
func NewIPRateLimiter(r rate.Limit, b int) *IPRateLimiter
func (*IPRateLimiter) AddIP ¶
func (i *IPRateLimiter) AddIP(ip string) *rate.Limiter
AddIP creates a new rate limiter and adds it to the ips map, using the IP address as the key
func (*IPRateLimiter) GetLimiter ¶
func (i *IPRateLimiter) GetLimiter(ip string) *rate.Limiter
GetLimiter returns the rate limiter for the provided IP address if it exists. Otherwise calls AddIP to add IP address to the map
Click to show internal directories.
Click to hide internal directories.