File: dnsoverride.go

package info (click to toggle)
chasquid 1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,672 kB
  • sloc: sh: 1,040; python: 286; makefile: 71
file content (36 lines) | stat: -rw-r--r-- 881 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Support for overriding DNS lookups, for testing purposes.
// This is only used in tests, when the "dnsoverride" tag is active.
// It requires Go >= 1.8.
//
// +build dnsoverride

package main

import (
	"context"
	"flag"
	"net"
	"time"
)

var (
	dnsAddr = flag.String("testing__dns_addr", "127.0.0.1:9053",
		"DNS server address to use, for testing purposes only")
)

var dialer = &net.Dialer{
	// We're going to talk to localhost, so have a short timeout so we fail
	// fast. Otherwise the callers might hang indefinitely when trying to
	// dial the DNS server.
	Timeout: 2 * time.Second,
}

func dial(ctx context.Context, network, address string) (net.Conn, error) {
	return dialer.DialContext(ctx, network, *dnsAddr)
}

func init() {
	// Override the resolver to talk with our local server for testing.
	net.DefaultResolver.PreferGo = true
	net.DefaultResolver.Dial = dial
}