diff --git a/src/api/middleware/otp_limiter.go b/src/api/middleware/otp_limiter.go index 0d2d3fb..c1fa1cb 100644 --- a/src/api/middleware/otp_limiter.go +++ b/src/api/middleware/otp_limiter.go @@ -2,6 +2,7 @@ package middleware import ( "errors" + "net" "net/http" "time" @@ -15,7 +16,7 @@ import ( func OtpLimiter(cfg *config.Config) gin.HandlerFunc { var limiter = limiter.NewIPRateLimiter(rate.Every(cfg.Otp.Limiter*time.Second), 1) return func(c *gin.Context) { - limiter := limiter.GetLimiter(c.Request.RemoteAddr) + limiter := limiter.GetLimiter(getIP(c.Request.RemoteAddr)) if !limiter.Allow() { c.AbortWithStatusJSON(http.StatusTooManyRequests, helper.GenerateBaseResponseWithError(nil, false, helper.OtpLimiterError, errors.New("not allowed"))) c.Abort() @@ -24,3 +25,11 @@ func OtpLimiter(cfg *config.Config) gin.HandlerFunc { } } } + +func getIP(remoteAddr string) string { + ip, _, err := net.SplitHostPort(remoteAddr) + if err != nil { + return remoteAddr + } + return ip +}