Documentation
¶
Overview ¶
Package geoip is the GeoIP country-lookup engine: a local MaxMind GeoLite2 (.mmdb) reader with an in-memory IP→country cache. It performs no DNS resolution and no SSRF/private-range filtering — callers resolve a host to a public net.IP first (see resolveHost in package main, which owns the shared SSRF check) and then call LookupByIP. This keeps the engine a true leaf with no dependency on the rest of Culvert (extracted under ADR-0002, option F).
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildTime ¶ added in v1.0.207
BuildTime returns the loaded database's MaxMind build timestamp — embedded in the .mmdb file's own metadata, not the file's mtime — and whether a database is currently loaded. Culvert never auto-refreshes this file, and GeoLite2 country data degrades in accuracy over time, so callers use this to surface staleness to the operator (there is otherwise no signal that the loaded database predates a country's IP allocations).
func InitGeoDB ¶
InitGeoDB opens the GeoLite2-Country .mmdb file.
**Call once at startup, and only at startup.** A failure is recorded for LoadError and left for the caller to log; it is cleared by a subsequent successful call so a fixed path stops reporting a stale error.
GEO-1 (CHAOS-64). This function used to claim that "subsequent calls replace the open reader atomically". The POINTER swap is atomic; the CLOSE is not safe, and the difference is a process kill rather than an error. geoCache.lookup copies `db := geoDB` under the read lock, RELEASES it, and only then calls db.Country(ip) — while the swap here calls old.Close() immediately after releasing the write lock. geoip2's Close reaches maxminddb's, which **munmaps the backing buffer** (reader_mmap.go), so an in-flight lookup holding the old reader reads unmapped memory: a SIGSEGV/SIGBUS that Go's recover() cannot catch, that crashguard.go never sees, and that leaves no log line — a total gateway outage with no evidence.
It is NOT reachable today: loadGeoIP (geoip_startup.go) is the only production caller and runs during startup, before the proxy listener serves. The hazard is that a reload path is a natural next feature — CLAUDE.md mandates a GUI surface for every config option — and the old comment told whoever adds it that the swap was already safe. It is not. Before adding any runtime reload, the reader lifetime must be made safe first: hold the read lock across db.Country, or reference-count the reader, or simply never close the old one (a leaked mapping is strictly cheaper than a crash). Recorded in roadmap/CHAOS-ENGINEERING-REVIEW.md §34.7 rather than fixed here, because building unreachable lifetime machinery for a path with no caller is the wrong trade — the trap was the claim, and the claim is what is corrected.
func LoadError ¶ added in v1.0.207
LoadError reports the most recent InitGeoDB failure and when it occurred. ok is false when no database was ever configured, or the last attempt (if any) succeeded — i.e. there is nothing for an operator to act on.
func LookupByIP ¶
LookupByIP returns the ISO country code and full name for an already-resolved public IP. Returns ("", "") when GeoIP is disabled, the IP is nil, or the lookup fails. Host resolution + SSRF filtering is the caller's responsibility.
Types ¶
This section is empty.