geoip

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: AGPL-3.0 Imports: 17 Imported by: 2 modules, 1 package

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 provides IP origin and country allowlist enforcement.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Checker

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

Checker resolves IP addresses to country codes or named origins using a MaxMind MMDB file and optional named CIDR groups, and enforces an allowlist.

func NewChecker

func NewChecker(ctx context.Context, log *slog.Logger, allowlistStr, dbPath, cacheDir string) (*Checker, error)

NewChecker builds a Checker from a comma-separated allowlist string and an optional geo DB path. Named origins that require network fetches are resolved automatically. Country codes must occur in the database; their presence is checked at startup before fetching named origins. Results are cached under cacheDir for 24 hours; stale cache entries are used when refresh fails. Fetch failures are logged as warnings and do not abort startup.

func Open

func Open(dbPath string) (*Checker, error)

Open opens an MMDB file for enrichment without filtering. It classifies loopback, private, unspecified and link-local addresses as local, and 100.64.0.0/10 as tailscale; all other addresses use their MMDB country code.

func (*Checker) CheckOrigin

func (c *Checker) CheckOrigin(clientIP string) (string, bool)

CheckOrigin resolves the IP origin and reports whether it is allowlisted.

It returns the resolved origin even when the allowlist blocks the IP, so callers can use the same lookup result for logging and error messages. It returns "", false for invalid IPs unless no allowlist is configured.

func (*Checker) Close

func (c *Checker) Close() error

Close releases MMDB reader resources.

type Config

type Config struct {
	DB           string   `toml:"geo_db"`
	AllowOrigins []string `toml:"allow_origins"`
}

Config selects optional country enrichment and origin filtering. Immutable after startup. Hosts own database path expansion and the cache directory path.

func (*Config) Open

func (c *Config) Open(ctx context.Context, log *slog.Logger, cacheDir string) (*Checker, error)

Open validates the configuration and opens the optional checker. With neither setting it returns nil, nil. DB alone enriches without filtering or network fetches. Origin filtering uses the named-network cache in cacheDir; Open creates that directory only when a registered source needs caching. Empty cacheDir selects uncached resolution. The caller owns the returned checker.

func (*Config) Validate

func (c *Config) Validate() error

Validate checks origin tokens and requires geo_db when filtering by country. Origin tokens are CIDRs, two-letter country tokens (case-insensitive), or registered named origins. Open checks country tokens against the database. An empty list disables filtering; empty DB skips country enrichment. Validation performs no filesystem or network operations.

Directories

Path Synopsis
Package data defines the on-disk named IP-origin cache.
Package data defines the on-disk named IP-origin cache.

Jump to

Keyboard shortcuts

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