caddy_geoip

package module
v0.0.0-...-a1a0c21 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2023 License: MIT Imports: 18 Imported by: 0

README

GeoIP

Provides middleware for resolving a users IP address against the Maxmind Geo IP Database.

Manages Downloading and Refreshing the Maxmind Database via https://github.com/maxmind/geoipupdate

Examples


{
  http_port     8080
  https_port    8443
  order geo_ip first
}

localhost:8080 {

  geo_ip {
    db_path GeoLite2-Country.mmdb
    trust_header X-Real-IP
  }

  respond / 200 {
    body "Hello from {geoip.country_code}"
  }
}

Configuration

account - this is the Maxmind Account, required for all download operations. Can be supplied as an ENV Variable GEO_ACCOUNT_ID.

db_path - is the path to load the database from. The filename is used to determine the edition of the file to download Valid values tested with are GeoIP2-Country | GeoLite2-Country

trust_header - this is used to determine the header to load the users ip address from, if empty it will use the requests RemoteAddr

api_key - this is a Maxmind API Key. If blank no attempt will be made to download the database. Can be supplied as an ENV Variable GEO_API_KEY.

download_frequency - this is how often to download the database from the Maxmind Server (requires an APIKey)

reload_frequency - this is how often to check for updated versions of the database on disk. This can be used when an external process is responsible for downloading the database. If the database is being managed via the api_key and download_frequency then there is no need to specify the reload_frequency

override_country_code - this setting allows for the country code to be configured for local development. It will attempt to use the database if there is one, and use the value if no record could be found.

Release Process

git tag v0.4.0
git push origin v0.4.0

Builds on the good work by

https://github.com/porech/caddy-maxmind-geolocation

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type City

type City struct {
	Names Names `maxminddb:"names"`
}

type Country

type Country struct {
	GeonameId         int    `maxminddb:"geoname_id"`
	IsInEuropeanUnion bool   `maxminddb:"is_in_european_union"`
	ISOCode           string `maxminddb:"iso_code"`
	Names             Names  `maxminddb:"names"`
}

type GeoIP

type GeoIP struct {

	// The AccountID of the maxmind account
	AccountID int `json:"account_id"`

	// The API Key used to download the latest file
	APIKey string `json:"api_key"`

	// The path of the MaxMind GeoLite2-Country.mmdb file.
	DbPath string `json:"db_path"`

	// The frequency to download a fresh version of the database file
	DownloadFrequency caddy.Duration `json:"download_frequency"`

	// The frequency to reload the database file
	ReloadFrequency caddy.Duration `json:"reload_frequency"`

	// The header to trust instead of the `RemoteAddr`
	TrustHeader string `json:"trust_header"`

	// Only trust the above header when from these IP addresses
	RequireTrusted bool `json:"require_trusted_proxy"`

	// The Country Code to set if no value could be found
	OverrideCountryCode string `json:"override_country_code"`
	// contains filtered or unexported fields
}

Allows finding the Country Code of an IP address using the Maxmind database

func (GeoIP) CaddyModule

func (GeoIP) CaddyModule() caddy.ModuleInfo

func (*GeoIP) Provision

func (m *GeoIP) Provision(ctx caddy.Context) error

func (*GeoIP) ServeHTTP

func (m *GeoIP) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error

func (*GeoIP) UnmarshalCaddyfile

func (m *GeoIP) UnmarshalCaddyfile(d *caddyfile.Dispenser) error

type Location

type Location struct {
	MetroCode      int `maxminddb:"metro_code"`
	AccuracyRadius int `maxminddb:"accuracy_radius"`
}

type Names

type Names struct {
	De   string `maxminddb:"de"`
	En   string `maxminddb:"en"`
	Es   string `maxminddb:"es"`
	Fr   string `maxminddb:"fr"`
	PtBr string `maxminddb:"pt-BR"`
	Ru   string `maxminddb:"ru"`
	ZhCn string `maxminddb:"zh-CN"`
}

type Record

type Record struct {
	Country      Country      `maxminddb:"country"`
	Location     Location     `maxminddb:"location"`
	Subdivisions Subdivisions `maxminddb:"subdivisions"`
	City         City         `maxminddb:"city"`
}

type Subdivision

type Subdivision struct {
	ISOCode string `maxminddb:"iso_code"`
	Names   Names  `maxminddb:"names"`
}

type Subdivisions

type Subdivisions []Subdivision

func (Subdivisions) CommaSeparatedISOCodes

func (s Subdivisions) CommaSeparatedISOCodes() string

func (Subdivisions) GetISOCodes

func (s Subdivisions) GetISOCodes() []string

Jump to

Keyboard shortcuts

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