ipres

package
v0.1.16 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2025 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package ipres provider 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 AS0 uint32 = 0

AS0 represents the default ASN value for unknown addresses.

Variables

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

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

Functions

This section is empty.

Types

type DBRecord

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

DBRecord contains the information of a database record.

type ParserFn

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

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

type ResTree

type ResTree = itree.ITree[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() *Resolver

NewResolver creates a new IP resolver.

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() error

Update updates the databases used by the resolver.

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