dns

package
v0.0.0-...-e960053 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2024 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Overview

Package dns provides a k6 module for DNS resolution and lookup.

Index

Constants

View Source
const (
	// FormatError is a DNS error kind that represents a Format Error.
	// It is based on the DNS Response Code FormErr, as per [RFC1035].
	//
	// [RFC1035]: https://www.iana.org/go/rfc1035
	FormatError errorKind = 1

	// ServerFailure is a DNS error kind that represents a Server Failure.
	// It is based on the DNS Response Code ServFail, as per [RFC1035].
	//
	// [RFC1035]: https://www.iana.org/go/rfc1035
	ServerFailure errorKind = 2

	// NonExistingDomain is a DNS error kind that represents a Non-Existent Domain.
	// It is based on the DNS Response Code NXDomain, as per [RFC1035].
	//
	// [RFC1035]: https://www.iana.org/go/rfc1035
	NonExistingDomain errorKind = 3

	// NotImplemented is a DNS error kind that represents a Not Implemented error.
	// It is based on the DNS Response Code NotImp, as per [RFC1035].
	//
	// [RFC1035]: https://www.iana.org/go/rfc1035
	NotImplemented errorKind = 4

	// Refused is a DNS error kind that represents a Query Refused error.
	// It is based on the DNS Response Code Refused, as per [RFC1035].
	//
	// [RFC1035]: https://www.iana.org/go/rfc1035
	Refused errorKind = 5

	// YXDomain is a DNS error kind that represents a Name Exists when it should not.
	// It is based on the DNS Response Code YXDomain, as per [RFC2136].
	//
	// [RFC2136]: https://www.iana.org/go/rfc2136
	YXDomain errorKind = 6

	// YXRrset is a DNS error kind that represents a RR Set Exists when it should not.
	// It is based on the DNS Response Code YXRRSet, as per [RFC2136].
	//
	// [RFC2136]: https://www.iana.org/go/rfc2136
	YXRrset errorKind = 7

	// NXRrset is a DNS error kind that represents a RR Set that should exist does not.
	// It is based on the DNS Response Code NXRRSet, as per [RFC2136].
	//
	// [RFC2136]: https://www.iana.org/go/rfc2136
	NXRrset errorKind = 8

	// NotAuth is a DNS error kind that represents a Server Not Authoritative for zone.
	// It is based on the DNS Response Code NotAuth, as per [RFC2136].
	//
	// [RFC2136]: https://www.iana.org/go/rfc2136
	NotAuth errorKind = 9

	// NotZone is a DNS error kind that represents a Name not contained in zone.
	// It is based on the DNS Response Code NotZone, as per [RFC2136].
	//
	// [RFC2136]: https://www.iana.org/go/rfc2136
	NotZone errorKind = 10

	// BadVers is a DNS error kind that represents a Bad OPT Version.
	// It is based on the DNS Response Code BadVers, as per [RFC6891].
	//
	// [RFC6891]: https://www.iana.org/go/rfc6891
	BadVers errorKind = 16

	// BadSig is a DNS error kind that represents a TSIG Signature Failure.
	// It is based on the DNS Response Code BadSig, as per [RFC8945].
	//
	// [RFC8945]: https://www.iana.org/go/rfc8945
	BadSig errorKind = 16

	// BadKey is a DNS error kind that represents a Key not recognized.
	// It is based on the DNS Response Code BadKey, as per [RFC8945].
	//
	// [RFC8945]: https://www.iana.org/go/rfc8945
	BadKey errorKind = 17

	// BadTime is a DNS error kind that represents a Signature out of time window.
	// It is based on the DNS Response Code BadTime, as per [RFC8945].
	//
	// [RFC8945]: https://www.iana.org/go/rfc8945
	BadTime errorKind = 18

	// BadMode is a DNS error kind that represents a Bad TKEY Mode.
	// It is based on the DNS Response Code BadMode, as per [RFC2930].
	//
	// [RFC2930]: https://www.iana.org/go/rfc2930
	BadMode errorKind = 19

	// BadName is a DNS error kind that represents a Duplicate key name.
	// It is based on the DNS Response Code BadName, as per [RFC2930].
	//
	// [RFC2930]: https://www.iana.org/go/rfc2930
	BadName errorKind = 20

	// BadAlg is a DNS error kind that represents an Algorithm not supported.
	// It is based on the DNS Response Code BadAlg, as per [RFC2930].
	//
	// [RFC2930]: https://www.iana.org/go/rfc2930
	BadAlg errorKind = 21

	// BadTrunc is a DNS error kind that represents a Bad Truncation.
	// It is based on the DNS Response Code BadTrunc, as per [RFC8945].
	//
	// [RFC8945]: https://www.iana.org/go/rfc8945
	BadTrunc errorKind = 22

	// BadCookie is a DNS error kind that represents a Bad/missing Server Cookie.
	// It is based on the DNS Response Code BadCookie, as per [RFC7873].
	//
	// [RFC7873]: https://www.iana.org/go/rfc7873
	BadCookie errorKind = 23
)

DNS error kinds based on DNS Response Codes, see https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml

Variables

View Source
var ErrUnsupportedRecordType = errors.New("unsupported record type")

ErrUnsupportedRecordType is an error that is returned when a record type is not supported by the module.

Functions

This section is empty.

Types

type Client

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

Client is a DNS resolver that uses the `miekg/dns` package under the hood.

It implements the Resolver interface.

func NewDNSClient

func NewDNSClient() *Client

NewDNSClient creates a new Client.

func (*Client) Lookup

func (r *Client) Lookup(ctx context.Context, hostname string) ([]string, error)

Lookup resolves a domain name to a slice of IP addresses using the system's default resolver.

func (*Client) Resolve

func (r *Client) Resolve(
	ctx context.Context,
	query, recordType string,
	nameserver Nameserver,
) ([]string, error)

Resolve resolves a domain name to a slice of IP addresses using the given nameserver. It returns a slice of IP addresses as strings.

type Error

type Error struct {
	// Name holds the descriptive name of the error.
	Name string `json:"name"`

	// Message holds the error message.
	Message string `json:"message"`

	// kind holds the underlying error kind, matching the DNS response codes defined in
	// DNS' [specification] of RCodes.
	//
	// [specification]: https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-6).
	Kind errorKind `json:"kind"`
}

Error represents a DNS error.

func (*Error) Error

func (e *Error) Error() string

Error returns the error message.

type Lookuper

type Lookuper interface {
	Lookup(ctx context.Context, hostname string) ([]string, error)
}

Lookuper is the interface that wraps the Lookup method.

As opposed to a Resolver which uses a specific nameserver to resolve the query, a Lookuper uses the system's default resolver.

Lookup resolves a domain name to an IP address. It returns a slice of IP addresses as strings.

type ModuleInstance

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

ModuleInstance is the module instance that will be created for each VU.

func (*ModuleInstance) Exports

func (mi *ModuleInstance) Exports() modules.Exports

Exports returns the module exports, that will be available in the runtime.

func (*ModuleInstance) Lookup

func (mi *ModuleInstance) Lookup(hostname sobek.Value) *sobek.Promise

Lookup resolves a domain name to an IP address using the default system nameservers.

func (*ModuleInstance) Resolve

func (mi *ModuleInstance) Resolve(query, recordType, nameserverAddr sobek.Value) *sobek.Promise

Resolve resolves a domain name to an IP address.

type Nameserver

type Nameserver struct {
	// IPAddr is the IP address of the nameserver.
	IP net.IP

	// Port is the port of the nameserver.
	Port uint16
}

Nameserver represents a DNS nameserver.

func (Nameserver) Addr

func (n Nameserver) Addr() string

Addr returns the address of the nameserver as a string.

type RecordType

type RecordType uint16

RecordType represents a DNS record type.

It is a string type with a restricted set of values. The values are the DNS record types supported by the module.

The currently supported values are: - A - AAAA

The supported values are the ones that are most likely to be used by the users of this extension and package, as they are those returning IP addresses. Other record types could be supported later on, as long as we extend our resolver's logic to support them.

We use a custom type to restrict the set of values, and to avoid leaking the underlying dns package's types to the users of the reusable abstractions defined by this module.

const (
	RecordTypeA    RecordType = RecordType(dns.TypeA)
	RecordTypeAAAA RecordType = RecordType(dns.TypeAAAA)
)

Note that we aligned the values of the RecordType enum values with the corresponding values of the dns package's types for convenience.

Note that the RecordType enum values are explicitly typed to allow enumer to detect them.

func RecordTypeString

func RecordTypeString(s string) (RecordType, error)

RecordTypeString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.

func RecordTypeValues

func RecordTypeValues() []RecordType

RecordTypeValues returns all values of the enum

func (RecordType) IsARecordType

func (i RecordType) IsARecordType() bool

IsARecordType returns "true" if the value is listed in the enum definition. "false" otherwise

func (RecordType) String

func (i RecordType) String() string

type Resolver

type Resolver interface {
	Resolve(ctx context.Context, query, recordType string, nameserver Nameserver) ([]string, error)
}

Resolver is the interface that wraps the Resolve method.

Resolve resolves a domain name to an IP address. It returns a slice of IP addresses as strings.

type RootModule

type RootModule struct{}

RootModule is the module that will be registered with the runtime.

func New

func New() *RootModule

New creates a new RootModule instance.

func (*RootModule) NewModuleInstance

func (rm *RootModule) NewModuleInstance(vu modules.VU) modules.Instance

NewModuleInstance creates a new instance of the module for a specific VU.

Jump to

Keyboard shortcuts

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