Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoIP is returned by the Consensus when no vote was casted successfully ErrNoIP = errors.New("no IP could be found") // ErrInsufficientWeight is returned when a voter's weight is invalid ErrInsufficientWeight = errors.New("a voter's weight has to be at least 1") // ErrNoSource is returned when a voter is added, // which doesn't have a source specified ErrNoSource = errors.New("no voter's source given") )
Functions ¶
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(cfg *ConsensusConfig, logger *log.Logger) *Consensus
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(cfg *ConsensusConfig, logger *log.Logger) *Consensus
NewConsensus creates a new Consensus, with no sources. When the given cfg is <nil>, the `DefaultConsensusConfig` will be used.
type ConsensusConfig ¶
ConsensusConfig is used to configure the Consensus, while creating it.
func DefaultConsensusConfig ¶
func DefaultConsensusConfig() *ConsensusConfig
DefaultConsensusConfig returns the ConsensusConfig, with the default values:
- Timeout: 30 seconds;
func (*ConsensusConfig) WithTimeout ¶
func (cfg *ConsensusConfig) WithTimeout(timeout time.Duration) *ConsensusConfig
WithTimeout sets the voting timeout of this config, returning the config itself at the end, to allow for chaining
type ContentParser ¶
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) 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 InvalidIPError ¶
type InvalidIPError string
InvalidIPError is returned when an value returned is invalid. This error should be returned by the source itself.
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(timeout time.Duration, logger *log.Logger) (net.IP, error) }
Source defines the part of a voter which gives the actual voting value (IP).