geoip

package module
v0.0.0-...-7ce4b3d Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2021 License: BSD-3-Clause Imports: 13 Imported by: 6

README

geoip go library

Get the country code for an IP address using the tor's geoip database.

example

Needs the path of the geoip database in the initialization. For example in debian it is provided by tor-geoipdb and the path is /usr/share/tor/geoip and /usr/share/tor/geoip6.

geo, err := geoip.New("/usr/share/tor/geoip", "/usr/share/tor/geoip6")
if err != nil {
	// Handle error
}

ip := net.ParseIP("12.13.14.15")
country, ok := geo.GetCountryByAddr(ip)
if !ok {
	fmt.Println("Not found any country for the IP")
} else {
	fmt.Println("The ip corresponds to the country:", country)
}

Documentation

Overview

This code is for loading database data that maps ip addresses to countries for collecting and presenting statistics on snowflake use that might alert us to censorship events.

The functions here are heavily based off of how tor maintains and searches their geoip database

The tables used for geoip data must be structured as follows:

Recognized line format for IPv4 is:

INTIPLOW,INTIPHIGH,CC
    where INTIPLOW and INTIPHIGH are IPv4 addresses encoded as big-endian 4-byte unsigned
    integers, and CC is a country code.

Note that the IPv4 line format

"INTIPLOW","INTIPHIGH","CC","CC3","COUNTRY NAME"

is not currently supported.

Recognized line format for IPv6 is:

IPV6LOW,IPV6HIGH,CC
    where IPV6LOW and IPV6HIGH are IPv6 addresses and CC is a country code.

It also recognizes, and skips over, blank lines and lines that start with '#' (comments).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Geoip

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

func New

func New(geoIPv4DBpath, geoIPv6DBpath string) (*Geoip, error)

New creates a new Geoip struct loading the content of the files at geoIPv4DBpath and geoIPv6DBpath

func (*Geoip) GetCountryByAddr

func (g *Geoip) GetCountryByAddr(ip net.IP) (string, bool)

GetCountryByAddr returns the country location of an IP address, and a boolean value that indicates whether the IP address was present in the geoip database

Jump to

Keyboard shortcuts

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