From 85a6de8aba386b69356a5293007fbf16620524d1 Mon Sep 17 00:00:00 2001 From: gurkenhabicht Date: Sun, 6 Aug 2023 21:23:28 +0200 Subject: [PATCH] moved from JSON configuration file to TOML --- src/configure/mod.rs | 99 +++++++++++++++++++++++++++----------------- src/main.rs | 20 +++++++-- src/parser.toml | 11 +++++ 3 files changed, 89 insertions(+), 41 deletions(-) create mode 100644 src/parser.toml diff --git a/src/configure/mod.rs b/src/configure/mod.rs index 3ffa766..2c5e153 100644 --- a/src/configure/mod.rs +++ b/src/configure/mod.rs @@ -3,6 +3,8 @@ extern crate serde_json; use byteorder::{ByteOrder, LittleEndian}; +use serde::Deserialize; +use toml; use std::collections::HashMap; use std::fs; use std::fs::File; @@ -14,18 +16,41 @@ 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]; +#[derive(Debug, Deserialize)] pub struct Config { pub filter: String, pub regex_filter: String, pub insert_max: usize, pub pcap_file: String, pub tablename: String, - pub connection: String, + pub database_host: String, + pub database_user: String, + pub database_password: String, pub device: String, - pub is_device: bool, + pub from_device: bool, pub pcap_dir: String, } +impl Config { + pub fn from_file(file_path: &str) -> Result> { + let toml_str = fs::read_to_string(file_path)?; + let config: Config = toml::from_str(&toml_str)?; + + Ok(config) + } +} + +impl Config { + pub fn build_postgres_connection_string(&self) -> String { + format!( + "host={} user={} password={}", + self.database_host, + self.database_user, + self.database_password + ) + } +} + #[derive(Debug)] pub struct FileInfo { pub encapsulation_type: u16, @@ -44,41 +69,41 @@ impl FileInfo { } } -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(), // Not in use atm - tablename: json - .get("database_tablename") - .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 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(), // Not in use atm +// tablename: json +// .get("database_tablename") +// .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(), +// }) +// } /* File signature and encapsulation type from file diff --git a/src/main.rs b/src/main.rs index c366d86..48416c0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,7 +37,17 @@ fn query_string(insert_max: &usize, table_name: &str) -> String { #[tokio::main] async fn main() -> Result<(), Box> { /* Init values from file */ - let config: configure::Config = configure::from_json_file().unwrap(); + //let config: configure::Config = configure::from_json_file().unwrap(); + let config_file = "parser.toml"; + let config: configure::Config = configure::Config::from_file(config_file)?; + println!("filter: {}", config.filter); + println!("regex_filter: {}", config.regex_filter); + println!("insert_max: {}", config.insert_max); + println!("pcap_file:{}", config.pcap_file); + println!("tablename:{}", config.tablename); + println!("device: {}", config.device); + println!("is_device: {}", config.from_device); + println!("pcap_dir {}", config.pcap_dir); let pcap_map = configure::map_pcap_dir(&config.pcap_dir).unwrap(); // TODO: Create db table with pcap file hashes @@ -48,14 +58,16 @@ async fn main() -> Result<(), Box> { // println!("{:?}", pcap_map); /* db connection */ + + let connection_string = configure::Config::build_postgres_connection_string(&config); - if let Err(err) = database::postgresql::connect_to_postgres(&config.connection).await { + if let Err(err) = database::postgresql::connect_to_postgres(&connection_string).await { println!("Error chain: {:?}", err); println!("Error: {}", err); eprintln!("Error chain: {:?}", err); } - let client = database::postgresql::connect_to_postgres(&config.connection).await.unwrap(); + let client = database::postgresql::connect_to_postgres(&connection_string).await.unwrap(); client .execute(&*format!("DROP TABLE IF EXISTS {}", &config.tablename), &[]) .await?; @@ -70,7 +82,7 @@ async fn main() -> Result<(), Box> { .await?; /* device or file input */ - match config.is_device { + match config.from_device { FROM_FILE => { for (_pcap_file, _pcap_info) in pcap_map.iter() { //println!("{:?}: {:?}", &_pcap_file, &_pcap_info); diff --git a/src/parser.toml b/src/parser.toml new file mode 100644 index 0000000..e5cefc6 --- /dev/null +++ b/src/parser.toml @@ -0,0 +1,11 @@ +insert_max = 20000 +filter = "tcp" +regex_filter = "(?:http|https)[[:punct:]]+[[:alnum:]]+[[:punct:]][[:alnum:]]+[[:punct:]](?:com|de|org|net)" +from_device = false +device = "enp7s0" +pcap_file = "" +pcap_dir = "../target/files" +tablename = "json_dump" +database_user = "postgres" +database_host = "172.17.0.2" +database_password = "password"