geoip2

package module
v1.9.3 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: ISC Imports: 11 Imported by: 0

README

GeoIP2 Reader for Go

PkgGoDev

This library reads MaxMind GeoLite2 and GeoIP2 databases.

This library is built using the Go maxminddb reader. All data for the database record is decoded using this library. If you only need several fields, you may get superior performance by using maxminddb's Lookup directly with a result struct that only contains the required fields. (See example_test.go in the maxminddb repository for an example of this.)

Installation

go get github.com/oschwald/geoip2-golang

Usage

See GoDoc for documentation and examples.

Example

package main

import (
	"fmt"
	"log"
	"net"

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

func main() {
	db, err := geoip2.Open("GeoIP2-City.mmdb")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()
	// If you are using strings that may be invalid, check that ip is not nil
	ip := net.ParseIP("81.2.69.142")
	record, err := db.City(ip)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Portuguese (BR) city name: %v\n", record.City.Names["pt-BR"])
	if len(record.Subdivisions) > 0 {
		fmt.Printf("English subdivision name: %v\n", record.Subdivisions[0].Names["en"])
	}
	fmt.Printf("Russian country name: %v\n", record.Country.Names["ru"])
	fmt.Printf("ISO country code: %v\n", record.Country.IsoCode)
	fmt.Printf("Time zone: %v\n", record.Location.TimeZone)
	fmt.Printf("Coordinates: %v, %v\n", record.Location.Latitude, record.Location.Longitude)
	// Output:
	// Portuguese (BR) city name: Londres
	// English subdivision name: England
	// Russian country name: Великобритания
	// ISO country code: GB
	// Time zone: Europe/London
	// Coordinates: 51.5142, -0.0931
}

Testing

Make sure you checked out test data submodule:

git submodule init
git submodule update

Execute test suite:

go test

Contributing

Contributions welcome! Please fork the repository and open a pull request with your changes.

License

This is free software, licensed under the ISC license.

Documentation

Overview

Package geoip2 provides an easy-to-use API for the MaxMind GeoIP2 and GeoLite2 databases; this package does not support GeoIP Legacy databases.

The structs provided by this package match the internal structure of the data in the MaxMind databases.

See github.com/oschwald/maxminddb-golang for more advanced used cases.

Example

Example provides a basic example of using the API. Use of the Country method is analogous to that of the City method.

db, err := Open("test-data/test-data/GeoIP2-City-Test.mmdb")
if err != nil {
	log.Panic(err)
}
defer db.Close()
// If you are using strings that may be invalid, check that ip is not nil
ip := net.ParseIP("81.2.69.142")
record, err := db.City(ip)
if err != nil {
	log.Panic(err)
}
fmt.Printf("Portuguese (BR) city name: %v\n", record.City.Names["pt-BR"])
fmt.Printf("English subdivision name: %v\n", record.Subdivisions[0].Names["en"])
fmt.Printf("Russian country name: %v\n", record.Country.Names["ru"])
fmt.Printf("ISO country code: %v\n", record.Country.IsoCode)
fmt.Printf("Time zone: %v\n", record.Location.TimeZone)
fmt.Printf("Coordinates: %v, %v\n", record.Location.Latitude, record.Location.Longitude)
Output:

Portuguese (BR) city name: Londres
English subdivision name: England
Russian country name: Великобритания
ISO country code: GB
Time zone: Europe/London
Coordinates: 51.5142, -0.0931

Index

Examples

Constants

View Source
const ApiUrl = "https://minfraud.maxmind.com/minfraud/v2.0"
View Source
const ApiVersion = "v1.23.0"

Variables

View Source
var ErrReceived = errors.New("received error from Minfraud API")

Functions

This section is empty.

Types

type InvalidMethodError

type InvalidMethodError struct {
	Method       string
	DatabaseType string
}

InvalidMethodError is returned when a lookup method is called on a database that it does not support. For instance, calling the ISP method on a City database.

func (InvalidMethodError) Error

func (e InvalidMethodError) Error() string

type MaxmindClient added in v1.9.0

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

func NewMaxmindClient added in v1.9.0

func NewMaxmindClient(accountID string, licenseKey string) *MaxmindClient

func (*MaxmindClient) Post added in v1.9.0

func (c *MaxmindClient) Post(path string, data interface{}) ([]byte, int, error)

type MinFraud added in v1.9.0

type MinFraud struct {
	AccountID  string
	LicenseKey string
	Req        *model.MinfraudReq
	// contains filtered or unexported fields
}

func NewMinFraud added in v1.9.0

func NewMinFraud(accountID string, licenseKey string) (*MinFraud, error)

func (*MinFraud) Factors added in v1.9.0

func (mf *MinFraud) Factors() (*model.Factors, error)

func (*MinFraud) Insights added in v1.9.0

func (mf *MinFraud) Insights() (*model.Insights, error, *model.Error)

Insights returns the MaxMind Risk Score along with device and user data. The 3rd return value is if error == ErrReceived.

func (*MinFraud) Score added in v1.9.0

func (mf *MinFraud) Score() (*model.Score, error, *model.Error)

Score returns the MaxMind Risk Score. The 3rd return value is if error == ErrReceived.

func (*MinFraud) WithAccount added in v1.9.0

func (mf *MinFraud) WithAccount(data *model.AccountReq) *MinFraud

func (*MinFraud) WithBilling added in v1.9.0

func (mf *MinFraud) WithBilling(data *model.BillingReq) *MinFraud

func (*MinFraud) WithCreditCard added in v1.9.0

func (mf *MinFraud) WithCreditCard(data *model.CreditCardReq) *MinFraud

func (*MinFraud) WithCustomInputs added in v1.9.0

func (mf *MinFraud) WithCustomInputs(data *model.CustomInputsReq) *MinFraud

func (*MinFraud) WithDevice added in v1.9.0

func (mf *MinFraud) WithDevice(data *model.DeviceReq) *MinFraud

func (*MinFraud) WithEmail added in v1.9.0

func (mf *MinFraud) WithEmail(data *model.EmailReq) *MinFraud

func (*MinFraud) WithEvent added in v1.9.0

func (mf *MinFraud) WithEvent(data *model.EventReq) *MinFraud

func (*MinFraud) WithOrder added in v1.9.0

func (mf *MinFraud) WithOrder(data *model.OrderReq) *MinFraud

func (*MinFraud) WithPayment added in v1.9.0

func (mf *MinFraud) WithPayment(data *model.PaymentReq) *MinFraud

func (*MinFraud) WithShipping added in v1.9.0

func (mf *MinFraud) WithShipping(data *model.ShippingReq) *MinFraud

func (*MinFraud) WithShoppingCartItem added in v1.9.0

func (mf *MinFraud) WithShoppingCartItem(data *model.ShoppingCartReq) *MinFraud

type Reader

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

Reader holds the maxminddb.Reader struct. It can be created using the Open and FromBytes functions.

func FromBytes

func FromBytes(bytes []byte) (*Reader, error)

FromBytes takes a byte slice corresponding to a GeoIP2/GeoLite2 database file and returns a Reader struct or an error. Note that the byte slice is used directly; any modification of it after opening the database will result in errors while reading from the database.

func Open

func Open(file string) (*Reader, error)

Open takes a string path to a file and returns a Reader struct or an error. The database file is opened using a memory map. Use the Close method on the Reader object to return the resources to the system.

func (*Reader) ASN

func (r *Reader) ASN(ipAddress net.IP) (*model.ASN, error)

ASN takes an IP address as a net.IP struct and returns a ASN struct and/or an error.

func (*Reader) AnonymousIP

func (r *Reader) AnonymousIP(ipAddress net.IP) (*model.AnonymousIP, error)

AnonymousIP takes an IP address as a net.IP struct and returns a AnonymousIP struct and/or an error.

func (*Reader) City

func (r *Reader) City(ipAddress net.IP) (*model.City, error)

City takes an IP address as a net.IP struct and returns a City struct and/or an error. Although this can be used with other databases, this method generally should be used with the GeoIP2 or GeoLite2 City databases.

func (*Reader) Close

func (r *Reader) Close() error

Close unmaps the database file from virtual memory and returns the resources to the system.

func (*Reader) ConnectionType

func (r *Reader) ConnectionType(ipAddress net.IP) (*model.ConnectionType, error)

ConnectionType takes an IP address as a net.IP struct and returns a ConnectionType struct and/or an error.

func (*Reader) Country

func (r *Reader) Country(ipAddress net.IP) (*model.Country, error)

Country takes an IP address as a net.IP struct and returns a Country struct and/or an error. Although this can be used with other databases, this method generally should be used with the GeoIP2 or GeoLite2 Country databases.

func (*Reader) Domain

func (r *Reader) Domain(ipAddress net.IP) (*model.Domain, error)

Domain takes an IP address as a net.IP struct and returns a Domain struct and/or an error.

func (*Reader) Enterprise

func (r *Reader) Enterprise(ipAddress net.IP) (*model.Enterprise, error)

Enterprise takes an IP address as a net.IP struct and returns an Enterprise struct and/or an error. This is intended to be used with the GeoIP2 Enterprise database.

func (*Reader) ISP

func (r *Reader) ISP(ipAddress net.IP) (*model.ISP, error)

ISP takes an IP address as a net.IP struct and returns a ISP struct and/or an error.

func (*Reader) Metadata

func (r *Reader) Metadata() maxminddb.Metadata

Metadata takes no arguments and returns a struct containing metadata about the MaxMind database in use by the Reader.

type UnknownDatabaseTypeError

type UnknownDatabaseTypeError struct {
	DatabaseType string
}

UnknownDatabaseTypeError is returned when an unknown database type is opened.

func (UnknownDatabaseTypeError) Error

func (e UnknownDatabaseTypeError) Error() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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