From c46820b6d42702f513bf1352b2692dff0f893738 Mon Sep 17 00:00:00 2001 From: httpjamesm Date: Thu, 29 Dec 2022 13:54:37 -0500 Subject: [PATCH] fix: add version, rolling ratelimit --- src/middleware/ratelimit.go | 14 ++++++++++++-- src/routes/options.go | 5 +++-- src/routes/question.go | 7 +++++++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/middleware/ratelimit.go b/src/middleware/ratelimit.go index 264e022..bb86e23 100644 --- a/src/middleware/ratelimit.go +++ b/src/middleware/ratelimit.go @@ -1,6 +1,7 @@ package middleware import ( + "anonymousoverflow/config" "sync" "time" @@ -34,14 +35,23 @@ func Ratelimit() gin.HandlerFunc { c.HTML(429, "home.html", gin.H{ "errorMessage": "You have exceeded the request limit. Please try again in a minute.", "theme": c.MustGet("theme").(string), + "version": config.Version, }) c.Abort() return } - // delete the ip from the map after 1 minute + // subtract 1 from the value after 1 minute if the value exists and is greater than 0 time.AfterFunc(time.Minute, func() { - ipMap.Delete(ip) + val, ok := ipMap.Load(ip) + if ok && val.(int) > 0 { + ipMap.Store(ip, val.(int)-1) + } + + // if the value is 0, delete the entry from the map + if val.(int) == 0 { + ipMap.Delete(ip) + } }) } } diff --git a/src/routes/options.go b/src/routes/options.go index f4c0b77..b1b3293 100644 --- a/src/routes/options.go +++ b/src/routes/options.go @@ -1,6 +1,7 @@ package routes import ( + "anonymousoverflow/config" "fmt" "os" "strings" @@ -21,6 +22,7 @@ func ChangeOptions(c *gin.Context) { c.HTML(200, "home.html", gin.H{ "successMessage": "Images are now " + text, "theme": c.MustGet("theme").(string), + "version": config.Version, }) case "theme": @@ -40,9 +42,8 @@ func ChangeOptions(c *gin.Context) { } c.Redirect(302, redirectUrl) + default: c.String(400, "400 Bad Request") } - - c.Redirect(302, "/") } diff --git a/src/routes/question.go b/src/routes/question.go index 5c4e37f..c84eb65 100644 --- a/src/routes/question.go +++ b/src/routes/question.go @@ -1,6 +1,7 @@ package routes import ( + "anonymousoverflow/config" "anonymousoverflow/src/utils" "fmt" "html" @@ -28,6 +29,7 @@ func ViewQuestion(c *gin.Context) { c.HTML(400, "home.html", gin.H{ "errorMessage": "Invalid question ID", "theme": c.MustGet("theme").(string), + "version": config.Version, }) return } @@ -36,6 +38,7 @@ func ViewQuestion(c *gin.Context) { c.HTML(400, "home.html", gin.H{ "errorMessage": "Invalid question ID", "theme": c.MustGet("theme").(string), + "version": config.Version, }) return } @@ -49,6 +52,7 @@ func ViewQuestion(c *gin.Context) { c.HTML(500, "home.html", gin.H{ "errorMessage": "Unable to fetch question data", "theme": c.MustGet("theme").(string), + "version": config.Version, }) return } @@ -57,6 +61,7 @@ func ViewQuestion(c *gin.Context) { c.HTML(500, "home.html", gin.H{ "errorMessage": "Received a non-OK status code", "theme": c.MustGet("theme").(string), + "version": config.Version, }) return } @@ -70,6 +75,7 @@ func ViewQuestion(c *gin.Context) { c.HTML(500, "home.html", gin.H{ "errorMessage": "Unable to parse question data", "theme": c.MustGet("theme").(string), + "version": config.Version, }) return } @@ -91,6 +97,7 @@ func ViewQuestion(c *gin.Context) { c.HTML(500, "home.html", gin.H{ "errorMessage": "Unable to parse question body", "theme": c.MustGet("theme").(string), + "version": config.Version, }) return }