geoip

package
v0.0.0-...-36c2227 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

README

geoip

Name

geoip - Lookup maxmind geoip2 databases using the client IP, then add associated geoip data to the context request.

Description

The geoip plugin add geo location data associated with the client IP, it allows you to configure a geoIP2 maxmind database to add the geo location data associated with the IP address.

The data is added leveraging the metadata plugin, values can then be retrieved using it as well, for example:

import (
    "strconv"
    "github.com/khulnasoft-lab/dnserver/plugin/metadata"
)
// ...
if getLongitude := metadata.ValueFunc(ctx, "geoip/longitude"); getLongitude != nil {
    if longitude, err := strconv.ParseFloat(getLongitude(), 64); err == nil {
        // Do something useful with longitude.
    }
} else {
    // The metadata label geoip/longitude for some reason, was not set.
}
// ...

Databases

The supported databases use city schema such as City and Enterprise. Other databases types with different schemas are not supported yet.

You can download a free and public City database.

Syntax

geoip [DBFILE]

or

geoip [DBFILE] {
    [edns-subnet]
}
  • DBFILE the mmdb database file path. We recommend updating your mmdb database periodically for more accurate results.

  • edns-subnet: Optional. Use EDNS0 subnet (if present) for Geo IP instead of the source IP of the DNS request. This helps identifying the closest source IP address through intermediary DNS resolvers, and it also makes GeoIP testing easy: dig +subnet=1.2.3.4 @dns-server.example.com www.geo-aware.com.

    NOTE: due to security reasons, recursive DNS resolvers may mask a few bits off of the clients' IP address, which can cause inaccuracies in GeoIP resolution.

    There is no defined mask size in the standards, but there are examples: RFC 7871's example conceals the last 72 bits of an IPv6 source address, and NS1 Help Center mentions that ECS-enabled DNS resolvers send only the first three octets (eg. /24) of the source IPv4 address.

Examples

The following configuration configures the City database, and looks up geolocation based on EDNS0 subnet if present.

. {
    geoip /opt/geoip2/db/GeoLite2-City.mmdb {
      edns-subnet
    }
    metadata # Note that metadata plugin must be enabled as well.
}

The view plugin can use geoip metadata as selection criteria to provide GSLB functionality. In this example, clients from the city "Exampleshire" will receive answers for example.com from the zone defined in example.com.exampleshire-db. All other clients will receive answers from the zone defined in example.com.db. Note that the order of the two example.com server blocks below is important; the default viewless server block must be last.

example.com {
    view exampleshire {
      expr metadata('geoip/city/name') == 'Exampleshire'
    }
    geoip /opt/geoip2/db/GeoLite2-City.mmdb
    metadata
    file example.com.exampleshire-db
}

example.com {
    file example.com.db
}

Metadata Labels

A limited set of fields will be exported as labels, all values are stored using strings regardless of their underlying value type, and therefore you may have to convert it back to its original type, note that numeric values are always represented in base 10.

Label Type Example Description
geoip/city/name string Cambridge Then city name in English language.
geoip/country/code string GB Country ISO 3166-1 code.
geoip/country/name string United Kingdom The country name in English language.
geoip/country/is_in_european_union bool false Either true or false.
geoip/continent/code string EU See Continent codes.
geoip/continent/name string Europe The continent name in English language.
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 timezone.
geoip/postalcode string CB4 The postal code.

Continent Codes

Value Continent (EN)
AF Africa
AN Antarctica
AS Asia
EU Europe
NA North America
OC Oceania
SA South America

Documentation

Overview

Package geoip implements a max mind database plugin.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GeoIP

type GeoIP struct {
	Next plugin.Handler
	// contains filtered or unexported fields
}

GeoIP is a plugin that add geo location data to the request context by looking up a maxmind geoIP2 database, and which data can be later consumed by other middlewares.

func (GeoIP) Metadata

func (g GeoIP) Metadata(ctx context.Context, state request.Request) context.Context

Metadata implements the metadata.Provider Interface in the metadata plugin, and is used to store the data associated with the source IP of every request.

func (GeoIP) Name

func (g GeoIP) Name() string

Name implements the Handler interface.

func (GeoIP) ServeDNS

func (g GeoIP) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error)

ServeDNS implements the plugin.Handler interface.

Jump to

Keyboard shortcuts

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