geo

package
v0.7.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: Apache-2.0 Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package geo looks a client's country up in a country table (design 0011 §5.1): the sorted IP ranges, each with a country code, that the operator compiles a country database (MMDB) into and sends to engines (inkwall.geo.v1.CountryTable).

A Table holds the ranges in pointer-free slices, one per family, so the garbage collector never scans them, and an index on the top 16 bits of the address narrows the binary search to a few steps: a lookup takes well under 150 ns at the bound of 32 MiB, with no allocation. A Table is checked completely when it is loaded (Check), and trusted after: a bad table can only give a wrong country, never a crash.

Index

Constants

View Source
const (
	// MaxRangeBytes bounds the ranges: 6 bytes per IPv4 range (a start and
	// a code) and 18 per IPv6 range. 5.5 million IPv4 ranges, or 1.8
	// million IPv6 ranges, or any mix in between.
	MaxRangeBytes = 32 << 20
	// MaxAliases bounds the IPv6 prefixes that alias the IPv4 table.
	// MaxMind's databases have three.
	MaxAliases = 256
	// MaxDatabaseType bounds the database type, in bytes.
	MaxDatabaseType = 256
	// MaxTableBytes bounds a serialized CountryTable: the ranges and their
	// framing (field tags, lengths, the metadata, the aliases).
	MaxTableBytes = MaxRangeBytes + 64<<10
	// MaxCompressedBytes bounds the gzipped table, as it is sent and
	// stored: 16 Secrets of 900 KiB (pkg/bundlesecret).
	MaxCompressedBytes = 16 * 900 << 10
)

Bounds of a table (design 0011 §4.2).

Variables

View Source
var ErrHash = errors.New("country table: the bytes don't match the table hash")

ErrHash is returned when a table's bytes don't match the hash that names them.

Functions

func Check

func Check(m *geov1.CountryTable) error

Check checks a table completely: the lengths match, the starts are strictly increasing from zero, the codes are two letters or none, the aliases are prefixes of /1 to /96 outside ::/96, sorted and disjoint, and the table is within the bounds.

func Encode

func Encode(m *geov1.CountryTable) (compressed []byte, size int, err error)

Encode checks m and returns it serialized and gzipped, as it is sent and stored, and its serialized size (what a bundle's GeoTableRef names). The same m gives the same bytes, so the same database gives the same table hash.

func Hash

func Hash(compressed []byte) [sha256.Size]byte

Hash returns the table hash of a gzipped table: its SHA-256.

Types

type Country

type Country uint16

Country is a two-letter country code, (c1 << 8) | c2 with each letter 'A' to 'Z': DE is 'D'<<8 | 'E'. The zero Country is no country.

const NoCountry Country = 0

NoCountry is the Country of an address the table has no country for.

func ParseCountry

func ParseCountry(s string) (Country, bool)

ParseCountry returns the Country of s, two upper-case ASCII letters, and whether s is one. Lower case is refused rather than folded, as the CRD refuses it.

func (Country) String

func (c Country) String() string

String returns the two letters of c, "" for no country.

type Result

type Result uint8

Result is what a lookup found.

const (
	// ResultCountry: the table maps the address to a country.
	ResultCountry Result = iota
	// ResultPrivate: no country, and the address is loopback, link-local,
	// private (RFC 1918, RFC 4193), shared (RFC 6598, CGNAT) or
	// unspecified, or there is no valid address at all. Geo never denies
	// it.
	ResultPrivate
	// ResultUnknown: no country for a public address.
	ResultUnknown
	// ResultNoTable: there is no table to look in.
	ResultNoTable
)

Results of a lookup (design 0011 §2.2).

func (Result) String

func (r Result) String() string

String returns the result as the metrics label it (design 0011 §8).

type Set

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

Set is a set of countries: a bit for each of the 26 × 26 two-letter codes, 88 bytes, so a policy's deny or allow list is one bit test. The zero Set is empty.

func (*Set) Add

func (s *Set) Add(c Country)

Add adds c to s. No country is never added.

func (*Set) Has

func (s *Set) Has(c Country) bool

Has reports whether c is in s. No country is in no Set.

func (*Set) Len

func (s *Set) Len() int

Len is the number of countries in s.

type Table

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

Table is a checked country table. It is immutable and safe for concurrent use. The nil Table holds no countries (ResultNoTable).

func Decode

func Decode(compressed, hash []byte, size uint64) (*Table, error)

Decode loads a gzipped table: it checks the bytes against hash, the table hash a signed bundle names, before it decompresses them, then decompresses through size, the serialized size the bundle names, and checks the table.

func FromProto

func FromProto(m *geov1.CountryTable) (*Table, error)

FromProto checks m and returns its Table. The Table keeps m's slices: m must not change after.

func Unmarshal

func Unmarshal(raw []byte) (*Table, error)

Unmarshal loads a serialized, uncompressed table and checks it.

func (*Table) Built

func (t *Table) Built() time.Time

Built is when the database was built.

func (*Table) Bytes

func (t *Table) Bytes() int

Bytes is the table's size in memory: its ranges and their index.

func (*Table) DatabaseType

func (t *Table) DatabaseType() string

DatabaseType is the type of the database the table was compiled from, e.g. GeoLite2-Country.

func (*Table) Hash

func (t *Table) Hash() [sha256.Size]byte

Hash returns the table hash: the SHA-256 of the gzipped table, as Decode checked it. Zero for a Table made by FromProto.

func (*Table) Lookup

func (t *Table) Lookup(a netip.Addr) (Country, Result)

Lookup returns a's country and what the lookup found. A country found always wins, even for a private address a custom database maps. An IPv4-mapped IPv6 address is its IPv4 address, and an address in ::/96 or in one of the table's aliases (MaxMind's ::ffff:0:0/96, 2001::/32 and 2002::/16) is looked up in the IPv4 table by the 32 bits after the prefix, as the database's own reader resolves it.

func (*Table) Ranges

func (t *Table) Ranges() int

Ranges is the number of ranges, IPv4 and IPv6.

Jump to

Keyboard shortcuts

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