Documentation
¶
Overview ¶
Package geoip reads MaxMind-DB files — the free DB-IP lite country and ASN databases, or the operator's own — with the standard library alone, and answers "where is this address from" for the agent (rules and exemptions by country or AS, SPEC §5.3) and for the controller (the map, the facets). Public: the controller imports it, so both sides read the same files the same way.
Package geoip answers "where is this address from" — a country and an autonomous system — from MaxMind-DB files on the controller's disk (SPEC §5.1: enrichment from local databases only, plain files updated out of band). The reader is this file: the format is small, and a dependency for it would be larger than the code.
Index ¶
- type AS
- type DB
- type Network
- type Origin
- type Resolver
- func (r *Resolver) CityStatus() (kind string, built time.Time, loaded bool)
- func (r *Resolver) Country(code string, max int) (prefixes []netip.Prefix, count int, err error)
- func (r *Resolver) Index() error
- func (r *Resolver) Indexed() bool
- func (r *Resolver) Load(countryPath, asnPath string) error
- func (r *Resolver) LoadCity(path string) error
- func (r *Resolver) Lookup(value string) Origin
- func (r *Resolver) LookupAddr(ip netip.Addr) Origin
- func (r *Resolver) Network(asn uint32, max int) (Network, error)
- func (r *Resolver) Ready() bool
- func (r *Resolver) Search(q string, max int) []AS
- func (r *Resolver) Status() (country, asn string, built time.Time)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AS ¶
AS is one autonomous system as the ASN database knows it: its number, its name, and how many prefixes it announces there. What a person picks a network from — by name or by number — without knowing either exactly.
type DB ¶
type DB struct {
// Type describes the database ("DBIP-Country-Lite", "GeoLite2-ASN"),
// Built when it was made — shown so an operator sees what they run.
Type string
Built int64
// contains filtered or unexported fields
}
DB is one MaxMind-DB file, read whole into memory: the free country and ASN databases are a few megabytes, and a lookup is a tree walk.
func (*DB) Lookup ¶
Lookup returns the record for an address as decoded data — a map for the databases this package reads — or nil when the address is not in the database.
func (*DB) Prefixes ¶
Prefixes walks the whole tree and returns every prefix whose record satisfies keep — how "which ranges belong to this network" is answered from a database that only maps the other way. A full walk of the free ASN database is a few hundred thousand leaves: tens of milliseconds, so callers cache what they ask often. Bounded by max results.
type Network ¶
type Network struct {
ASN uint32
Name string
Prefixes []netip.Prefix // bounded; Count is the true number
Count int
}
Network is what the ASN database holds for one autonomous system.
type Origin ¶
type Origin struct {
Country string // ISO 3166-1 alpha-2, "" when unknown
CountryName string
ASN uint32 // 0 when unknown
ASName string
// From a city database, when one is loaded (the controller's option;
// an agent never needs it): the city and its region, in English, and
// the point the database gives — a rough one on free data.
City string
Region string
Lat, Lon float64
}
Origin is what the databases say about an address.
type Resolver ¶
Resolver answers from a country database and an ASN database, either of which may be missing. Safe for concurrent use; Reload swaps files.
func (*Resolver) CityStatus ¶
CityStatus describes the city database, if one is loaded.
func (*Resolver) Country ¶
Country lists the prefixes the country database places in one country (ISO code), at most max, by walking it; Count is the true number. A country is tens of thousands of leaves at most: a walk, not a cache.
func (*Resolver) Index ¶
Index builds the AS index from the loaded ASN database; a second call on the same database is a no-op. It walks the whole tree, so callers run it off the hot path.
func (*Resolver) Lookup ¶
Lookup resolves an address, or the address of a range (its first). A value that is not an address, or an unknown one, is an empty Origin.
func (*Resolver) LookupAddr ¶
LookupAddr resolves one address — the hot path: no parsing.
func (*Resolver) Network ¶
Network lists an autonomous system's prefixes, at most max, by walking the ASN database; the answer is cached for an hour.