geoiplegacy

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 24, 2024 License: BSD-2-Clause Imports: 7 Imported by: 0

README

geoip-legacy

A port of libGeoIP from C to pure Go. It supports IPv4 and IPv6 country databases.

Example usage

For extensive examples, see geoip_test.go, but here is a relatively simple example. GetCountryByAddr supports IP addresses and can use the net package in the standard library to resolve a domain to an IP and look up the IP in the database.

IPv4-only DB example:

db, err := geoiplegacy.OpenDB("/usr/share/GeoIP/GeoIP.dat", nil)
if err != nil {
	panic(err)
}
country, err = db.GetCountryByAddr("8.8.8.8")
if err != nil {
	panic(err)
}
fmt.Printf("Country code: %s\nCountry name: %s\n", country.Code, country.NameUTF8)

IPv6-only DB example:

db, err := geoiplegacy.OpenDB("/usr/share/GeoIP/GeoIPv6.dat", &GeoIPOptions{
	IsIPv6: true,
})
if err != nil {
	panic(err)
}
country, err = db.GetCountryByAddr("2801::1")
if err != nil {
	panic(err)
}
fmt.Printf("Country code: %s\nCountry name: %s\n", country.Code, country.NameUTF8)

Combined DB example usage

The above examples are only usable for looking up either IPv4 or IPv6 addresses. To be able to look up both, you can use a CombinedDB. Example usage:

db, err := geoiplegacy.OpenCombinedDB("/usr/share/GeoIP/GeoIP.dat", "/usr/share/GeoIP/GeoIPv6.dat")
if err != nil {
	panic(err)
}
country, err = db.GetCountryByAddr("8.8.8.8")
// ...
country, err = db.GetCountryByAddr("2801::1")
// ...
country, err = db.GetCountryByAddr("google.com")
// ...

A regular DB can also handle domains, but a domain may or may not resolve to an IPv6 address, so a CombinedDB is recommended.

Documentation

Index

Constants

View Source
const (
	TeredoBit          ExtFlags = 0
	Charset_ISO_8859_1 Charset  = 0
	Charset_UTF_8      Charset  = 1

	CountryBegin         = 16776960
	LargeCountryBegin    = 16515072
	StateBeginRev0       = 16700000
	StateBeginRev1       = 16000000
	StructureInfoMaxSize = 20
	DBInfoMaxSize        = 100
	MaxOrgRecordLength   = 300
	USOffset             = 1
	CanadaOffset         = 677
	WorldOffset          = 1353
	FIPSRange            = 360

	SegmentRecordLength      = 3
	LargeSegmentRecordLength = 4
	StandardRecordLength     = 3
	OrgRecordLength          = 4
	MaxRecordLength          = 4
)

Variables

View Source
var (
	ErrEmptyIPv4Path = errors.New("empty IPv4 DB path not allowed in CombinedDB initialization")
	ErrEmptyIPv6Path = errors.New("empty IPv6 DB path not allowed in CombinedDB initialization")
)
View Source
var (
	ErrNoSegments       = errors.New("database has no segments, file may be corrupt")
	ErrInvalidCountryID = errors.New("invalid country id")
	ErrInvalidIP        = errors.New("invalid IP address")
	ErrNotIPv6          = errors.New("expected IPv6, got IPv4")
	ErrNegativeIndex    = errors.New("index size is negative, database may be corrupt")
	ErrSegmentNotRead   = errors.New("didn't read full segment")
)

Functions

This section is empty.

Types

type Charset

type Charset int

type CombinedDB

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

CombinedDB is a wrapper around the DB struct, allowing automatic IPv4 and IPv6 GeoIP usage

func OpenCombinedDB

func OpenCombinedDB(path4, path6 string) (*CombinedDB, error)

OpenCombinedDB opens the given IPv4 and IPv6 databases and returns a combined database for automatically determining which database file to use when looking up an address. It will return an error if either path is an empty string, and any errors that occured when opening the databases

func (*CombinedDB) Close

func (db *CombinedDB) Close() error

Close closes the databases and returns any errors that occured

func (*CombinedDB) DBv4Path

func (db *CombinedDB) DBv4Path() string

DBv4Path returns the path to the IPv4 database file

func (*CombinedDB) DBv6Path

func (db *CombinedDB) DBv6Path() string

DBv4Path returns the path to the IPv6 database file

func (*CombinedDB) GetCountryByAddr

func (db *CombinedDB) GetCountryByAddr(addr string) (*CountryResult, error)

GetCountryByAddr looks up the country of the given domain or IP address string and returns the result, as well as any errors

func (*CombinedDB) GetCountryByIP

func (db *CombinedDB) GetCountryByIP(ip net.IP) (*CountryResult, error)

GetCountryByIP looks up the country of the given IP address and returns the result, as well as any errors

type CountryResult

type CountryResult struct {
	Code      string
	Code3     string
	NameASCII string
	NameUTF8  string
	Continent string
}

CountryResult is the result of scanning the database for the location of a network address

type DB

type DB struct {
	Type         DBType
	ModTime      time.Time
	Options      *GeoIPOptions
	Size         int64
	RecordLength uint8
	Charset      Charset
	// contains filtered or unexported fields
}

DB represents a legacy GeoIP database, usually having a .dat extension

func OpenDB

func OpenDB(dbPath string, options *GeoIPOptions) (*DB, error)

OpenDB opens and returns the MaxMind GeoIP v1 database, returning the database and any errors

func (*DB) Close

func (db *DB) Close() error

Close closes the database file if it is not nil

func (*DB) GetCountryByAddr

func (db *DB) GetCountryByAddr(addr string) (*CountryResult, error)

GetCountryByAddr scans the database for the given IP address or domain, returning the result. If a domain is passed to it, it tries to resolve it to an IP, then looks that up.

func (*DB) GetCountryByIP

func (db *DB) GetCountryByIP(ip net.IP) (*CountryResult, error)

GetCountryByIP scans the database for the given IP address, returning the result

func (*DB) GetIndexSize

func (db *DB) GetIndexSize() int32

GetIndexSize returns the size of the database index. If it is negative, something has gone wrong during a read

func (*DB) IsIPv6

func (db *DB) IsIPv6() bool

func (*DB) Path

func (db *DB) Path() string

type DBType

type DBType int
const (
	NumDBTypes = (AccuracyRadiusEditionV6 + 1)

	// GeoIPDBTypes enum
	InvalidVersion DBType = iota - 1
	CountryEdition
	CityEditionRev1
	RegionEditionRev1
	ISPEdition
	OrgEdition
	CityEditionRev0
	RegionEditionRev0
	ProxyEdition
	ASNEdition
	NetSpeedEdition
	DomainEdition
	CountryEditionV6
	LocationAEdition
	AccuracyRadiusEdition
	CityConfidenceEdition     // unsupported
	CityConfidenceDistEdition // unsupported
	LargeCountryEdition
	LargeCountryEditionV6
	CityConfidenceDistISPOrgEdition // unused but gaps are not allowed
	CCMCountryEdition               // unused but gaps are not allowed
	ASNEditionV6
	ISPEditionV6
	OrgEditionV6
	DomainEditionV6
	LocationAEditionV6
	RegistrarEdition
	RegistrarEditionV6
	UserTypeEdition
	UserTypeEditionV6
	CityEditionRev1V6
	CityEditionRev0V6
	NetSpeedEditionRev1
	NetSpeedEditionRev1V6
	CountryConfEdition
	CityConfEdition
	RegionConfEdition
	PostalConfEdition
	AccuracyRadiusEditionV6
)

func (DBType) String

func (dt DBType) String() string

type ExtFlags

type ExtFlags uint

type GeoIPDB

type GeoIPDB interface {
	GetCountryByIP(ip net.IP) (*CountryResult, error)
	GetCountryByAddr(addr string) (*CountryResult, error)
	Close() error
}

GeoIPDB can be used by functions for geoip-legacy DB and CombinedDB

type GeoIPOptions

type GeoIPOptions struct {
	IsIPv6 bool
	Teredo bool
}

GeoIPOptions are used when reading the database file

type NetSpeedValue

type NetSpeedValue int
const (
	// GeoIPNetspeedValues enum
	UnknownSpeed NetSpeedValue = iota
	DialupSpeed
	CableDSLSpeed
	CorporateSpeed
)

type ProxyType

type ProxyType int
const (
	// GeoIPProxyTypes enum
	AnonProxy ProxyType = iota + 1
	HTTPXForwardedForProxy
)

Jump to

Keyboard shortcuts

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