geoip

package
v1.0.430 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 5 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 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

func BuildTime() (built time.Time, ok bool)

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 Enabled

func Enabled() bool

Enabled reports whether a GeoIP database is loaded.

func InitGeoDB

func InitGeoDB(path string) error

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

func LoadError() (msg string, at time.Time, ok bool)

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

func LookupByIP(ip net.IP) (code, name string)

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.

func LookupCachedByIP

func LookupCachedByIP(ip net.IP) (code string, ok bool)

LookupCachedByIP returns the country code only if already cached; it never triggers a new database lookup. Returns ("", false) on a cache miss or a nil IP.

Types

This section is empty.

Jump to

Keyboard shortcuts

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