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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
use crate::fake_hash_map::FakeHashMap;
use serde::de::DeserializeOwned;
use std::collections::HashMap;
use std::net::IpAddr;
pub mod api_types;
mod custom_deserializers;
pub mod errors;
mod fake_hash_map;
pub mod ftl_types;
use crate::api_types::*;
use std::borrow::Borrow;

const NO_PARAMS: [(&str, &str); 0] = [];

trait PiHoleAPIHost {
    fn get_host(&self) -> &str;
}

trait PiHoleAPIKey {
    fn get_api_key(&self) -> &str;
}

/// Pi Hole API Struct
#[derive(Debug)]
pub struct PiHoleAPIConfig {
    /// Pi Hole host
    host: String,
}

impl PiHoleAPIConfig {
    /// Creates a new Pi Hole API instance.
    /// `host` must begin with the protocol e.g. http:// or https://
    pub fn new(host: String) -> Self {
        Self { host }
    }
}

/// Pi Hole API Struct
#[derive(Debug)]
pub struct PiHoleAPIConfigWithKey {
    /// Pi Hole host
    host: String,

    /// API key
    api_key: String,
}

impl PiHoleAPIConfigWithKey {
    /// Creates a new Pi Hole API instance.
    /// `host` must begin with the protocol e.g. http:// or https://
    pub fn new(host: String, api_key: String) -> Self {
        Self { host, api_key }
    }
}

impl PiHoleAPIHost for PiHoleAPIConfig {
    fn get_host(&self) -> &str {
        &self.host
    }
}

impl PiHoleAPIHost for PiHoleAPIConfigWithKey {
    fn get_host(&self) -> &str {
        &self.host
    }
}

impl PiHoleAPIKey for PiHoleAPIConfigWithKey {
    fn get_api_key(&self) -> &str {
        &self.api_key
    }
}

pub trait UnauthenticatedPiHoleAPI {
    /// Get statistics in a raw format (no number format)
    fn get_summary_raw(&self) -> Result<SummaryRaw, errors::APIError>;

    /// Get statistics in a formatted style
    fn get_summary(&self) -> Result<Summary, errors::APIError>;

    /// Get statistics on the number of domains and ads for each 10 minute period
    fn get_over_time_data_10_mins(&self) -> Result<OverTimeData, errors::APIError>;

    /// Get the Pi-Hole version.
    fn get_version(&self) -> Result<u32, errors::APIError>;

    /// Get the detailed Pi-Hole versions for core, FTL and web interface.
    fn get_versions(&self) -> Result<Versions, errors::APIError>;
}

fn simple_json_request<T, I, K, V>(
    host: &str,
    path_query: &str,
    params: I,
) -> Result<T, errors::APIError>
where
    T: DeserializeOwned,
    I: IntoIterator,
    K: AsRef<str>,
    V: AsRef<str>,
    <I as IntoIterator>::Item: Borrow<(K, V)>,
{
    let path = format!("{}{}", host, path_query);
    let response = reqwest::blocking::get(
        reqwest::Url::parse_with_params(&path, params).expect("Invalid URL"),
    )?;
    Ok(response.json()?)
}

impl<T> UnauthenticatedPiHoleAPI for T
where
    T: PiHoleAPIHost,
{
    fn get_summary_raw(&self) -> Result<SummaryRaw, errors::APIError> {
        simple_json_request(self.get_host(), "/admin/api.php?summaryRaw", &NO_PARAMS)
    }

    fn get_summary(&self) -> Result<Summary, errors::APIError> {
        simple_json_request(self.get_host(), "/admin/api.php?summary", &NO_PARAMS)
    }

    fn get_over_time_data_10_mins(&self) -> Result<OverTimeData, errors::APIError> {
        simple_json_request(
            self.get_host(),
            "/admin/api.php?overTimeData10mins",
            &NO_PARAMS,
        )
    }

    /// Get simple PiHole version
    fn get_version(&self) -> Result<u32, errors::APIError> {
        let raw_version: Version =
            simple_json_request(self.get_host(), "/admin/api.php?version", &NO_PARAMS)?;
        Ok(raw_version.version)
    }

    /// Get versions of core, FTL and web and if updates are available
    fn get_versions(&self) -> Result<Versions, errors::APIError> {
        simple_json_request(self.get_host(), "/admin/api.php?versions", &NO_PARAMS)
    }
}

pub trait AuthenticatedPiHoleAPI {
    /// Get the top domains and ads and the number of queries for each. Limit the number of items with `count`.
    fn get_top_items(&self, count: &Option<u32>) -> Result<TopItems, errors::APIError>;

    /// Get the top clients and the number of queries for each. Limit the number of items with `count`.
    fn get_top_clients(&self, count: &Option<u32>) -> Result<TopClients, errors::APIError>;

    /// Get the top clients blocked and the number of queries for each. Limit the number of items with `count`.
    fn get_top_clients_blocked(
        &self,
        count: Option<u32>,
    ) -> Result<TopClientsBlocked, errors::APIError>;

    /// Get the percentage of queries forwarded to each target.
    fn get_forward_destinations(
        &self,
        unsorted: bool,
    ) -> Result<ForwardDestinations, errors::APIError>;

    /// Get the number of queries per type.
    fn get_query_types(&self) -> Result<QueryTypes, errors::APIError>;

    /// Get all DNS query data. Limit the number of items with `count`.
    fn get_all_queries(&self, count: u32) -> Result<Vec<Query>, errors::APIError>;

    /// Enable the Pi-Hole.
    fn enable(&self) -> Result<Status, errors::APIError>;

    /// Disable the Pi-Hole for `seconds` seconds.
    fn disable(&self, seconds: u64) -> Result<Status, errors::APIError>;

    /// Get statistics about the DNS cache.
    fn get_cache_info(&self) -> Result<CacheInfo, errors::APIError>;

    /// Get hostname and IP for hosts
    fn get_client_names(&self) -> Result<Vec<ClientName>, errors::APIError>;

    /// Get queries by client over time. Maps timestamp to the number of queries by clients.
    /// Order of clients in the Vector is the same as for get_client_names
    fn get_over_time_data_clients(&self) -> Result<HashMap<String, Vec<u64>>, errors::APIError>;

    /// Get information about network clients.
    fn get_network(&self) -> Result<Network, errors::APIError>;

    /// Get the total number of queries received.
    fn get_queries_count(&self) -> Result<u64, errors::APIError>;

    /// Add domains to a custom white/blacklist.
    /// Acceptable lists are: `white`, `black`, `white_regex`, `black_regex`, `white_wild`, `black_wild`, `audit`.
    fn list_add(
        &self,
        domain: &str,
        list: &str,
    ) -> Result<ListModificationResponse, errors::APIError>;

    /// Remove domain to a custom white/blacklist.
    /// Acceptable lists are: `white`, `black`, `white_regex`, `black_regex`, `white_wild`, `black_wild`, `audit`.
    fn list_remove(
        &self,
        domain: &str,
        list: &str,
    ) -> Result<ListModificationResponse, errors::APIError>;

    /// Get a list of domains on a particular custom white/blacklist
    /// Acceptable lists are: `white`, `black`, `white_regex`, `black_regex`, `white_wild`, `black_wild`, `audit`.
    fn list_get_domains(
        &self,
        list: &str,
    ) -> Result<Vec<CustomListDomainDetails>, errors::APIError>;

    /// Get a list of custom DNS records
    fn get_custom_dns_records(&self) -> Result<Vec<CustomDNSRecord>, errors::APIError>;

    /// Add a custom DNS record
    fn add_custom_dns_record(
        &self,
        ip: &IpAddr,
        domain: &str,
    ) -> Result<ListModificationResponse, errors::APIError>;

    /// Delete a custom DNS record
    fn delete_custom_dns_record(
        &self,
        ip: &IpAddr,
        domain: &str,
    ) -> Result<ListModificationResponse, errors::APIError>;

    /// Get a list of custom CNAME records
    fn get_custom_cname_records(&self) -> Result<Vec<CustomCNAMERecord>, errors::APIError>;

    /// Add a custom CNAME record
    fn add_custom_cname_record(
        &self,
        domain: &str,
        target_domain: &str,
    ) -> Result<ListModificationResponse, errors::APIError>;

    /// Delete a custom CNAME record
    fn delete_custom_cname_record(
        &self,
        domain: &str,
        target_domain: &str,
    ) -> Result<ListModificationResponse, errors::APIError>;

    /// Get max logage
    fn get_max_logage(&self) -> Result<f32, errors::APIError>;
}

fn authenticated_json_request<'a, T, I, K, V>(
    host: &str,
    path_query: &str,
    params: I,
    api_key: &'a str,
) -> Result<T, errors::APIError>
where
    T: DeserializeOwned,
    I: IntoIterator<Item = (K, V)>,
    K: AsRef<str>,
    V: AsRef<str>,
    // <I as IntoIterator>::Item: Borrow<(K, V)>,
{
    let path = format!("{}{}", host, path_query);
    let auth_params = [("auth".to_string(), api_key.to_string())];
    let converted_params: Vec<(String, String)> = params
        .into_iter()
        .map(|(k, v)| (k.as_ref().to_string(), v.as_ref().to_string()))
        .collect();
    let url =
        reqwest::Url::parse_with_params(&path, converted_params.iter().chain(auth_params.iter()))
            .expect("Invalid URL");
    let response_text = reqwest::blocking::get(url)?.text()?;
    errors::detect_response_errors(&response_text)?;
    match serde_json::from_str::<T>(&response_text) {
        Ok(response) => Ok(response),
        Err(error) => Err(error.into()),
    }
}

impl<T> AuthenticatedPiHoleAPI for T
where
    T: PiHoleAPIHost + PiHoleAPIKey,
{
    fn get_top_items(&self, count: &Option<u32>) -> Result<TopItems, errors::APIError> {
        authenticated_json_request(
            self.get_host(),
            "/admin/api.php",
            [("topItems", count.unwrap_or(10).to_string())],
            self.get_api_key(),
        )
    }

    fn get_top_clients(&self, count: &Option<u32>) -> Result<TopClients, errors::APIError> {
        authenticated_json_request(
            self.get_host(),
            "/admin/api.php?",
            [("topClients", count.unwrap_or(10).to_string())],
            self.get_api_key(),
        )
    }

    fn get_top_clients_blocked(
        &self,
        count: Option<u32>,
    ) -> Result<TopClientsBlocked, errors::APIError> {
        authenticated_json_request(
            self.get_host(),
            "/admin/api.php?",
            [("topClientsBlocked", count.unwrap_or(10).to_string())],
            self.get_api_key(),
        )
    }

    fn get_forward_destinations(
        &self,
        unsorted: bool,
    ) -> Result<ForwardDestinations, errors::APIError> {
        let param_value = if unsorted { "unsorted" } else { "" };
        authenticated_json_request(
            self.get_host(),
            "/admin/api.php",
            [("getForwardDestinations", param_value)],
            self.get_api_key(),
        )
    }

    fn get_query_types(&self) -> Result<QueryTypes, errors::APIError> {
        authenticated_json_request(
            self.get_host(),
            "/admin/api.php",
            [("getQueryTypes", "")],
            self.get_api_key(),
        )
    }

    fn get_all_queries(&self, count: u32) -> Result<Vec<Query>, errors::APIError> {
        let mut raw_data: HashMap<String, Vec<Query>> = authenticated_json_request(
            self.get_host(),
            "/admin/api.php",
            [("getAllQueries", count.to_string())],
            self.get_api_key(),
        )?;
        Ok(raw_data.remove("data").unwrap())
    }

    fn enable(&self) -> Result<Status, errors::APIError> {
        authenticated_json_request(
            self.get_host(),
            "/admin/api.php?",
            [("enable", "")],
            self.get_api_key(),
        )
    }

    fn disable(&self, seconds: u64) -> Result<Status, errors::APIError> {
        authenticated_json_request(
            self.get_host(),
            "/admin/api.php",
            [("disable", seconds.to_string())],
            self.get_api_key(),
        )
    }

    fn get_cache_info(&self) -> Result<CacheInfo, errors::APIError> {
        let mut raw_data: HashMap<String, CacheInfo> = authenticated_json_request(
            self.get_host(),
            "/admin/api.php",
            [("getCacheInfo", "")],
            self.get_api_key(),
        )?;
        Ok(raw_data.remove("cacheinfo").expect("Missing cache info"))
    }

    fn get_client_names(&self) -> Result<Vec<ClientName>, errors::APIError> {
        let mut raw_data: HashMap<String, Vec<ClientName>> = authenticated_json_request(
            self.get_host(),
            "/admin/api.php",
            [("getClientNames", "")],
            self.get_api_key(),
        )?;
        Ok(raw_data
            .remove("clients")
            .expect("Missing clients attribute"))
    }

    fn get_over_time_data_clients(&self) -> Result<HashMap<String, Vec<u64>>, errors::APIError> {
        let mut raw_data: HashMap<String, FakeHashMap<String, Vec<u64>>> =
            authenticated_json_request(
                self.get_host(),
                "/admin/api.php",
                [("overTimeDataClients", "")],
                self.get_api_key(),
            )?;

        Ok(raw_data
            .remove("over_time")
            .expect("Missing over_time attribute")
            .into())
    }

    fn get_network(&self) -> Result<Network, errors::APIError> {
        authenticated_json_request(
            self.get_host(),
            "/admin/api_db.php",
            [("network", "")],
            self.get_api_key(),
        )
    }

    fn get_queries_count(&self) -> Result<u64, errors::APIError> {
        let raw_data: HashMap<String, u64> = authenticated_json_request(
            self.get_host(),
            "/admin/api_db.php",
            [("getQueriesCount", "")],
            self.get_api_key(),
        )?;
        Ok(*raw_data.get("count").expect("Missing count attribute"))
    }

    fn list_add(
        &self,
        domain: &str,
        list: &str,
    ) -> Result<ListModificationResponse, errors::APIError> {
        authenticated_json_request(
            self.get_host(),
            "/admin/api.php",
            [("add", domain), ("list", list)],
            self.get_api_key(),
        )
    }

    fn list_remove(
        &self,
        domain: &str,
        list: &str,
    ) -> Result<ListModificationResponse, errors::APIError> {
        authenticated_json_request(
            self.get_host(),
            "/admin/api.php",
            [("sub", domain), ("list", list)],
            self.get_api_key(),
        )
    }

    fn list_get_domains(
        &self,
        list: &str,
    ) -> Result<Vec<CustomListDomainDetails>, errors::APIError> {
        // if not "add" or "sub", api.php defaults to the "get_domains" action
        let mut raw_data: HashMap<String, Vec<CustomListDomainDetails>> =
            authenticated_json_request(
                self.get_host(),
                "/admin/api.php",
                [("get", ""), ("list", list)],
                self.get_api_key(),
            )?;
        Ok(raw_data.remove("data").unwrap())
    }

    fn get_custom_dns_records(&self) -> Result<Vec<CustomDNSRecord>, errors::APIError> {
        let mut raw_data: HashMap<String, Vec<Vec<String>>> = authenticated_json_request(
            self.get_host(),
            "/admin/api.php",
            [("customdns", ""), ("action", "get")],
            self.get_api_key(),
        )?;

        Ok(raw_data
            .remove("data")
            .unwrap()
            .into_iter()
            .map(|list_record| CustomDNSRecord {
                domain: list_record[0].clone(),
                ip_address: list_record[1].parse().unwrap(),
            })
            .collect())
    }

    fn add_custom_dns_record(
        &self,
        ip: &IpAddr,
        domain: &str,
    ) -> Result<ListModificationResponse, errors::APIError> {
        authenticated_json_request(
            self.get_host(),
            "/admin/api.php",
            [
                ("customdns", ""),
                ("action", "add"),
                ("ip", &ip.to_string()),
                ("domain", domain),
            ],
            self.get_api_key(),
        )
    }

    fn delete_custom_dns_record(
        &self,
        ip: &IpAddr,
        domain: &str,
    ) -> Result<ListModificationResponse, errors::APIError> {
        authenticated_json_request(
            self.get_host(),
            "/admin/api.php",
            [
                ("customdns", ""),
                ("action", "delete"),
                ("ip", &ip.to_string()),
                ("domain", domain),
            ],
            self.get_api_key(),
        )
    }

    fn get_custom_cname_records(&self) -> Result<Vec<CustomCNAMERecord>, errors::APIError> {
        let mut raw_data: HashMap<String, Vec<Vec<String>>> = authenticated_json_request(
            self.get_host(),
            "/admin/api.php",
            [("customcname", ""), ("action", "get")],
            self.get_api_key(),
        )?;

        Ok(raw_data
            .remove("data")
            .unwrap()
            .into_iter()
            .map(|list_record| CustomCNAMERecord {
                domain: list_record[0].clone(),
                target_domain: list_record[1].clone(),
            })
            .collect())
    }

    fn add_custom_cname_record(
        &self,
        domain: &str,
        target_domain: &str,
    ) -> Result<ListModificationResponse, errors::APIError> {
        authenticated_json_request(
            self.get_host(),
            "/admin/api.php",
            [
                ("customcname", ""),
                ("action", "add"),
                ("domain", domain),
                ("target", target_domain),
            ],
            self.get_api_key(),
        )
    }

    fn delete_custom_cname_record(
        &self,
        domain: &str,
        target_domain: &str,
    ) -> Result<ListModificationResponse, errors::APIError> {
        authenticated_json_request(
            self.get_host(),
            "/admin/api.php",
            [
                ("customcname", ""),
                ("action", "delete"),
                ("domain", domain),
                ("target", target_domain),
            ],
            self.get_api_key(),
        )
    }

    fn get_max_logage(&self) -> Result<f32, errors::APIError> {
        let mut raw_data: HashMap<String, f32> = authenticated_json_request(
            self.get_host(),
            "/admin/api.php",
            [("getMaxlogage", "")],
            self.get_api_key(),
        )?;
        Ok(raw_data.remove("maxlogage").unwrap())
    }
}