fix: getting client real ip
This commit is contained in:
@@ -34,11 +34,8 @@ func (access AccessPolicy) AccessPolicyMiddleware(next http.Handler) http.Handle
|
|||||||
// Get the client's IP address
|
// Get the client's IP address
|
||||||
clientIP, _, err := net.SplitHostPort(getRealIP(r))
|
clientIP, _, err := net.SplitHostPort(getRealIP(r))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("Unable to parse IP address")
|
clientIP = getRealIP(r)
|
||||||
RespondWithError(w, http.StatusUnauthorized, "Unable to parse IP address")
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check IP against source ranges
|
// Check IP against source ranges
|
||||||
isAllowed := access.Action != "DENY"
|
isAllowed := access.Action != "DENY"
|
||||||
for _, entry := range access.SourceRanges {
|
for _, entry := range access.SourceRanges {
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/jkaninda/goma-gateway/pkg/logger"
|
"github.com/jkaninda/goma-gateway/pkg/logger"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -51,7 +52,11 @@ func (rl *RateLimiter) RateLimitMiddleware() mux.MiddlewareFunc {
|
|||||||
}
|
}
|
||||||
return func(next http.Handler) http.Handler {
|
return func(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
clientIP := getRealIP(r)
|
// Get the client's IP address
|
||||||
|
clientIP, _, err := net.SplitHostPort(getRealIP(r))
|
||||||
|
if err != nil {
|
||||||
|
clientIP = getRealIP(r)
|
||||||
|
}
|
||||||
clientID := fmt.Sprintf("%s-%s", rl.id, clientIP) // Generate client Id, ID+ route ID
|
clientID := fmt.Sprintf("%s-%s", rl.id, clientIP) // Generate client Id, ID+ route ID
|
||||||
if rl.redisBased {
|
if rl.redisBased {
|
||||||
err := redisRateLimiter(clientID, rl.unit, rl.requests)
|
err := redisRateLimiter(clientID, rl.unit, rl.requests)
|
||||||
|
|||||||
Reference in New Issue
Block a user