Introducing a new way to review your PRs.

Read the announcement

techarohq / anubis

Weighs the soul of incoming HTTP requests to stop AI crawlers

View on GitHub

19 documents · Updated Nov 23, 2026

docs / geoip-enrichment

GeoIP enrichment and database updates

GeoIP enrichment turns a client IP address into country and autonomous system number (ASN) context. Policy checks and optional ASN logging use this context, while a managed database life cycle keeps the data current without replacing a file that an active lookup may still use.

This page is a child of Network and identity intelligence. For other address and host signals, see DNS, reputation, and service signals.

GeoIP role

The GeoIP subsystem provides two related signals: the network that announces an address and the country associated with it. Each signal can be enabled on its own, so a deployment can use only the data needed by its policy.

SignalUsed forResult
ASNasns policy checks and optional ASN loggingASN number and organization
Countrygeoip policy checksLowercase ISO 3166-1 alpha-2 code

The policy layer turns configured ASN and country sets into checks. A missing database or an address with no matching record produces no match rather than a guessed value. References: lib/…/geoip.go• L1-4, lib/…/checker.go• L13-28, lib/…/checker.go• L55-70, lib/…/policy.go• L217-232

Context construction

The GeoIP database handle is the shared context for enrichment. Policy validation creates it from the configured database sources, or uses a database already attached to the request context when no policy database is configured.

Policy database selection

An explicit geoip policy block has priority over a context-provided database. The resulting handle may contain an ASN source, a country source, or both. The policy layer warns when a rule or ASN logging asks for a source that is not available.

References: lib/…/context.go• L7-15 lib/…/policy.go• L114-132 lib/…/policy.go• L217-232

Address lookups

ASN and country lookups accept IPv4 and IPv6 addresses. The country result uses the configured country record: country by default or registered_country when selected. If the preferred record is empty, the other country record can supply the result.

References: lib/…/geoip.go• L164-235 lib/…/geoip_databases.go• L33-50 lib/…/geoip_databases.go• L138-144

Database compatibility

Database compatibility protects the meaning of each configured source. The loader checks the database metadata before it becomes active, so an ASN path cannot silently load country data and a country path cannot silently load unrelated data.

Database kinds

The loader accepts database types that provide the required record shape. ASN sources accept ASN data and compatible ISP data. Country sources accept country data and compatible city data. A database with another type is rejected before lookups use it.

References: lib/…/geoip.go• L268-286 lib/…/compatibility_test.go• L18-99

Configuration checks

Configuration validation requires at least one database and a path for each configured source. It also limits country selection to the supported country fields. Automatic updates require an account ID and a license-key environment variable. The update interval must be at least one hour.

References: lib/…/geoip_databases.go• L11-31 lib/…/geoip_databases.go• L61-95 lib/…/geoip_databases.go• L114-135 lib/…/geoip_databases.go• L163-201

Database versioning

Automatic updates use versioned sibling files instead of rewriting the file that supplies active lookups. A downloaded copy gets a Unix-time suffix, and the newest valid copy becomes the active source at startup.

Versioned files support safe replacement across platforms and preserve the old reader while a lookup is still using it. Old downloads and interrupted staging files are removed when they are no longer the active copy. References: lib/…/versions.go• L14-18, lib/…/versions.go• L22-67, lib/…/versions.go• L86-124, lib/…/geoip.go• L238-265, lib/…/geoip.go• L289-300

Update lifecycle

The update lifecycle has two modes. With auto_update, the subsystem checks the configured MaxMind service at the selected interval. Without it, the subsystem checks the configured files for changes so an external updater can replace them safely.

The following flow shows how a new database moves from a remote response or changed file to an active lookup source.

flowchart TD
    A[Start with configured source] --> B{Source available?}
    B -- No and auto-update enabled --> C[Download edition]
    B -- Yes --> D[Open and check database type]
    C --> E[Write staged version]
    E --> F[Check size and checksum]
    F --> D
    D -- Invalid --> G[Keep current source and report error]
    D -- Valid --> H[Install new reader]
    H --> I[Expose build time metric]
    I --> J[Clean old versions]
    B -- No and auto-update disabled --> G

Remote updates

A remote update downloads only when the service reports a newer edition. The subsystem limits the download size, checks the returned checksum, and checks the database kind before activation. If any step fails, the current source stays in service.

The update account, license key, interval, and optional endpoint come from auto_update. The license key value comes from the named environment variable rather than from the policy file.

References: lib/…/update.go• L21-29 lib/…/update.go• L41-61 lib/…/update.go• L64-99 lib/…/geoip_databases.go• L155-217

External file reloads

When automatic updates are off, a changed file is reloaded only after its file time changes and the replacement passes the same database checks. A failed reload does not discard the active source. External tools must replace a memory-mapped database atomically rather than write into the existing file.

References: lib/…/update.go• L151-177 lib/…/update.go• L206-227 docs/…/geoip.mdx• L17-21

Metrics

The GeoIP subsystem exposes one gauge and one counter for operational review. Both metrics use the kind label, whose values identify the ASN or Country source.

MetricTypeMeaning
anubis_geoip_database_build_timestamp_secondsGaugeBuild time reported by the active database
anubis_geoip_update_errors_totalCounterFailed downloads, file checks, or reloads

The build-time gauge changes when a source is installed. The error counter increases when an update or reload fails, while the existing source remains active when possible. References: lib/…/metrics.go• L8-17, lib/…/geoip.go• L289-300, lib/…/update.go• L180-223

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep accepted database metadata types aligned with the fields decoded by ASN and country lookups.
  • Keep the configured country field aligned with both policy country matching and the country record fallback behavior.
  • Preserve the versioned-file and atomic-replacement contract when changing update or reload behavior.
  • Keep the kind labels and metric meaning aligned with both source installation and failure paths.
  • Keep policy warnings aligned with the availability of ASN and country sources.

Important failure modes

TriggerConsequenceGuard
An ASN or country path points to the wrong database kindStartup or reload rejects the sourceMetadata kind validation before installation
A download fails checksum, size, or database validationThe current database stays active and an update error is countedStaged download checks before reader installation
An external updater overwrites a memory-mapped fileActive lookups can become corruptAtomic file replacement or Anubis-managed versioned updates
A policy uses ASN or country checks without the matching sourceThe check is skipped and a warning is loggedConfigure the required source and validate the policy
The MaxMind account or license key is invalidAutomatic updates fail while the prior source remains activeValidate credentials and monitor update errors

Key files

FileWhy to read it
lib/…/geoip.goMain database lifecycle and ASN/country lookup contract
lib/…/checker.goConnection between lookup results and policy checks
lib/…/context.goContext handoff and database precedence rule
lib/…/update.goRemote update and external reload behavior
lib/…/versions.goVersioned file naming and cleanup rules
lib/…/metrics.goPrometheus metrics exposed by the subsystem
lib/…/geoip_databases.goDatabase, country-field, and update validation rules
lib/…/policy.goPolicy-time database selection and source availability handling
docs/…/geoip.mdxOperator-facing setup and safe replacement guidance