diff --git a/src/cli.rs b/src/cli.rs index 49ecf35..591bb29 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -235,5 +235,3 @@ pub fn cli_template() -> Command { ), ) } - - diff --git a/src/logger.rs b/src/logger.rs index baab109..cb14ba4 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -1,11 +1,11 @@ use std::fs::File; -use std::io::{ Write, stdout, stderr}; +use std::io::{stderr, stdout, Write}; use std::path::PathBuf; use chrono::Local; // Import the Local struct from the chrono crate -use log::{Level, Record, Metadata, Log }; +use log::{Level, Log, Metadata, Record}; -pub enum Logstdout{ +pub enum Logstdout { Stdout, StdErr, } @@ -21,7 +21,7 @@ impl Logger { pub fn new(write_to_file: bool, file_path: Option) -> Self { let mut path = file_path; if path.is_none() { - path = Some(PathBuf::from( + path = Some(PathBuf::from( #[cfg(target_os = "linux")] "/var/log/fluere/fluere.log", #[cfg(target_os = "windows")] @@ -30,20 +30,30 @@ impl Logger { "/Library/Logs/fluere/fluere.log", #[cfg(target_os = "bsd")] "/var/log/fluere/fluere.log", - #[cfg(not(any(target_os = "linux", target_os = "windows", target_os = "macos", target_os = "bsd")))] + #[cfg(not(any( + target_os = "linux", + target_os = "windows", + target_os = "macos", + target_os = "bsd" + )))] "/var/log/fluere/fluere.log", )); } let mut file = None; if write_to_file { file = Some(File::create(path.as_ref().unwrap()).unwrap()); - } - Logger {write_to_file: true, write_to_std: None, severity: Level::Info , file} + } + Logger { + write_to_file: true, + write_to_std: None, + severity: Level::Info, + file, + } } // pub fn log(&mut self, severity: Level, message: &str) { - // let timestamp = Local::now(); // Get the current timestamp using Local::now() - // let log_message = format!("{:?} {}: {}", timestamp, severity, message); // Format the timestamp and append it to the log message + // let timestamp = Local::now(); // Get the current timestamp using Local::now() + // let log_message = format!("{:?} {}: {}", timestamp, severity, message); // Format the timestamp and append it to the log message // } } @@ -55,24 +65,42 @@ impl Log for Logger { fn log(&self, record: &Record) { let timestamp = Local::now(); - if self.write_to_std.as_ref().is_some() && record.level() <= self.severity{ + if self.write_to_std.as_ref().is_some() && record.level() <= self.severity { match self.write_to_std.as_ref().unwrap() { Logstdout::Stdout => { - writeln!(stdout(), "[{}]: {} {}",timestamp, record.level(), record.args()).unwrap(); + writeln!( + stdout(), + "[{}]: {}: {}", + timestamp, + record.level(), + record.args() + ) + .unwrap(); } Logstdout::StdErr => { - writeln!(stderr(), "[{}]: {} {}",timestamp, record.level(), record.args()).unwrap(); + writeln!( + stderr(), + "[{}]: {}: {}", + timestamp, + record.level(), + record.args() + ) + .unwrap(); } } } if self.write_to_file { - - writeln!(self.file.as_ref().unwrap(), "[{}]: {} {}",timestamp, record.level(), record.args()).unwrap(); + writeln!( + self.file.as_ref().unwrap(), + "[{}]: {}: {}", + timestamp, + record.level(), + record.args() + ) + .unwrap(); } } - fn flush(&self) { - - } + fn flush(&self) {} } diff --git a/src/main.rs b/src/main.rs index 91e1a05..983a937 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,18 +3,65 @@ // It also supports live capture and conversion of NetFlow data. // This file contains the main function which parses the command line arguments and calls the appropriate functions based on the arguments. +pub mod cli; +pub mod logger; pub mod net; pub mod plugin; pub mod types; pub mod utils; -pub mod cli; use pnet::datalink; +// use env_logger::{init, Logger}; +use log::Level; +use crate::logger::Logger; use crate::net::list_devices; +use std::fmt::Display; use std::process::exit; +enum Mode { + Offline, + Online, + Live, + Pcap, +} +impl Display for Mode { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Mode::Offline => write!(f, "Offline"), + Mode::Online => write!(f, "Online"), + Mode::Live => write!(f, "Live"), + Mode::Pcap => write!(f, "Pcap"), + } + } +} + +struct Fluere { + interface: String, + args: types::Args, + mode: Mode, + logger: Logger, + verbose: Level, +} +impl Fluere { + fn new( + interface: String, + args: types::Args, + mode: Mode, + logger: Logger, + verbose: Level, + ) -> Fluere { + Fluere { + interface, + args, + mode, + logger, + verbose, + } + } +} + // This is the main function of the application. // It gets the command line arguments, parses them, and calls the appropriate functions based on the arguments. #[tokio::main] diff --git a/src/net/capture.rs b/src/net/capture.rs index abae835..6f1feca 100644 --- a/src/net/capture.rs +++ b/src/net/capture.rs @@ -1,7 +1,7 @@ use pcap::{Active, Address, Capture, Device, Error as PcapError}; -use std::time::Instant; use std::fmt; +use std::time::Instant; #[derive(Debug)] pub enum DeviceError { diff --git a/src/net/packet_pcap.rs b/src/net/packet_pcap.rs index f2c9aa2..b727191 100644 --- a/src/net/packet_pcap.rs +++ b/src/net/packet_pcap.rs @@ -4,10 +4,10 @@ use std::fs; use tokio::time::sleep; +use crate::net::find_device; +use crate::net::CaptureDevice; use crate::types::Args; use crate::utils::cur_time_file; -use crate::net::CaptureDevice; -use crate::net::find_device; use std::time::{Duration, Instant}; diff --git a/src/utils/log.rs b/src/utils/log.rs index 77666b1..8f3c759 100644 --- a/src/utils/log.rs +++ b/src/utils/log.rs @@ -1,44 +1,106 @@ use std::fs::File; -use std::io::Write; -use std::path::Path; +use std::io::{stderr, stdout, Write}; +use std::path::PathBuf; + use chrono::Local; // Import the Local struct from the chrono crate +use log::{Level, Log, Metadata, Record}; -pub struct Log { - file: File, +pub enum Logstdout { + Stdout, + StdErr, } -impl Log { - #[cfg(target_os = "linux")] - pub fn new() -> Self { - let path = "/var/log/fluere/fluere.log"; - let file = File::create(&path).expect("Failed to create log file"); - Log { file } - } +pub struct Logger { + write_to_file: bool, + write_to_std: Option, + severity: Level, + file: Option, +} - #[cfg(target_os = "windows")] - pub fn new() -> Self { - let path = "C:\\Program Files\\fluere\\fluere.log"; - let file = File::create(&path).expect("Failed to create log file"); - Log { file } +impl Logger { + pub fn new(write_to_file: bool, file_path: Option) -> Self { + let mut path = file_path; + if path.is_none() { + path = Some(PathBuf::from( + #[cfg(target_os = "linux")] + "/var/log/fluere/fluere.log", + #[cfg(target_os = "windows")] + "C:\\Program Files\\fluere\\fluere.log", + #[cfg(target_os = "macos")] + "/Library/Logs/fluere/fluere.log", + #[cfg(target_os = "bsd")] + "/var/log/fluere/fluere.log", + #[cfg(not(any( + target_os = "linux", + target_os = "windows", + target_os = "macos", + target_os = "bsd" + )))] + "/var/log/fluere/fluere.log", + )); + } + let mut file = None; + if write_to_file { + file = Some(File::create(path.as_ref().unwrap()).unwrap()); + } + Logger { + write_to_file: true, + write_to_std: None, + severity: Level::Info, + file, + } } - #[cfg(target_os = "macos")] - pub fn new() -> Self { - let path = "/Library/Logs/fluere/fluere.log"; - let file = File::create(&path).expect("Failed to create log file"); - Log { file } - } + // pub fn log(&mut self, severity: Level, message: &str) { + // let timestamp = Local::now(); // Get the current timestamp using Local::now() + // let log_message = format!("{:?} {}: {}", timestamp, severity, message); // Format the timestamp and append it to the log message + // } +} - #[cfg(target_os = "bsd")] - pub fn new() -> Self { - let path = "/var/log/fluere/fluere.log"; - let file = File::create(&path).expect("Failed to create log file"); - Log { file } +impl Log for Logger { + fn enabled(&self, _metadata: &Metadata) -> bool { + true } - pub fn log(&mut self, severity: Severity, message: &str) { - let timestamp = Local::now(); // Get the current timestamp using Local::now() - let log_message = format!("{:?} {}: {}", timestamp, severity, message); // Format the timestamp and append it to the log message - self.file.write_all(log_message.as_bytes()).expect("Failed to write to log file"); + fn log(&self, record: &Record) { + let timestamp = Local::now(); + + if self.write_to_std.as_ref().is_some() && record.level() <= self.severity { + match self.write_to_std.as_ref().unwrap() { + Logstdout::Stdout => { + write!( + stdout(), + "[{}]: {} {}", + timestamp, + record.level(), + record.args() + ) + .unwrap(); + } + Logstdout::StdErr => { + write!( + stderr(), + "[{}]: {} {}", + timestamp, + record.level(), + record.args() + ) + .unwrap(); + } + } + } + + if self.write_to_file { + writeln!( + self.file.as_ref().unwrap(), + "[{}]: {} {}", + timestamp, + record.level(), + record.args() + ) + .unwrap(); + } } + + fn flush(&self) {} }