-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpihole.go
311 lines (258 loc) · 7.47 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
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
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"sort"
"strconv"
)
// PiHConnector represents base API connector type.
// Host: DNS or IP address of your Pi-Hole
// Token: API Token (see /etc/pihole/setupVars.conf)
type PiHConnector struct {
Host string
Token string
}
// PiHType coitains Pi-Hole backend type (PHP or FTL).
type PiHType struct {
Type string `json:"type"`
}
// PiHVersion contains Pi-Hole API version.
type PiHVersion struct {
Version float32 `json:"version"`
}
// PiHSummary contains Pi-Hole summary data.
type PiHSummary struct {
AdsBlocked string `json:"ads_blocked_today"`
AdsPercentage string `json:"ads_percentage_today"`
ClientsEverSeen string `json:"clients_ever_seen"`
DNSQueries string `json:"dns_queries_today"`
DomainsBlocked string `json:"domains_being_blocked"`
QueriesCached string `json:"queries_cached"`
QueriesForwarded string `json:"queries_forwarded"`
Status string `json:"status"`
UniqueClients string `json:"unique_clients"`
UniqueDomains string `json:"unique_domains"`
}
// PiHTimeData represents statistics over time.
// Each record contains number of queries/blocked ads within 10min timeframe.
type PiHTimeData struct {
AdsOverTime map[string]int `json:"ads_over_time"`
DomainsOverTime map[string]int `json:"domains_over_time"`
}
// PiHTopItems contains top queries and top blocked domains.
// Format: "DNS": Frequency
type PiHTopItems struct {
Queries map[string]int `json:"top_queries"`
Blocked map[string]int `json:"top_ads"`
}
// PiHTopClients represents Pi-Hole client IPs with corresponding number of requests.
type PiHTopClients struct {
Clients map[string]int `json:"top_sources"`
}
// PiHForwardDestinations represents number of queries that have been forwarded and the target.
type PiHForwardDestinations struct {
Destinations map[string]float32 `json:"forward_destinations"`
}
// PiHQueryTypes contains DNS query type and number of queries.
type PiHQueryTypes struct {
Types map[string]float32 `json:"querytypes"`
}
// PiHQueries contains all DNS queries.
// This is slice of slices of strings.
// Each slice contains: timestamp of query, type of query (IPv4, IPv6), requested DNS, requesting client, answer type.
// Answer types: 1 = blocked by gravity.list, 2 = forwarded to upstream server, 3 = answered by local cache, 4 = blocked by wildcard blocking
type PiHQueries struct {
Data [][]string `json:"data"`
}
// Get performes API request. Returns slice of bytes.
func (ph *PiHConnector) Get(endpoint string) []byte {
var requestString = "http://" + ph.Host + "/admin/api.php?" + endpoint
if ph.Token != "" {
requestString += "&auth=" + ph.Token
}
resp, err := http.Get(requestString)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
return body
}
// Type returns Pi-Hole API type as a PiHType object.
func (ph *PiHConnector) Type() PiHType {
bs := ph.Get("type")
s := &PiHType{}
err := json.Unmarshal(bs, s)
if err != nil {
log.Fatal(err)
}
return *s
}
// Version returns Pi-Hole API version as an object.
func (ph *PiHConnector) Version() PiHVersion {
bs := ph.Get("version")
s := &PiHVersion{}
err := json.Unmarshal(bs, s)
if err != nil {
log.Fatal(err)
}
return *s
}
// Summary returns statistics in formatted style.
func (ph *PiHConnector) Summary() PiHSummary {
bs := ph.Get("summary")
s := &PiHSummary{}
err := json.Unmarshal(bs, s)
if err != nil {
log.Fatal(err)
}
return *s
}
// TimeData returns PiHTimeData object which contains requests statistics.
func (ph *PiHConnector) TimeData() PiHTimeData {
bs := ph.Get("overTimeData10mins")
s := &PiHTimeData{}
err := json.Unmarshal(bs, s)
if err != nil {
log.Fatal(err)
}
return *s
}
// Top returns top blocked and requested domains.
func (ph *PiHConnector) Top(n int) PiHTopItems {
bs := ph.Get("topItems=" + strconv.Itoa(n))
s := &PiHTopItems{}
err := json.Unmarshal(bs, s)
if err != nil {
log.Fatal(err)
}
return *s
}
// Clients returns top clients.
func (ph *PiHConnector) Clients(n int) PiHTopClients {
bs := ph.Get("topClients=" + strconv.Itoa(n))
s := &PiHTopClients{}
err := json.Unmarshal(bs, s)
if err != nil {
log.Fatal(err)
}
return *s
}
// ForwardDestinations returns forward destinations (DNS servers).
func (ph *PiHConnector) ForwardDestinations() PiHForwardDestinations {
bs := ph.Get("getForwardDestinations")
s := &PiHForwardDestinations{}
err := json.Unmarshal(bs, s)
if err != nil {
log.Fatal(err)
}
return *s
}
// QueryTypes returns DNS query type and frequency as a PiHQueryTypes object.
func (ph *PiHConnector) QueryTypes() PiHQueryTypes {
bs := ph.Get("getQueryTypes")
s := &PiHQueryTypes{}
err := json.Unmarshal(bs, s)
if err != nil {
log.Fatal(err)
}
return *s
}
// Queries returns all DNS queries as a PiHQueries object.
func (ph *PiHConnector) Queries() PiHQueries {
bs := ph.Get("getAllQueries")
s := &PiHQueries{}
err := json.Unmarshal(bs, s)
if err != nil {
log.Fatal(err)
}
return *s
}
// Enable enables Pi-Hole server.
func (ph *PiHConnector) Enable() error {
bs := ph.Get("enable")
resp := make(map[string]string)
err := json.Unmarshal(bs, &resp)
if err != nil {
log.Fatal(err)
}
if resp["status"] != "enabled" {
return errors.New("something went wrong")
}
return nil
}
// Disable disables Pi-Hole server permanently.
func (ph *PiHConnector) Disable() error {
bs := ph.Get("disable")
resp := make(map[string]string)
err := json.Unmarshal(bs, &resp)
if err != nil {
log.Fatal(err)
}
if resp["status"] != "disabled" {
return errors.New("something went wrong")
}
return nil
}
// RecentBlocked returns string with the last blocked DNS record.
func (ph *PiHConnector) RecentBlocked() string {
bs := ph.Get("recentBlocked")
return string(bs)
}
// Show returns 24h Summary of PiHole System.
func (ph *PiHSummary) Show() {
fmt.Println("=== 24h Summary:")
fmt.Printf("- Blocked Domains: %s\n", ph.AdsBlocked)
fmt.Printf("- Blocked Percentage: %s\n", ph.AdsPercentage)
fmt.Printf("- Queries: %s\n", ph.DNSQueries)
fmt.Printf("- Clients Ever Seen: %s\n", ph.ClientsEverSeen)
}
// ShowBlocked returns sorted top Blocked domains over last 24h.
func (ph *PiHTopItems) ShowBlocked() {
reverseMapBlocked := make(map[int]string)
var freqBlocked []int
for k, v := range ph.Blocked {
reverseMapBlocked[v] = k
freqBlocked = append(freqBlocked, v)
}
sort.Ints(freqBlocked)
fmt.Println("=== Blocked domains over last 24h:")
for i := len(freqBlocked) - 1; i >= 0; i-- {
fmt.Printf("- %s : %d\n", reverseMapBlocked[freqBlocked[i]], freqBlocked[i])
}
}
// ShowQueries returns sorted top queries over last 24h.
func (ph *PiHTopItems) ShowQueries() {
reverseMapQueries := make(map[int]string)
var freqQueries []int
for k, v := range ph.Queries {
reverseMapQueries[v] = k
freqQueries = append(freqQueries, v)
}
sort.Ints(freqQueries)
fmt.Println("=== Queries over last 24h:")
for i := len(freqQueries) - 1; i >= 0; i-- {
fmt.Printf("- %s : %d\n", reverseMapQueries[freqQueries[i]], freqQueries[i])
}
}
// Show returns sorted top clients over last 24h.
func (ph *PiHTopClients) Show() {
reverseMapClients := make(map[int]string)
var freqClients []int
for k, v := range ph.Clients {
reverseMapClients[v] = k
freqClients = append(freqClients, v)
}
sort.Ints(freqClients)
fmt.Println("=== Clients over last 24h:")
for i := len(freqClients) - 1; i >= 0; i-- {
fmt.Printf("- %s : %d\n", reverseMapClients[freqClients[i]], freqClients[i])
}
}