Skip to content

Customize GeoIP database

Dashboard v0.18.3+ uses an embedded GeoIP database to query IP locations. An IPinfo format mmdb database is required to be placed at pkg/geoip/geoip.db to compile the Dashboard with working GeoIP queries.

An IPinfo format mmdb is constructed by those fields:

  • An IP subnet as an entry
  • continent:Continent code
  • continent_name:Continent name
  • country:Country code
  • country_name:Country name

Dashboard only requires the country field, and other fields can be left empty.

Editing database

It is recommended to use mmdbwriter and maxminddb-golang libraries for editing mmdb databases as of now.

If your goal is to correct geographic information, it is recommended to modify directly based on the original IPinfo database. For similar operations, you can refer to the sing-geoip project.

Using the correction of IPinfo data as an example, you need to use the ReplaceWith inserter (which is the default value) and write the mmdbtype.Map information:

go
subnet := &net.IPNet{
    IP:   net.ParseIP("114.5.1.4")
    Mask: net.CIDRMask(32, 32)
}
countryMap := make(map[mmdbtype.String]mmdbtype.String)

countryMap[mmdbtype.String("country")] = mmdbtype.String("JP")

if err := writer.Insert(subnet, mmdbtype.Map(countryMap)); err != nil {
	return err
}

A writer is an instance of *mmdbwriter.Tree and needs to be created using the mmdbwriter.New method. The specific operations and the process of inserting other fields are not mentioned here.

After writing the database information to a file, you can use the mmdbinspect CLI tool to test the database information. For example:

shell
$ mmdbinspect -db ./country.mmdb 114.5.1.4
[
    {
        "Database": "./country.mmdb",
        "Records": [
            {
                "Network": "114.5.1.4/32",
                "Record": {
                    "continent": "AS",
                    "continent_name": "Asia",
                    "country": "JP",
                    "country_name": "Japan"
                }
            }
        ],
        "Lookup": "114.5.1.4"
    }
]

Once the format and information are confirmed to be correct, the database can be used for compiling the Dashboard. Afterwards, you can test if the query API is working correctly using grpcurl or similar tools:

shell
grpcurl -proto ./nezha/proto/nezha.proto -plaintext -H 'client_secret: test' -d '{"ip": "114.5.1.4"}' 127.0.0.1:5555 proto.NezhaService/LookupGeoIP