Skip to content

Commit 0dda9bd

Browse files
authored
fix: RemoteAddr returns ip with port that changes each time so we should get ip without port (#8)
1 parent 52c8419 commit 0dda9bd

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/api/middleware/otp_limiter.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package middleware
22

33
import (
44
"errors"
5+
"net"
56
"net/http"
67
"time"
78

@@ -15,7 +16,7 @@ import (
1516
func OtpLimiter(cfg *config.Config) gin.HandlerFunc {
1617
var limiter = limiter.NewIPRateLimiter(rate.Every(cfg.Otp.Limiter*time.Second), 1)
1718
return func(c *gin.Context) {
18-
limiter := limiter.GetLimiter(c.Request.RemoteAddr)
19+
limiter := limiter.GetLimiter(getIP(c.Request.RemoteAddr))
1920
if !limiter.Allow() {
2021
c.AbortWithStatusJSON(http.StatusTooManyRequests, helper.GenerateBaseResponseWithError(nil, false, helper.OtpLimiterError, errors.New("not allowed")))
2122
c.Abort()
@@ -24,3 +25,11 @@ func OtpLimiter(cfg *config.Config) gin.HandlerFunc {
2425
}
2526
}
2627
}
28+
29+
func getIP(remoteAddr string) string {
30+
ip, _, err := net.SplitHostPort(remoteAddr)
31+
if err != nil {
32+
return remoteAddr
33+
}
34+
return ip
35+
}

0 commit comments

Comments
 (0)