moved from JSON configuration file to TOML

This commit is contained in:
gurkenhabicht 2023-08-06 21:23:28 +02:00
parent 1722a9a1fa
commit 85a6de8aba
3 changed files with 89 additions and 41 deletions

View File

@ -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<Config, Box<dyn std::error::Error>> {
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<Config> {
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<Config> {
// 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

View File

@ -37,7 +37,17 @@ fn query_string(insert_max: &usize, table_name: &str) -> String {
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
/* 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
@ -49,13 +59,15 @@ async fn main() -> Result<(), Box<dyn Error>> {
/* db connection */
if let Err(err) = database::postgresql::connect_to_postgres(&config.connection).await {
let connection_string = configure::Config::build_postgres_connection_string(&config);
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<dyn Error>> {
.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);

11
src/parser.toml Normal file
View File

@ -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 = "<not in use right now>"
pcap_dir = "../target/files"
tablename = "json_dump"
database_user = "postgres"
database_host = "172.17.0.2"
database_password = "password"