srvclient

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2023 License: MIT Imports: 14 Imported by: 12

README

go-srvclient

GoDoc Report

A simple package for resolving DNS SRV records, according to the algorithm set forth in that project's README. It simply takes in a hostname and does a SRV request against it. It wil then look at all the returned entries and make a weighted random choice of one of them, returning a string which is the "host:port" of the picked entry.

Does not work on Windows.

Install

go get github.com/levenlabs/go-srvclient

Example

package main

import "github.com/levenlabs/go-srvclient"

func main() {
  addr, err := srvclient.SRV("foo.skydns.local")
  if err != nil {
    fmt.Fatal(err)
  }

  log.Printf("%s was chosen!", addr)
}

Binary client

This project also has an installable binary client which can be easily used. It can be installed with:

go install github.com/levenlabs/go-srvclient/srvclient

And used like so:

# srvclient some.host.name
8.9.10.11:1213

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultSRVClient = new(SRVClient)

DefaultSRVClient is an instance of SRVClient with all zero'd values, used as the default client for all global methods. It can be overwritten prior to any of the methods being used in order to modify their behavior

Functions

func AllSRV added in v0.3.0

func AllSRV(hostname string) ([]string, error)

AllSRV calls the AllSRV method on the DefaultSRVClient

func AllSRVContext added in v1.1.0

func AllSRVContext(ctx context.Context, hostname string) ([]string, error)

AllSRVContext calls the AllSRVContext method on the DefaultSRVClient

func AllSRVTranslate added in v1.0.0

func AllSRVTranslate(hostname string) ([]string, error)

AllSRVTranslate calls the AllSRVTranslate method on the DefaultSRVClient

func AllSRVTranslateContext added in v1.1.0

func AllSRVTranslateContext(ctx context.Context, hostname string) ([]string, error)

AllSRVTranslateContext calls the AllSRVTranslateContext method on the DefaultSRVClient

func MaybeSRV added in v0.3.0

func MaybeSRV(host string) string

MaybeSRV calls the MaybeSRV method on the DefaultSRVClient

func MaybeSRVURL added in v0.4.0

func MaybeSRVURL(host string) string

MaybeSRVURL calls the MaybeSRVURL method on the DefaultSRVClient

func MaybeSRVURLContext added in v1.1.0

func MaybeSRVURLContext(ctx context.Context, host string) string

MaybeSRVURLContext calls the MaybeSRVURLContext method on the DefaultSRVClient

func SRV

func SRV(hostname string) (string, error)

SRV calls the SRV method on the DefaultSRVClient

func SRVContext added in v1.1.0

func SRVContext(ctx context.Context, hostname string) (string, error)

SRVContext calls the SRVContext method on the DefaultSRVClient

func SRVNoPort

func SRVNoPort(hostname string) (string, error)

SRVNoPort calls the SRVNoPort method on the DefaultSRVClient

func SRVNoPortContext added in v1.1.0

func SRVNoPortContext(ctx context.Context, hostname string) (string, error)

SRVNoPortContext calls the SRVNoPortContext method on the DefaultSRVClient

func SRVNoTranslate added in v0.5.0

func SRVNoTranslate(hostname string) (string, error)

SRVNoTranslate calls the SRVNoTranslate method on the DefaultSRVClient

func SRVNoTranslateContext added in v1.1.0

func SRVNoTranslateContext(ctx context.Context, hostname string) (string, error)

SRVNoTranslateContext calls the SRVNoTranslateContext method on the DefaultSRVClient

func WithoutCancel added in v1.1.0

func WithoutCancel(ctx context.Context) context.Context

WithoutCancel returns a context identical to the given one, but which will never be canceled, regardless of if the given one is because the context methods will only propagate cancellations if the Done() method returns non-nil, but we override the Done method to return nil. Additionally, this will overwrite the Deadline method to return no deadline so if you call WithTimeout/WithDeadline it'll use the new value and not retain the parent one if its sooner.

Types

type ErrNotFound added in v0.5.0

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

ErrNotFound is returned when there were no SRV records for the given hostname

func (*ErrNotFound) Error added in v0.5.0

func (err *ErrNotFound) Error() string

Error implements the error interface

type SRVClient added in v0.4.0

type SRVClient struct {

	// OnExchangeError specifies an optional function to call for exchange errors
	// that otherwise might be ignored if another server did not error.
	OnExchangeError func(ctx context.Context, hostname string, server string, error error)

	// UDPSize specifies the maximum receive buffer for UDP messages
	UDPSize uint16

	// If IgnoreTruncated is true, then lookups will NOT fallback to TCP when
	// they were truncated over UDP.
	IgnoreTruncated bool

	// A list of addresses ("ip:port") which should be used as the resolver
	// list. If none are set then the resolver settings in /etc/resolv.conf are
	// used. This can only be updated before the SRVClient is used for the first
	// time.
	ResolverAddrs []string

	// If non-nill, will be called on messages returned from dns servers prior
	// to them being processed (i.e. before they are cached, sorted,
	// ip-replaced, etc...)
	Preprocess func(*dns.Msg)

	// SingleInFlight will combine duplicate lookups and only issue a single DNS
	// query, mirroring the response to all callers.
	SingleInFlight bool
	// contains filtered or unexported fields
}

SRVClient is a holder for methods related to SRV lookups. Use new(SRVClient) to initialize one.

func (*SRVClient) AllSRV added in v0.4.0

func (sc *SRVClient) AllSRV(hostname string) ([]string, error)

AllSRV calls AllSRVContext with an empty context

func (*SRVClient) AllSRVContext added in v1.1.0

func (sc *SRVClient) AllSRVContext(ctx context.Context, hostname string) ([]string, error)

AllSRVContext returns the list of all hostnames and ports for the SRV lookup The results are sorted by priority and then weight. Like SRV, if hostname contained a port then the port on all results will be replaced with the originally-passed port AllSRV will NOT replace hostnames with their respective IPs

func (*SRVClient) AllSRVTranslate added in v1.0.0

func (sc *SRVClient) AllSRVTranslate(hostname string) ([]string, error)

AllSRVTranslate calls AllSRVTranslateContext with an empty context

func (*SRVClient) AllSRVTranslateContext added in v1.1.0

func (sc *SRVClient) AllSRVTranslateContext(ctx context.Context, hostname string) ([]string, error)

AllSRVTranslateContext returns the list of all IPs and ports for the SRV lookup The results are sorted by priority and then weight. Like SRV, if hostname contained a port then the port on all results will be replaced with the originally-passed port

func (*SRVClient) EnableCacheLast added in v0.4.0

func (sc *SRVClient) EnableCacheLast()

EnableCacheLast is used to make SRVClient cache the last successful SRV response for each domain requested, and if the next request results in some kind of error it will use that last response instead.

func (*SRVClient) MaybeSRV added in v0.4.0

func (sc *SRVClient) MaybeSRV(host string) string

MaybeSRV calls MaybeSRVContext with an empty context

func (*SRVClient) MaybeSRVContext added in v1.1.0

func (sc *SRVClient) MaybeSRVContext(ctx context.Context, host string) string

MaybeSRVContext attempts a SRV lookup if the host doesn't contain a port and if the SRV lookup succeeds it'll rewrite the host and return it with the lookup result. If it fails it'll just return the host originally sent

func (*SRVClient) MaybeSRVURL added in v0.4.0

func (sc *SRVClient) MaybeSRVURL(host string) string

MaybeSRVURL calls MaybeSRVURLContext with an empty context

func (*SRVClient) MaybeSRVURLContext added in v1.1.0

func (sc *SRVClient) MaybeSRVURLContext(ctx context.Context, host string) string

MaybeSRVURLContext calls MaybeSRVContext and also prepends http:// if no scheme was sent

func (*SRVClient) SRV added in v0.4.0

func (sc *SRVClient) SRV(hostname string) (string, error)

SRV calls SRVContext with an empty context

func (*SRVClient) SRVContext added in v1.1.0

func (sc *SRVClient) SRVContext(ctx context.Context, hostname string) (string, error)

SRVContext will perform a SRV request on the given hostname, and then choose one of the returned entries randomly based on the priority and weight fields it sees. It will return the address ("host:port") of the winning entry, or an error if the query couldn't be made or it returned no entries. If the DNS server provided the A records for the hosts, then the result will have the target replaced with its respective IP.

If the given hostname already has a ":port" appended to it, only the ip will be looked up from the SRV request, but the port given will be returned

If the given hostname is "ip:port", it'll just immediately return what you sent.

func (*SRVClient) SRVNoPort added in v0.4.0

func (sc *SRVClient) SRVNoPort(hostname string) (string, error)

SRVNoPort behaves the same as SRV, but the returned address string will not contain the port

func (*SRVClient) SRVNoPortContext added in v1.1.0

func (sc *SRVClient) SRVNoPortContext(ctx context.Context, hostname string) (string, error)

SRVNoPortContext behaves the same as SRVContext, but the returned address string will not contain the port

func (*SRVClient) SRVNoTranslate added in v0.5.0

func (sc *SRVClient) SRVNoTranslate(hostname string) (string, error)

SRVNoTranslate is exactly like SRV except it won't translate names to their respective IPs

func (*SRVClient) SRVNoTranslateContext added in v1.1.0

func (sc *SRVClient) SRVNoTranslateContext(ctx context.Context, hostname string) (string, error)

SRVNoTranslateContext is exactly like SRVContext except it won't translate names to their respective IPs

func (*SRVClient) Stats added in v0.5.0

func (sc *SRVClient) Stats() SRVStats

Stats returns the latest SRVStats struct for the given client

type SRVStats added in v0.5.0

type SRVStats struct {
	UDPQueries         int64
	TCPQueries         int64
	TruncatedResponses int64
	ExchangeErrors     int64
	CacheLastHits      int64
	CacheLastMisses    int64
	InFlightHits       int64
}

SRVStats contains lifetime counts for various statistics

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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