From 9511b7f08e91429ba9b79534a842482a0f4f42e0 Mon Sep 17 00:00:00 2001 From: Diego Cena Date: Fri, 24 Jan 2025 16:44:48 -0300 Subject: [PATCH 1/2] added support to list like headers ie: x-forwarded-for --- CONTRIBUTING.md | 12 ++++++++++++ handler.go | 12 +++++++++++- logger/README.md | 5 ++--- 3 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 CONTRIBUTING.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..d209327 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,12 @@ +# How to contribute + +Thank you for your interest in improving droneip. + +Here is how to contribute back some code or documentation: + +- Fork repo +- Create a feature branch off of the `dev` branch +- Make some useful change +- Submit a pull request against the `dev` branch. +- Be kind + diff --git a/handler.go b/handler.go index d2d7bce..bd8eb1f 100644 --- a/handler.go +++ b/handler.go @@ -30,7 +30,7 @@ func (h *DroneHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { inspectHeader := config.Get("INSPECT_HEADER") if inspectHeader != "" { - remoteIP = r.Header.Get(inspectHeader) + remoteIP = getRemoteIP(r.Header.Get(inspectHeader)) } cacheKey := fmt.Sprintf("droneip-%s", remoteIP) @@ -79,3 +79,13 @@ func (h *DroneHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { io.Copy(w, res.Body) } + +func getRemoteIP(ips string) string { + if !strings.Contains(ips, ",") { + return ips + } + + ipList := strings.Split(ips, ",") + + return strings.Trim(ipList[0], " ") +} diff --git a/logger/README.md b/logger/README.md index 9050385..f76edbf 100644 --- a/logger/README.md +++ b/logger/README.md @@ -1,9 +1,8 @@ [![Go Report Card](https://goreportcard.com/badge/github.com/diegohce/logger)](https://goreportcard.com/report/github.com/diegohce/logger) [![GoDoc](https://godoc.org/github.com/diegohce/logger?status.svg)](https://godoc.org/github.com/diegohce/logger) -[![GPLv3 license](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://github.com/diegohce/logger/blob/master/LICENSE) +[![GPLv3 license](https://img.shields.io/badge/License-apache_2.0-blue.svg)](https://github.com/diegohce/logger/blob/master/LICENSE) [![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg)](https://github.com/diegohce/logger/graphs/commit-activity) -[![HitCount](http://hits.dwyl.io/diegohce/logger.svg)](http://hits.dwyl.io/diegohce/logger) -[![Sourcegraph](https://sourcegraph.com/github.com/diegohce/logger/-/badge.svg)](https://sourcegraph.com/github.com/diegohce/logger?badge) +[![Sourcegraph](https://sourcegraph.com/github.com/diegohce/droneip/logger/-/badge.svg)](https://sourcegraph.com/github.com/diegohce/droneip/logger?badge) # logger From fb6be2a308e23a06f4eade8010b442c0a048c7e1 Mon Sep 17 00:00:00 2001 From: Diego Cena Date: Fri, 24 Jan 2025 16:52:05 -0300 Subject: [PATCH 2/2] added x-forwarded-for test cases --- service_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/service_test.go b/service_test.go index 23985ae..e090bc4 100644 --- a/service_test.go +++ b/service_test.go @@ -93,3 +93,29 @@ func TestAdmin(t *testing.T) { } } + +func TestGetRemoteIP(t *testing.T) { + + cases := []struct { + ips string + want string + }{ + { + ips: "10.10.0.1, 10.10.0.2, 10.10.0.3, 10.10.0.4, 10.10.0.5, 10.10.0.6", + want: "10.10.0.1", + }, + { + ips: "10.10.0.1", + want: "10.10.0.1", + }, + } + + for _, c := range cases { + got := getRemoteIP(c.ips) + if got != c.want { + t.Errorf("got %s want %s", got, c.want) + } + + } + +}