oschwald/geoip2-golang

Error returned for unknown DBIP-ASN-Lite mmdb file

mhf-ir opened this issue · 3 comments

$ mmdblookup --version

  mmdblookup version 1.4.2

$ mmdblookup --file dbip-asn.mmdb --ip 1.1.1.1

  {
    "autonomous_system_number": 
      13335 <uint32>
    "autonomous_system_organization": 
      "Cloudflare, Inc." <utf8_string>
  }

mmdb file:

https://db-ip.com/db/download/ip-to-asn-lite

Failed on my code:

package main

import (
	"fmt"
	"net"

	mmdb "github.com/oschwald/geoip2-golang"
)

func main() {

	mmdbASN, err := mmdb.Open("dbip-asn.mmdb")
	if err != nil {
		panic(err) // HERE the panic
	}
	ipParsed := net.ParseIP("1.1.1.1")
	asnData, asnErr := mmdbASN.ASN(ipParsed)
	if asnErr != nil {
		fmt.Println(asnData)
	}

	return
}
$ panic: geoip2: reader does not support the "DBIP-ASN-Lite (compat=GeoLite2-ASN)" database type

goroutine 1 [running]:
main.main()
        main.go:14 +0x11b

Seems be this bug mapping from https://github.com/oschwald/maxminddb-golang

package main

import (
	"fmt"
	"log"
	"net"

	mmdb "github.com/oschwald/maxminddb-golang"
)

func main() {

	mmdbASN, err := mmdb.Open("dbip-asn.mmdb")
	if err != nil {
		panic(err)
	}
	ipParsed := net.ParseIP("1.1.1.1")
	var record interface{}
	err = mmdbASN.Lookup(ipParsed, &record)
	if err != nil {
		log.Panic(err)
	}
	fmt.Printf("%v", record)

	return
}

Output:

map[autonomous_system_number:13335 autonomous_system_organization:Cloudflare, Inc.]

The reader doesn't panic. The panic is coming from your panic(err) call. The error is returned as the reader doesn't know about this database type. Assuming the database is similar to the MaxMind GeoLite2 ASN database, it could likely be added to that case in the switch statement:

case "GeoLite2-ASN":

fmt.Println(db.Metadata.DatabaseType)
// DBIP-ASN-Lite (compat=GeoLite2-ASN)

I think it's better using regex/match instead of switch. MaxMind is db structure and database information might be different if you build your own database.

https://github.com/maxmind/MaxMind-DB-Writer-perl

database_type         => 'My-Special-ASN-List-InOurCompany (compat=GeoLite2-ASN)',