51 lines
870 B
Go
51 lines
870 B
Go
package ipgeo
|
|
|
|
import (
|
|
"flag"
|
|
"github.com/ipipdotnet/ipdb-go"
|
|
"log"
|
|
)
|
|
|
|
var (
|
|
requiredFields = []string{
|
|
"country_name",
|
|
"region_name",
|
|
"city_name",
|
|
"isp_domain",
|
|
"country_code",
|
|
"continent_code",
|
|
}
|
|
ipipdb = flag.String("ipdb-database", "ipip.db", "path to ipip database")
|
|
db *ipdb.City
|
|
)
|
|
|
|
func Init() {
|
|
var err error
|
|
db, err = ipdb.NewCity(*ipipdb)
|
|
if err != nil {
|
|
log.Fatalln("Unable to open ipdb:", err.Error())
|
|
}
|
|
if !db.IsIPv4() {
|
|
log.Fatalln("This IPIP.net database has no IPv4 information!")
|
|
}
|
|
{
|
|
fields := db.Fields()
|
|
for _, requiredField := range requiredFields {
|
|
ok := false
|
|
for _, field := range fields {
|
|
if field == requiredField {
|
|
ok = true
|
|
break
|
|
}
|
|
}
|
|
if !ok {
|
|
log.Fatalln("This IPIP.net database has no required field", requiredField)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func Get() *ipdb.City {
|
|
return db
|
|
}
|