Skip to content

Commit

Permalink
minor rename
Browse files Browse the repository at this point in the history
  • Loading branch information
ragibkl committed Aug 22, 2024
1 parent e7b49c3 commit a4929e7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ use axum::{
};
use handlebars::Handlebars;

use crate::logs_store::{DNSQueryLog, LogsStore};
use crate::logs_store::{LogsStore, QueryLog};

static GET_LOGS_TEMPLATE: &str = include_str!("./get_logs.hbs");

#[derive(serde::Serialize, Debug, Clone)]
pub struct GetLogsOutput {
ip: String,
queries: Vec<DNSQueryLog>,
queries: Vec<QueryLog>,
}

fn get_ip(addr: SocketAddr) -> String {
Expand Down
42 changes: 21 additions & 21 deletions src/logs_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct RawLog {
}

#[derive(serde::Serialize, Debug, Clone, PartialEq)]
pub struct DNSQueryLog {
pub struct QueryLog {
ip: String,
query_time: chrono::DateTime<Utc>,
question: String,
Expand All @@ -44,7 +44,7 @@ fn parse_query_time(query_time: &str) -> DateTime<Utc> {
query_time.and_utc()
}

fn extract_query(raw_log: &RawLog) -> DNSQueryLog {
fn extract_query(raw_log: &RawLog) -> QueryLog {
let ip = raw_log.message.query_address.to_string();
let query_time = parse_query_time(&raw_log.message.query_time);
let response_message = &raw_log.message.response_message;
Expand All @@ -67,15 +67,15 @@ fn extract_query(raw_log: &RawLog) -> DNSQueryLog {
.map(|s| s.to_string())
.collect();

DNSQueryLog {
QueryLog {
ip,
query_time,
question,
answers,
}
}

fn extract_queries(content: &str) -> HashMap<String, Vec<DNSQueryLog>> {
fn extract_queries(content: &str) -> HashMap<String, Vec<QueryLog>> {
let mut raw_logs: Vec<RawLog> = Vec::new();
for document in serde_yaml::Deserializer::from_str(content) {
let Ok(log) = RawLog::deserialize(document) else {
Expand All @@ -85,7 +85,7 @@ fn extract_queries(content: &str) -> HashMap<String, Vec<DNSQueryLog>> {
raw_logs.push(log);
}

let mut logs_store: HashMap<String, Vec<DNSQueryLog>> = HashMap::new();
let mut logs_store: HashMap<String, Vec<QueryLog>> = HashMap::new();
for raw_log in raw_logs.into_iter() {
let query_log = extract_query(&raw_log);

Expand All @@ -104,31 +104,31 @@ fn extract_queries(content: &str) -> HashMap<String, Vec<DNSQueryLog>> {

#[derive(Debug, Clone, Default)]
pub struct LogsStore {
logs_store: Arc<Mutex<HashMap<String, Arc<Mutex<Vec<DNSQueryLog>>>>>>,
logs_store: Arc<Mutex<HashMap<String, Arc<Mutex<Vec<QueryLog>>>>>>,
}

impl LogsStore {
pub fn remove_expired_logs(&self) {
let query_time_cutoff = Utc::now() - Duration::minutes(10);

let logs_store_guard = self.logs_store.lock().unwrap();
for queries in logs_store_guard.values() {
queries
for query_logs in logs_store_guard.values() {
query_logs
.lock()
.unwrap()
.retain(|q| q.query_time > query_time_cutoff);
}
}

pub fn merge_logs(&self, logs: HashMap<String, Vec<DNSQueryLog>>) {
pub fn merge_logs(&self, logs_hash_map: HashMap<String, Vec<QueryLog>>) {
let mut logs_store_guard = self.logs_store.lock().unwrap();
for (ip, queries) in logs.into_iter() {
match logs_store_guard.get(&ip).cloned() {
Some(existing) => {
existing.lock().unwrap().extend(queries);
for (ip, logs) in logs_hash_map.into_iter() {
match logs_store_guard.get(&ip) {
Some(existing_logs) => {
existing_logs.lock().unwrap().extend(logs);
}
None => {
logs_store_guard.insert(ip, Arc::new(Mutex::new(queries)));
logs_store_guard.insert(ip, Arc::new(Mutex::new(logs)));
}
}
}
Expand All @@ -139,14 +139,14 @@ impl LogsStore {

let content = std::fs::read_to_string("./logs.yaml").unwrap_or_default();
let _ = std::fs::write("./logs.yaml", "");
let logs_store = extract_queries(&content);
let logs_hash_map = extract_queries(&content);

self.merge_logs(logs_store);
self.merge_logs(logs_hash_map);
}

pub fn get_logs_for_ip(&self, ip: &str) -> Vec<DNSQueryLog> {
match self.logs_store.lock().unwrap().get(ip) {
Some(v) => v.lock().unwrap().clone(),
pub fn get_logs_for_ip(&self, ip: &str) -> Vec<QueryLog> {
match self.logs_store.lock().unwrap().get(ip).cloned() {
Some(logs) => logs.lock().unwrap().clone(),
None => Vec::new(),
}
}
Expand All @@ -158,7 +158,7 @@ mod tests {

use chrono::TimeZone;

use crate::logs_store::{parse_query_time, DNSQueryLog};
use crate::logs_store::{parse_query_time, QueryLog};

use super::extract_queries;

Expand Down Expand Up @@ -204,7 +204,7 @@ message:

let expected = HashMap::from([(
"127.0.0.1".to_string(),
vec![DNSQueryLog {
vec![QueryLog {
ip: "127.0.0.1".to_string(),
query_time: chrono::Utc.with_ymd_and_hms(2022, 2, 26, 9, 25, 7).unwrap(),
question: ";zedo.com.IN A".to_string(),
Expand Down

0 comments on commit a4929e7

Please sign in to comment.