dns

package
v0.3.7 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package dns implements low-level DNS message encoding and decoding to interface with RFC 8484 "DNS Queries over HTTPS" (DoH) services.

Example:

qq := &dns.Message{
	RD: 1,
	Question: []dns.Question{{
		Name:  "www.google.com",
		Type:  1,
		Class: 1,
	}},
}
result, err := dns.DoH(context.Background(), qq, "https://1.1.1.1/dns-query")
if err != nil {
	fmt.Fprintf(os.Stderr, "dns.DoH: %v", err)
	os.Exit(1)
}
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", "  ")
enc.Encode(result)

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDecodeError = errors.New("decode error")
)

Functions

func RRType

func RRType(t string) uint16

RRType returns the RR type id for a type name.

Types

type CAA added in v0.1.4

type CAA struct {
	Flags uint8  `json:"flags"`
	Tag   string `json:"tag"`
	Value string `json:"value"`
}

CAA represents a CAA Resource Record.

type CERT added in v0.1.4

type CERT struct {
	Type        uint16 `json:"type"`
	KeyTag      uint16 `json:"keytag"`
	Algorithm   uint8  `json:"algorithm"`
	Certificate []byte `json:"certificate"`
}

CERT represents a CERT Resource Record.

type DNSKEY added in v0.1.4

type DNSKEY struct {
	Flags     uint16 `json:"flags"`
	Protocol  uint8  `json:"protocol"`
	Algorithm uint8  `json:"algorithm"`
	PublicKey []byte `json:"publickey"`
}

DNSKEY represents a DNSKEY Resource Record.

type DS added in v0.1.4

type DS struct {
	KeyTag     uint16 `json:"keytag"`
	Algorithm  uint8  `json:"algorithm"`
	DigestType uint8  `json:"digesttype"`
	Digest     []byte `json:"digest"`
}

DS represents a DS Resource Record.

type HTTPS

type HTTPS struct {
	Priority      uint16   `json:"priority"`
	Target        string   `json:"target,omitempty"`
	ALPN          []string `json:"alpn,omitempty"`
	NoDefaultALPN bool     `json:"no-default-alpn,omitempty"`
	Port          uint16   `json:"port,omitempty"`
	IPv4Hint      []net.IP `json:"ipv4hint,omitempty"`
	IPv6Hint      []net.IP `json:"ipv6hint,omitempty"`
	ECH           []byte   `json:"ech,omitempty"`
}

HTTPS represents a HTTPS Resource Record. RFC 9460

func (HTTPS) String

func (h HTTPS) String() string

type LOC added in v0.1.4

type LOC struct {
	Version   uint8   `json:"version"`
	Size      float64 `json:"size"`      // meters
	HorizPre  float64 `json:"horizpre"`  // meters
	VertPre   float64 `json:"vertpre"`   // meters
	Latitude  float64 `json:"latitude"`  // degrees
	Longitude float64 `json:"longitude"` // degrees
	Altitude  float64 `json:"altitude"`  // meters above reference altitude
}

LOC represents a LOC Resource Record. RFC 1876

type MX

type MX struct {
	Preference uint16 `json:"preference"`
	Exchange   string `json:"exchange"`
}

MX represents a MX Resource Record.

type Message

type Message struct {
	// Header
	ID     uint16 `json:"id,omitempty"`
	QR     uint8  `json:"qr"`
	OpCode uint8  `json:"opcode,omitempty"`
	AA     uint8  `json:"aa,omitempty"`
	TC     uint8  `json:"tc,omitempty"`
	RD     uint8  `json:"rd,omitempty"`
	RA     uint8  `json:"ra,omitempty"`
	RCode  uint8  `json:"rcode"`

	// Question section
	Question []Question `json:"question,omitempty"`
	// Answer section
	Answer []RR `json:"answer,omitempty"`
	// Authority section
	Authority []RR `json:"authority,omitempty"`
	// Additional information section
	Additional []RR `json:"additional,omitempty"`
}

Message is a RFC 1035 DNS Message.

func DecodeMessage

func DecodeMessage(m []byte) (*Message, error)

DecodeMessage decodes a DNS message.

func DoH

func DoH(ctx context.Context, msg *Message, URL string) (*Message, error)

DoH sends a RFC 8484 DoH (DNS-over-HTTPS) request to URL.

func (*Message) AddPadding added in v0.2.6

func (m *Message) AddPadding()

AddPadding adds padding to a message to make its size a multiple of 128.

func (Message) Bytes

func (m Message) Bytes() []byte

Bytes returns the serialized message. It includes only the header and the question section.

func (Message) ResponseCode added in v0.2.6

func (m Message) ResponseCode() uint16

ResponseCode returns the Extended RCODE.

type NSEC added in v0.1.4

type NSEC struct {
	NextDomainName string `json:"nextdomainname"`
	TypeBitMaps    []byte `json:"typebitmaps"`
}

NSEC represents a NSEC Resource Record.

type Option added in v0.2.6

type Option struct {
	Code uint16 `json:"code"`
	Data []byte `json:"data"`
}

Option represents a OPT pseudo Resource Record option.

type Question

type Question struct {
	Name  string `json:"name"`
	Type  uint16 `json:"type"`
	Class uint16 `json:"class"`
}

A question for a name server.

type RR

type RR struct {
	Name  string `json:"name"`
	Type  uint16 `json:"type"`
	Class uint16 `json:"class"`
	TTL   uint32 `json:"ttl"`
	Data  any    `json:"data"`
}

A Resource Record.

func (RR) Bytes added in v0.1.6

func (rr RR) Bytes() []byte

type RRSIG added in v0.1.4

type RRSIG struct {
	TypeCovered         uint16 `json:"typecovered"`
	Algorithm           uint8  `json:"algorithm"`
	Labels              uint8  `json:"labels"`
	OriginalTTL         uint32 `json:"originalttl"`
	SignatureExpiration uint32 `json:"signatureexpiration"`
	SignatureInception  uint32 `json:"signatureinception"`
	KeyTag              uint16 `json:"keytag"`
	SignerName          string `json:"signername"`
	Signature           []byte `json:"signature"`
}

RRSIG represents a RRSIG Resource Record.

type SOA

type SOA struct {
	MName   string `json:"mname"`
	RName   string `json:"rname"`
	Serial  uint32 `json:"serial"`
	Refresh uint32 `json:"refresh"`
	Retry   uint32 `json:"retry"`
	Expire  uint32 `json:"expire"`
	Minimum uint32 `json:"minimum"`
}

SOA represents a SOA Resource Record.

type SRV

type SRV struct {
	Priority uint16 `json:"priority"`
	Weight   uint16 `json:"weight"`
	Port     uint16 `json:"port"`
	Target   string `json:"target"`
}

SRV represents a SRV Resource Record.

type SVCB

type SVCB struct {
	Priority uint16      `json:"priority"`
	Target   string      `json:"target"`
	Params   []SVCBParam `json:"params"`
}

SVCB represents a SVCB Resource Record. RFC 9460

type SVCBParam

type SVCBParam struct {
	Key   uint16 `json:"key"`
	Value []byte `json:"value,omitempty"`
}

type TXT

type TXT []string

TXT represents a TXT Resource Record.

type URI added in v0.1.4

type URI struct {
	Priority uint16 `json:"priority"`
	Weight   uint16 `json:"weight"`
	Target   string `json:"target"`
}

URI represents a URI Resource Record. RFC 7553

Directories

Path Synopsis
This is an example showing how to send a DoH request using dns.DoH and dns.Message.
This is an example showing how to send a DoH request using dns.DoH and dns.Message.

Jump to

Keyboard shortcuts

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