geoip

package
v0.0.0-...-ddb7c7b Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2015 License: MIT, Apache-2.0 Imports: 7 Imported by: 0

README

GeoIP API for Go

This package wraps the libgeoip C library for access from Go (golang). Build Status

Install with go get github.com/abh/geoip and use godoc geoip to read the documentation.

There's a small example in the ex/ subdirectory.

You can download the free GeoLite Country database or you can subscribe to updates.

Examples

file := "/usr/share/GeoIP/GeoIP.dat"

gi, err := geoip.Open(file)
if err != nil {
	fmt.Printf("Could not open GeoIP database\n")
}

if gi != nil {
	country, netmask := gi.GetCountry("207.171.7.51")
}

// Setup gi6 by opening the optional IPv6 database and then...
country := gi6.GetCountry_v6("2607:f238:2::5")
fmt.Println(country)

Contact

Copyright 2012-2013 Ask Bjørn Hansen ask@develooper.com. The package is MIT licensed, see the LICENSE file. Originally based on example code from blasux@blasux.ru.

Documentation

Overview

Go (cgo) interface to libgeoip

Index

Constants

View Source
const (
	GEOIP_COUNTRY_EDITION            = 1
	GEOIP_REGION_EDITION_REV0        = 7
	GEOIP_CITY_EDITION_REV0          = 6
	GEOIP_ORG_EDITION                = 5
	GEOIP_ISP_EDITION                = 4
	GEOIP_CITY_EDITION_REV1          = 2
	GEOIP_REGION_EDITION_REV1        = 3
	GEOIP_PROXY_EDITION              = 8
	GEOIP_ASNUM_EDITION              = 9
	GEOIP_NETSPEED_EDITION           = 10
	GEOIP_DOMAIN_EDITION             = 11
	GEOIP_COUNTRY_EDITION_V6         = 12
	GEOIP_LOCATIONA_EDITION          = 13
	GEOIP_ACCURACYRADIUS_EDITION     = 14
	GEOIP_CITYCONFIDENCE_EDITION     = 15
	GEOIP_CITYCONFIDENCEDIST_EDITION = 16
	GEOIP_LARGE_COUNTRY_EDITION      = 17
	GEOIP_LARGE_COUNTRY_EDITION_V6   = 18
	GEOIP_ASNUM_EDITION_V6           = 21
	GEOIP_ISP_EDITION_V6             = 22
	GEOIP_ORG_EDITION_V6             = 23
	GEOIP_DOMAIN_EDITION_V6          = 24
	GEOIP_LOCATIONA_EDITION_V6       = 25
	GEOIP_REGISTRAR_EDITION          = 26
	GEOIP_REGISTRAR_EDITION_V6       = 27
	GEOIP_USERTYPE_EDITION           = 28
	GEOIP_USERTYPE_EDITION_V6        = 29
	GEOIP_CITY_EDITION_REV1_V6       = 30
	GEOIP_CITY_EDITION_REV0_V6       = 31
	GEOIP_NETSPEED_EDITION_REV1      = 32
	GEOIP_NETSPEED_EDITION_REV1_V6   = 33
)

GeoIPDBTypes enum in GeoIP.h

View Source
const (
	GEOIP_STANDARD     = 0
	GEOIP_MEMORY_CACHE = 1
	GEOIP_CHECK_CACHE  = 2
	GEOIP_INDEX_CACHE  = 4
	GEOIP_MMAP_CACHE   = 8
)

GeoIPOptions enum in GeoIP.h

Variables

This section is empty.

Functions

func GetRegionName

func GetRegionName(countryCode, regionCode string) string

Returns the region name given a country code and region code

func SetCustomDirectory

func SetCustomDirectory(dir string)

SetCustomDirectory sets the default location for the GeoIP .dat files used when calling OpenType()

Types

type GeoIP

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

func Open

func Open(files ...string) (*GeoIP, error)

Default convenience wrapper around OpenDb

func OpenDb

func OpenDb(files []string, flag int) (*GeoIP, error)

Opens a GeoIP database by filename with specified GeoIPOptions flag. All formats supported by libgeoip are supported though there are only functions to access some of the databases in this API. If you don't pass a filename, it will try opening the database from a list of common paths.

func OpenType

func OpenType(dbType int) (*GeoIP, error)

OpenType opens a specified GeoIP database type in the default location and the 'memory cache' flag. Use OpenTypeFlag() to specify flag.

func OpenTypeFlag

func OpenTypeFlag(dbType int, flag int) (*GeoIP, error)

OpenType opens a specified GeoIP database type in the default location with the specified GeoIPOptions flag. Constants are defined for each database type (for example GEOIP_COUNTRY_EDITION).

func (*GeoIP) GetCountry

func (gi *GeoIP) GetCountry(ip string) (cc string, netmask int)

Takes an IPv4 address string and returns the country code for that IP and the netmask for that IP range.

func (*GeoIP) GetCountry_v6

func (gi *GeoIP) GetCountry_v6(ip string) (cc string, netmask int)

GetCountry_v6 works the same as GetCountry except for IPv6 addresses, be sure to load a database with IPv6 data to get any results.

func (*GeoIP) GetName

func (gi *GeoIP) GetName(ip string) (name string, netmask int)

Works on the ASN, Netspeed, Organization and probably other databases, takes and IP string and returns a "name" and the netmask.

func (*GeoIP) GetNameV6

func (gi *GeoIP) GetNameV6(ip string) (name string, netmask int)

Same as GetName() but for IPv6 addresses.

func (*GeoIP) GetOrg

func (gi *GeoIP) GetOrg(ip string) string

Takes an IPv4 address string and returns the organization name for that IP. Requires the GeoIP organization database.

func (*GeoIP) GetRecord

func (gi *GeoIP) GetRecord(ip string) *GeoIPRecord

Returns the "City Record" for an IP address. Requires the GeoCity(Lite) database - http://www.maxmind.com/en/city

func (*GeoIP) GetRegion

func (gi *GeoIP) GetRegion(ip string) (string, string)

Returns the country code and region code for an IP address. Requires the GeoIP Region database.

type GeoIPRecord

type GeoIPRecord struct {
	CountryCode   string
	CountryCode3  string
	CountryName   string
	Region        string
	City          string
	PostalCode    string
	Latitude      float32
	Longitude     float32
	MetroCode     int
	AreaCode      int
	CharSet       int
	ContinentCode string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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