feat: add forward client real IP
This commit is contained in:
@@ -14,6 +14,7 @@ import (
|
|||||||
"github.com/common-nighthawk/go-figure"
|
"github.com/common-nighthawk/go-figure"
|
||||||
"github.com/jedib0t/go-pretty/v6/table"
|
"github.com/jedib0t/go-pretty/v6/table"
|
||||||
"github.com/jkaninda/goma-gateway/util"
|
"github.com/jkaninda/goma-gateway/util"
|
||||||
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Intro() {
|
func Intro() {
|
||||||
@@ -31,3 +32,12 @@ func printRoute(routes []Route) {
|
|||||||
}
|
}
|
||||||
fmt.Println(t.Render())
|
fmt.Println(t.Render())
|
||||||
}
|
}
|
||||||
|
func getRealIP(r *http.Request) string {
|
||||||
|
if ip := r.Header.Get("X-Real-IP"); ip != "" {
|
||||||
|
return ip
|
||||||
|
}
|
||||||
|
if ip := r.Header.Get("X-Forwarded-For"); ip != "" {
|
||||||
|
return ip
|
||||||
|
}
|
||||||
|
return r.RemoteAddr
|
||||||
|
}
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ limitations under the License.
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/jkaninda/goma-gateway/internal/logger"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -52,11 +51,7 @@ func (rl *TokenRateLimiter) RateLimitMiddleware() mux.MiddlewareFunc {
|
|||||||
func (rl *RateLimiter) RateLimitMiddleware() mux.MiddlewareFunc {
|
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) {
|
||||||
|
clientID := getRealIP(r)
|
||||||
//TODO:
|
|
||||||
clientID := r.RemoteAddr
|
|
||||||
logger.Info(clientID)
|
|
||||||
|
|
||||||
rl.mu.Lock()
|
rl.mu.Lock()
|
||||||
client, exists := rl.ClientMap[clientID]
|
client, exists := rl.ClientMap[clientID]
|
||||||
if !exists || time.Now().After(client.ExpiresAt) {
|
if !exists || time.Now().After(client.ExpiresAt) {
|
||||||
@@ -82,9 +77,17 @@ func (rl *RateLimiter) RateLimitMiddleware() mux.MiddlewareFunc {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Proceed to the next handler if rate limit is not exceeded
|
// Proceed to the next handler if rate limit is not exceeded
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func getRealIP(r *http.Request) string {
|
||||||
|
if ip := r.Header.Get("X-Real-IP"); ip != "" {
|
||||||
|
return ip
|
||||||
|
}
|
||||||
|
if ip := r.Header.Get("X-Forwarded-For"); ip != "" {
|
||||||
|
return ip
|
||||||
|
}
|
||||||
|
return r.RemoteAddr
|
||||||
|
}
|
||||||
|
|||||||
@@ -36,7 +36,8 @@ type ProxyRoute struct {
|
|||||||
// ProxyHandler proxies requests to the backend
|
// ProxyHandler proxies requests to the backend
|
||||||
func (proxyRoute ProxyRoute) ProxyHandler() http.HandlerFunc {
|
func (proxyRoute ProxyRoute) ProxyHandler() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
logger.Info("%s %s %s %s", r.Method, r.RemoteAddr, r.URL, r.UserAgent())
|
realIP := getRealIP(r)
|
||||||
|
logger.Info("%s %s %s %s", r.Method, realIP, r.URL, r.UserAgent())
|
||||||
// Set CORS headers from the cors config
|
// Set CORS headers from the cors config
|
||||||
//Update Cors Headers
|
//Update Cors Headers
|
||||||
for k, v := range proxyRoute.cors.Headers {
|
for k, v := range proxyRoute.cors.Headers {
|
||||||
@@ -76,8 +77,8 @@ func (proxyRoute ProxyRoute) ProxyHandler() http.HandlerFunc {
|
|||||||
r.URL.Host = targetURL.Host
|
r.URL.Host = targetURL.Host
|
||||||
r.URL.Scheme = targetURL.Scheme
|
r.URL.Scheme = targetURL.Scheme
|
||||||
r.Header.Set("X-Forwarded-Host", r.Header.Get("Host"))
|
r.Header.Set("X-Forwarded-Host", r.Header.Get("Host"))
|
||||||
r.Header.Set("X-Forwarded-For", r.RemoteAddr)
|
r.Header.Set("X-Forwarded-For", realIP)
|
||||||
r.Header.Set("X-Real-IP", r.RemoteAddr)
|
r.Header.Set("X-Real-IP", realIP)
|
||||||
r.Host = targetURL.Host
|
r.Host = targetURL.Host
|
||||||
}
|
}
|
||||||
// Create proxy
|
// Create proxy
|
||||||
|
|||||||
Reference in New Issue
Block a user