Better rate limiting
This commit is contained in:
		@ -1,6 +1,10 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import "github.com/gin-gonic/gin"
 | 
			
		||||
import (
 | 
			
		||||
	"log"
 | 
			
		||||
 | 
			
		||||
	"github.com/gin-gonic/gin"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
import "github.com/manucorporat/stats"
 | 
			
		||||
 | 
			
		||||
@ -8,8 +12,11 @@ var ips = stats.New()
 | 
			
		||||
 | 
			
		||||
func ratelimit(c *gin.Context) {
 | 
			
		||||
	ip := c.ClientIP()
 | 
			
		||||
	value := ips.Add(ip, 1)
 | 
			
		||||
	if value > 400 {
 | 
			
		||||
	value := uint64(ips.Add(ip, 1))
 | 
			
		||||
	if value >= 400 {
 | 
			
		||||
		if value%400 == 0 {
 | 
			
		||||
			log.Printf("BlockedIP:%s Requests:%d\n", ip, value)
 | 
			
		||||
		}
 | 
			
		||||
		c.AbortWithStatus(401)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -9,7 +9,7 @@ import (
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	router := gin.New()
 | 
			
		||||
	router.Use(gin.Logger(), ratelimit)
 | 
			
		||||
	router.Use(ratelimit, gin.Recovery(), gin.Logger())
 | 
			
		||||
 | 
			
		||||
	router.LoadHTMLGlob("resources/*.templ.html")
 | 
			
		||||
	router.Static("/static", "resources/static")
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user