ip2location

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2017 License: LGPL-3.0 Imports: 14 Imported by: 0

README

IP2Location Go Package

This Go package provides a fast lookup of country, region, city, latitude, longitude, ZIP code, time zone, ISP, domain name, connection type, IDD code, area code, weather station code, station name, mcc, mnc, mobile brand, elevation, and usage type from IP address by using IP2Location database. This package uses a file based database available at IP2Location.com. This database simply contains IP blocks as keys, and other information such as country, region, city, latitude, longitude, ZIP code, time zone, ISP, domain name, connection type, IDD code, area code, weather station code, station name, mcc, mnc, mobile brand, elevation, and usage type as values. It supports both IP address in IPv4 and IPv6.

This package can be used in many types of projects such as:

  • select the geographically closest mirror
  • analyze your web server logs to determine the countries of your visitors
  • credit card fraud detection
  • software export controls
  • display native language and currency
  • prevent password sharing and abuse of service
  • geotargeting in advertisement

The database will be updated in monthly basis for the greater accuracy. Free LITE databases are available at https://lite.ip2location.com/ upon registration.

The paid databases are available at https://www.ip2location.com under Premium subscription package.

Installation

go get github.com/ip2location/ip2location-go

Example

package main

import (
	"fmt"
	"github.com/ip2location/ip2location-go"
)

func main() {
	ip2location.Open("./IPV6-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE.BIN")
	ip := "8.8.8.8"
	
	results := ip2location.Get_all(ip)
	
	fmt.Printf("country_short: %s\n", results.Country_short)
	fmt.Printf("country_long: %s\n", results.Country_long)
	fmt.Printf("region: %s\n", results.Region)
	fmt.Printf("city: %s\n", results.City)
	fmt.Printf("isp: %s\n", results.Isp)
	fmt.Printf("latitude: %f\n", results.Latitude)
	fmt.Printf("longitude: %f\n", results.Longitude)
	fmt.Printf("domain: %s\n", results.Domain)
	fmt.Printf("zipcode: %s\n", results.Zipcode)
	fmt.Printf("timezone: %s\n", results.Timezone)
	fmt.Printf("netspeed: %s\n", results.Netspeed)
	fmt.Printf("iddcode: %s\n", results.Iddcode)
	fmt.Printf("areacode: %s\n", results.Areacode)
	fmt.Printf("weatherstationcode: %s\n", results.Weatherstationcode)
	fmt.Printf("weatherstationname: %s\n", results.Weatherstationname)
	fmt.Printf("mcc: %s\n", results.Mcc)
	fmt.Printf("mnc: %s\n", results.Mnc)
	fmt.Printf("mobilebrand: %s\n", results.Mobilebrand)
	fmt.Printf("elevation: %f\n", results.Elevation)
	fmt.Printf("usagetype: %s\n", results.Usagetype)
	fmt.Printf("api version: %s\n", ip2location.Api_version())
	
	ip2location.Close()
}

Dependencies

The complete database is available at http://www.ip2location.com under subscription package.

Copyright (C) 2016 by IP2Location.com, support@ip2location.com

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	MissingFileError            = errors.New("Invalid database file.")
	NotSupportedError           = errors.New("This parameter is unavailable for selected data file. Please upgrade the data file.")
	InvalidAddressError         = errors.New("Invalid IP address.")
	UnsupportedAddressTypeError = errors.New("Unsupported IP address type.")
	NoMatchError                = errors.New("No matching IP range found.")
)
View Source
var NotRunningError = errors.New("DB service not running")

Functions

func ApiVersion

func ApiVersion() string

get api version

Types

type DB

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

func NewDB

func NewDB(r io.ReaderAt) (db *DB, err error)

func OpenDB

func OpenDB(dbpath string) (*DB, error)

initialize the component with the database path

func (*DB) Close

func (db *DB) Close()

func (*DB) Index

func (db *DB) Index(ip *big.Int, t IPType) uint32

func (*DB) Lookup

func (db *DB) Lookup(ip *big.Int, t IPType) (lo, hi uint32)

func (*DB) Query

func (db *DB) Query(ipaddress string, x *Record, mode QueryMode) (err error)

main Query

type DBMeta

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

func (*DBMeta) Date

func (m *DBMeta) Date() time.Time

func (*DBMeta) Has

func (m *DBMeta) Has(t IPType) bool

func (*DBMeta) HasIndex

func (m *DBMeta) HasIndex(t IPType) bool

func (*DBMeta) Indexes

func (m *DBMeta) Indexes(t IPType) (start, end, colsize uint32, max *big.Int)

func (*DBMeta) Read

func (m *DBMeta) Read(r io.ReaderAt) (err error)

func (*DBMeta) Type

func (m *DBMeta) Type() DBType

type DBType

type DBType uint8
const (
	DB1 DBType = iota + 1
	DB2
	DB3
	DB4
	DB5
	DB6
	DB7
	DB8
	DB9
	DB10
	DB11
	DB12
	DB13
	DB14
	DB15
	DB16
	DB17
	DB18
	DB19
	DB20
	DB21
	DB22
	DB23
	DB24
)

type FileDB

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

func (*FileDB) Close

func (fdb *FileDB) Close()

func (*FileDB) Query

func (fd *FileDB) Query(ip string, r *Record, mode QueryMode) error

type IP2LocationDB

type IP2LocationDB interface {
	Query(string, *Record, QueryMode) error
	Close()
}

func NewDirDB

func NewDirDB(path string, mmap bool) (IP2LocationDB, error)

func NewFileDB

func NewFileDB(path string, mmap bool) (IP2LocationDB, error)

type IPType

type IPType int
const (
	IPv4 IPType = 4
	IPv6 IPType = 6
)

func ParseIP

func ParseIP(ips string) (ip *big.Int, ipt IPType)

get IP type and calculate IP number; calculates index too if exists

type MultiDB

type MultiDB []IP2LocationDB

func (MultiDB) Close

func (md MultiDB) Close()

func (MultiDB) Query

func (md MultiDB) Query(ip string, r *Record, mode QueryMode) error

type PoolDB

type PoolDB struct {
	Factory func() (IP2LocationDB, error)
	// contains filtered or unexported fields
}

PoolDB allows for db pooling

func (*PoolDB) Close

func (p *PoolDB) Close()

func (*PoolDB) Query

func (p *PoolDB) Query(ip string, r *Record, m QueryMode) error

type QueryMode

type QueryMode uint32
const (
	QueryCountryCode        QueryMode = 0x00001
	QueryCountryName        QueryMode = 0x00002
	QueryRegion             QueryMode = 0x00004
	QueryCity               QueryMode = 0x00008
	QueryISP                QueryMode = 0x00010
	QueryLatitude           QueryMode = 0x00020
	QueryLongitude          QueryMode = 0x00040
	QueryDomain             QueryMode = 0x00080
	QueryZipCode            QueryMode = 0x00100
	QueryTimeZone           QueryMode = 0x00200
	QueryNetSpeed           QueryMode = 0x00400
	QueryIDDCode            QueryMode = 0x00800
	QueryAreaCode           QueryMode = 0x01000
	QueryWeatherStationCode QueryMode = 0x02000
	QueryWeatherStationName QueryMode = 0x04000
	QueryMCC                QueryMode = 0x08000
	QueryMNC                QueryMode = 0x10000
	QueryMobileBrand        QueryMode = 0x20000
	QueryElevation          QueryMode = 0x40000
	QueryUsageType          QueryMode = 0x80000
	QueryAll                QueryMode = QueryCountryCode | QueryCountryName | QueryRegion | QueryCity | QueryISP | QueryLatitude | QueryLongitude | QueryDomain | QueryZipCode | QueryTimeZone | QueryNetSpeed | QueryIDDCode | QueryAreaCode | QueryWeatherStationCode | QueryWeatherStationName | QueryMCC | QueryMNC | QueryMobileBrand | QueryElevation | QueryUsageType
)

type Record

type Record struct {
	CountryCode        string
	CountryName        string
	Region             string
	City               string
	ISP                string
	Latitude           float32
	Longitude          float32
	Domain             string
	ZipCode            string
	Timezone           string
	NetSpeed           string
	IDDCode            string
	Areacode           string
	WeatherStationCode string
	WeatherStationName string
	MCC                string
	MNC                string
	MobileBrand        string
	Elevation          float64
	UsageType          string
}

func (Record) Print

func (x Record) Print()

for debugging purposes

type SafeDB

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

func NewSafeDB

func NewSafeDB(db IP2LocationDB) *SafeDB

func (*SafeDB) Close

func (d *SafeDB) Close()

func (*SafeDB) Query

func (d *SafeDB) Query(ip string, r *Record, q QueryMode) error

Jump to

Keyboard shortcuts

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