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") // ErrInvalidProtocol is used when setting an invalid ip protocol on the conensus ErrInvalidProtocol = errors.New("invalid ip protocol specified") )
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.
func (*Consensus) AddVoter ¶
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) ExternalIP ¶
ExternalIP requests asynchronously 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 ¶
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 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, protocol uint) (net.IP, error) }
Source defines the part of a voter which gives the actual voting value (IP).