ipgeo

package
v0.10.5 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2026 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Overview

Package ipgeo provides IP origin and country allowlist enforcement.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetClientIP

func GetClientIP(r *http.Request) string

GetClientIP extracts the real client IP from a request, checking X-Forwarded-For and X-Real-IP headers for proxied requests.

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 added in v0.5.4

func NewChecker(ctx context.Context, 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. 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 country lookups.

func (*Checker) CheckOrigin added in v0.10.1

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 OriginSource added in v0.10.1

type OriginSource interface {
	Name() string
	Cacheable() bool
	Prefixes(ctx context.Context) ([]netip.Prefix, error)
}

OriginSource provides CIDR prefixes for a named IP origin.