Adds rate limiting
This commit is contained in:
parent
eed6d93095
commit
3f48e0d45c
@ -26,7 +26,8 @@ func StartWorkers() {
|
|||||||
func StartGin() {
|
func StartGin() {
|
||||||
gin.SetMode(gin.ReleaseMode)
|
gin.SetMode(gin.ReleaseMode)
|
||||||
|
|
||||||
router := gin.Default()
|
router := gin.New()
|
||||||
|
router.Use(rateLimit, gin.Recovery(), gin.Logger())
|
||||||
router.LoadHTMLGlob("resources/*.templ.html")
|
router.LoadHTMLGlob("resources/*.templ.html")
|
||||||
router.Static("/static", "resources/static")
|
router.Static("/static", "resources/static")
|
||||||
router.GET("/", index)
|
router.GET("/", index)
|
||||||
|
@ -3,12 +3,24 @@ package main
|
|||||||
import (
|
import (
|
||||||
"html"
|
"html"
|
||||||
"io"
|
"io"
|
||||||
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func rateLimit(c *gin.Context) {
|
||||||
|
ip := c.ClientIP()
|
||||||
|
value := ips.Add(ip, 1)
|
||||||
|
if value > 800 {
|
||||||
|
if int(value)%700 == 0 {
|
||||||
|
log.Printf("ip block: %s, count: %f\n", ip, value)
|
||||||
|
}
|
||||||
|
c.AbortWithStatus(503)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func index(c *gin.Context) {
|
func index(c *gin.Context) {
|
||||||
c.Redirect(301, "/room/hn")
|
c.Redirect(301, "/room/hn")
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
"github.com/manucorporat/stats"
|
"github.com/manucorporat/stats"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var ips = stats.New()
|
||||||
var messages = stats.New()
|
var messages = stats.New()
|
||||||
var users = stats.New()
|
var users = stats.New()
|
||||||
var mutexStats sync.RWMutex
|
var mutexStats sync.RWMutex
|
||||||
|
Loading…
Reference in New Issue
Block a user