api

package
v0.0.0-...-9933dd3 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTokenNotValid   = errors.New("token is not valid")
	ErrTooManyRequests = errors.New("too many requests sent for this month")
	ErrBadHTTPStatus   = errors.New("bad HTTP status received")
	ErrServiceLimited  = errors.New("service is limited")
)
View Source
var ErrCustomURLNotValid = errors.New("custom URL is not valid")
View Source
var ErrFetchersAllRateLimited = errors.New("all fetchers are rate limited")
View Source
var ErrProviderNotValid = errors.New("API name is not valid")

Functions

func FetchMultiInfo

func FetchMultiInfo(ctx context.Context, fetcher InfoFetcher, ips []netip.Addr) (
	results []models.PublicIP, err error,
)

FetchMultiInfo obtains the public IP address information for every IP addresses provided and returns a slice of results with the corresponding order as to the IP addresses slice order. If an error is encountered, all the operations are canceled and an error is returned, so the results returned should be considered incomplete in this case.

Types

type Fetcher

type Fetcher interface {
	String() string
	CanFetchAnyIP() bool
	Token() (token string)
	InfoFetcher
}

func New

func New(nameTokenPairs []NameToken, client *http.Client) (
	fetchers []Fetcher, err error,
)

type InfoFetcher

type InfoFetcher interface {
	FetchInfo(ctx context.Context, ip netip.Addr) (
		result models.PublicIP, err error)
}

type NameToken

type NameToken struct {
	Name  string
	Token string
}

type Provider

type Provider string
const (
	Cloudflare  Provider = "cloudflare"
	IfConfigCo  Provider = "ifconfigco"
	IPInfo      Provider = "ipinfo"
	IP2Location Provider = "ip2location"
)

func ParseProvider

func ParseProvider(s string) (provider Provider, err error)

type ResilientFetcher

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

func NewResilient

func NewResilient(fetchers []Fetcher, logger Warner) *ResilientFetcher

NewResilient creates a 'resilient' fetcher given multiple fetchers. For example, it can handle bans and move on to another fetcher if one fails.

func (*ResilientFetcher) CanFetchAnyIP

func (r *ResilientFetcher) CanFetchAnyIP() bool

CanFetchAnyIP returns true if any of the fetchers can fetch any IP address and is not banned.

func (*ResilientFetcher) FetchInfo

func (r *ResilientFetcher) FetchInfo(ctx context.Context, ip netip.Addr) (
	result models.PublicIP, err error,
)

FetchInfo obtains information on the ip address provided. If the ip is the zero value, the public IP address of the machine is used as the IP. If a fetcher gets banned, the next one is tried – until all have been exhausted. Fetchers still within their banned period are skipped. If an error unrelated to being banned is encountered, it is returned and more fetchers are tried.

func (*ResilientFetcher) String

func (r *ResilientFetcher) String() string

func (*ResilientFetcher) Token

func (r *ResilientFetcher) Token() string

func (*ResilientFetcher) UpdateFetchers

func (r *ResilientFetcher) UpdateFetchers(fetchers []Fetcher)

type Warner

type Warner interface {
	Warn(message string)
}

Jump to

Keyboard shortcuts

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