dnscoretest

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2025 License: GPL-3.0 Imports: 11 Imported by: 2

Documentation

Overview

Package dnscoretest contains fake servers to test dnscore.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ExampleComAddrA = net.IPv4(93, 184, 215, 14)

ExampleComAddrA is the A address of example.com.

Functions

This section is empty.

Types

type Handler

type Handler interface {
	Handle(rw ResponseWriter, rawQuery []byte)
}

Handler is a function that handles a DNS query.

func NewExampleComHandler

func NewExampleComHandler() Handler

NewExampleComHandler returns a handler that responds with a valid DNS response for example.com.

type HandlerFunc

type HandlerFunc func(rw ResponseWriter, rawQuery []byte)

HandlerFunc is an adapter to allow the use of ordinary functions as DNS handlers.

func (HandlerFunc) Handle

func (hf HandlerFunc) Handle(rw ResponseWriter, rawQuery []byte)

Handle implements Handler.

type ResponseWriter

type ResponseWriter interface {
	io.Writer
}

ResponseWriter allows writing raw DNS responses.

type Server

type Server struct {
	// Addr is the address of the server for DNS-over-UDP,
	// DNS-over-TCP, and DNS-over-TLS.
	Addr string

	// Listen is an optional func to override the default
	// function used to create a [net.Listener].
	Listen func(network, address string) (net.Listener, error)

	// ListenPacket is an optional func to override the default
	// function used to create a listening [net.PacketConn].
	ListenPacket func(network, address string) (net.PacketConn, error)

	// ListenTLS is an optional func to override the default
	// function used to listen using TLS.
	ListenTLS func(network, address string, config *tls.Config) (net.Listener, error)

	// RootCAs contains the cert pool the client should use
	// for DNS-over-TLS and DNS-over-HTTPS.
	RootCAs *x509.CertPool

	// URL is the URL for DNS-over-HTTPS.
	URL string
	// contains filtered or unexported fields
}

Server is a fake DNS server.

The zero value is a valid server.

Example (Udp)
package main

import (
	"fmt"
	"log"
	"slices"
	"strings"

	"github.com/miekg/dns"
	"github.com/rbmk-project/dnscore/dnscoretest"
)

func main() {
	// Create a fake UDP server using the example.com handler
	server := &dnscoretest.Server{}
	handler := dnscoretest.NewExampleComHandler()
	<-server.StartUDP(handler)
	defer server.Close()

	// Create a DNS client
	client := &dns.Client{Net: "udp"}
	query := new(dns.Msg)
	query.SetQuestion("example.com.", dns.TypeA)

	// Send the query to the fake server
	resp, _, err := client.Exchange(query, server.Addr)
	if err != nil {
		log.Fatal(err)
	}

	// print the results
	var addrs []string
	for _, rr := range resp.Answer {
		switch rr := rr.(type) {
		case *dns.A:
			addrs = append(addrs, rr.A.String())
		}
	}
	slices.Sort(addrs)
	fmt.Printf("%s\n", strings.Join(addrs, "\n"))

}
Output:

93.184.215.14

func (*Server) Close

func (s *Server) Close() error

Close closes the server.

func (*Server) StartHTTPS

func (s *Server) StartHTTPS(handler Handler) <-chan struct{}

StartHTTPS starts an HTTPS server and handles incoming DNS queries.

This method panics in case of failure.

func (*Server) StartTCP

func (s *Server) StartTCP(handler Handler) <-chan struct{}

StartTCP starts a TCP listener and listens for incoming DNS queries.

This method panics in case of failure.

func (*Server) StartTLS

func (s *Server) StartTLS(handler Handler) <-chan struct{}

StartTLS starts a TLS listener and listens for incoming DNS queries.

This method panics in case of failure.

func (*Server) StartUDP

func (s *Server) StartUDP(handler Handler) <-chan struct{}

StartUDP starts an UDP listener and listens for incoming DNS queries.

This method panics in case of failure.

Jump to

Keyboard shortcuts

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