dns

package
Version: v0.1.39 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2022 License: GPL-3.0 Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DefaultResolver

type DefaultResolver interface {
	LookupTXT(name string) ([]string, error)
	LookupAddress(name string) ([]string, error)
	LookupCNAME(name string) (string, error)
	LookupMX(name string) ([]*MxResp, error)
	Reroute(address, network string) error
	Reset() error
}

func NewResolver

func NewResolver() (DefaultResolver, error)

NewResolver creates and uses a default resolver

Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/dns"
	"bitbucket.org/taubyte/go-sdk/dns"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	symbols.MockNew(5)

	resolver, err := dns.NewResolver()
	if err != nil {
		return
	}

	fmt.Printf("%d\n", resolver)
}
Output:

5

type MxResp

type MxResp struct {
	Host string
	Pref uint16
}

type Resolver

type Resolver uint32
Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/dns"
	"bitbucket.org/taubyte/go-sdk/dns"
	"bitbucket.org/taubyte/go-sdk/event"
)

func dnsFunction(e event.Event) uint32 {
	resolver, err := dns.NewResolver()
	if err != nil {
		return 1
	}

	values, err := resolver.LookupAddress("8.8.8.8")
	if err != nil {
		return 1
	}
	fmt.Println("Address:", values)

	value, err := resolver.LookupCNAME("taubyte.com.")
	if err != nil {
		return 1
	}
	fmt.Println("CNAME:", value)

	mxValues, err := resolver.LookupMX("google.com")
	if err != nil {
		return 1
	}
	fmt.Println("MX:", mxValues[0].Host, mxValues[0].Pref)

	values, err = resolver.LookupTXT("google.com")
	if err != nil {
		return 1
	}
	fmt.Println("TxT:", values[0])

	err = resolver.Reroute("8.8.8.8:53", "udp")
	if err != nil {
		return 1
	}

	err = resolver.Reset()
	if err != nil {
		return 1
	}

	return 0
}

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	symbols.MockData{Id: 5,
		TxtData: map[string][]byte{
			"google.com": symbols.Convert([]string{
				"MS=15412F62916164C0B20BB",
				"verification=J9cpOJM0nikft0jAgjmsQ",
			}),
		},
		AddressData: map[string][]byte{
			"8.8.8.8": symbols.Convert([]string{
				"dns.google",
			}),
		},
		CnameData: map[string][]byte{
			"taubyte.com.": symbols.Convert([]string{
				"nodes.taubyte.com.",
			}),
		},
		MxData: map[string][]byte{
			"google.com": []byte("smtp.google.com.\x00/10\x00"),
		},
	}.Mock()

	e := dnsFunction(0)
	if e > 0 {
		fmt.Println(e)
		return
	}

	fmt.Println("Success")
}
Output:

Address: [dns.google]
CNAME: nodes.taubyte.com.
MX: smtp.google.com. 10
TxT: MS=15412F62916164C0B20BB
Success

func (Resolver) LookupAddress

func (r Resolver) LookupAddress(ip string) ([]string, error)

LookupAddr performs a reverse lookup for the given address, returning a list of names mapping to that address and an error.

Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/dns"
	"bitbucket.org/taubyte/go-sdk/dns"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	testData := map[string][]byte{
		"8.8.8.8": symbols.Convert([]string{
			"dns.google",
		}),
	}
	symbols.MockData{AddressData: testData}.Mock()

	r, err := dns.NewResolver()
	if err != nil {
		return
	}

	values, err := r.LookupAddress("8.8.8.8")
	if err != nil {
		return
	}

	fmt.Println(values[0])
}
Output:

dns.google

func (Resolver) LookupCNAME

func (r Resolver) LookupCNAME(fqdn string) (string, error)

LookupCNAME returns the canonical name for the given host.

LookupCNAME does not return an error if the given name does not contain DNS "CNAME" records, as long as the name resolves to address records.

Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/dns"
	"bitbucket.org/taubyte/go-sdk/dns"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	testData := map[string][]byte{
		"taubyte.com.": symbols.Convert([]string{
			"nodes.taubyte.com.",
		}),
	}
	symbols.MockData{CnameData: testData}.Mock()

	r, err := dns.NewResolver()
	if err != nil {
		return
	}

	values, err := r.LookupCNAME("taubyte.com.")
	if err != nil {
		return
	}

	fmt.Println(values)
}
Output:

nodes.taubyte.com.

func (Resolver) LookupMX

func (r Resolver) LookupMX(fqdn string) ([]*MxResp, error)

LookupMX returns the DNS MX records for the given name and an error.

Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/dns"
	"bitbucket.org/taubyte/go-sdk/dns"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	testData := map[string][]byte{
		"google.com": []byte("smtp.google.com.\x00/10\x00"),
	}
	symbols.MockData{MxData: testData}.Mock()

	r, err := dns.NewResolver()
	if err != nil {
		return
	}

	values, err := r.LookupMX("google.com")
	if err != nil {
		return
	}

	fmt.Println(values[0].Host, values[0].Pref)
}
Output:

smtp.google.com. 10

func (Resolver) LookupTXT

func (r Resolver) LookupTXT(fqdn string) ([]string, error)

LookupTXT returns the DNS TXT records for the given domain name.

Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/dns"
	"bitbucket.org/taubyte/go-sdk/dns"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	testData := map[string][]byte{
		"google.com": symbols.Convert([]string{
			"MS=15412F62916164C0B20BB",
			"verification=J9cpOJM0nikft0jAgjmsQ",
		}),
	}
	symbols.MockData{TxtData: testData}.Mock()

	r, err := dns.NewResolver()
	if err != nil {
		return
	}

	values, err := r.LookupTXT("google.com")
	if err != nil {
		return
	}

	fmt.Println(values[0])
}
Output:

MS=15412F62916164C0B20BB

func (Resolver) Reroute added in v0.1.27

func (r Resolver) Reroute(address, network string) error

Reroute reroutes the resolver to the provided address and network Return an error

Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/dns"
	"bitbucket.org/taubyte/go-sdk/dns"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	symbols.MockNew(0)
	symbols.MockReroute(0, "8.8.8.8:53", "udp")

	r, err := dns.NewResolver()
	if err != nil {
		return
	}

	err = r.Reroute("8.8.8.8:53", "udp")
	if err != nil {
		return
	}

	fmt.Println("Success")
}
Output:

Success

func (Resolver) Reset added in v0.1.27

func (r Resolver) Reset() error

Reset sets the current resolver to the default provided resolver Returns an error

Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/dns"
	"bitbucket.org/taubyte/go-sdk/dns"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	symbols.MockData{}.Mock()

	r, err := dns.NewResolver()
	if err != nil {
		return
	}

	err = r.Reset()
	if err != nil {
		return
	}

	fmt.Println("Success")
}
Output:

Success

Jump to

Keyboard shortcuts

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