repositorypackage
1.1.0
Repository: https://github.com/eggbertx/geoip-legacy.git
Documentation: pkg.go.dev
# README
geoip-legacy
A port of libGeoIP from C to pure Go. It supports IPv4 and IPv6 country databases.
Example usage
For extensive examples, see geoip_test.go, but here is a relatively simple example. GetCountryByAddr supports IP addresses and can use the net
package in the standard library to resolve a domain to an IP and look up the IP in the database.
IPv4-only DB example:
db, err := geoiplegacy.OpenDB("/usr/share/GeoIP/GeoIP.dat", nil)
if err != nil {
panic(err)
}
country, err = db.GetCountryByAddr("8.8.8.8")
if err != nil {
panic(err)
}
fmt.Printf("Country code: %s\nCountry name: %s\n", country.Code, country.NameUTF8)
IPv6-only DB example:
db, err := geoiplegacy.OpenDB("/usr/share/GeoIP/GeoIPv6.dat", &GeoIPOptions{
IsIPv6: true,
})
if err != nil {
panic(err)
}
country, err = db.GetCountryByAddr("2801::1")
if err != nil {
panic(err)
}
fmt.Printf("Country code: %s\nCountry name: %s\n", country.Code, country.NameUTF8)
Combined DB example usage
The above examples are only usable for looking up either IPv4 or IPv6 addresses. To be able to look up both, you can use a CombinedDB
.
Example usage:
db, err := geoiplegacy.OpenCombinedDB("/usr/share/GeoIP/GeoIP.dat", "/usr/share/GeoIP/GeoIPv6.dat")
if err != nil {
panic(err)
}
country, err = db.GetCountryByAddr("8.8.8.8")
// ...
country, err = db.GetCountryByAddr("2801::1")
// ...
country, err = db.GetCountryByAddr("google.com")
// ...
A regular DB can also handle domains, but a domain may or may not resolve to an IPv6 address, so a CombinedDB
is recommended.