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
use reqwest::{
  header::{HeaderValue, CONTENT_LENGTH, AUTHORIZATION},
};
use serde::Deserialize;

#[derive(Deserialize)]
pub struct QueryResponse {
    pub data: Vec<Query>,
}

#[derive(Deserialize)]
pub struct Query {
    pub cached: bool,
    pub client: String,
    pub upstream: String,
    #[serde(rename = "elapsedMs")]
    pub elapsed_ms: String,
    pub question: Question,
    pub reason: String,
    pub time: String,
}

#[derive(Deserialize)]
pub struct Question {
    pub class: String,
    pub name: String,
    #[serde(rename = "type")]
    pub question_type: String,
}

pub async fn fetch_adguard_query_log(
  client: &reqwest::Client,
  endpoint: &str,
  username: &str,
  password: &str,
) -> Result<QueryResponse, anyhow::Error> {
  let auth_string = format!("{}:{}", username, password);
  let auth_header_value = format!("Basic {}", base64::encode(&auth_string));
  let mut headers = reqwest::header::HeaderMap::new();
  headers.insert(AUTHORIZATION, auth_header_value.parse()?);
  headers.insert(CONTENT_LENGTH, HeaderValue::from_static("0"));

  let url = format!("{}/control/querylog", endpoint);
  let response = client.get(&url).headers(headers).send().await?;
  if !response.status().is_success() {
      return Err(anyhow::anyhow!("Request failed with status code {}", response.status()));
  }

  let data = response.json().await?;
  Ok(data)
}