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

BIN
plugin/geoip/testdata/GeoLite2-ASN.mmdb vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@@ -4,7 +4,7 @@ This directory contains mmdb database files used during the testing of this plug
# Create mmdb database files
If you need to change them to add a new value, or field the best is to recreate them, the code snipped used to create them initially is provided next.
```golang
```go
package main
import (
@@ -17,13 +17,15 @@ import (
"github.com/maxmind/mmdbwriter/mmdbtype"
)
const cdir = "81.2.69.142/32"
const cidr = "81.2.69.142/32"
// Create new mmdb database fixtures in this directory.
func main() {
createCityDB("GeoLite2-City.mmdb", "DBIP-City-Lite")
// Create unkwnon database type.
// Create unknown database type.
createCityDB("GeoLite2-UnknownDbType.mmdb", "UnknownDbType")
// Create ASN database.
createASNDB("GeoLite2-ASN.mmdb", "GeoLite2-ASN")
}
func createCityDB(dbName, dbType string) {
@@ -34,7 +36,7 @@ func createCityDB(dbName, dbType string) {
}
// Define and insert the new data.
_, ip, err := net.ParseCIDR(cdir)
_, ip, err := net.ParseCIDR(cidr)
if err != nil {
log.Fatal(err)
}
@@ -109,4 +111,55 @@ func createCityDB(dbName, dbType string) {
log.Fatal(err)
}
}
func createASNDB(dbName, dbType string) {
// Load a database writer.
// IncludeReservedNetworks allows inserting private IP ranges like 10.0.0.0/8.
writer, err := mmdbwriter.New(mmdbwriter.Options{
DatabaseType: dbType,
IncludeReservedNetworks: true,
})
if err != nil {
log.Fatal(err)
}
// Define and insert the new data.
_, ip, err := net.ParseCIDR(cidr)
if err != nil {
log.Fatal(err)
}
record := mmdbtype.Map{
"autonomous_system_number": mmdbtype.Uint64(12345),
"autonomous_system_organization": mmdbtype.String("Test ASN Organization"),
}
if err := writer.InsertFunc(ip, inserter.TopLevelMergeWith(record)); err != nil {
log.Fatal(err)
}
// Add "Not routed" entry for private IP range (ASN=0).
// This tests edge cases from iptoasn.com data where some ranges have no ASN.
_, notRoutedIP, err := net.ParseCIDR("10.0.0.0/8")
if err != nil {
log.Fatal(err)
}
notRoutedRecord := mmdbtype.Map{
"autonomous_system_number": mmdbtype.Uint64(0),
"autonomous_system_organization": mmdbtype.String("Not routed"),
}
if err := writer.InsertFunc(notRoutedIP, inserter.TopLevelMergeWith(notRoutedRecord)); err != nil {
log.Fatal(err)
}
// Write the DB to the filesystem.
fh, err := os.Create(dbName)
if err != nil {
log.Fatal(err)
}
_, err = writer.WriteTo(fh)
if err != nil {
log.Fatal(err)
}
}
```