-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdns_requests.go
99 lines (87 loc) · 2.01 KB
/
dns_requests.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
package main
import (
"fmt"
"net"
"net/url"
"time"
)
// DNSRequest represents a request response, with the return code and the duration
type DNSRequest struct {
status string
url string
criticity criticityLevel
duration time.Duration
err error
}
// String will return the string representing the request
func (r *DNSRequest) String() string {
if r.IsError() {
return fmt.Sprintf("| %s | %13s | Get %s : %s", red("ERR"), r.duration, r.url, r.Error())
}
return fmt.Sprintf("| %s | %13s | Get %s", criticityColor[r.criticity](r.status), r.duration, r.url)
}
// Duration returns the duration of the request
func (r DNSRequest) Duration() time.Duration {
return r.duration
}
// Error implements the error interface
func (r DNSRequest) Error() string {
var errName string
switch e := r.err.(type) {
case *net.DNSError:
errName = "DNS lookup error"
case *net.DNSConfigError:
errName = "DNS config error"
case *net.AddrError:
errName = "Addr Error"
case *net.OpError:
errName = "Op Error"
case *url.Error:
if e.Timeout() {
errName = "URL Timeout"
} else {
errName = "URL Error"
}
case net.Error:
errName = "Net Error"
default:
errName = e.Error()
}
return errName
}
// Size returns the size of the request
func (r DNSRequest) Size() int64 {
return 0
}
// Status returns the status of the request
func (r DNSRequest) Status() string {
return r.status
}
// IsError returns true if the request is an error
func (r DNSRequest) IsError() bool {
return r.err != nil
}
// lookupURL will make a DNS request on a given URL and return a Request
func lookupURL(url string) Request {
var dur time.Duration
t := time.Now()
// Make the DNS request
_, err := net.LookupHost(url)
if err != nil {
dur = time.Since(t)
return &DNSRequest{
duration: dur,
url: url,
err: err,
criticity: Critical,
}
}
// Record the duration of the request
dur = time.Since(t)
return &DNSRequest{
duration: dur,
status: "OK ",
criticity: Success,
url: url,
}
}