Skip to content

Commit

Permalink
feat: merge lat & long headers into coordinates header
Browse files Browse the repository at this point in the history
Signed-off-by: George Kutsurua <[email protected]>
  • Loading branch information
suquant committed Jan 24, 2023
1 parent e456cac commit a05308b
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 39 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.idea/
.DS_Store
*.code-workspace
*.mmdb
geolite2.tgz
7 changes: 5 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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


15 changes: 6 additions & 9 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
}
6 changes: 2 additions & 4 deletions middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,15 @@ 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")
instance.ServeHTTP(httptest.NewRecorder(), req)
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) {
Expand Down
42 changes: 18 additions & 24 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -50,23 +47,21 @@ 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
}
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
Expand All @@ -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
}
Expand Down

0 comments on commit a05308b

Please sign in to comment.