Просмотр исходного кода

Implement default values if no geo data

When using placeholders like `{geoip_country_code}`, especially when
logging the data, empty strings can be problematic. So if there is no
geoip data for an address substitute some unambiguous text.

Resolves #8
Kurtis Rader 5 лет назад
Родитель
Сommit
432d2a0f6e
2 измененных файлов с 46 добавлено и 9 удалено
  1. 18 1
      README.md
  2. 28 8
      setup.go

+ 18 - 1
README.md

@@ -1,7 +1,9 @@
 [![Build Status](https://travis-ci.org/kodnaplakal/caddy-geoip.svg?branch=master)](https://travis-ci.org/kodnaplakal/caddy-geoip)
 ## Overview
 
-`geoip` is a Caddy plugin that allow to determine user Geolocation by IP address using MaxMind database.
+`geoip` is a Caddy plugin that allow to determine
+user Geolocation by IP address using a
+[MaxMind database](https://www.maxmind.com/en/geoip2-services-and-databases).
 
 ## Placeholders
 
@@ -20,6 +22,15 @@ The following placeholders are available:
   geoip_geohash - Geohash of latitude and longitude
 ```
 
+## Missing geolocation data
+
+If there is no geolocation data for an IP address most of the placeholders
+listed above will be empty. The exceptions are `geoip_country_code`,
+`geoip_country_name`, and `geoip_city_name`. If the request originated over
+the system loopback interface (e.g., 127.0.0.1) those vars will be set
+to `**`, `Loopback`, and `Loopback` respectively. For any other address,
+including private addresses such as 192.168.0.1, the values will be `!!`,
+`No Country`, and `No City` respectively.
 
 ## Examples
 
@@ -47,6 +58,12 @@ proxy / localhost:3000 {
 }
 ```
 
+(3) Include the geolocation info in the access log:
+
+```
+log / {$HOME}/log/access.log "{when_iso} {status} {method} {latency_ms} ms {size} bytes {geoip_country_code} {remote} {host} {proto} \"{uri}\" \"{>User-Agent}\""
+```
+
 ## Contributing
 
 1. [Fork it](https://github.com/kodnaplakal/caddy-geoip/fork)

+ 28 - 8
setup.go

@@ -80,15 +80,9 @@ func (gip GeoIP) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)
 }
 
 func (gip GeoIP) lookupLocation(w http.ResponseWriter, r *http.Request) {
-	clientIP, _ := getClientIP(r, true)
-	replacer := newReplacer(r)
-
-	var record = GeoIPRecord{}
-	err := gip.DBHandler.Lookup(clientIP, &record)
-	if err != nil {
-		log.Println(err)
-	}
+	record := gip.fetch_geoip_data(r)
 
+	replacer := newReplacer(r)
 	replacer.Set("geoip_country_code", record.Country.ISOCode)
 	replacer.Set("geoip_country_name", record.Country.Names["en"])
 	replacer.Set("geoip_country_eu", strconv.FormatBool(record.Country.IsInEuropeanUnion))
@@ -105,6 +99,32 @@ func (gip GeoIP) lookupLocation(w http.ResponseWriter, r *http.Request) {
 	}
 }
 
+func (gip GeoIP) fetch_geoip_data(r *http.Request) GeoIPRecord {
+	clientIP, _ := getClientIP(r, true)
+
+	var record = GeoIPRecord{}
+	err := gip.DBHandler.Lookup(clientIP, &record)
+	if err != nil {
+		log.Println(err)
+	}
+
+	if record.Country.ISOCode == "" {
+		record.Country.Names = make(map[string]string)
+		record.City.Names = make(map[string]string)
+		if clientIP.IsLoopback() {
+			record.Country.ISOCode = "**"
+			record.Country.Names["en"] = "Loopback"
+			record.City.Names["en"] = "Loopback"
+		} else {
+			record.Country.ISOCode = "!!"
+			record.Country.Names["en"] = "No Country"
+			record.City.Names["en"] = "No City"
+		}
+	}
+
+	return record
+}
+
 func getClientIP(r *http.Request, strict bool) (net.IP, error) {
 	var ip string