moved from JSON configuration file to TOML
This commit is contained in:
		
							parent
							
								
									1722a9a1fa
								
							
						
					
					
						commit
						85a6de8aba
					
				| 
						 | 
					@ -3,6 +3,8 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
extern crate serde_json;
 | 
					extern crate serde_json;
 | 
				
			||||||
use byteorder::{ByteOrder, LittleEndian};
 | 
					use byteorder::{ByteOrder, LittleEndian};
 | 
				
			||||||
 | 
					use serde::Deserialize;
 | 
				
			||||||
 | 
					use toml;
 | 
				
			||||||
use std::collections::HashMap;
 | 
					use std::collections::HashMap;
 | 
				
			||||||
use std::fs;
 | 
					use std::fs;
 | 
				
			||||||
use std::fs::File;
 | 
					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: [u8; 4] = [0xd4, 0xc3, 0xb2, 0xa1];
 | 
				
			||||||
const PCAP_SIGNATURE_BE: [u8; 4] = [0xa1, 0xb2, 0xc3, 0xa1];
 | 
					const PCAP_SIGNATURE_BE: [u8; 4] = [0xa1, 0xb2, 0xc3, 0xa1];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[derive(Debug, Deserialize)]
 | 
				
			||||||
pub struct Config {
 | 
					pub struct Config {
 | 
				
			||||||
    pub filter: String,
 | 
					    pub filter: String,
 | 
				
			||||||
    pub regex_filter: String,
 | 
					    pub regex_filter: String,
 | 
				
			||||||
    pub insert_max: usize,
 | 
					    pub insert_max: usize,
 | 
				
			||||||
    pub pcap_file: String,
 | 
					    pub pcap_file: String,
 | 
				
			||||||
    pub tablename: String,
 | 
					    pub tablename: String,
 | 
				
			||||||
    pub connection: String,
 | 
					    pub database_host: String,
 | 
				
			||||||
 | 
					    pub database_user: String,
 | 
				
			||||||
 | 
					    pub database_password: String,
 | 
				
			||||||
    pub device: String,
 | 
					    pub device: String,
 | 
				
			||||||
    pub is_device: bool,
 | 
					    pub from_device: bool,
 | 
				
			||||||
    pub pcap_dir: String,
 | 
					    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)]
 | 
					#[derive(Debug)]
 | 
				
			||||||
pub struct FileInfo {
 | 
					pub struct FileInfo {
 | 
				
			||||||
    pub encapsulation_type: u16,
 | 
					    pub encapsulation_type: u16,
 | 
				
			||||||
| 
						 | 
					@ -44,41 +69,41 @@ impl FileInfo {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn from_json_file() -> Option<Config> {
 | 
					// pub fn from_json_file() -> Option<Config> {
 | 
				
			||||||
    let config_file = File::open("parser.json").expect("file should open read only");
 | 
					//     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();
 | 
					//     let json: serde_json::Value = serde_json::from_reader(config_file).unwrap();
 | 
				
			||||||
    Some(Config {
 | 
					//     Some(Config {
 | 
				
			||||||
        filter: json.get("filter").unwrap().as_str().unwrap().to_owned(),
 | 
					//         filter: json.get("filter").unwrap().as_str().unwrap().to_owned(),
 | 
				
			||||||
        regex_filter: json
 | 
					//         regex_filter: json
 | 
				
			||||||
            .get("regex_filter")
 | 
					//             .get("regex_filter")
 | 
				
			||||||
            .unwrap()
 | 
					//             .unwrap()
 | 
				
			||||||
            .as_str()
 | 
					//             .as_str()
 | 
				
			||||||
            .unwrap()
 | 
					//             .unwrap()
 | 
				
			||||||
            .to_owned(),
 | 
					//             .to_owned(),
 | 
				
			||||||
        insert_max: json.get("insert_max").unwrap().as_u64().unwrap() as usize,
 | 
					//         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
 | 
					//         pcap_file: json.get("pcap_file").unwrap().as_str().unwrap().to_owned(), // Not in use atm
 | 
				
			||||||
        tablename: json
 | 
					//         tablename: json
 | 
				
			||||||
            .get("database_tablename")
 | 
					//             .get("database_tablename")
 | 
				
			||||||
            .unwrap()
 | 
					//             .unwrap()
 | 
				
			||||||
            .as_str()
 | 
					//             .as_str()
 | 
				
			||||||
            .unwrap()
 | 
					//             .unwrap()
 | 
				
			||||||
            .to_owned(),
 | 
					//             .to_owned(),
 | 
				
			||||||
        connection: format!(
 | 
					//         connection: format!(
 | 
				
			||||||
            "host={} user={} password={}",
 | 
					//             "host={} user={} password={}",
 | 
				
			||||||
            json.get("database_host").unwrap().as_str().unwrap(),
 | 
					//             json.get("database_host").unwrap().as_str().unwrap(),
 | 
				
			||||||
            json.get("database_user").unwrap().as_str().unwrap(),
 | 
					//             json.get("database_user").unwrap().as_str().unwrap(),
 | 
				
			||||||
            json.get("database_password").unwrap().as_str().unwrap(),
 | 
					//             json.get("database_password").unwrap().as_str().unwrap(),
 | 
				
			||||||
        ),
 | 
					//         ),
 | 
				
			||||||
        device: json
 | 
					//         device: json
 | 
				
			||||||
            .get("parse_device")
 | 
					//             .get("parse_device")
 | 
				
			||||||
            .unwrap()
 | 
					//             .unwrap()
 | 
				
			||||||
            .as_str()
 | 
					//             .as_str()
 | 
				
			||||||
            .unwrap()
 | 
					//             .unwrap()
 | 
				
			||||||
            .to_owned(),
 | 
					//             .to_owned(),
 | 
				
			||||||
        is_device: json.get("from_device").unwrap().as_bool().unwrap(),
 | 
					//         is_device: json.get("from_device").unwrap().as_bool().unwrap(),
 | 
				
			||||||
        pcap_dir: json.get("pcap_dir").unwrap().as_str().unwrap().to_owned(),
 | 
					//         pcap_dir: json.get("pcap_dir").unwrap().as_str().unwrap().to_owned(),
 | 
				
			||||||
    })
 | 
					//     })
 | 
				
			||||||
}
 | 
					// }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
File signature and encapsulation type from file
 | 
					File signature and encapsulation type from file
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										20
									
								
								src/main.rs
								
								
								
								
							
							
						
						
									
										20
									
								
								src/main.rs
								
								
								
								
							| 
						 | 
					@ -37,7 +37,17 @@ fn query_string(insert_max: &usize, table_name: &str) -> String {
 | 
				
			||||||
#[tokio::main]
 | 
					#[tokio::main]
 | 
				
			||||||
async fn main() -> Result<(), Box<dyn Error>> {
 | 
					async fn main() -> Result<(), Box<dyn Error>> {
 | 
				
			||||||
    /* Init values from file */
 | 
					    /* 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();
 | 
					    let pcap_map = configure::map_pcap_dir(&config.pcap_dir).unwrap();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // TODO: Create db table with pcap file hashes
 | 
					    // TODO: Create db table with pcap file hashes
 | 
				
			||||||
| 
						 | 
					@ -48,14 +58,16 @@ async fn main() -> Result<(), Box<dyn Error>> {
 | 
				
			||||||
    // println!("{:?}", pcap_map);
 | 
					    // println!("{:?}", pcap_map);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* db connection */
 | 
					    /* 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 chain: {:?}", err);
 | 
				
			||||||
        println!("Error: {}", err);
 | 
					        println!("Error: {}", err);
 | 
				
			||||||
        eprintln!("Error chain: {:?}", 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
 | 
					    client
 | 
				
			||||||
        .execute(&*format!("DROP TABLE IF EXISTS {}", &config.tablename), &[])
 | 
					        .execute(&*format!("DROP TABLE IF EXISTS {}", &config.tablename), &[])
 | 
				
			||||||
        .await?;
 | 
					        .await?;
 | 
				
			||||||
| 
						 | 
					@ -70,7 +82,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
 | 
				
			||||||
        .await?;
 | 
					        .await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* device or file input */
 | 
					    /* device or file input */
 | 
				
			||||||
    match config.is_device {
 | 
					    match config.from_device {
 | 
				
			||||||
        FROM_FILE => {
 | 
					        FROM_FILE => {
 | 
				
			||||||
            for (_pcap_file, _pcap_info) in pcap_map.iter() {
 | 
					            for (_pcap_file, _pcap_info) in pcap_map.iter() {
 | 
				
			||||||
                //println!("{:?}: {:?}", &_pcap_file, &_pcap_info);
 | 
					                //println!("{:?}: {:?}", &_pcap_file, &_pcap_info);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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" 
 | 
				
			||||||
		Loading…
	
		Reference in New Issue