Documentation
¶
Overview ¶
Package ipinfo provides an IP resolver that returns information about an IP address.
Index ¶
Constants ¶
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
const ( DBTypeCountry = "country" DBTypeASN = "asn" )
Database type constants for metrics.
const ( IPVersion4 = "4" IPVersion6 = "6" )
IP version constants for metrics.
const AS0 uint32 = 0
AS0 represents the default ASN value for unknown addresses.
Variables ¶
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
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.
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
ParseASNRecord parses an ASN database record.
func ParseCountryRecord ¶ added in v0.5.3
ParseCountryRecord parses a country database record.
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 HTTPFetcher ¶ added in v0.5.3
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.
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.
type ParserFunc ¶ added in v0.5.7
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.