Movatterモバイル変換


[0]ホーム

URL:


Notice  The highest tagged major version isv2.

maxminddb

packagemodule
v1.13.1Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2024 License:ISCImports:12Imported by:716

Details

Repository

github.com/oschwald/maxminddb-golang

Links

README

MaxMind DB Reader for Go

GoDoc

This is a Go reader for the MaxMind DB format. Although this can be used toreadGeoLite2 andGeoIP2 databases,geoip2 provides a higher-levelAPI for doing so.

This is not an official MaxMind API.

Installation

go get github.com/oschwald/maxminddb-golang

Usage

See GoDoc fordocumentation and examples.

Examples

SeeGoDoc orexample_test.go for examples.

Contributing

Contributions welcome! Please fork the repository and open a pull requestwith your changes.

License

This is free software, licensed under the ISC License.

Documentation

Overview

Package maxminddb provides a reader for the MaxMind DB file format.

Index

Examples

Constants

View Source
const (// NotFound is returned by LookupOffset when a matched root record offset// cannot be found.NotFound = ^uintptr(0))

Variables

This section is empty.

Functions

funcSkipAliasedNetworksadded inv1.8.0

func SkipAliasedNetworks(networks *Networks)

SkipAliasedNetworks is an option for Networks and NetworksWithin thatmakes them not iterate over aliases of the IPv4 subtree in an IPv6database, e.g., ::ffff:0:0/96, 2001::/32, and 2002::/16.

You most likely want to set this. The only reason it isn't the defaultbehavior is to provide backwards compatibility to existing users.

Types

typeInvalidDatabaseErroradded inv1.0.0

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

InvalidDatabaseError is returned when the database contains invalid dataand cannot be parsed.

func (InvalidDatabaseError)Erroradded inv1.0.0

typeMetadata

type Metadata struct {Description              map[string]string `maxminddb:"description"`DatabaseTypestring            `maxminddb:"database_type"`Languages                []string          `maxminddb:"languages"`BinaryFormatMajorVersionuint              `maxminddb:"binary_format_major_version"`BinaryFormatMinorVersionuint              `maxminddb:"binary_format_minor_version"`BuildEpochuint              `maxminddb:"build_epoch"`IPVersionuint              `maxminddb:"ip_version"`NodeCountuint              `maxminddb:"node_count"`RecordSizeuint              `maxminddb:"record_size"`}

Metadata holds the metadata decoded from the MaxMind DB file. In particularit has the format version, the build time as Unix epoch time, the databasetype and description, the IP version supported, and a slice of the naturallanguages included.

typeNetworksadded inv1.0.0

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

Networks represents a set of subnets that we are iterating over.

func (*Networks)Erradded inv1.0.0

func (n *Networks) Err()error

Err returns an error, if any, that was encountered during iteration.

func (*Networks)Networkadded inv1.0.0

func (n *Networks) Network(resultany) (*net.IPNet,error)

Network returns the current network or an error if there is a problemdecoding the data for the network. It takes a pointer to a result value todecode the network's data into.

func (*Networks)Nextadded inv1.0.0

func (n *Networks) Next()bool

Next prepares the next network for reading with the Network method. Itreturns true if there is another network to be processed and false if thereare no more networks or if there is an error.

typeNetworksOptionadded inv1.8.0

type NetworksOption func(*Networks)

NetworksOption are options for Networks and NetworksWithin.

typeReader

type Reader struct {MetadataMetadata// contains filtered or unexported fields}

Reader holds the data corresponding to the MaxMind DB file. Its only publicfield is Metadata, which contains the metadata from the MaxMind DB file.

All of the methods on Reader are thread-safe. The struct may be safelyshared across goroutines.

funcFromBytes

func FromBytes(buffer []byte) (*Reader,error)

FromBytes takes a byte slice corresponding to a MaxMind DB file and returnsa Reader structure or an error.

funcOpen

func Open(filestring) (*Reader,error)

Open takes a string path to a MaxMind DB file and returns a Readerstructure or an error. The database file is opened using a memory mapon supported platforms. On platforms without memory map support, suchas WebAssembly or Google App Engine, the database is loaded into memory.Use the Close method on the Reader object to return the resources to the system.

func (*Reader)Close

func (r *Reader) Close()error

Close returns the resources used by the database to the system.

func (*Reader)Decodeadded inv1.0.0

func (r *Reader) Decode(offsetuintptr, resultany)error

Decode the record at |offset| into |result|. The result value pointed tomust be a data value that corresponds to a record in the database. This mayinclude a struct representation of the data, a map capable of holding thedata or an empty any value.

If result is a pointer to a struct, the struct need not include a fieldfor every value that may be in the database. If a field is not present inthe structure, the decoder will not decode that field, reducing the timerequired to decode the record.

As a special case, a struct field of type uintptr will be used to capturethe offset of the value. Decode may later be used to extract the storedvalue from the offset. MaxMind DBs are highly normalized: for example inthe City database, all records of the same country will reference asingle representative record for that country. This uintptr behavior allowsclients to leverage this normalization in their own sub-record caching.

func (*Reader)Lookup

func (r *Reader) Lookup(ipnet.IP, resultany)error

Lookup retrieves the database record for ip and stores it in the valuepointed to by result. If result is nil or not a pointer, an error isreturned. If the data in the database record cannot be stored in resultbecause of type differences, an UnmarshalTypeError is returned. If thedatabase is invalid or otherwise cannot be read, an InvalidDatabaseErroris returned.

Example (Interface)

This example demonstrates how to decode to an any.

db, err := maxminddb.Open("test-data/test-data/GeoIP2-City-Test.mmdb")if err != nil {log.Fatal(err)}defer db.Close()ip := net.ParseIP("81.2.69.142")var record anyerr = db.Lookup(ip, &record)if err != nil {log.Panic(err)}fmt.Printf("%v", record)//nolint:lll
Output:map[city:map[geoname_id:2643743 names:map[de:London en:London es:Londres fr:Londres ja:ロンドン pt-BR:Londres ru:Лондон]] continent:map[code:EU geoname_id:6255148 names:map[de:Europa en:Europe es:Europa fr:Europe ja:ヨーロッパ pt-BR:Europa ru:Европа zh-CN:欧洲]] country:map[geoname_id:2635167 iso_code:GB names:map[de:Vereinigtes Königreich en:United Kingdom es:Reino Unido fr:Royaume-Uni ja:イギリス pt-BR:Reino Unido ru:Великобритания zh-CN:英国]] location:map[accuracy_radius:10 latitude:51.5142 longitude:-0.0931 time_zone:Europe/London] registered_country:map[geoname_id:6252001 iso_code:US names:map[de:USA en:United States es:Estados Unidos fr:États-Unis ja:アメリカ合衆国 pt-BR:Estados Unidos ru:США zh-CN:美国]] subdivisions:[map[geoname_id:6269131 iso_code:ENG names:map[en:England es:Inglaterra fr:Angleterre pt-BR:Inglaterra]]]]
Example (Struct)

This example shows how to decode to a struct.

db, err := maxminddb.Open("test-data/test-data/GeoIP2-City-Test.mmdb")if err != nil {log.Fatal(err)}defer db.Close()ip := net.ParseIP("81.2.69.142")var record struct {Country struct {ISOCode string `maxminddb:"iso_code"`} `maxminddb:"country"`} // Or any appropriate structerr = db.Lookup(ip, &record)if err != nil {log.Panic(err)}fmt.Print(record.Country.ISOCode)
Output:GB

func (*Reader)LookupNetworkadded inv1.4.0

func (r *Reader) LookupNetwork(ipnet.IP,resultany,) (network *net.IPNet, okbool, errerror)

LookupNetwork retrieves the database record for ip and stores it in thevalue pointed to by result. The network returned is the network associatedwith the data record in the database. The ok return value indicates whetherthe database contained a record for the ip.

If result is nil or not a pointer, an error is returned. If the data in thedatabase record cannot be stored in result because of type differences, anUnmarshalTypeError is returned. If the database is invalid or otherwisecannot be read, an InvalidDatabaseError is returned.

func (*Reader)LookupOffsetadded inv1.0.0

func (r *Reader) LookupOffset(ipnet.IP) (uintptr,error)

LookupOffset maps an argument net.IP to a corresponding record offset in thedatabase. NotFound is returned if no such record is found, and a record mayotherwise be extracted by passing the returned offset to Decode. LookupOffsetis an advanced API, which exists to provide clients with a means to cachepreviously-decoded records.

func (*Reader)Networksadded inv1.0.0

func (r *Reader) Networks(options ...NetworksOption) *Networks

Networks returns an iterator that can be used to traverse all networks inthe database.

Please note that a MaxMind DB may map IPv4 networks into several locationsin an IPv6 database. This iterator will iterate over all of these locationsseparately. To only iterate over the IPv4 networks once, use theSkipAliasedNetworks option.

Example

This example demonstrates how to iterate over all networks in thedatabase.

db, err := maxminddb.Open("test-data/test-data/GeoIP2-Connection-Type-Test.mmdb")if err != nil {log.Fatal(err)}defer db.Close()record := struct {Domain string `maxminddb:"connection_type"`}{}networks := db.Networks(maxminddb.SkipAliasedNetworks)for networks.Next() {subnet, err := networks.Network(&record)if err != nil {log.Panic(err)}fmt.Printf("%s: %s\n", subnet.String(), record.Domain)}if networks.Err() != nil {log.Panic(networks.Err())}
Output:1.0.0.0/24: Cable/DSL1.0.1.0/24: Cellular1.0.2.0/23: Cable/DSL1.0.4.0/22: Cable/DSL1.0.8.0/21: Cable/DSL1.0.16.0/20: Cable/DSL1.0.32.0/19: Cable/DSL1.0.64.0/18: Cable/DSL1.0.128.0/17: Cable/DSL2.125.160.216/29: Cable/DSL67.43.156.0/24: Cellular80.214.0.0/20: Cellular96.1.0.0/16: Cable/DSL96.10.0.0/15: Cable/DSL96.69.0.0/16: Cable/DSL96.94.0.0/15: Cable/DSL108.96.0.0/11: Cellular149.101.100.0/28: Cellular175.16.199.0/24: Cable/DSL187.156.138.0/24: Cable/DSL201.243.200.0/24: Corporate207.179.48.0/20: Cellular216.160.83.56/29: Corporate2003::/24: Cable/DSL

func (*Reader)NetworksWithinadded inv1.7.0

func (r *Reader) NetworksWithin(network *net.IPNet, options ...NetworksOption) *Networks

NetworksWithin returns an iterator that can be used to traverse all networksin the database which are contained in a given network.

Please note that a MaxMind DB may map IPv4 networks into several locationsin an IPv6 database. This iterator will iterate over all of these locationsseparately. To only iterate over the IPv4 networks once, use theSkipAliasedNetworks option.

If the provided network is contained within a network in the database, theiterator will iterate over exactly one network, the containing network.

Example

This example demonstrates how to iterate over all networks in thedatabase which are contained within an arbitrary network.

db, err := maxminddb.Open("test-data/test-data/GeoIP2-Connection-Type-Test.mmdb")if err != nil {log.Fatal(err)}defer db.Close()record := struct {Domain string `maxminddb:"connection_type"`}{}_, network, err := net.ParseCIDR("1.0.0.0/8")if err != nil {log.Panic(err)}networks := db.NetworksWithin(network, maxminddb.SkipAliasedNetworks)for networks.Next() {subnet, err := networks.Network(&record)if err != nil {log.Panic(err)}fmt.Printf("%s: %s\n", subnet.String(), record.Domain)}if networks.Err() != nil {log.Panic(networks.Err())}
Output:1.0.0.0/24: Cable/DSL1.0.1.0/24: Cellular1.0.2.0/23: Cable/DSL1.0.4.0/22: Cable/DSL1.0.8.0/21: Cable/DSL1.0.16.0/20: Cable/DSL1.0.32.0/19: Cable/DSL1.0.64.0/18: Cable/DSL1.0.128.0/17: Cable/DSL

func (*Reader)Verifyadded inv1.0.0

func (r *Reader) Verify()error

Verify checks that the database is valid. It validates the search tree,the data section, and the metadata section. This verifier is stricter thanthe specification and may return errors on databases that are readable.

typeUnmarshalTypeErroradded inv1.0.0

type UnmarshalTypeError struct {Typereflect.TypeValuestring}

UnmarshalTypeError is returned when the value in the database cannot beassigned to the specified data type.

func (UnmarshalTypeError)Erroradded inv1.0.0

func (eUnmarshalTypeError) Error()string

Source Files

View all Source files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp