mirror of
https://github.com/coredns/coredns.git
synced 2025-11-01 18:53:43 -04:00
Create geoip plugin (#4688)
* Create geoip plugin Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Update plugin/geoip/README.md Co-authored-by: Miek Gieben <miek@miek.nl> Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Update plugin/geoip/README.md Co-authored-by: Miek Gieben <miek@miek.nl> Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Update plugin/geoip/README.md Co-authored-by: Miek Gieben <miek@miek.nl> Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Move DBFILE bullet below example Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Update plugin/geoip/README.md Co-authored-by: Miek Gieben <miek@miek.nl> Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Remove plugin name test case Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Remove languages option Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Update free database link Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Remove last language bits Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Use 127.0.0.1 as probing IP Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Update plugin/geoip/geoip.go Co-authored-by: Miek Gieben <miek@miek.nl> Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Update plugin/geoip/geoip.go Co-authored-by: Miek Gieben <miek@miek.nl> Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Use relative path for fixtures dir Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Set names with default string zero value Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Remove unused db types Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Remove non city databases in testdata Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Remove create databases main Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Fix metadata label format test case Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Fix import path block Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * go fmt after changes Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Tidy up go.mod and go.sum Signed-off-by: Sven Nebel <nebel.sven@gmail.com> * Add plugin to CODEOWNERS Signed-off-by: Sven Nebel <nebel.sven@gmail.com> Co-authored-by: Miek Gieben <miek@miek.nl>
This commit is contained in:
112
plugin/geoip/testdata/README.md
vendored
Normal file
112
plugin/geoip/testdata/README.md
vendored
Normal file
@@ -0,0 +1,112 @@
|
||||
# testdata
|
||||
This directory contains mmdb database files used during the testing of this plugin.
|
||||
|
||||
# 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
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
|
||||
"github.com/maxmind/mmdbwriter"
|
||||
"github.com/maxmind/mmdbwriter/inserter"
|
||||
"github.com/maxmind/mmdbwriter/mmdbtype"
|
||||
)
|
||||
|
||||
const cdir = "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.
|
||||
createCityDB("GeoLite2-UnknownDbType.mmdb", "UnknownDbType")
|
||||
}
|
||||
|
||||
func createCityDB(dbName, dbType string) {
|
||||
// Load a database writer.
|
||||
writer, err := mmdbwriter.New(mmdbwriter.Options{DatabaseType: dbType})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Define and insert the new data.
|
||||
_, ip, err := net.ParseCIDR(cdir)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// TODO(snebel29): Find an alternative location in Europe Union.
|
||||
record := mmdbtype.Map{
|
||||
"city": mmdbtype.Map{
|
||||
"geoname_id": mmdbtype.Uint64(2653941),
|
||||
"names": mmdbtype.Map{
|
||||
"en": mmdbtype.String("Cambridge"),
|
||||
"es": mmdbtype.String("Cambridge"),
|
||||
},
|
||||
},
|
||||
"continent": mmdbtype.Map{
|
||||
"code": mmdbtype.String("EU"),
|
||||
"geoname_id": mmdbtype.Uint64(6255148),
|
||||
"names": mmdbtype.Map{
|
||||
"en": mmdbtype.String("Europe"),
|
||||
"es": mmdbtype.String("Europa"),
|
||||
},
|
||||
},
|
||||
"country": mmdbtype.Map{
|
||||
"iso_code": mmdbtype.String("GB"),
|
||||
"geoname_id": mmdbtype.Uint64(2635167),
|
||||
"names": mmdbtype.Map{
|
||||
"en": mmdbtype.String("United Kingdom"),
|
||||
"es": mmdbtype.String("Reino Unido"),
|
||||
},
|
||||
"is_in_european_union": mmdbtype.Bool(true),
|
||||
},
|
||||
"location": mmdbtype.Map{
|
||||
"accuracy_radius": mmdbtype.Uint16(200),
|
||||
"latitude": mmdbtype.Float64(52.2242),
|
||||
"longitude": mmdbtype.Float64(0.1315),
|
||||
"metro_code": mmdbtype.Uint64(0),
|
||||
"time_zone": mmdbtype.String("Europe/London"),
|
||||
},
|
||||
"postal": mmdbtype.Map{
|
||||
"code": mmdbtype.String("CB4"),
|
||||
},
|
||||
"registered_country": mmdbtype.Map{
|
||||
"iso_code": mmdbtype.String("GB"),
|
||||
"geoname_id": mmdbtype.Uint64(2635167),
|
||||
"names": mmdbtype.Map{"en": mmdbtype.String("United Kingdom")},
|
||||
"is_in_european_union": mmdbtype.Bool(false),
|
||||
},
|
||||
"subdivisions": mmdbtype.Slice{
|
||||
mmdbtype.Map{
|
||||
"iso_code": mmdbtype.String("ENG"),
|
||||
"geoname_id": mmdbtype.Uint64(6269131),
|
||||
"names": mmdbtype.Map{"en": mmdbtype.String("England")},
|
||||
},
|
||||
mmdbtype.Map{
|
||||
"iso_code": mmdbtype.String("CAM"),
|
||||
"geoname_id": mmdbtype.Uint64(2653940),
|
||||
"names": mmdbtype.Map{"en": mmdbtype.String("Cambridgeshire")},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
if err := writer.InsertFunc(ip, inserter.TopLevelMergeWith(record)); 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)
|
||||
}
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user