geo

package module
v0.0.0-...-50692a1 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2024 License: Apache-2.0 Imports: 10 Imported by: 5

README

Go Actions Status

geo

Looks up geoinfo from MaxMind GeoLite2 Database fetched from web. Currently only country code is supported but later can be extend to city, ISP, etc.

Example:

  // Keep in sync with the database from geolite2_url every day, without
  // keeping a local copy in disk.
  lookup = geo.New(geolite2_url, 24*time.Hour, "")
  // or local copy so it's available immediately when the service restarts.
  lookup = geo.New(geolite2_url, 24*time.Hour, "local-file-path")
  country = lookup.CountryCode(net.ParseIP("1.1.1.1"))

Documentation

Overview

Package geo provides functionality for looking up country code and ISP name of the given IP address.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ASN

func ASN(db *geoip2.Reader, ip net.IP) (string, error)

func City

func City(db *geoip2.Reader, ip net.IP) (string, string, error)

func CountryCode

func CountryCode(db *geoip2.Reader, ip net.IP) (string, error)

func FromFile

func FromFile(filePath string) (*lookup, error)

FromFile uses the local database file for lookup

func FromWeb

func FromWeb(dbURL string, nameInTarball string, syncInterval time.Duration, filePath string, lookupForValidation func(*geoip2.Reader, net.IP) (string, error)) *lookup

FromWeb is same as New but allows downloading a different MaxMind database lookupForValidation is a function that we call to validate new databases as they're loaded.

func ISP

func ISP(db *geoip2.Reader, ip net.IP) (string, error)

func LatLong

func LatLong(db *geoip2.Reader, ip net.IP) (float64, float64, error)

func LatLongFromWeb

func LatLongFromWeb(dbURL string, nameInTarball string, syncInterval time.Duration, filePath string, lookupForValidation func(*geoip2.Reader, net.IP) (float64, float64, error)) *lookup

LatLongFromWeb is same as New but allows downloading a different MaxMind database for lat/long lookup lookupForValidation is a function that we call to validate new databases as they're loaded.

func New

func New(dbURL string, syncInterval time.Duration, filePath string, lookupForValidation func(*geoip2.Reader, net.IP) (string, error)) *lookup

New constructs a new Lookup from the MaxMind GeoLite2 Country database fetched from the given URL and keeps in sync with it every syncInterval. If filePath is not empty, it saves the database file to filePath and uses the file if available. lookupForValidation is a function that we call to validate new databases as they're loaded.

Types

type CityLookup

type CityLookup interface {
	// City looks up the city and country for the given IP address and returns "", "" if there was an error.
	City(ip net.IP) (string, string)
}

type CountryLookup

type CountryLookup interface {
	// CountryCode looks up the 2 digit ISO 3166 country code in upper case for
	// the given IP address and returns "" if there was an error.
	CountryCode(ip net.IP) string
}

CountryLookup allows looking up the country for an IP address

type ISPLookup

type ISPLookup interface {
	// ISP looks up the ISP name for the given IP address and returns "" if there was an error.
	ISP(ip net.IP) string

	// ASN looks up the ASN number (e.g. AS62041) for the given IP address and returns "" if there was an error.
	ASN(ip net.IP) string
}

ISPLookup allows looking up ISP information for an IP address

type LatLongLookup

type LatLongLookup interface {
	// LatLong looks up the latitude and longitude for the given IP address and returns 0, 0 if there was an error.
	LatLong(ip net.IP) (float64, float64)
}

type Lookup

type Lookup interface {
	CountryLookup
	ISPLookup
	CityLookup
	LatLongLookup
	// Ready returns a channel which is closed when the lookup is ready to
	// serve requests.
	Ready() <-chan struct{}
}

type NoLookup

type NoLookup struct{}

NoLookup is a Lookup implementation which always return empty result.

func (NoLookup) ASN

func (l NoLookup) ASN(ip net.IP) string

func (NoLookup) City

func (l NoLookup) City(ip net.IP) (string, string)

func (NoLookup) CountryCode

func (l NoLookup) CountryCode(ip net.IP) string

func (NoLookup) ISP

func (l NoLookup) ISP(ip net.IP) string

func (NoLookup) LatLong

func (l NoLookup) LatLong(ip net.IP) (float64, float64)

func (NoLookup) Ready

func (l NoLookup) Ready() <-chan struct{}

Jump to

Keyboard shortcuts

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