// Init of configuration files could also be done via Config crate. // But at this point of development it seems like unjustified overhead. extern crate serde_json; use std::fs::File; use std::collections::HashMap; use std::fs; use std::io::prelude::*; //use std::thread::{spawn, JoinHandle}; //use std::sync::mpsc::{channel, Receiver}; const PCAPNG_SIGNATURE: [u8; 4] = [0x0a, 0x0d, 0x0d, 0x0a]; const PCAP_SIGNATURE: [u8; 4] = [0xd4, 0xc3, 0xb2, 0xa1]; const PCAP_SIGNATURE_BE: [u8; 4] = [0xa1, 0xb2, 0xc3, 0xa1]; pub struct Config { pub filter: String, pub regex_filter: String, pub insert_max: usize, pub pcap_file: String, pub connection: String, pub device: String, pub is_device: bool, pub pcap_dir: String, } pub fn from_json_file() -> Option { let config_file = File::open("parser.json").expect("file should open read only"); let json: serde_json::Value = serde_json::from_reader(config_file).unwrap(); Some(Config { filter: json.get("filter").unwrap().as_str().unwrap().to_owned(), regex_filter: json.get("regex_filter").unwrap().as_str().unwrap().to_owned(), insert_max: json.get("insert_max").unwrap().as_u64().unwrap() as usize, pcap_file: json.get("pcap_file").unwrap().as_str().unwrap().to_owned(), connection: format!( "host={} user={} password={}", json.get("database_host").unwrap().as_str().unwrap(), json.get("database_user").unwrap().as_str().unwrap(), json.get("database_password").unwrap().as_str().unwrap(), ), device: json .get("parse_device") .unwrap() .as_str() .unwrap() .to_owned(), is_device: json.get("from_device").unwrap().as_bool().unwrap(), pcap_dir: json.get("pcap_dir").unwrap().as_str().unwrap().to_owned(), }) } pub fn map_pcap_dir ( pcap_dir: &str ) -> Option> { let mut pcap_map = HashMap::new(); if let Ok(entries) = fs::read_dir(pcap_dir) { for entry in entries { if let Ok(entry) = entry { if let Ok(_file_type) = entry.file_type() { if entry.metadata().unwrap().is_file() { let mut magic_number: [u8; 4] = [0; 4]; let _signature = File::open(entry.path().to_owned()) .unwrap() .read_exact(&mut magic_number) .unwrap(); match magic_number { PCAPNG_SIGNATURE => pcap_map.insert(entry.path(), entry.metadata().unwrap()), PCAP_SIGNATURE | PCAP_SIGNATURE_BE => pcap_map.insert(entry.path(), entry.metadata().unwrap()), _ => None, }; // println!("{:?}", &entry.metadata().unwrap().modified()); } } else { println!("Couldn't get file type for {:?}", entry.path()); } } } } Some(pcap_map) }