From 30afd17b8f219694f2a0ea64e949f8e4409294f5 Mon Sep 17 00:00:00 2001 From: Dylan Butler Date: Mon, 21 Aug 2023 19:15:23 +1000 Subject: [PATCH 1/3] use x-forwarded-for ip --- shortener/internal/server/server.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/shortener/internal/server/server.go b/shortener/internal/server/server.go index 7671680f..f2ef4648 100644 --- a/shortener/internal/server/server.go +++ b/shortener/internal/server/server.go @@ -112,7 +112,9 @@ func (s *Server) shorten(c *gin.Context) { u.Scheme = "https" } - link, err := s.shortener.Shorten(c, u.String(), shortener.WithExpiry(time.Now().Add(time.Minute*10)), shortener.WithIP(c.ClientIP())) + ip := c.GetHeader("X-Forwarded-For") + + link, err := s.shortener.Shorten(c, u.String(), shortener.WithExpiry(time.Now().Add(time.Minute*10)), shortener.WithIP(ip)) if err != nil { switch err { case shortener.ErrAlreadyExists: From 2d0e770fbd3ed4f9bad80d7154b88de1e843c5be Mon Sep 17 00:00:00 2001 From: Dylan Butler Date: Mon, 21 Aug 2023 19:17:04 +1000 Subject: [PATCH 2/3] update dependabot.yml --- .github/dependabot.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/dependabot.yml b/.github/dependabot.yml index b444581e..804939f9 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -6,6 +6,6 @@ version: 2 updates: - package-ecosystem: "gomod" # See documentation for possible values - directory: "/" # Location of package manifests + directory: "/shortener" # Location of package manifests schedule: interval: "daily" From b462b21969ba9ebde5dc3508f99129813b2de30e Mon Sep 17 00:00:00 2001 From: Dylan Butler Date: Mon, 21 Aug 2023 19:22:38 +1000 Subject: [PATCH 3/3] use leftmost ip --- shortener/internal/server/server.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/shortener/internal/server/server.go b/shortener/internal/server/server.go index f2ef4648..71314486 100644 --- a/shortener/internal/server/server.go +++ b/shortener/internal/server/server.go @@ -4,6 +4,7 @@ import ( "fmt" "net/http" "net/url" + "strings" "time" "github.com/dbut2/butla/shortener/internal/pages" @@ -112,7 +113,8 @@ func (s *Server) shorten(c *gin.Context) { u.Scheme = "https" } - ip := c.GetHeader("X-Forwarded-For") + ips := strings.Split(c.GetHeader("X-Forwarded-For"), ",") + ip := ips[0] link, err := s.shortener.Shorten(c, u.String(), shortener.WithExpiry(time.Now().Add(time.Minute*10)), shortener.WithIP(ip)) if err != nil {