From fa6e102cd88eb3c99849b9eb1383dd203302cb37 Mon Sep 17 00:00:00 2001 From: Jonas Kaninda Date: Mon, 28 Oct 2024 02:07:28 +0100 Subject: [PATCH 1/4] chore: remove error log to undefined health check route --- .gitignore | 3 ++- pkg/handler.go | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 1996d9d..d7dae71 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,5 @@ goma bin Makefile NOTES.md -tests \ No newline at end of file +tests +configs \ No newline at end of file diff --git a/pkg/handler.go b/pkg/handler.go index d380a4c..000a90c 100644 --- a/pkg/handler.go +++ b/pkg/handler.go @@ -88,7 +88,7 @@ func (heathRoute HealthCheckRoute) HealthCheckHandler(w http.ResponseWriter, r * continue } } else { - logger.Error("Route %s's healthCheck is undefined", route.Name) + logger.Warn("Route %s's healthCheck is undefined", route.Name) routes = append(routes, HealthCheckRouteResponse{Name: route.Name, Status: "undefined", Error: ""}) continue From 8250c8ed5a06dce90a0199f879f3a2828570cedc Mon Sep 17 00:00:00 2001 From: Jonas Kaninda Date: Mon, 28 Oct 2024 02:26:02 +0100 Subject: [PATCH 2/4] feat: add forward client real IP --- pkg/helpers.go | 10 ++++++++++ pkg/middleware/rate_limiter.go | 17 ++++++++++------- pkg/proxy.go | 7 ++++--- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/pkg/helpers.go b/pkg/helpers.go index 553f222..a73212f 100644 --- a/pkg/helpers.go +++ b/pkg/helpers.go @@ -14,6 +14,7 @@ import ( "github.com/common-nighthawk/go-figure" "github.com/jedib0t/go-pretty/v6/table" "github.com/jkaninda/goma-gateway/util" + "net/http" ) func Intro() { @@ -31,3 +32,12 @@ func printRoute(routes []Route) { } 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 +} diff --git a/pkg/middleware/rate_limiter.go b/pkg/middleware/rate_limiter.go index 8508cb8..38bf896 100644 --- a/pkg/middleware/rate_limiter.go +++ b/pkg/middleware/rate_limiter.go @@ -18,7 +18,6 @@ limitations under the License. import ( "encoding/json" "github.com/gorilla/mux" - "github.com/jkaninda/goma-gateway/internal/logger" "net/http" "time" ) @@ -52,11 +51,7 @@ func (rl *TokenRateLimiter) RateLimitMiddleware() mux.MiddlewareFunc { func (rl *RateLimiter) RateLimitMiddleware() mux.MiddlewareFunc { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - - //TODO: - clientID := r.RemoteAddr - logger.Info(clientID) - + clientID := getRealIP(r) rl.mu.Lock() client, exists := rl.ClientMap[clientID] if !exists || time.Now().After(client.ExpiresAt) { @@ -82,9 +77,17 @@ func (rl *RateLimiter) RateLimitMiddleware() mux.MiddlewareFunc { } return } - // Proceed to the next handler if rate limit is not exceeded 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 +} diff --git a/pkg/proxy.go b/pkg/proxy.go index 235b7c0..97a91b9 100644 --- a/pkg/proxy.go +++ b/pkg/proxy.go @@ -36,7 +36,8 @@ type ProxyRoute struct { // ProxyHandler proxies requests to the backend func (proxyRoute ProxyRoute) ProxyHandler() http.HandlerFunc { 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 //Update 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.Scheme = targetURL.Scheme r.Header.Set("X-Forwarded-Host", r.Header.Get("Host")) - r.Header.Set("X-Forwarded-For", r.RemoteAddr) - r.Header.Set("X-Real-IP", r.RemoteAddr) + r.Header.Set("X-Forwarded-For", realIP) + r.Header.Set("X-Real-IP", realIP) r.Host = targetURL.Host } // Create proxy From 37f3ff98681d6bea03994f27396cc53934ef717e Mon Sep 17 00:00:00 2001 From: Jonas Kaninda Date: Mon, 28 Oct 2024 02:27:10 +0100 Subject: [PATCH 3/4] refactor: move intro function into server file --- cmd/server.go | 12 +++++++++++- pkg/helpers.go | 9 --------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/cmd/server.go b/cmd/server.go index 5634272..fd9ba64 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -18,8 +18,11 @@ package cmd import ( "context" + "fmt" + "github.com/common-nighthawk/go-figure" "github.com/jkaninda/goma-gateway/internal/logger" "github.com/jkaninda/goma-gateway/pkg" + "github.com/jkaninda/goma-gateway/util" "github.com/spf13/cobra" ) @@ -27,7 +30,7 @@ var ServerCmd = &cobra.Command{ Use: "server", Short: "Start server", Run: func(cmd *cobra.Command, args []string) { - pkg.Intro() + intro() configFile, _ := cmd.Flags().GetString("config") if configFile == "" { configFile = pkg.GetConfigPaths() @@ -49,3 +52,10 @@ var ServerCmd = &cobra.Command{ func init() { ServerCmd.Flags().StringP("config", "", "", "Goma config file") } +func intro() { + nameFigure := figure.NewFigure("Goma", "", true) + nameFigure.Print() + fmt.Printf("Version: %s\n", util.FullVersion()) + fmt.Println("Copyright (c) 2024 Jonas Kaninda") + fmt.Println("Starting Goma Gateway server...") +} diff --git a/pkg/helpers.go b/pkg/helpers.go index a73212f..a568def 100644 --- a/pkg/helpers.go +++ b/pkg/helpers.go @@ -11,19 +11,10 @@ You may get a copy of the License at */ import ( "fmt" - "github.com/common-nighthawk/go-figure" "github.com/jedib0t/go-pretty/v6/table" - "github.com/jkaninda/goma-gateway/util" "net/http" ) -func Intro() { - nameFigure := figure.NewFigure("Goma", "", true) - nameFigure.Print() - fmt.Printf("Version: %s\n", util.FullVersion()) - fmt.Println("Copyright (c) 2024 Jonas Kaninda") - fmt.Println("Starting Goma Gateway server...") -} func printRoute(routes []Route) { t := table.NewWriter() t.AppendHeader(table.Row{"Name", "Route", "Rewrite", "Destination"}) From 95777bfd33ba97dce1069f5694cc0a3a13a43cfa Mon Sep 17 00:00:00 2001 From: Jonas Kaninda Date: Mon, 28 Oct 2024 02:43:13 +0100 Subject: [PATCH 4/4] chore: add warning message from Too many access client --- pkg/middleware/rate_limiter.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/middleware/rate_limiter.go b/pkg/middleware/rate_limiter.go index 38bf896..914b6bc 100644 --- a/pkg/middleware/rate_limiter.go +++ b/pkg/middleware/rate_limiter.go @@ -18,6 +18,7 @@ limitations under the License. import ( "encoding/json" "github.com/gorilla/mux" + "github.com/jkaninda/goma-gateway/internal/logger" "net/http" "time" ) @@ -65,6 +66,7 @@ func (rl *RateLimiter) RateLimitMiddleware() mux.MiddlewareFunc { rl.mu.Unlock() if client.RequestCount > rl.Requests { + logger.Warn("Too many request from IP: %s %s %s", clientID, r.URL, r.UserAgent()) w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusTooManyRequests) err := json.NewEncoder(w).Encode(ProxyResponseError{