-
Notifications
You must be signed in to change notification settings - Fork 0
/
pihole.go
175 lines (156 loc) · 3.49 KB
/
pihole.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
package main
import (
"context"
"database/sql"
"fmt"
"log"
"time"
)
const defaultDBTimeout = 30 * time.Second
const piholeQuery = `
SELECT
type,
status,
client,
forward,
reply_type,
count(id)
FROM
queries
WHERE
timestamp >= %d AND
timestamp < %d
GROUP BY
type,
status,
client,
forward,
reply_type
;`
type PiholeStats struct {
QueryTypes map[string]float64
AllowedQueries map[string]map[string]float64
BlockedQueries map[string]float64
BlockedCNAMEQueries map[string]float64
ClientQueries map[string]float64
QueryReplies map[string]float64
}
var queryTypes = []string{
"A",
"AAAA",
"ANY",
"SRV",
"SOA",
"PTR",
"TXT",
"NAPTR",
"MX",
"DS",
"RRSIG",
"DNSKEY",
"NS",
"OTHER",
"SVCB",
"HTTPS",
}
var queryStatuses = []string{
"unknown",
"gravity",
"forwarded",
"cache_hit",
"regex_blacklist",
"exact_blacklist",
"known_upstream",
"unspecified_upstream",
"nxdomain_upstream",
"gravity_cname", // during deep CNAME inspection
"regex_blacklist_cname", // during deep CNAME inspection
"exact_blacklist_cname", // during deep CNAME inspection
"retried_query",
"retried_ignored_query",
"already_forwarded",
"database_busy",
"special_domain",
}
var replyTypes = []string{
"unknown",
"nodata",
"nxdomain",
"cname",
"ip",
"domain",
"rrname",
"servfail",
"refused",
"notimp",
"other",
"dnssec",
"none", // query was dropped intentionally
"blob", // binary data
}
func queryPihole(db *sql.DB, since, now int64) (*PiholeStats, error) {
ctx, cancel := context.WithTimeout(context.Background(), defaultDBTimeout)
defer cancel()
stats := &PiholeStats{
QueryTypes: make(map[string]float64),
AllowedQueries: make(map[string]map[string]float64),
BlockedQueries: make(map[string]float64),
BlockedCNAMEQueries: make(map[string]float64),
ClientQueries: make(map[string]float64),
QueryReplies: make(map[string]float64),
}
query := fmt.Sprintf(piholeQuery, since, now)
rows, err := db.QueryContext(ctx, query)
if err != nil {
return nil, fmt.Errorf("query db: %w", err)
}
for rows.Next() {
var (
queryType int
status int
client string
forward sql.NullString
replyType int
numQueries float64
)
err = rows.Scan(&queryType, &status, &client, &forward, &replyType, &numQueries)
if err != nil {
return nil, fmt.Errorf("scan row: %w", err)
}
if queryType < 1 || queryType > len(queryTypes) {
log.Printf("WARN: unknown query type: %d", queryType)
continue
}
typeKey := queryTypes[queryType-1]
stats.QueryTypes[typeKey] += numQueries
if replyType < 0 || replyType > len(replyTypes) {
log.Printf("WARN: unknown reply type: %d", replyType)
continue
}
replyKey := replyTypes[replyType]
stats.QueryReplies[replyKey] += numQueries
stats.ClientQueries[client] += numQueries
switch status {
case 0, 2, 3, 12, 13, 14:
statusKey := queryStatuses[status]
upstream := "cache"
if forward.Valid || status == 0 {
upstream = forward.String
}
if stats.AllowedQueries[statusKey] == nil {
stats.AllowedQueries[statusKey] = make(map[string]float64)
}
stats.AllowedQueries[statusKey][upstream] += numQueries
case 1, 4, 5, 6, 7, 8, 15, 16:
statusKey := queryStatuses[status]
stats.BlockedQueries[statusKey] += numQueries
case 9, 10, 11:
statusKey := queryStatuses[status]
stats.BlockedCNAMEQueries[statusKey] += numQueries
default:
log.Printf("WARN: unexpected status: %d", status)
continue
}
}
return stats, nil
}