Skip to content

Commit

Permalink
fix: RemoteAddr returns ip with port that changes each time so we sho…
Browse files Browse the repository at this point in the history
…uld get ip without port
  • Loading branch information
erfanwd authored Dec 17, 2024
1 parent 52c8419 commit cd5ce7e
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/api/middleware/otp_limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package middleware

import (
"errors"
"net"
"net/http"
"time"

Expand All @@ -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()
Expand All @@ -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
}

0 comments on commit cd5ce7e

Please sign in to comment.