-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdnsbl.go
159 lines (130 loc) · 2.95 KB
/
dnsbl.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
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"time"
)
var (
resolverUrl = url.URL{
Scheme: "https",
Host: "cloudflare-dns.com",
Path: "dns-query",
RawQuery: "type=A&do=false&cd=false",
}
ipRegex = regexp.MustCompile(`^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$`)
reverseIpSub = "$4.$3.$2.$1.%s"
metrics []*Metric
nextCheck time.Time
)
type stringSet map[string]*struct{}
func (set stringSet) Add(v string) {
set[v] = nil
}
func (set stringSet) ToList() []string {
var l []string
for k, _ := range set {
l = append(l, k)
}
return l
}
type DnsResponseAnswer struct {
Name string
Type int
Data string
}
type DnsResponse struct {
Status int
Answer []*DnsResponseAnswer
}
type Metric struct {
Hostname string
IpAddress string
Value uint16
Lists []string
ListCount int
}
func getIps(hostname string) []string {
query := resolverUrl.Query()
query.Set("name", hostname)
resolverUrl.RawQuery = query.Encode()
requestContext, _ := context.WithTimeout(context.Background(), time.Second*1)
request, requestError := http.NewRequest("GET", resolverUrl.String(), nil)
if requestError != nil {
panic(requestError)
}
request.Header.Add("Accept", "application/dns-json")
request.WithContext(requestContext)
response, responseError := http.DefaultClient.Do(request)
if responseError != nil {
panic(responseError)
}
out, readError := io.ReadAll(response.Body)
if readError != nil {
panic(readError)
}
closeError := response.Body.Close()
if closeError != nil {
panic(closeError)
}
dnsResponse := DnsResponse{}
jsonError := json.Unmarshal(out, &dnsResponse)
if jsonError != nil {
panic(jsonError)
}
var ipAddresses []string
if dnsResponse.Status == 0 {
for _, result := range dnsResponse.Answer {
if result.Type == 1 {
ipAddresses = append(ipAddresses, result.Data)
}
}
}
return ipAddresses
}
func getMetrics(addresses []string) []*Metric {
if updateCheck(time.Duration(config.Interval) * time.Second) {
metrics = metrics[:0]
for _, address := range addresses {
var ips []string
if ipRegex.MatchString(address) {
ips = []string{address}
} else {
ips = getIps(address)
}
for _, ip := range ips {
blHostFormat := ipRegex.ReplaceAllString(ip, reverseIpSub)
blResults := getIps(fmt.Sprintf(blHostFormat, config.Blacklist))
var ipListCount = 0
var ipListNames = &stringSet{}
for _, result := range blResults {
ipListCount++
if name, ok := config.ListCodes[result]; ok {
ipListNames.Add(name)
} else {
ipListNames.Add(result)
}
}
metrics = append(metrics, &Metric{
Hostname: address,
IpAddress: ip,
Lists: ipListNames.ToList(),
ListCount: ipListCount,
})
}
}
}
return metrics
}
func updateCheck(interval time.Duration) bool {
now := time.Now()
if nextCheck.Before(now) {
nextCheck = now.Add(interval)
return true
}
return false
}