-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdnsstress.go
More file actions
156 lines (133 loc) · 3.35 KB
/
Copy pathdnsstress.go
File metadata and controls
156 lines (133 loc) · 3.35 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"flag"
"fmt"
"log"
"math"
"os"
"os/signal"
"runtime"
"strings"
"syscall"
"github.com/DataDog/datadog-go/statsd"
)
var (
concurrency int
maxMessages int
resolver string
runForever bool
reqPerSec int
dnsProtocol string
protocol string
)
type DNSProtocol string
const (
DNS DNSProtocol = "dns"
DNSOverHTTPS DNSProtocol = "doh"
)
func (d DNSProtocol) String() string {
return string(d)
}
func (d DNSProtocol) Validate() error {
switch d {
case DNS, DNSOverHTTPS:
return nil
default:
return fmt.Errorf("invalid protocol %s", d)
}
}
func init() {
flag.IntVar(&concurrency, "concurrency", runtime.NumCPU(),
"Number of concurrent goroutines used for sending")
flag.IntVar(&maxMessages, "m", 100000,
"Maximum number of messages to send before stopping. Can be overridden to never stop with -inf")
flag.IntVar(&reqPerSec, "t", 0,
"Target request rate per second, defaults to unlimited")
flag.StringVar(&dnsProtocol, "d", DNS.String(),
"Type of DNS protocol to use, either 'dns' or 'doh' (default 'dns')")
flag.StringVar(&resolver, "r", "127.0.0.1:53",
"Resolver to test against")
flag.StringVar(&protocol, "p", "udp",
"Protocol to use, only applies if using DNS as protocol")
flag.BoolVar(&runForever, "inf", false,
"Run Forever")
}
func main() {
fmt.Printf("dnsstress - dns stress tool\n\n")
flag.Usage = func() {
fmt.Fprint(os.Stderr, strings.Join([]string{
"Send DNS requests as fast as possible to a given server and display the rate.",
"",
"Usage: dnsstress [option ...] targetdomain [targetdomain [...] ]",
"",
}, "\n"))
flag.PrintDefaults()
}
flag.Parse()
// We need at least one target domain
if flag.NArg() < 1 {
flag.Usage()
os.Exit(1)
}
if concurrency < 1 {
flag.Usage()
os.Exit(1)
}
sdClient, err := statsd.New("127.0.0.1:8125")
if err != nil {
log.Fatal(err)
return
}
defer sdClient.Close()
if DNSProtocol(dnsProtocol).Validate() != nil {
flag.Usage()
os.Exit(1)
}
if !strings.Contains(resolver, ":") { // TODO: improve this test to make it work with IPv6 addresses
// Automatically append the default port number if missing
resolver = resolver + ":53"
}
// all remaining parameters are treated as domains to be used in round-robin in the threads
targetDomains := make([]string, flag.NArg())
for index, element := range flag.Args() {
if element[len(element)-1] == '.' {
targetDomains[index] = element
} else {
targetDomains[index] = element + "."
}
}
if dnsProtocol == DNS.String() {
protocol = strings.ToLower(protocol)
switch protocol {
case "udp":
case "tcp":
default:
log.Fatalf("unknown protocol %s", protocol)
}
}
fmt.Printf("Target domains: %v.\n", targetDomains)
exit := make(chan struct{})
go handleSignals(exit)
if runForever {
maxMessages = math.MaxInt64
}
dnsResolver := NewResolver(resolver, targetDomains[0], sdClient, ResolverOptions{
Concurrency: concurrency,
MaxMessages: maxMessages,
RequestsPerSecond: reqPerSec,
Protocol: protocol,
DNSProtocol: DNSProtocol(dnsProtocol),
})
go func() {
<-exit
dnsResolver.Stop()
}()
dnsResolver.RunResolver()
}
func handleSignals(exit chan struct{}) {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
sig := <-sigs
fmt.Printf("caught signal %s, stopping...\n", sig)
close(exit)
}