mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2024-11-15 13:41:23 +08:00
37 lines
613 B
Go
37 lines
613 B
Go
|
package mmdb
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"net"
|
||
|
|
||
|
"github.com/oschwald/maxminddb-golang"
|
||
|
)
|
||
|
|
||
|
type geoip2Country struct {
|
||
|
Country struct {
|
||
|
IsoCode string `maxminddb:"iso_code"`
|
||
|
} `maxminddb:"country"`
|
||
|
}
|
||
|
|
||
|
type Reader struct {
|
||
|
*maxminddb.Reader
|
||
|
databaseType
|
||
|
}
|
||
|
|
||
|
func (r Reader) LookupCode(ipAddress net.IP) string {
|
||
|
switch r.databaseType {
|
||
|
case typeMaxmind:
|
||
|
var country geoip2Country
|
||
|
_ = r.Lookup(ipAddress, &country)
|
||
|
return country.Country.IsoCode
|
||
|
|
||
|
case typeSing:
|
||
|
var code string
|
||
|
_ = r.Lookup(ipAddress, &code)
|
||
|
return code
|
||
|
|
||
|
default:
|
||
|
panic(fmt.Sprint("unknown geoip database type:", r.databaseType))
|
||
|
}
|
||
|
}
|