plugin/geoip: Add ASN schema support (#7730)

Adds the ability to query ASN .mmdb databases, in addition to the existing City db functionality.

Signed-off-by: Eric Case <eric.case@gmail.com>
This commit is contained in:
Eric Case
2025-11-30 08:36:33 -08:00
committed by GitHub
parent 604e1675cf
commit 54dd258d57
7 changed files with 213 additions and 37 deletions

View File

@@ -2,13 +2,15 @@
## Name
*geoip* - Lookup maxmind geoip2 databases using the client IP, then add associated geoip data to the context request.
*geoip* - Lookup `.mmdb` ([MaxMind db file format](https://maxmind.github.io/MaxMind-DB/)) 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](https://dev.maxmind.com/geoip/docs/databases) to add the geo location data associated with the IP address.
The *geoip* plugin allows you to enrich the data associated with Client IP addresses, e.g. geoip information like City, Country, and Network ASN. GeoIP data is commonly available in the `.mmdb` format, a database format that maps IPv4 and IPv6 addresses to data records using a binary search tree.
The data is added leveraging the *metadata* plugin, values can then be retrieved using it as well, for example:
The data is added leveraging the *metadata* plugin, values can then be retrieved using it as well.
**Longitude example:**
```go
import (
@@ -26,11 +28,47 @@ if getLongitude := metadata.ValueFunc(ctx, "geoip/longitude"); getLongitude != n
// ...
```
**City example:**
```go
import (
"github.com/coredns/coredns/plugin/metadata"
)
// ...
if getCity := metadata.ValueFunc(ctx, "geoip/city/name"); getCity != nil {
city := getCity()
// Do something useful with city.
} else {
// The metadata label geoip/city/name for some reason, was not set.
}
// ...
```
**ASN example:**
```go
import (
"strconv"
"github.com/coredns/coredns/plugin/metadata"
)
// ...
if getASN := metadata.ValueFunc(ctx, "geoip/asn/number"); getASN != nil {
if asn, err := strconv.ParseUint(getASN(), 10, 32); err == nil {
// Do something useful with asn.
}
}
if getASNOrg := metadata.ValueFunc(ctx, "geoip/asn/org"); getASNOrg != nil {
asnOrg := getASNOrg()
// Do something useful with asnOrg.
}
// ...
```
## Databases
The supported databases use city schema such as `City` and `Enterprise`. Other databases types with different schemas are not supported yet.
The supported databases use city schema such as `ASN`, `City`, and `Enterprise`. `.mmdb` files are generally supported, as long as their field names correctly map to the Metadata Labels below. Other database types with different schemas are not supported yet.
You can download a [free and public City database](https://dev.maxmind.com/geoip/geolite2-free-geolocation-data).
Free and commercial GeoIP `.mmdb` files are commonly available from vendors like [MaxMind](https://dev.maxmind.com/geoip/docs/databases), [IPinfo](https://ipinfo.io/developers/database-download), and [IPtoASN](https://iptoasn.com/) which is [Public Domain-licensed](https://opendatacommons.org/licenses/pddl/1-0/).
## Syntax
@@ -46,7 +84,7 @@ geoip [DBFILE] {
}
```
* **DBFILE** the mmdb database file path. We recommend updating your mmdb database periodically for more accurate results.
* **DBFILE** the `mmdb` database file path. We recommend updating your `mmdb` database periodically for more accurate results.
* `edns-subnet`: Optional. Use [EDNS0 subnet](https://en.wikipedia.org/wiki/EDNS_Client_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.
@@ -103,6 +141,8 @@ A limited set of fields will be exported as labels, all values are stored using
| `geoip/longitude` | `float64` | `0.1315` | Base 10, max available precision.
| `geoip/timezone` | `string` | `Europe/London` | The timezone.
| `geoip/postalcode` | `string` | `CB4` | The postal code.
| `geoip/asn/number` | `uint` | `396982` | The autonomous system number.
| `geoip/asn/org` | `string` | `GOOGLE-CLOUD-PLATFORM` | The autonomous system organization.
## Continent Codes