geoip2

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2020 License: MIT Imports: 15 Imported by: 0

README

go-geoip2

Go Report Card license

Overview

This project can search for IP information through Maxmind geoip2 databases.

This project is built using Reader in oschwald/maxminddb-golang.

You can read Maxmind databases using a local file.
Either you can read Maxmind databases using Maxmind download URL.

If you use reading databases with Maxmind download URL(only support gzip link), it is possible to update the latest databases periodically.
It mean you will be automatically downloaded and updated to target-path in background.

So you don't need to update the latest Maxmind databases manually, So very useful.

[warning] Maxmind download API has a daily quota of requests.
Set to appropriate update interval.

Getting Started

// pseudo code
package main
import (
  "net"
  "github.com/gjbae1212/go-geoip2"
  
)

func main() {
   // db, err := Open("local-file-path")
   db, err := OpenURL("maxmind license key", "GeoLite2-Country", "/tmp",
      geoip2.WithUpdateInterval(6 * time.Hour), geoip2.WithRetries(2), geoip2.WithSuccessFunc(func(){}),...)
   if err != nil {
   	  panic(err)
   }
   
   ip := net.ParseIP("8.8.8.8")
   record, err := db.City(ip)
   if err != nil {
      panic(err)
   }
}

Inspiration

This project was inspired by oschwald/geoip2-golang

LICENSE

This project is following The MIT.

Documentation

Index

Constants

View Source
const (
	MaxmindDownloadFormat = "https://download.maxmind.com/app/geoip_download?license_key=%s&edition_id=%s&suffix=%s"
	GZIP                  = MaxmindDownloadSuffix("tar.gz")
	MD5                   = MaxmindDownloadSuffix("tar.gz.md5")
)

Variables

View Source
var (
	ErrInvalidParameters = fmt.Errorf("[err] invalid parameters")
	ErrNotFoundDatabase  = fmt.Errorf("[err] not found database")
	ErrFirstDownloadFail = fmt.Errorf("[err] first download fail")
)

Functions

func MaxmindDownloadURL

func MaxmindDownloadURL(licenseKey, editionId string, suffix MaxmindDownloadSuffix) (string, error)

maxmindDownloadURL returns Maxmind download URL reference: maxmind URL https://dev.maxmind.com/geoip/geoipupdate/#Direct_Downloads

Types

type DownloadOption

type DownloadOption interface {
	// contains filtered or unexported methods
}

type DownloadOptionFunc

type DownloadOptionFunc func(cfg *downloadConfig)

func WithErrorFunc

func WithErrorFunc(f func(error)) DownloadOptionFunc

WithErrorFunc returns a function for setting a method to call if a download failed.

func WithFirstDownloadWait

func WithFirstDownloadWait(d time.Duration) DownloadOptionFunc

WithFirstDownloadWait returns a function for setting first download wait time.

func WithRetries

func WithRetries(retries int) DownloadOptionFunc

WithUpdateInterval returns a function for setting download retry count if a download is failed.

func WithSuccessFunc

func WithSuccessFunc(f func()) DownloadOptionFunc

WithSuccessFunc returns a function for setting a method to call if a download succeeded.

func WithUpdateInterval

func WithUpdateInterval(d time.Duration) DownloadOptionFunc

WithUpdateInterval returns a function for setting download time interval.

type MaxmindDownloadSuffix

type MaxmindDownloadSuffix string

type Reader

type Reader interface {
	ASN(ipAddress net.IP) (*geoip2_golang.ASN, error)
	AnonymousIP(ipAddress net.IP) (*geoip2_golang.AnonymousIP, error)
	City(ipAddress net.IP) (*geoip2_golang.City, error)
	ConnectionType(ipAddress net.IP) (*geoip2_golang.ConnectionType, error)
	Country(ipAddress net.IP) (*geoip2_golang.Country, error)
	Domain(ipAddress net.IP) (*geoip2_golang.Domain, error)
	Enterprise(ipAddress net.IP) (*geoip2_golang.Enterprise, error)
	ISP(ipAddress net.IP) (*geoip2_golang.ISP, error)
	Metadata() maxminddb.Metadata
	Close() error
}

support to interface for oschwald/geoip2-golang.

func Open

func Open(file string) (Reader, error)

Open returns geoip Reader from a local file.

func OpenURL

func OpenURL(licenseKey, editionId, storeDir string, opts ...DownloadOption) (Reader, error)

OpenURL returns geoip Reader from maxmind download URL and updates automatically the latest maxmind databases. reference: maxmind URL https://dev.maxmind.com/geoip/geoipupdate/#Direct_Downloads

Jump to

Keyboard shortcuts

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