geoip

package module
v0.0.0-...-690350e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 9, 2019 License: MIT Imports: 12 Imported by: 0

README

Build Status

Overview

geoip is a Caddy plugin that allow to determine user Geolocation by IP address using a MaxMind database.

Placeholders

The following placeholders are available:

  geoip_country_code - Country ISO code, example CY for Cyprus
  geoip_country_geoname_id - GeoNameID of the city, example 146669
  geoip_latitude - Latitude, example 34.684100
  geoip_longitude - Longitude, example 33.037900
  geoip_time_zone - Time zone, example Asia/Nicosia
  geoip_country_eu - Return 'true' if country in Europen Union
  geoip_country_name - Full country name
  geoip_city_name - City name
  geoip_city_geoname_id - GeoNameID of the city, example 146384
  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

(1) Set database path and return country code header:

geoip /path/to/db/GeoLite2-City.mmdb
header Country-Code {geoip_country_code}

(2) Proxy pass headers to backend:

localhost
geoip /path/to/db/GeoLite2-City.mmdb
proxy / localhost:3000 {
  header_upstream Country-Name {geoip_country_name}
  header_upstream Country-Code {geoip_country_code}
  header_upstream Country-Eu {geoip_country_eu}
  header_upstream City-Name {geoip_city_name}
  header_upstream Latitude {geoip_latitude}
  header_upstream Longitude {geoip_longitude}
  header_upstream Time-Zone {geoip_time_zone}
  header_upstream Geohash {geoip_geohash}
}

(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
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	DatabasePath string
}

Config specifies configuration parsed for Caddyfile

type GeoIP

type GeoIP struct {
	Next      httpserver.Handler
	DBHandler *maxminddb.Reader
	ASNDB     *asndb.ASNDB
	Config    Config
}

GeoIP represents a middleware instance

func (GeoIP) ServeHTTP

func (gip GeoIP) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)

type GeoIPRecord

type GeoIPRecord struct {
	Country struct {
		ISOCode           string            `maxminddb:"iso_code"`
		IsInEuropeanUnion bool              `maxminddb:"is_in_european_union"`
		Names             map[string]string `maxminddb:"names"`
		GeoNameID         uint64            `maxminddb:"geoname_id"`
	} `maxminddb:"country"`

	City struct {
		Names     map[string]string `maxminddb:"names"`
		GeoNameID uint64            `maxminddb:"geoname_id"`
	} `maxminddb:"city"`

	Location struct {
		Latitude  float64 `maxminddb:"latitude"`
		Longitude float64 `maxminddb:"longitude"`
		TimeZone  string  `maxminddb:"time_zone"`
	} `maxminddb:"location"`

	ASN *asndb.ASN
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL