oschwald / geoip2-golang

Unofficial MaxMind GeoIP2 Reader for Go
ISC License
1.86k stars 191 forks source link

support ipinfo free database #103

Closed Misaka-blog closed 1 year ago

Misaka-blog commented 1 year ago

support ipinfo free database https://ipinfo.io/products/free-ip-database

oschwald commented 1 year ago

I don't think this would work. I hadn't looked at their database previously, but after downloading it, it appears that records have the following format:

mmdblookup -f asn.mmdb  -i 1.1.1.1

  {
    "asn":
      "AS13335" <utf8_string>
    "domain":
      "cloudflare.com" <utf8_string>
    "name":
      "Cloudflare, Inc." <utf8_string>
  }

These keys do not match the ones used by MaxMind's GeoLite2 ASN and will not work with this library. I would suggest using github.com/oschwald/maxminddb-golang directly. I think something like this would work:

db, err := maxminddb.Open("asn.mmdb")
if err != nil {
    log.Fatal(err)
}

ip := net.ParseIP("1.1.1.1")

var record struct {
    ASN string `maxminddb:"asn"`
    Domain string `maxminddb:"domain"`
    Name string `maxminddb:"name"`
}

err = db.Lookup(ip, &record)
if err != nil {
    log.Panic(err)
}