ipinfo

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2026 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Overview

Package ipinfo provides an IP resolver that returns information about an IP address.

Index

Constants

View Source
const (
	CountryIPv4URL = "https://cdn.jsdelivr.net/npm/@ip-location-db/geolite2-country/geolite2-country-ipv4.csv"
	CountryIPv6URL = "https://cdn.jsdelivr.net/npm/@ip-location-db/geolite2-country/geolite2-country-ipv6.csv"
	ASNIPv4URL     = "https://cdn.jsdelivr.net/npm/@ip-location-db/geolite2-asn/geolite2-asn-ipv4.csv"
	ASNIPv6URL     = "https://cdn.jsdelivr.net/npm/@ip-location-db/geolite2-asn/geolite2-asn-ipv6.csv"
)

URLs of the CSV IP location databases

View Source
const (
	DBTypeCountry = "country"
	DBTypeASN     = "asn"
)

Database type constants for metrics.

View Source
const (
	IPVersion4 = "4"
	IPVersion6 = "6"
)

IP version constants for metrics.

View Source
const AS0 uint32 = 0

AS0 represents the default ASN value for unknown addresses.

Variables

View Source
var (
	ErrRecordLength = errors.New("invalid record length")
	ErrInvalidASN   = errors.New("invalid ASN")
)

ErrRecordLength is returned when a CSV record has an unexpected length.

Functions

This section is empty.

Types

type CacheLogger added in v0.5.5

type CacheLogger interface {
	Warn(msg, path string, err error)
}

CacheLogger is the interface for logging cache operations.

type CachedFetcher added in v0.5.5

type CachedFetcher struct {
	CacheDir string
	MaxAge   time.Duration
	Fetcher  Fetcher
	Logger   CacheLogger
}

CachedFetcher is a Fetcher that caches fetched CSV records in a local directory. It checks the cache before fetching, and updates the cache after fetching.

The cache entries are considered valid for a specified maximum age.

func NewCachedFetcher added in v0.5.5

func NewCachedFetcher(
	cacheDir string,
	maxAge time.Duration,
	fetcher Fetcher,
	logger CacheLogger,
) *CachedFetcher

NewCachedFetcher creates a new CachedFetcher with the given cache directory, maximum age for cache entries, underlying fetcher, and logger.

func (*CachedFetcher) Fetch added in v0.5.5

func (c *CachedFetcher) Fetch(ctx context.Context, url string) ([][]string, error)

Fetch fetches CSV records from the given URL, using the cache if possible.

type DBRecord

type DBRecord struct {
	StartIP    netip.Addr
	EndIP      netip.Addr
	Resolution Resolution
}

DBRecord contains the information of a database record.

func ParseASNRecord added in v0.5.3

func ParseASNRecord(record []string) (*DBRecord, error)

ParseASNRecord parses an ASN database record.

func ParseCountryRecord added in v0.5.3

func ParseCountryRecord(record []string) (*DBRecord, error)

ParseCountryRecord parses a country database record.

type DBSource added in v0.5.2

type DBSource struct {
	DBType    string
	IPVersion string
}

DBSource identifies a database entry by type and IP version.

type DBSourceSpec added in v0.5.3

type DBSourceSpec struct {
	Source DBSource
	URL    string
	Parser ParserFunc
}

DBSourceSpec defines a database source with its URL and parser.

type DBUpdateCollector added in v0.5.2

type DBUpdateCollector interface {
	RecordDBUpdate(entries map[DBSource]uint64, duration time.Duration)
}

DBUpdateCollector collects metrics for database updates.

type Fetcher added in v0.5.3

type Fetcher interface {
	Fetch(ctx context.Context, url string) ([][]string, error)
}

Fetcher fetches CSV records from a URL.

type HTTPFetcher added in v0.5.3

type HTTPFetcher struct {
	Client *http.Client
}

HTTPFetcher is the default Fetcher implementation that fetches CSV records over HTTP.

func NewHTTPFetcher added in v0.5.3

func NewHTTPFetcher() *HTTPFetcher

NewHTTPFetcher creates a new HTTPFetcher with a default HTTP client.

func (*HTTPFetcher) Fetch added in v0.5.3

func (f *HTTPFetcher) Fetch(ctx context.Context, url string) ([][]string, error)

Fetch fetches CSV records from the given URL.

type Loader added in v0.5.3

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

Loader loads database records from a source into an interval tree.

func NewLoader added in v0.5.3

func NewLoader(fetcher Fetcher) *Loader

NewLoader creates a new Loader with the given fetcher.

func (*Loader) Load added in v0.5.3

func (l *Loader) Load(
	ctx context.Context,
	db *ResTree,
	src DBSourceSpec,
) (uint64, error)

Load fetches records from the source and inserts them into the database.

type ParserFunc added in v0.5.7

type ParserFunc func([]string) (*DBRecord, error)

ParserFunc is a function that parses a CSV record into a database record.

type ResTree

type ResTree = itree.Tree[netip.Addr, Resolution]

ResTree is a type alias for an interval tree that maps IP addresses to resolutions.

type Resolution

type Resolution struct {
	CountryCode  string // ISO 3166-1 alpha-2 country code
	Organization string // Organization name
	ASN          uint32 // Autonomous System Number
}

Resolution contains the result of resolving an IP address.

type Resolver

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

Resolver is an IP resolver that returns information about an IP address.

func NewResolver

func NewResolver(
	collector DBUpdateCollector,
	fetcher Fetcher,
) *Resolver

NewResolver creates a new IP resolver with the given metrics collector and fetcher.

func (*Resolver) Resolve

func (r *Resolver) Resolve(ip netip.Addr) Resolution

Resolve resolves the given IP address to a country code and an ASN.

It is the caller's responsibility to check if the IP is valid.

If the country of the IP is not found, the CountryCode field of the result will be an empty string. If the ASN of the IP is not found, the ASN field of the result will be zero.

The Organization field is present for informational purposes only. It is not used by the rules engine.

func (*Resolver) Update

func (r *Resolver) Update(ctx context.Context) error

Update updates the databases used by the resolver. The context can be used to cancel the update operation.

If an error occurs while updating a database, the function proceeds to update the next database and returns all the errors at the end.

Jump to

Keyboard shortcuts

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