dns

package module
v0.0.0-...-eedf519 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2025 License: MIT Imports: 7 Imported by: 1

README

dns

Go Reference Go Report Card

go get git.gorbe.io/go/dns@latest

Get the latest commit (if Go module proxy is not updated):

go get "git.gorbe.io/go/dns@$(curl -s 'https://git.gorbe.io/api/v1/repos/go/dns/commits' | jq -r '.[0].sha')"

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (

	// CloudflareDNS is a predefined [Client] to use the Cloudflare DNS servers.
	CloudflareDNS = &Client{
		NameServers: []NameServer{
			{protocol: "udp", host: "1.1.1.1", port: "53"},
			{protocol: "udp", host: "1.0.0.1", port: "53"},
		},
		Timeout:  5 * time.Second,
		Attempts: 2,
	}

	// GoogleDNS is a predefined [Client] to use the Google DNS servers.
	GoogleDNS = &Client{
		NameServers: []NameServer{
			{protocol: "udp", host: "8.8.8.8", port: "53"},
			{protocol: "udp", host: "8.8.8.4", port: "53"},
		},
		Timeout:  5 * time.Second,
		Attempts: 2,
	}

	// CommonDNS is a predefined [Client] to use the common DNS servers (currently Cloudflare and Google).
	CommonDNS *Client = &Client{
		NameServers: []NameServer{
			{protocol: "udp", host: "1.1.1.1", port: "53"},
			{protocol: "udp", host: "1.0.0.1", port: "53"},
			{protocol: "udp", host: "8.8.8.8", port: "53"},
			{protocol: "udp", host: "8.8.8.4", port: "53"},
		},
		Timeout:  5 * time.Second,
		Attempts: 2,
	}
)

Functions

This section is empty.

Types

type A

type A net.IP

func QueryA

func QueryA(name string) ([]A, error)

QueryA uses DefaultClient to query the a NameServer returned by Client.NameServer.

Example
package main

import (
	"fmt"

	"git.gorbe.io/go/dns"
)

func main() {

	records, err := dns.QueryA("example.com")
	if err != nil {
		// Handle error
		return
	}

	for i := range records {
		fmt.Printf("%s\n", records[i])
	}
}

func (A) String

func (a A) String() string

String implements fmt.Stringer interface.

type AAAA

type AAAA net.IP

func QueryAAAA

func QueryAAAA(name string) ([]AAAA, error)

QueryAAAA uses DefaultClient to query the a NameServer returned by Client.NameServer.

Example
package main

import (
	"fmt"

	"git.gorbe.io/go/dns"
)

func main() {

	records, err := dns.QueryAAAA("example.com")
	if err != nil {
		// Handle error
		return
	}

	for i := range records {
		fmt.Printf("%s\n", records[i])
	}
}

func (AAAA) String

func (aaaa AAAA) String() string

String implements fmt.Stringer interface.

type Client

type Client struct {
	NameServers []NameServer
	Timeout     time.Duration
	Attempts    int // TODO
}
var (
	// DefaultClient is used by QueryX functions.
	//
	// Package dns tries to parse "/etc/resolv.conf" into [DefaultClient] with [ParseResolvConf].
	// If failed to parse "/etc/resolv.conf" sets to [CommonDNS].
	DefaultClient *Client
)

func ParseResolvConf

func ParseResolvConf(name string) (*Client, error)

ParseResolvConf parses a resolv.conf style file in path name.

func (*Client) NameServer

func (c *Client) NameServer() *NameServer

NameServer returns a random nameserver from the stored nameservers.

func (*Client) QueryA

func (c *Client) QueryA(name string) ([]A, error)

QueryA queries the next NameServer returned by Client.NameServer.

Example
package main

import (
	"fmt"

	"git.gorbe.io/go/dns"
)

func main() {

	c, err := dns.ParseResolvConf("/etc/resolv.conf")
	if err != nil {
		// Handle error
		return
	}

	records, err := c.QueryA("example.com")
	if err != nil {
		// Handle error
		return
	}

	for i := range records {
		fmt.Printf("%s\n", records[i])
	}
}

func (*Client) QueryAAAA

func (c *Client) QueryAAAA(name string) ([]AAAA, error)

QueryAAAA queries the next NameServer returned by Client.NameServer.

Example
package main

import (
	"fmt"

	"git.gorbe.io/go/dns"
)

func main() {

	c, err := dns.ParseResolvConf("/etc/resolv.conf")
	if err != nil {
		// Handle error
		return
	}

	records, err := c.QueryAAAA("example.com")
	if err != nil {
		// Handle error
		return
	}

	for i := range records {
		fmt.Printf("%s\n", records[i])
	}
}

func (*Client) QueryHTTPS

func (c *Client) QueryHTTPS(name string) ([]HTTPS, error)

QueryHTTPS queries the next NameServer returned by Client.NameServer.

Example
package main

import (
	"fmt"

	"git.gorbe.io/go/dns"
)

func main() {

	c, err := dns.ParseResolvConf("/etc/resolv.conf")
	if err != nil {
		// Handle error
		return
	}

	records, err := c.QueryHTTPS("google.com")
	if err != nil {
		// Handle error
		return
	}

	for i := range records {
		fmt.Printf("%s\n", records[i])
	}
}

func (*Client) QueryNS

func (c *Client) QueryNS(name string) ([]NS, error)

QueryNS queries the next NameServer returned by Client.NameServer.

func (*Client) QuerySOA

func (c *Client) QuerySOA(name string) (*SOA, error)

QuerySOA queries the next NameServer returned by Client.NameServer.

If return nil/nil, then no SOA record found in the answer section.

Example
package main

import (
	"fmt"

	"git.gorbe.io/go/dns"
)

func main() {

	c, err := dns.ParseResolvConf("/etc/resolv.conf")
	if err != nil {
		// Handle error
		return
	}

	record, err := c.QuerySOA("example.com")
	if err != nil {
		// Handle error
		return
	}

	fmt.Printf("%s\n", record)
}

func (*Client) QuerySVCB

func (c *Client) QuerySVCB(name string) ([]SVCB, error)

QuerySVCB queries the next NameServer returned by Client.NameServer.

Example
package main

import (
	"fmt"

	"git.gorbe.io/go/dns"
)

func main() {

	c, err := dns.ParseResolvConf("/etc/resolv.conf")
	if err != nil {
		// Handle error
		return
	}

	records, err := c.QuerySVCB("google.com")
	if err != nil {
		// Handle error
		return
	}

	for i := range records {
		fmt.Printf("%s\n", records[i])
	}
}

type HTTPS

type HTTPS struct {
	Priority uint16
	Target   string
	Params   map[string]string
}

func QueryHTTPS

func QueryHTTPS(name string) ([]HTTPS, error)

func (HTTPS) String

func (h HTTPS) String() string

String implements fmt.Stringer interface.

type NS

type NS string

func QueryNS

func QueryNS(name string) ([]NS, error)

type NameServer

type NameServer struct {
	// contains filtered or unexported fields
}
Example
package main

import (
	"fmt"

	"git.gorbe.io/go/dns"
)

func main() {

	ns, err := dns.NewNameServer("", "127.0.0.1", "")
	if err != nil {
		// Handle error
	}

	fmt.Printf("%s\n", ns)
}
Output:
127.0.0.1:53

func NewNameServer

func NewNameServer(protocol, host string, port string) (NameServer, error)

NewNameServer sets the default values and a NameServer.

- If protocol is empty, sets to "udp" - If hostname is empty, returns an error - If port is empty, sets to "53"

func (*NameServer) Host

func (ns *NameServer) Host() string

Host returns the set host for NameServer ns.

func (*NameServer) Port

func (ns *NameServer) Port() string

Port returns the set port for NameServer ns.

If the port is unset, sets to "53".

func (*NameServer) Protocol

func (ns *NameServer) Protocol() string

Protocol returns the set protocol for NameServer ns.

If the protocol is unset, sets to "udp".

func (NameServer) String

func (ns NameServer) String() string

String implements the fmt.Stringer interface. Returns the nameserver's address in the following format:

[host]:port

type Rcode

type Rcode int
Example
package main

import (
	"fmt"

	"git.gorbe.io/go/dns"
)

func main() {

	rcode := dns.Rcode(3)

	fmt.Printf("%s", rcode)
}
Output:
NXDOMAIN

func (Rcode) Error

func (r Rcode) Error() string

Error returns the error string associated with the rcode. If Rcode r is unknown, returns the rcode as a string.

type SOA

type SOA struct {
	MName   string
	RName   string
	Serial  uint32
	Refresh uint32
	Retry   uint32
	Expire  uint32
	Minimum uint32
}

func QuerySOA

func QuerySOA(name string) (*SOA, error)

QuerySOA uses DefaultClient to query the a NameServer returned by Client.NameServer.

Example
package main

import (
	"fmt"

	"git.gorbe.io/go/dns"
)

func main() {

	record, err := dns.QuerySOA("example.com")
	if err != nil {
		// Handle error
		return
	}

	fmt.Printf("%s\n", record)
}

func (SOA) String

func (s SOA) String() string

String implements fmt.Stringer interface.

type SVCB

type SVCB struct {
	Priority uint16
	Target   string
	Params   map[string]string
}

func QuerySVCB

func QuerySVCB(name string) ([]SVCB, error)

func (SVCB) String

func (svcb SVCB) String() string

String implements fmt.Stringer interface.

Jump to

Keyboard shortcuts

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