geoip

package
v0.0.0-...-889a765 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2023 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package geoip is a library that allows you to find the external IP of a system. This implementation is based on the github.com/GlenDC/go-external-ip library.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoIP = errors.New("no IP could be found")

ErrNoIP is returned when no IP could be found.

Functions

This section is empty.

Types

type Consensus

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

Consensus the type at the center of this library, and is the main entry point for users. Its `ExternalIP` method allows you to ask for your ExternalIP, influenced by all its added voters.

func DefaultConsensus

func DefaultConsensus() (*Consensus, error)

DefaultConsensus returns a consensus filled with default and recommended HTTPSources. TLS-Protected providers get more power, compared to plain-text providers.

func NewConsensus

func NewConsensus() (*Consensus, error)

NewConsensus creates a new Consensus, with no sources.

func (*Consensus) AddVoter

func (c *Consensus) AddVoter(source Source, weight uint) error

AddVoter adds a voter to this consensus. The source cannot be <nil> and the weight has to be of a value of 1 or above.

func (*Consensus) ResolveExternalIP

func (c *Consensus) ResolveExternalIP(ctx context.Context) (net.IP, error)

ResolveExternalIP resolves the externalIP from all added voters, returning the IP which received the most votes. The returned IP will always be valid, in case the returned error is <nil>.

func (*Consensus) UseIPProtocol

func (c *Consensus) UseIPProtocol(protocol uint) error

UseIPProtocol will set the IP Protocol to use for http requests to the sources. If zero, it will not discriminate. This is useful when you want to get the external IP in a specific protocol. Protocol only supports 0, 4 or 6.

type ContentParser

type ContentParser func(string) (string, error)

ContentParser can be used to add a parser to an HTTPSource to parse the raw content returned from a website, and return the IP. Spacing before and after the IP will be trimmed by the Consensus.

type HTTPSource

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

HTTPSource is the default source, to get the external IP from. It does so by requesting the IP from a URL, via an HTTP GET Request.

func NewHTTPSource

func NewHTTPSource(url string) *HTTPSource

NewHTTPSource creates a HTTP Source object, which can be used to request the (external) IP from. The Default HTTP Client will be used if no client is given.

func (*HTTPSource) IP

func (s *HTTPSource) IP(ctx context.Context, protocol uint) (net.IP, error)

IP implements Source.IP

func (*HTTPSource) WithParser

func (s *HTTPSource) WithParser(parser ContentParser) *HTTPSource

WithParser sets the parser value as the value to be used by this HTTPSource, and returns the pointer to this source, to allow for chaining.

type Source

type Source interface {
	// IP returns IPv4/IPv6 address in a non-error case
	// net.IP should never be <nil> when error is <nil>
	// It is recommended that the IP function times out,
	// if no result could be found, after the given timeout duration.
	IP(ctx context.Context, protocol uint) (net.IP, error)
}

Source defines the part of a voter which gives the actual voting value (IP).

Jump to

Keyboard shortcuts

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