geoip

package
v0.5.26 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2025 License: BSD-3-Clause Imports: 9 Imported by: 0

README

Name

geoip - add geographical location data

Description

The geoip handler adds geographical location data associated with the client IP. You can install a database on Debian systems with apt-get install geoip-database, or see https://mailfud.org/geoip-legacy/.

There is no automatic reloading of the databases under the assumption they will not change that often.

If an IP address does not have associated geographical location data, nothing is added to the context.

Syntax

geoip [subnet] {
    city DBFILE4 [DBFILE6]
    asn DBFILE4 [DBFILE6]
}
  • subnet signals if given to use EDNS0 subnet (if present) for the lookup instead of the source IP address. NOTE: due to security reasons, recursive DNS resolvers may mask a few bits off of the clients' IP address, which can cause inaccuracies in resolution.
  • city and asn define the database files that should be used for country, city or AS number lookups. If the path is relative the path from root will be prepended. At least one database must be loaded.

Context Keys

The following values will be stored in the context of a request and can be used by other handlers.

The continent codes are: AF: Africa, AN: Antarctica, AS: Asia, EU: Europe, NA: North America, OC: Oceania, SA: South America.

Key Type Example Description
geoip/city string Cambridge The city name in English language.
geoip/country string GB Country ISO 3166-1 code.
geoip/country/eu bool false Country is EU member.
geoip/continent string EU Continent code.
geoip/latitude float64 52.2242 Base 10, max available precision.
geoip/longitude float64 0.1315 Base 10, max available precision.
geoip/timezone string Europe/London The time zone.
geoip/asn int 37 The AS number.
geoip/asn/organization string Example Org The AS organization.

Example

Here we add location data to the request's context, so that template can use it in the template creation.

example.org. {
    geoip {
        city testdata/GeoIPCity.dat
    }

    template .* {
        mytemplate.go.tmpl
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Err

func Err(err error) slog.Attr

Types

type Geoip

type Geoip struct {
	City  *geoip2.Reader
	City6 *geoip2.Reader
	Asn   *geoip2.Reader
	Asn6  *geoip2.Reader

	Subnet bool
}

Geoip adds location data to the context.

func (*Geoip) Err

func (h *Geoip) Err(err error) error

func (*Geoip) HandlerFunc

func (g *Geoip) HandlerFunc(next dns.HandlerFunc) dns.HandlerFunc

func (*Geoip) Key

func (h *Geoip) Key() string

func (*Geoip) Setup

func (g *Geoip) Setup(co *dnsserver.Controller) (err error)

Jump to

Keyboard shortcuts

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