69 lines
2.3 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
outline: deep
---
# 自定义 IP 地理位置数据库
Dashboard v0.18.3+ 采用离线数据库获取 GeoIP 信息,如需编译正确显示地理位置信息的 Dashboard必须在 `pkg/geoip/geoip.db` 路径下放置符合 IPinfo 格式的 mmdb 数据库。
IPinfo 格式包含以下字段:
- 作为条目的 IP 子网
- `continent`:大洲代码
- `continent_name`:大洲名称
- `country`:国家/地区代码
- `country_name`:国家/地区名称
其中 Dashboard 只需要用到 `country` 信息,其它字段可留空。
## 编辑数据库
目前推荐使用 MaxMind 官方使用的 `mmdbwriter``maxminddb-golang` 这两个库进行 mmdb 的编辑操作。
如果你的主要目的是为了校正地理信息,建议直接在 IPinfo 原版数据库的基础上修改。类似的操作可以参考 [sing-geoip](https://github.com/SagerNet/sing-geoip) 项目。
这里以校正 IPinfo 数据为例,你需要使用 `ReplaceWith` inserter此为默认值并写入 `mmdbtype.Map` 信息:
```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
}
```
其中 `writer` 为一个 `*mmdbwriter.Tree` 的实例,需要通过 `mmdbwriter.New` 方法创建。具体操作和插入其它字段的过程不再赘述。
编辑完成并将数据库信息写入到文件后,可以使用 `mmdbinspect` 命令行工具测试数据库信息。例如:
```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"
}
]
```
确定格式、信息无误后便可用于 Dashboard 编译。之后可通过 `grpcurl` 或类似工具测试查询 API 是否工作正常:
```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
```