From a05308bc9c3963b4205da053cc50209c0efcb05e Mon Sep 17 00:00:00 2001 From: George Kutsurua Date: Tue, 24 Jan 2023 15:03:12 +0400 Subject: [PATCH] feat: merge lat & long headers into coordinates header Signed-off-by: George Kutsurua --- .dockerignore | 5 +++++ Dockerfile | 7 +++++-- middleware.go | 15 ++++++--------- middleware_test.go | 6 ++---- types.go | 42 ++++++++++++++++++------------------------ 5 files changed, 36 insertions(+), 39 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..d9d1b7f --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +.idea/ +.DS_Store +*.code-workspace +*.mmdb +geolite2.tgz diff --git a/Dockerfile b/Dockerfile index 6e02bf4..deb692c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,10 @@ -FROM traefik:2.4.9 +FROM traefik:2.9.6 # COPY *.yml *.mmdb go.* ./geoip.go /plugins/go/src/github.com/GiGInnovationLabs/traefikgeoip2/ # COPY vendor/ /plugins/go/src/github.com/GiGInnovationLabs/traefikgeoip2/vendor/ -COPY GeoLite2-City.mmdb /var/lib/traefikgeoip2/ +# COPY GeoLite2-City.mmdb /var/lib/traefikgeoip2/ + +COPY . plugins-local/src/github.com/forestvpn/traefikgeoip2 + diff --git a/middleware.go b/middleware.go index 3ff9518..13446b7 100644 --- a/middleware.go +++ b/middleware.go @@ -97,8 +97,7 @@ func (mw *TraefikGeoIP2) ServeHTTP(reqWr http.ResponseWriter, req *http.Request) req.Header.Set(CountryHeader, Unknown) req.Header.Set(RegionHeader, Unknown) req.Header.Set(CityHeader, Unknown) - req.Header.Set(LatitudeHeader, Unknown) - req.Header.Set(LongitudeHeader, Unknown) + req.Header.Set(CoordinatesHeader, Unknown) mw.next.ServeHTTP(reqWr, req) return } @@ -113,19 +112,17 @@ func (mw *TraefikGeoIP2) ServeHTTP(reqWr http.ResponseWriter, req *http.Request) if err != nil { log.Printf("[geoip2] Unable to find: ip=%s, err=%v", ipStr, err) res = &GeoIPResult{ - country: Unknown, - region: Unknown, - city: Unknown, - latitude: Unknown, - longitude: Unknown, + country: Unknown, + region: Unknown, + city: Unknown, + coordinates: Unknown, } } req.Header.Set(CountryHeader, res.country) req.Header.Set(RegionHeader, res.region) req.Header.Set(CityHeader, res.city) - req.Header.Set(LatitudeHeader, res.latitude) - req.Header.Set(LongitudeHeader, res.longitude) + req.Header.Set(CoordinatesHeader, res.coordinates) mw.next.ServeHTTP(reqWr, req) } diff --git a/middleware_test.go b/middleware_test.go index ad86aa2..12c5f2a 100644 --- a/middleware_test.go +++ b/middleware_test.go @@ -135,8 +135,7 @@ func TestGeoIPFromIPHeader(t *testing.T) { assertHeader(t, req, mw.CountryHeader, "DE") assertHeader(t, req, mw.RegionHeader, "BY") assertHeader(t, req, mw.CityHeader, "Munich") - assertHeader(t, req, mw.LatitudeHeader, "48.1663") - assertHeader(t, req, mw.LongitudeHeader, "11.5683") + assertHeader(t, req, mw.CoordinatesHeader, "48.1663, 11.5683") req = httptest.NewRequest(http.MethodGet, "http://localhost", nil) req.Header.Set(ipHeader, "qwerty") @@ -144,8 +143,7 @@ func TestGeoIPFromIPHeader(t *testing.T) { assertHeader(t, req, mw.CountryHeader, mw.Unknown) assertHeader(t, req, mw.RegionHeader, mw.Unknown) assertHeader(t, req, mw.CityHeader, mw.Unknown) - assertHeader(t, req, mw.LatitudeHeader, mw.Unknown) - assertHeader(t, req, mw.LongitudeHeader, mw.Unknown) + assertHeader(t, req, mw.CoordinatesHeader, mw.Unknown) } func TestGeoIPCountryDBFromRemoteAddr(t *testing.T) { diff --git a/types.go b/types.go index b6e42e8..8d6af4d 100644 --- a/types.go +++ b/types.go @@ -24,19 +24,16 @@ const ( RegionHeader = "X-GeoIP2-Region" // CityHeader city header name. CityHeader = "X-GeoIP2-City" - // LatitudeHeader latitude header name. - LatitudeHeader = "X-GeoIP2-Latitude" - // LongitudeHeader longitude header name. - LongitudeHeader = "X-GeoIP2-Longitude" + // CoordinatesHeader geo coordinates header name. + CoordinatesHeader = "X-GeoIP2-Coordinates" ) // GeoIPResult GeoIPResult. type GeoIPResult struct { - country string - region string - city string - latitude string - longitude string + country string + region string + city string + coordinates string } // LookupGeoIP2 LookupGeoIP2. @@ -50,11 +47,10 @@ func CreateCityDBLookup(rdr *geoip2.CityReader) LookupGeoIP2 { return nil, fmt.Errorf("%w", err) } retval := GeoIPResult{ - country: rec.Country.ISOCode, - region: Unknown, - city: Unknown, - latitude: Unknown, - longitude: Unknown, + country: rec.Country.ISOCode, + region: Unknown, + city: Unknown, + coordinates: Unknown, } if city, ok := rec.City.Names["en"]; ok { retval.city = city @@ -62,11 +58,10 @@ func CreateCityDBLookup(rdr *geoip2.CityReader) LookupGeoIP2 { if rec.Subdivisions != nil { retval.region = rec.Subdivisions[0].ISOCode } - if rec.Location.Latitude != 0 { - retval.latitude = strconv.FormatFloat(rec.Location.Latitude, 'f', -1, 64) - } - if rec.Location.Longitude != 0 { - retval.longitude = strconv.FormatFloat(rec.Location.Longitude, 'f', -1, 64) + if rec.Location.Latitude != 0 && rec.Location.Longitude != 0 { + retval.coordinates = strconv.FormatFloat( + rec.Location.Latitude, 'f', -1, 64) + ", " + strconv.FormatFloat( + rec.Location.Longitude, 'f', -1, 64) } return &retval, nil @@ -81,11 +76,10 @@ func CreateCountryDBLookup(rdr *geoip2.CountryReader) LookupGeoIP2 { return nil, fmt.Errorf("%w", err) } retval := GeoIPResult{ - country: rec.Country.ISOCode, - region: Unknown, - city: Unknown, - latitude: Unknown, - longitude: Unknown, + country: rec.Country.ISOCode, + region: Unknown, + city: Unknown, + coordinates: Unknown, } return &retval, nil }