goiplookup

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 25, 2019 License: MIT Imports: 17 Imported by: 0

README

Golang IP Lookup Library

GoDoc Go Report Card

This library maps an IP address to a country without depending on external geo databases like Maxmind.

Description

The library uses the Regional Internet Registries (APNIC, ARIN, LACNIC, RIPE NCC and AFRINIC) to populate a database with the IP address blocks allocated to a country. It then uses this database, which is refreshed on a daily basis, to lookup country to which an IP address belongs to.

Examples

Examples in examples/ directory.

examples directory also has an example of an HTTP server that uses this library.

How to load data into database?
package main

import (
	"log"
	"net/http"
	"time"

	"github.com/abvarun226/goiplookup"
)

func main() {
	h := goiplookup.New(
		goiplookup.WithDBPath(DBPath),
		goiplookup.WithClient(&http.Client{Timeout: 60 * time.Minute}),
		goiplookup.WithDownloadRIRFiles(),
	)
	defer h.Close()

	if errPop := h.PopulateData(); errPop != nil {
		log.Printf("error populating geoip data: %v", errPop)
	}
}

const (
	// DBPath is the location of bolt db file.
	DBPath = "../my.db"
)
How to look up geo location?
package main

import (
	"flag"
	"fmt"
	"os"

	"github.com/abvarun226/goiplookup"
)

func main() {
	flag.Parse()

	if flag.NArg() <= 0 {
		fmt.Printf("please provide at least one ip address as an argument")
		os.Exit(1)
	}

	ips := flag.Args()

	h := goiplookup.New(
		goiplookup.WithDBPath(DBPath),
	)
	defer h.Close()

	for _, ip := range ips {
		country, err := h.Lookup(ip)
		if err != nil {
			fmt.Printf("error when looking up ip address: %v", err)
		}
		fmt.Printf("%15s : %s\n", ip, country)
	}
}

const (
	// DBPath is the location of bolt db file.
	DBPath = "../my.db"
)

Documentation

Index

Constants

View Source
const (
	// BoltBucketv4 containing ipv4 data
	BoltBucketv4 = "ipv4"
	// BoltBucketv6 containing ipv6 data
	BoltBucketv6 = "ipv6"

	// IPv4 represents ipv4 address
	IPv4 = "ipv4"
	// IPv6 represents ipv6 address
	IPv6 = "ipv6"

	// IPv4ByteCount is the ipv4 byte count
	IPv4ByteCount = 32
	// IPv6ByteCount is the ipv6 byte count
	IPv6ByteCount = 128

	// URLs for each RIR containing geoip data.
	Arin    = "https://ftp.arin.net/pub/stats/arin/delegated-arin-extended-latest"
	RipeNcc = "https://ftp.ripe.net/ripe/stats/delegated-ripencc-extended-latest"
	Apnic   = "https://ftp.apnic.net/stats/apnic/delegated-apnic-extended-latest"
	Afrinic = "https://ftp.apnic.net/stats/afrinic/delegated-afrinic-extended-latest"
	Lacnic  = "https://ftp.apnic.net/stats/lacnic/delegated-lacnic-extended-latest"
)

Constants used in goiplookup.

View Source
const (
	// DefaultDBPath is the default location of bolt db file.
	DefaultDBPath = "/tmp/geoip.db"

	// DefaultTimeout is the default http client timeout.
	DefaultTimeout = 180 * time.Second
)

Variables

View Source
var (
	// GeoIPDataURLs is the string slice containing URL for each RIR.
	GeoIPDataURLs = []string{Arin, RipeNcc, Apnic, Afrinic, Lacnic}
)

Functions

This section is empty.

Types

type Handler

type Handler struct {
	Db   *bolt.DB
	Opts Options
}

Handler struct.

func New

func New(opt ...Option) *Handler

New returns a new handler.

func (*Handler) Close

func (h *Handler) Close()

Close will close the db connection.

func (*Handler) InitializeBuckets

func (h *Handler) InitializeBuckets() error

InitializeBuckets handler initializes the buckets in DB.

func (*Handler) IterateDB

func (h *Handler) IterateDB(bucket string) error

IterateDB iterates over a given bucket in DB.

func (*Handler) Lookup

func (h *Handler) Lookup(ip string) (string, error)

Lookup returns the country code given the ipv4/ipv6 address.

func (*Handler) PopulateData

func (h *Handler) PopulateData() error

PopulateData extracts the geoip data for each RIR and populates the database.

type Option

type Option func(*Options)

Option type.

func WithClient

func WithClient(client *http.Client) Option

WithClient sets HTTP client.

func WithDBPath

func WithDBPath(dbPath string) Option

WithDBPath sets db file location.

func WithDownloadRIRFiles

func WithDownloadRIRFiles() Option

WithDownloadRIRFiles ensures that the rir files are downloaded to populate new geoip data.

type Options

type Options struct {
	DBPath           string
	HTTPClient       *http.Client
	DownloadRIRFiles bool
}

Options struct.

func NewOptions

func NewOptions(options ...Option) Options

NewOptions returns a new Options object.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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