forked from tenta-browser/tenta-dns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.go
329 lines (293 loc) · 9.05 KB
/
monitor.go
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
** Monitor
** Basic operating principle:
** It can either read a config file, or ingest command line arguments (read flag definitions for more info)
** Firstly, it reads the configs of the running Tenta DNS instance, and parses the location of the module configs (recursor, nsnitch)
** Secondly, reads the module configs and parses all DNS Recursor IP addresses, and all Authoritative DNS addresses (and zone information it should serve)
** Thirdly, sets up TLS listener with the given parameters, and starts to listen for incoming requests
** Lastly, when a request comes in, it launches all preconfigured domains to be resolved by the recursor(s) within a predefined time interval
*/
package main
import (
"flag"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"github.com/BurntSushi/toml"
"github.com/miekg/dns"
"github.com/sirupsen/logrus"
"github.com/tenta-browser/tenta-dns/log"
"github.com/tenta-browser/tenta-dns/runtime"
"github.com/tenta-browser/tenta-dns/common"
)
const (
CHECK_HISTORY_LENGTH = 5
TESTING_PERIOD = 30
)
var (
config = flag.String("config", "", "Path to the configuration file for monitoring service")
dnsconf = flag.String("dnsconf", "", "Path to Tenta DNS main configuration file (Tenta DNS launch parameter)")
exclude = flag.String("exclude", "", "Comma separated list of IPs to exclude from testing")
ip = flag.String("ip", "", "Local IP address to bind service")
domain = flag.String("domain", "", "TLS domain name to serve API from")
cert = flag.String("cert", "", "Path to TLS certificate file for domain")
key = flag.String("key", "", "Path to TLS key file for domain")
target = flag.String("target", "example.com,example.org", "Comma separated list of domains to resolve")
timeout = flag.Int("timeout", 15, "Maximum duration the DNS server should finish a single query")
systemd = flag.Bool("systemd", false, "Set only if daemon is run via systemd")
)
var version string
type monitorConfig struct {
DnsConf, Ip, Domain, Cert, Key string
Target, Exclude []string
Timeout int
Systemd bool
}
type dnsInstance struct {
ip, net, hostname string
port int
}
type monitorRuntime struct {
c *monitorConfig
d []*dnsInstance
t *time.Ticker
m *sync.Mutex
r [20]bool
}
func checkExcludedIP(rt *monitorRuntime, ip string) bool {
for _, eip := range rt.c.Exclude {
if eip == ip {
return true
}
}
return false
}
func parseDNSConfig(rt *monitorRuntime, holder runtime.ConfigHolder) error {
for _, r := range holder.Recursors {
for _, d := range r.Domains {
if checkExcludedIP(rt, d.IPv4) {
continue
}
if d.DnsTcpPort != runtime.PORT_UNSET {
rt.d = append(rt.d, &dnsInstance{d.IPv4, "tcp", d.HostName, d.DnsTcpPort})
}
if d.DnsUdpPort != runtime.PORT_UNSET {
rt.d = append(rt.d, &dnsInstance{d.IPv4, "udp", d.HostName, d.DnsUdpPort})
}
if d.DnsTlsPort != runtime.PORT_UNSET {
rt.d = append(rt.d, &dnsInstance{d.IPv4, "tcp-tls", d.HostName, d.DnsTlsPort})
}
}
}
return nil
}
func pingdomWrapper(rt *monitorRuntime) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var response string
rt.m.Lock()
defer rt.m.Unlock()
for i := CHECK_HISTORY_LENGTH - 1; i >= 0; i-- {
if rt.r[i] == false {
response = fmt.Sprintf("<pingdom_http_custom_check>\n" +
" <status>FAIL</status>\n" +
" <response_time>1</response_time>\n" +
"</pingdom_http_custom_check>")
return
}
}
response = fmt.Sprintf("<pingdom_http_custom_check>\n" +
" <status>OK</status>\n" +
" <response_time>1</response_time>\n" +
"</pingdom_http_custom_check>")
w.Header().Set("Content-Disposition", "attachment; filename=status.xml")
w.Header().Set("Content-Type", "text/xml")
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(response)))
w.Write([]byte(response))
}
}
func sitrepWrapper(rt *monitorRuntime) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
rt.m.Lock()
defer rt.m.Unlock()
for i := CHECK_HISTORY_LENGTH - 1; i >= 0; i-- {
if rt.r[i] == false {
w.Write([]byte("FAIL"))
return
}
}
w.Write([]byte("OK"))
}
}
func createDialer(ip, network string) *net.Dialer {
switch network {
case "udp":
return &net.Dialer{LocalAddr: &net.UDPAddr{IP: net.ParseIP(ip)}}
break
case "tcp":
case "tcp-tls":
return &net.Dialer{LocalAddr: &net.TCPAddr{IP: net.ParseIP(ip)}}
break
}
return nil
}
func testDNS(rt *monitorRuntime, log *logrus.Entry) {
allOK := true
wg := &sync.WaitGroup{}
for _, _d := range rt.d {
wg.Add(1)
d := _d
go func() {
//log.Infof("Launching resolve for %s/%s", d.ip, d.net)
c := dns.Client{
Net: d.net,
SingleInflight: true,
}
if di := createDialer(rt.c.Ip, d.net); d != nil {
c.Dialer = di
}
if d.net == "tcp-tls" {
c.TLSConfig = common.TLSConfigDNS()
}
for _, testDomain := range rt.c.Target {
m := new(dns.Msg)
m.SetQuestion(dns.Fqdn(testDomain), dns.TypeA)
m.SetEdns0(4096, true)
r, rtt, e := c.Exchange(m, fmt.Sprintf("%s:%d", d.ip, d.port))
if e != nil {
log.Warnf("An error occured during DNS exchange. Setup: %s/%s [%s]. Cause [%s]", d.ip, d.net, testDomain, e.Error())
continue
}
if r.Rcode != dns.RcodeSuccess {
log.Warnf("DNS query %s/%s about %s returned non-success rcode [%s]. Failure notice sent.", d.ip, d.net, testDomain, dns.RcodeToString[r.Rcode])
allOK = false
break
}
if rtt > time.Duration(rt.c.Timeout)*time.Millisecond {
log.Warnf("Querying %s/%s about %s exceeded rtt threshold [%v]. Retrying.", d.ip, d.net, testDomain, rtt)
r, rtt, e = c.Exchange(m, fmt.Sprintf("%s:%d", d.ip, d.port))
if e != nil {
log.Warnf("An error occured during DNS exchange. Setup: %s/%s [%s]. Cause [%s]", d.ip, d.net, testDomain, e.Error())
allOK = false
break
}
if r.Rcode != dns.RcodeSuccess {
log.Warnf("DNS query %s/%s about %s returned non-success rcode [%s]. Failure notice sent.", d.ip, d.net, testDomain, dns.RcodeToString[r.Rcode])
allOK = false
break
}
if rtt > time.Duration(rt.c.Timeout)*time.Millisecond {
log.Warnf("Querying %s/%s about %s exceeded rtt threshold [%v]. Failure notice sent.", d.ip, d.net, testDomain, rtt)
allOK = false
break
}
}
}
wg.Done()
}()
}
wg.Wait()
if allOK {
log.Infof("SUCCESS for this round")
} else {
log.Infof("FAILURE for this round")
}
rt.m.Lock()
defer rt.m.Unlock()
for i := 18; i >= 0; i-- {
rt.r[i+1] = rt.r[i]
}
rt.r[0] = allOK
}
func usage() {
fmt.Printf("Tenta DNS Monitorization - build %s\nOptions:", version)
flag.PrintDefaults()
}
func main() {
log.SetLogLevel(logrus.InfoLevel)
lg := log.GetLogger("dnsmon")
/// Step 1. Make sure either we have command-line arguments or a config file (command line args take precedence)
flag.Usage = usage
flag.Parse()
rt := &monitorRuntime{m: &sync.Mutex{}}
cfg := &monitorConfig{}
for i := range rt.r {
rt.r[i] = true
}
if config == nil {
lg.Infof("Attempting to construct context from commandline arguments")
if dnsconf == nil || ip == nil || domain == nil || cert == nil || key == nil {
lg.Errorf("Service halted. Missing commandline arguments")
os.Exit(1)
}
cfg = &monitorConfig{
DnsConf: *dnsconf,
Exclude: strings.Split(*exclude, ","),
Ip: *ip,
Domain: *domain,
Cert: *cert,
Key: *key,
Target: strings.Split(*target, ","),
Timeout: *timeout,
Systemd: *systemd,
}
} else {
lg.Infof("Attempting to construct context from config file [%s]", *config)
if _, err := toml.DecodeFile(*config, cfg); err != nil {
lg.Errorf("Service halted. Configuration file error. [%s]", err.Error())
os.Exit(1)
}
}
rt.c = cfg
tentaHolder := runtime.ParseConfig(cfg.DnsConf, false, false)
if e := parseDNSConfig(rt, tentaHolder); e != nil {
lg.Errorf("Cannot parse DNS configuration. [%s]", e.Error())
os.Exit(1)
}
lg.Infof("Parsed Tenta DNS configurations")
rt.t = time.NewTicker(TESTING_PERIOD * time.Second)
mux := http.NewServeMux()
mux.HandleFunc("/api/v1/checkup", sitrepWrapper(rt))
mux.HandleFunc("/api/v1/pingdom", pingdomWrapper(rt))
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
})
go func() {
srv := &http.Server{
Addr: net.JoinHostPort(rt.c.Ip, "80"),
Handler: mux,
}
if e := srv.ListenAndServe(); e != nil {
lg.Errorf("HTTP listener error [%s]", e.Error())
}
}()
go func() {
srv := &http.Server{
Addr: net.JoinHostPort(rt.c.Ip, "443"),
Handler: mux,
TLSConfig: common.TLSConfigModernHTTPS(),
}
if e := srv.ListenAndServeTLS(rt.c.Cert, rt.c.Key); e != nil {
lg.Errorf("HTTPS listener error [%s]", e.Error())
}
}()
sig := make(chan os.Signal)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
for {
select {
case rs := <-sig:
lg.Infof("Received termination signal [%s]. Exiting.", rs)
os.Exit(0)
break
case <-rt.t.C:
lg.Debugf("Received test signal. Working.")
testDNS(rt, lg)
break
}
}
}