geoip2

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 27, 2022 License: MIT Imports: 8 Imported by: 26

README

Software License Go Report Card Go Doc

GeoIP2 Reader for Go

This library reads MaxMind GeoIP2 databases.

Inspired by oschwald/geoip2-golang.

Installation

go get github.com/IncSW/geoip2

Quick Start

import "github.com/IncSW/geoip2"

reader, err := geoip2.NewCityReaderFromFile("path/to/GeoIP2-City.mmdb")
if err != nil {
	panic(err)
}
record, err := reader.Lookup(net.ParseIP("81.2.69.142"))
if err != nil {
	panic(err)
}
println(record.Continent.Names["zh-CN"]) // 欧洲
println(record.City.Names["pt-BR"]) // Wimbledon
if len(record.Subdivisions) != 0 {
	println(record.Subdivisions[0].Names["en"]) // England
}
println(record.Country.Names["ru"]) // Великобритания
println(record.Country.ISOCode) // GB
println(record.Location.TimeZone) // Europe/London
println(record.Country.GeoNameID) // 2635167, https://www.geonames.org/2635167

Performance

IncSW/geoip2
city-24                          342847    2981 ns/op   2032 B/op    12 allocs/op
city_parallel-24                4477626     269 ns/op   2032 B/op    12 allocs/op
isp-24                          3539738     336 ns/op     64 B/op     1 allocs/op
isp_parallel-24                46938070    25.7 ns/op     64 B/op     1 allocs/op
connection_type-24              8759110     133 ns/op      0 B/op     0 allocs/op
connection_type_parallel-24   142261742    8.34 ns/op      0 B/op     0 allocs/op
oschwald/geoip2-golang
city-24                          109092   10717 ns/op   2848 B/op   103 allocs/op
city_parallel-24                 662510    1718 ns/op   2848 B/op   103 allocs/op
isp-24                          1688287     705 ns/op    112 B/op     4 allocs/op
isp_parallel-24                14285560    84.4 ns/op    112 B/op     4 allocs/op
connection_type-24              3883234     305 ns/op     32 B/op     2 allocs/op
connection_type_parallel-24    34284831    32.1 ns/op     32 B/op     2 allocs/op

Supported databases types

Country
  • GeoIP2-Country
  • GeoLite2-Country
  • DBIP-Country
  • DBIP-Country-Lite
City
  • GeoIP2-City
  • GeoLite2-City
  • GeoIP2-Enterprise
  • DBIP-City-Lite
ISP
  • GeoIP2-ISP
ASN
  • GeoLite2-ASN
  • DBIP-ASN-Lite
  • DBIP-ASN-Lite (compat=GeoLite2-ASN)
Connection Type
  • GeoIP2-Connection-Type
Anonymous IP
  • GeoIP2-Anonymous-IP
Domain
  • GeoIP2-Domain

License

MIT License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("not found")

Functions

This section is empty.

Types

type ASN added in v0.0.2

type ASN struct {
	AutonomousSystemNumber       uint32
	AutonomousSystemOrganization string
}

type ASNReader added in v0.0.2

type ASNReader struct {
	// contains filtered or unexported fields
}

func NewASNReader added in v0.0.2

func NewASNReader(buffer []byte) (*ASNReader, error)

func NewASNReaderFromFile added in v0.0.2

func NewASNReaderFromFile(filename string) (*ASNReader, error)

func (*ASNReader) Lookup added in v0.0.2

func (r *ASNReader) Lookup(ip net.IP) (*ASN, error)

type AnonymousIP added in v0.0.2

type AnonymousIP struct {
	IsAnonymous        bool
	IsAnonymousVPN     bool
	IsHostingProvider  bool
	IsPublicProxy      bool
	IsTorExitNode      bool
	IsResidentialProxy bool
}

type AnonymousIPReader added in v0.0.2

type AnonymousIPReader struct {
	// contains filtered or unexported fields
}

func NewAnonymousIPReader added in v0.0.2

func NewAnonymousIPReader(buffer []byte) (*AnonymousIPReader, error)

func NewAnonymousIPReaderFromFile added in v0.0.2

func NewAnonymousIPReaderFromFile(filename string) (*AnonymousIPReader, error)

func (*AnonymousIPReader) Lookup added in v0.0.2

func (r *AnonymousIPReader) Lookup(ip net.IP) (*AnonymousIP, error)

type City

type City struct {
	Names      map[string]string
	GeoNameID  uint32
	Confidence uint16 // Enterprise
}

type CityReader added in v0.0.2

type CityReader struct {
	// contains filtered or unexported fields
}

func NewCityReader added in v0.0.2

func NewCityReader(buffer []byte) (*CityReader, error)

func NewCityReaderFromFile added in v0.0.2

func NewCityReaderFromFile(filename string) (*CityReader, error)

func NewEnterpriseReader added in v0.0.2

func NewEnterpriseReader(buffer []byte) (*CityReader, error)

func NewEnterpriseReaderFromFile added in v0.0.2

func NewEnterpriseReaderFromFile(filename string) (*CityReader, error)

func (*CityReader) Lookup added in v0.0.2

func (r *CityReader) Lookup(ip net.IP) (*CityResult, error)

type CityResult added in v0.0.2

type CityResult struct {
	Continent          Continent
	Country            Country
	Subdivisions       []Subdivision
	City               City
	Location           Location
	Postal             Postal
	RegisteredCountry  Country
	RepresentedCountry Country
	Traits             Traits
}

type ConnectionType

type ConnectionType struct {
	ConnectionType string
}

type ConnectionTypeReader added in v0.0.2

type ConnectionTypeReader struct {
	// contains filtered or unexported fields
}

func NewConnectionTypeReader added in v0.0.2

func NewConnectionTypeReader(buffer []byte) (*ConnectionTypeReader, error)

func NewConnectionTypeReaderFromFile added in v0.0.2

func NewConnectionTypeReaderFromFile(filename string) (*ConnectionTypeReader, error)

func (*ConnectionTypeReader) Lookup added in v0.0.2

func (r *ConnectionTypeReader) Lookup(ip net.IP) (string, error)

type Continent

type Continent struct {
	GeoNameID uint32
	Code      string
	Names     map[string]string
}

type Country

type Country struct {
	ISOCode           string
	Names             map[string]string
	Type              string // [RepresentedCountry]
	GeoNameID         uint32
	Confidence        uint16 // Enterprise [Country, RegisteredCountry]
	IsInEuropeanUnion bool
}

type CountryReader added in v0.0.2

type CountryReader struct {
	// contains filtered or unexported fields
}

func NewCountryReader added in v0.0.2

func NewCountryReader(buffer []byte) (*CountryReader, error)

func NewCountryReaderFromFile added in v0.0.2

func NewCountryReaderFromFile(filename string) (*CountryReader, error)

func (*CountryReader) Lookup added in v0.0.2

func (r *CountryReader) Lookup(ip net.IP) (*CountryResult, error)

type CountryResult added in v0.0.2

type CountryResult struct {
	Continent          Continent
	Country            Country
	RegisteredCountry  Country
	RepresentedCountry Country
	Traits             Traits
}

type Domain added in v0.0.2

type Domain struct {
	Domain string
}

type DomainReader added in v0.0.2

type DomainReader struct {
	// contains filtered or unexported fields
}

func NewDomainReader added in v0.0.2

func NewDomainReader(buffer []byte) (*DomainReader, error)

func NewDomainReaderFromFile added in v0.0.2

func NewDomainReaderFromFile(filename string) (*DomainReader, error)

func (*DomainReader) Lookup added in v0.0.2

func (r *DomainReader) Lookup(ip net.IP) (string, error)

type ISP

type ISP struct {
	AutonomousSystemNumber       uint32
	AutonomousSystemOrganization string
	ISP                          string
	Organization                 string
	MobileCountryCode            string
	MobileNetworkCode            string
}

type ISPReader added in v0.0.2

type ISPReader struct {
	// contains filtered or unexported fields
}

func NewISPReader added in v0.0.2

func NewISPReader(buffer []byte) (*ISPReader, error)

func NewISPReaderFromFile added in v0.0.2

func NewISPReaderFromFile(filename string) (*ISPReader, error)

func (*ISPReader) Lookup added in v0.0.2

func (r *ISPReader) Lookup(ip net.IP) (*ISP, error)

type Location

type Location struct {
	Latitude       float64
	Longitude      float64
	TimeZone       string
	AccuracyRadius uint16
	MetroCode      uint16
}

type Metadata

type Metadata struct {
	NodeCount                uint32            // node_count This is an unsigned 32-bit integer indicating the number of nodes in the search tree.
	RecordSize               uint16            // record_size This is an unsigned 16-bit integer. It indicates the number of bits in a record in the search tree. Note that each node consists of two records.
	IPVersion                uint16            // ip_version This is an unsigned 16-bit integer which is always 4 or 6. It indicates whether the database contains IPv4 or IPv6 address data.
	DatabaseType             string            // database_type This is a string that indicates the structure of each data record associated with an IP address. The actual definition of these structures is left up to the database creator. Names starting with “GeoIP” are reserved for use by MaxMind (and “GeoIP” is a trademark anyway).
	Languages                []string          // languages An array of strings, each of which is a locale code. A given record may contain data items that have been localized to some or all of these locales. Records should not contain localized data for locales not included in this array. This is an optional key, as this may not be relevant for all types of data.
	BinaryFormatMajorVersion uint16            // binary_format_major_version This is an unsigned 16-bit integer indicating the major version number for the database’s binary format.
	BinaryFormatMinorVersion uint16            // binary_format_minor_version This is an unsigned 16-bit integer indicating the minor version number for the database’s binary format.
	BuildEpoch               uint64            // build_epoch This is an unsigned 64-bit integer that contains the database build timestamp as a Unix epoch value.
	Description              map[string]string // description This key will always point to a map. The keys of that map will be language codes, and the values will be a description in that language as a UTF-8 string. The codes may include additional information such as script or country identifiers, like “zh-TW” or “mn-Cyrl-MN”. The additional identifiers will be separated by a dash character (“-“).
}

type Postal

type Postal struct {
	Code       string
	Confidence uint16 // Enterprise
}

type Subdivision

type Subdivision struct {
	ISOCode    string
	Names      map[string]string
	GeoNameID  uint32
	Confidence uint16 // Enterprise
}

type Traits

type Traits struct {
	StaticIPScore                float64 // Enterprise
	ISP                          string  // Enterprise
	Organization                 string  // Enterprise
	ConnectionType               string  // Enterprise
	Domain                       string  // Enterprise
	UserType                     string  // Enterprise
	AutonomousSystemOrganization string  // Enterprise
	AutonomousSystemNumber       uint32  // Enterprise
	IsLegitimateProxy            bool    // Enterprise
	MobileCountryCode            string  // Enterprise
	MobileNetworkCode            string  // Enterprise
	IsAnonymousProxy             bool
	IsSatelliteProvider          bool
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL