geoip

package module
v0.0.0-...-da13074 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2013 License: MIT Imports: 7 Imported by: 1

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
)

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)

Opens a GeoIP database by filename, all formats supported by libgeoip are supported though there are only functions to access some of the databases in this API. The database is opened in MEMORY_CACHE mode, if you need to optimize for memory instead of performance you should change this. 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. 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
	// DMACode       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