From 0c67e7e6366eb63ef58cc6621845ec7e3cfbad4c Mon Sep 17 00:00:00 2001 From: Giuliano Bellini <100347457+GyulyVGC@users.noreply.github.com> Date: Tue, 13 Aug 2024 15:27:56 +0200 Subject: [PATCH 01/14] don't kill windows terminal in release mode --- src/main.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1522684c..29574219 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,5 @@ //! Module containing the entry point of application execution. -#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] - use std::borrow::Cow; use std::sync::{Arc, Mutex}; use std::{panic, process, thread}; From 7c3a23b83726bb45a2f22baa2488f4f9d92e558f Mon Sep 17 00:00:00 2001 From: GyulyVGC Date: Tue, 26 Nov 2024 23:35:32 +0100 Subject: [PATCH 02/14] initial redirect implementation --- Cargo.lock | 22 ++++++++++++++++++++++ Cargo.toml | 3 +++ src/main.rs | 11 +++++++++++ src/utils/formatted_strings.rs | 9 ++++++++- 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index b5040d69..5e936693 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1397,6 +1397,17 @@ dependencies = [ "simd-adler32", ] +[[package]] +name = "filedescriptor" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7199d965852c3bac31f779ef99cbb4537f80e952e2d6aa0ffeb30cce00f4f46e" +dependencies = [ + "libc", + "thiserror 1.0.69", + "winapi", +] + [[package]] name = "finl_unicode" version = "1.3.0" @@ -1637,6 +1648,16 @@ dependencies = [ "slab", ] +[[package]] +name = "gag" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a713bee13966e9fbffdf7193af71d54a6b35a0bb34997cd6c9519ebeb5005972" +dependencies = [ + "filedescriptor", + "tempfile", +] + [[package]] name = "generic-array" version = "0.14.7" @@ -4476,6 +4497,7 @@ dependencies = [ "ctrlc", "dns-lookup", "etherparse", + "gag", "iced", "maxminddb", "once_cell", diff --git a/Cargo.toml b/Cargo.toml index 56f8a702..32a98463 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -55,6 +55,9 @@ phf_shared = "0.11.2" splines = "4.4.0" clap = { version = "4.5.21", features = ["derive"] } +#[target.'cfg(windows)'.dependencies] +gag = "1.0.0" + [target.'cfg(not(target_arch = "powerpc64"))'.dependencies] reqwest = { version = "0.12.9", default-features = false, features = ["json", "blocking", "rustls-tls"] } diff --git a/src/main.rs b/src/main.rs index 777ef1bc..aa1a4d17 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,7 @@ //! Module containing the entry point of application execution. +#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] + use std::borrow::Cow; use std::sync::{Arc, Mutex}; use std::{panic, process, thread}; @@ -53,6 +55,15 @@ pub const SNIFFNET_TITLECASE: &str = "Sniffnet"; /// /// It initializes shared variables and loads configuration parameters pub fn main() -> iced::Result { + let _gag1: gag::Redirect; + let _gag2: gag::Redirect; + if let Ok(debug_file) = + std::fs::File::create(utils::formatted_strings::get_windows_debug_file_path()?) + { + _gag1 = gag::Redirect::stdout(debug_file.try_clone().unwrap()).unwrap(); + _gag2 = gag::Redirect::stderr(debug_file).unwrap(); + } + let configs = CONFIGS.clone(); let boot_task_chain = parse_cli_args(); diff --git a/src/utils/formatted_strings.rs b/src/utils/formatted_strings.rs index a022b766..b6a0279b 100644 --- a/src/utils/formatted_strings.rs +++ b/src/utils/formatted_strings.rs @@ -6,7 +6,7 @@ use crate::translations::translations::{ address_translation, ip_version_translation, protocol_translation, }; use crate::translations::translations_3::{invalid_filters_translation, port_translation}; -use crate::Language; +use crate::{Language, SNIFFNET_LOWERCASE}; /// Application version number (to be displayed in gui footer) pub const APP_VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -165,6 +165,13 @@ pub fn get_formatted_num_seconds(num_seconds: u128) -> String { } } +// #[cfg(target_os = "windows")] +pub fn get_windows_debug_file_path() -> Option { + let mut conf = confy::get_configuration_file_path(SNIFFNET_LOWERCASE, "debug").ok()?; + conf.set_extension("txt"); + Some(conf.to_str()?.to_string()) +} + #[cfg(test)] mod tests { use super::*; From ef0e5b7352431657d740272d037c98c2e439f42e Mon Sep 17 00:00:00 2001 From: GyulyVGC Date: Fri, 29 Nov 2024 18:32:31 +0100 Subject: [PATCH 03/14] redirect stdout and stderr to file on Windows releases --- src/main.rs | 11 ++++++----- src/utils/formatted_strings.rs | 26 +++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index aa1a4d17..bcf6ac10 100644 --- a/src/main.rs +++ b/src/main.rs @@ -55,13 +55,14 @@ pub const SNIFFNET_TITLECASE: &str = "Sniffnet"; /// /// It initializes shared variables and loads configuration parameters pub fn main() -> iced::Result { + #[cfg(all(windows, not(debug_assertions)))] let _gag1: gag::Redirect; + #[cfg(all(windows, not(debug_assertions)))] let _gag2: gag::Redirect; - if let Ok(debug_file) = - std::fs::File::create(utils::formatted_strings::get_windows_debug_file_path()?) - { - _gag1 = gag::Redirect::stdout(debug_file.try_clone().unwrap()).unwrap(); - _gag2 = gag::Redirect::stderr(debug_file).unwrap(); + #[cfg(all(windows, not(debug_assertions)))] + if let Some((gag1, gag2)) = utils::formatted_strings::redirect_stdout_stderr_to_file() { + _gag1 = gag1; + _gag2 = gag2; } let configs = CONFIGS.clone(); diff --git a/src/utils/formatted_strings.rs b/src/utils/formatted_strings.rs index b6a0279b..db4665f9 100644 --- a/src/utils/formatted_strings.rs +++ b/src/utils/formatted_strings.rs @@ -165,13 +165,27 @@ pub fn get_formatted_num_seconds(num_seconds: u128) -> String { } } -// #[cfg(target_os = "windows")] -pub fn get_windows_debug_file_path() -> Option { - let mut conf = confy::get_configuration_file_path(SNIFFNET_LOWERCASE, "debug").ok()?; +#[allow(dead_code)] +#[cfg(windows)] +fn get_windows_logs_file_path() -> Option { + let mut conf = confy::get_configuration_file_path(SNIFFNET_LOWERCASE, "logs").ok()?; conf.set_extension("txt"); Some(conf.to_str()?.to_string()) } +#[allow(dead_code)] +#[cfg(windows)] +pub fn redirect_stdout_stderr_to_file( +) -> Option<(gag::Redirect, gag::Redirect)> { + if let Ok(logs_file) = std::fs::File::create(get_windows_logs_file_path()?) { + return Some(( + gag::Redirect::stdout(logs_file.try_clone().ok()?).ok()?, + gag::Redirect::stderr(logs_file).ok()?, + )); + } + None +} + #[cfg(test)] mod tests { use super::*; @@ -207,4 +221,10 @@ mod tests { "94522879700260684295381835397713392:04:15" ); } + + #[cfg(windows)] + #[test] + fn test_redirect_stdout_stderr_to_file() { + assert!(redirect_stdout_stderr_to_file().is_some()); + } } From 55ea7b5ce2ea092762757c63ca21e636bc33df4d Mon Sep 17 00:00:00 2001 From: GyulyVGC Date: Fri, 29 Nov 2024 18:42:40 +0100 Subject: [PATCH 04/14] fix warning and only include gag dependency on Windows targets --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/utils/formatted_strings.rs | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 103a7370..0dc336cb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "ab_glyph" diff --git a/Cargo.toml b/Cargo.toml index 0ae5cfb5..bc753042 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -55,7 +55,7 @@ phf_shared = "0.11.2" splines = "4.4.0" clap = { version = "4.5.21", features = ["derive"] } -#[target.'cfg(windows)'.dependencies] +[target.'cfg(windows)'.dependencies] gag = "1.0.0" [target.'cfg(not(target_arch = "powerpc64"))'.dependencies] diff --git a/src/utils/formatted_strings.rs b/src/utils/formatted_strings.rs index db4665f9..bdd9b97b 100644 --- a/src/utils/formatted_strings.rs +++ b/src/utils/formatted_strings.rs @@ -6,7 +6,7 @@ use crate::translations::translations::{ address_translation, ip_version_translation, protocol_translation, }; use crate::translations::translations_3::{invalid_filters_translation, port_translation}; -use crate::{Language, SNIFFNET_LOWERCASE}; +use crate::Language; /// Application version number (to be displayed in gui footer) pub const APP_VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -168,7 +168,7 @@ pub fn get_formatted_num_seconds(num_seconds: u128) -> String { #[allow(dead_code)] #[cfg(windows)] fn get_windows_logs_file_path() -> Option { - let mut conf = confy::get_configuration_file_path(SNIFFNET_LOWERCASE, "logs").ok()?; + let mut conf = confy::get_configuration_file_path(crate::SNIFFNET_LOWERCASE, "logs").ok()?; conf.set_extension("txt"); Some(conf.to_str()?.to_string()) } From c70618560ecf0cb9f66874daad5bdac05817cf07 Mon Sep 17 00:00:00 2001 From: GyulyVGC Date: Sun, 1 Dec 2024 15:43:59 +0100 Subject: [PATCH 05/14] add CLI option --logs to show stdout and stderr of the latest app run --- src/cli/mod.rs | 18 ++++++++++++++++-- src/utils/formatted_strings.rs | 2 +- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 6e74a36d..fb02cc9b 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -17,20 +17,34 @@ struct Args { /// Start sniffing packets from the supplied network adapter #[arg(short, long, value_name = "NAME", default_missing_value = CONFIGS.device.device_name.as_str(), num_args = 0..=1)] adapter: Option, + #[cfg(windows)] + /// Show the logs (stdout and stderr) of the most recent application run + #[arg(short, long, exclusive = true)] + logs: bool, /// Restore default settings #[arg(short, long)] restore_default: bool, } pub fn parse_cli_args() -> Task { - let mut boot_task_chain = window::get_latest().map(Message::WindowId); - let args = Args::parse(); + #[cfg(windows)] + if args.logs { + std::process::Command::new("explorer") + .arg(crate::utils::formatted_strings::get_windows_logs_file_path().unwrap()) + .spawn() + .unwrap() + .wait() + .unwrap_or_default(); + std::process::exit(0); + } + if args.restore_default { Configs::default().store(); } + let mut boot_task_chain = window::get_latest().map(Message::WindowId); if let Some(adapter) = args.adapter { boot_task_chain = boot_task_chain .chain(Task::done(Message::AdapterSelection(adapter))) diff --git a/src/utils/formatted_strings.rs b/src/utils/formatted_strings.rs index bdd9b97b..adb34ea9 100644 --- a/src/utils/formatted_strings.rs +++ b/src/utils/formatted_strings.rs @@ -167,7 +167,7 @@ pub fn get_formatted_num_seconds(num_seconds: u128) -> String { #[allow(dead_code)] #[cfg(windows)] -fn get_windows_logs_file_path() -> Option { +pub fn get_windows_logs_file_path() -> Option { let mut conf = confy::get_configuration_file_path(crate::SNIFFNET_LOWERCASE, "logs").ok()?; conf.set_extension("txt"); Some(conf.to_str()?.to_string()) From 4eb0d44c3dd88f19f0eeebb59383cc5d5f62f7c7 Mon Sep 17 00:00:00 2001 From: GyulyVGC Date: Sun, 1 Dec 2024 21:27:19 +0100 Subject: [PATCH 06/14] fix Windows logs file path test --- src/cli/mod.rs | 2 +- src/utils/formatted_strings.rs | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/cli/mod.rs b/src/cli/mod.rs index fb02cc9b..7b02f308 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -32,7 +32,7 @@ pub fn parse_cli_args() -> Task { #[cfg(windows)] if args.logs { std::process::Command::new("explorer") - .arg(crate::utils::formatted_strings::get_windows_logs_file_path().unwrap()) + .arg(crate::utils::formatted_strings::get_logs_file_path().unwrap()) .spawn() .unwrap() .wait() diff --git a/src/utils/formatted_strings.rs b/src/utils/formatted_strings.rs index adb34ea9..4a8e41c1 100644 --- a/src/utils/formatted_strings.rs +++ b/src/utils/formatted_strings.rs @@ -167,7 +167,7 @@ pub fn get_formatted_num_seconds(num_seconds: u128) -> String { #[allow(dead_code)] #[cfg(windows)] -pub fn get_windows_logs_file_path() -> Option { +pub fn get_logs_file_path() -> Option { let mut conf = confy::get_configuration_file_path(crate::SNIFFNET_LOWERCASE, "logs").ok()?; conf.set_extension("txt"); Some(conf.to_str()?.to_string()) @@ -177,7 +177,7 @@ pub fn get_windows_logs_file_path() -> Option { #[cfg(windows)] pub fn redirect_stdout_stderr_to_file( ) -> Option<(gag::Redirect, gag::Redirect)> { - if let Ok(logs_file) = std::fs::File::create(get_windows_logs_file_path()?) { + if let Ok(logs_file) = std::fs::File::create(get_logs_file_path()?) { return Some(( gag::Redirect::stdout(logs_file.try_clone().ok()?).ok()?, gag::Redirect::stderr(logs_file).ok()?, @@ -224,7 +224,10 @@ mod tests { #[cfg(windows)] #[test] - fn test_redirect_stdout_stderr_to_file() { - assert!(redirect_stdout_stderr_to_file().is_some()); + fn test_logs_file_path() { + let file_path = std::path::PathBuf::from(get_logs_file_path().unwrap()); + assert!(file_path.is_absolute()); + assert_eq!(file_path.file_name().unwrap(), "logs.txt"); + assert!(file_path.parent().unwrap().is_dir()); } } From a6dc135171bbfb65b496fcb8a15c75668fa5d381 Mon Sep 17 00:00:00 2001 From: GyulyVGC Date: Sun, 1 Dec 2024 21:43:19 +0100 Subject: [PATCH 07/14] fix Windows logs file path test --- src/utils/formatted_strings.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/utils/formatted_strings.rs b/src/utils/formatted_strings.rs index 4a8e41c1..0b9cff06 100644 --- a/src/utils/formatted_strings.rs +++ b/src/utils/formatted_strings.rs @@ -228,6 +228,5 @@ mod tests { let file_path = std::path::PathBuf::from(get_logs_file_path().unwrap()); assert!(file_path.is_absolute()); assert_eq!(file_path.file_name().unwrap(), "logs.txt"); - assert!(file_path.parent().unwrap().is_dir()); } } From d24dc9a85b72e3e4096cc0b35499d33484c7e8e4 Mon Sep 17 00:00:00 2001 From: GyulyVGC Date: Sun, 1 Dec 2024 21:58:08 +0100 Subject: [PATCH 08/14] update CHANGELOG --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b3b957d9..79dcb432 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,8 +3,9 @@ All Sniffnet releases with the relative changes are documented in this file. ## [UNRELEASED] -- Added Vietnamese translation 🇻🇳 ([#577](https://github.com/GyulyVGC/sniffnet/pull/577)) - Added CLI argument `--adapter []` to allow immediately starting the capture from a given network interface ([#643](https://github.com/GyulyVGC/sniffnet/pull/643) — fixes [#636](https://github.com/GyulyVGC/sniffnet/issues/636)) +- Added Vietnamese translation 🇻🇳 ([#577](https://github.com/GyulyVGC/sniffnet/pull/577)) +- Redirect `stderr` and `stdout` to file on Windows release builds ([#645](https://github.com/GyulyVGC/sniffnet/pull/645) — fixes [#578](https://github.com/GyulyVGC/sniffnet/issues/578)) - Updated some of the existing translations to v1.3: - Chinese ([#575](https://github.com/GyulyVGC/sniffnet/pull/575)) - Korean ([#604](https://github.com/GyulyVGC/sniffnet/pull/604)) From 668ea3d0c742c6fba23670fc60a4b3e7a0ecc94f Mon Sep 17 00:00:00 2001 From: GyulyVGC Date: Thu, 5 Dec 2024 17:55:43 +0100 Subject: [PATCH 09/14] only truncate the log file if the --logs argument isn't passed --- src/cli/mod.rs | 6 ++---- src/main.rs | 15 ++++++++++----- src/utils/formatted_strings.rs | 14 +++++++++----- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 7b02f308..ec5aa361 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -13,7 +13,7 @@ use iced::{window, Task}; version = APP_VERSION, about = "Application to comfortably monitor your network traffic" )] -struct Args { +pub struct Args { /// Start sniffing packets from the supplied network adapter #[arg(short, long, value_name = "NAME", default_missing_value = CONFIGS.device.device_name.as_str(), num_args = 0..=1)] adapter: Option, @@ -26,9 +26,7 @@ struct Args { restore_default: bool, } -pub fn parse_cli_args() -> Task { - let args = Args::parse(); - +pub fn handle_cli_args(args: Args) -> Task { #[cfg(windows)] if args.logs { std::process::Command::new("explorer") diff --git a/src/main.rs b/src/main.rs index bcf6ac10..cd0a4525 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,13 +6,15 @@ use std::borrow::Cow; use std::sync::{Arc, Mutex}; use std::{panic, process, thread}; +use clap::Parser; #[cfg(target_os = "linux")] use iced::window::settings::PlatformSpecific; use iced::{application, window, Font, Pixels, Settings}; +use crate::cli::Args; use chart::types::chart_type::ChartType; use chart::types::traffic_chart::TrafficChart; -use cli::parse_cli_args; +use cli::handle_cli_args; use configs::types::config_device::ConfigDevice; use configs::types::config_settings::ConfigSettings; use gui::pages::types::running_page::RunningPage; @@ -55,19 +57,22 @@ pub const SNIFFNET_TITLECASE: &str = "Sniffnet"; /// /// It initializes shared variables and loads configuration parameters pub fn main() -> iced::Result { + let configs = CONFIGS.clone(); + + let args = Args::parse(); + #[cfg(all(windows, not(debug_assertions)))] let _gag1: gag::Redirect; #[cfg(all(windows, not(debug_assertions)))] let _gag2: gag::Redirect; #[cfg(all(windows, not(debug_assertions)))] - if let Some((gag1, gag2)) = utils::formatted_strings::redirect_stdout_stderr_to_file() { + if let Some((gag1, gag2)) = utils::formatted_strings::redirect_stdout_stderr_to_file(args.logs) + { _gag1 = gag1; _gag2 = gag2; } - let configs = CONFIGS.clone(); - - let boot_task_chain = parse_cli_args(); + let boot_task_chain = handle_cli_args(args); let configs1 = Arc::new(Mutex::new(configs)); let configs2 = configs1.clone(); diff --git a/src/utils/formatted_strings.rs b/src/utils/formatted_strings.rs index 0b9cff06..f5c68c52 100644 --- a/src/utils/formatted_strings.rs +++ b/src/utils/formatted_strings.rs @@ -176,12 +176,16 @@ pub fn get_logs_file_path() -> Option { #[allow(dead_code)] #[cfg(windows)] pub fn redirect_stdout_stderr_to_file( + logs: bool, ) -> Option<(gag::Redirect, gag::Redirect)> { - if let Ok(logs_file) = std::fs::File::create(get_logs_file_path()?) { - return Some(( - gag::Redirect::stdout(logs_file.try_clone().ok()?).ok()?, - gag::Redirect::stderr(logs_file).ok()?, - )); + // only truncate the log file if the --logs argument isn't passed + if !logs { + if let Ok(logs_file) = std::fs::File::create(get_logs_file_path()?) { + return Some(( + gag::Redirect::stdout(logs_file.try_clone().ok()?).ok()?, + gag::Redirect::stderr(logs_file).ok()?, + )); + } } None } From 2b02446ef5f4c5a1d987b881379d0e82f4fbdf5f Mon Sep 17 00:00:00 2001 From: GyulyVGC Date: Thu, 5 Dec 2024 18:07:02 +0100 Subject: [PATCH 10/14] make logs field of Args struct public --- src/cli/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cli/mod.rs b/src/cli/mod.rs index ec5aa361..2349a766 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -20,7 +20,7 @@ pub struct Args { #[cfg(windows)] /// Show the logs (stdout and stderr) of the most recent application run #[arg(short, long, exclusive = true)] - logs: bool, + pub logs: bool, /// Restore default settings #[arg(short, long)] restore_default: bool, From 1e047fc287805abe1cabea800cde5854c0f76bd4 Mon Sep 17 00:00:00 2001 From: GyulyVGC Date: Thu, 5 Dec 2024 18:21:06 +0100 Subject: [PATCH 11/14] terminate the process when receiving --restore_default CLI argument --- src/cli/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 2349a766..a898322e 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -22,7 +22,7 @@ pub struct Args { #[arg(short, long, exclusive = true)] pub logs: bool, /// Restore default settings - #[arg(short, long)] + #[arg(short, long, exclusive = true)] restore_default: bool, } @@ -40,6 +40,7 @@ pub fn handle_cli_args(args: Args) -> Task { if args.restore_default { Configs::default().store(); + std::process::exit(0); } let mut boot_task_chain = window::get_latest().map(Message::WindowId); From 44ccf528543f81a833f3d2f75b006f7c5289deb0 Mon Sep 17 00:00:00 2001 From: GyulyVGC Date: Sat, 7 Dec 2024 10:58:25 +0100 Subject: [PATCH 12/14] redirect before parsing CLI args; truncate only if --logs wasn't supplied --- src/cli/mod.rs | 39 ++++++++++++++++++++++------------ src/main.rs | 12 +++-------- src/utils/formatted_strings.rs | 22 +++++++++---------- 3 files changed, 40 insertions(+), 33 deletions(-) diff --git a/src/cli/mod.rs b/src/cli/mod.rs index a898322e..7dfe9e0c 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -13,29 +13,42 @@ use iced::{window, Task}; version = APP_VERSION, about = "Application to comfortably monitor your network traffic" )] -pub struct Args { +struct Args { /// Start sniffing packets from the supplied network adapter #[arg(short, long, value_name = "NAME", default_missing_value = CONFIGS.device.device_name.as_str(), num_args = 0..=1)] adapter: Option, - #[cfg(windows)] + #[cfg(all(windows, not(debug_assertions)))] /// Show the logs (stdout and stderr) of the most recent application run #[arg(short, long, exclusive = true)] - pub logs: bool, + logs: bool, /// Restore default settings #[arg(short, long, exclusive = true)] restore_default: bool, } -pub fn handle_cli_args(args: Args) -> Task { - #[cfg(windows)] - if args.logs { - std::process::Command::new("explorer") - .arg(crate::utils::formatted_strings::get_logs_file_path().unwrap()) - .spawn() - .unwrap() - .wait() - .unwrap_or_default(); - std::process::exit(0); +pub fn handle_cli_args() -> Task { + let args = Args::parse(); + + #[cfg(all(windows, not(debug_assertions)))] + if let Some(logs_file) = crate::utils::formatted_strings::get_logs_file_path() { + if args.logs { + std::process::Command::new("explorer") + .arg(logs_file) + .spawn() + .unwrap() + .wait() + .unwrap_or_default(); + std::process::exit(0); + } else { + // truncate logs file + unsafe { + std::fs::OpenOptions::new() + .write(true) + .truncate(true) + .open(logs_file) + .unwrap_unchecked(); + } + } } if args.restore_default { diff --git a/src/main.rs b/src/main.rs index cd0a4525..4171dd12 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,12 +6,10 @@ use std::borrow::Cow; use std::sync::{Arc, Mutex}; use std::{panic, process, thread}; -use clap::Parser; #[cfg(target_os = "linux")] use iced::window::settings::PlatformSpecific; use iced::{application, window, Font, Pixels, Settings}; -use crate::cli::Args; use chart::types::chart_type::ChartType; use chart::types::traffic_chart::TrafficChart; use cli::handle_cli_args; @@ -57,22 +55,18 @@ pub const SNIFFNET_TITLECASE: &str = "Sniffnet"; /// /// It initializes shared variables and loads configuration parameters pub fn main() -> iced::Result { - let configs = CONFIGS.clone(); - - let args = Args::parse(); - #[cfg(all(windows, not(debug_assertions)))] let _gag1: gag::Redirect; #[cfg(all(windows, not(debug_assertions)))] let _gag2: gag::Redirect; #[cfg(all(windows, not(debug_assertions)))] - if let Some((gag1, gag2)) = utils::formatted_strings::redirect_stdout_stderr_to_file(args.logs) - { + if let Some((gag1, gag2)) = utils::formatted_strings::redirect_stdout_stderr_to_file() { _gag1 = gag1; _gag2 = gag2; } - let boot_task_chain = handle_cli_args(args); + let configs = CONFIGS.clone(); + let boot_task_chain = handle_cli_args(); let configs1 = Arc::new(Mutex::new(configs)); let configs2 = configs1.clone(); diff --git a/src/utils/formatted_strings.rs b/src/utils/formatted_strings.rs index f5c68c52..3af31d96 100644 --- a/src/utils/formatted_strings.rs +++ b/src/utils/formatted_strings.rs @@ -173,19 +173,19 @@ pub fn get_logs_file_path() -> Option { Some(conf.to_str()?.to_string()) } -#[allow(dead_code)] -#[cfg(windows)] +#[cfg(all(windows, not(debug_assertions)))] pub fn redirect_stdout_stderr_to_file( - logs: bool, ) -> Option<(gag::Redirect, gag::Redirect)> { - // only truncate the log file if the --logs argument isn't passed - if !logs { - if let Ok(logs_file) = std::fs::File::create(get_logs_file_path()?) { - return Some(( - gag::Redirect::stdout(logs_file.try_clone().ok()?).ok()?, - gag::Redirect::stderr(logs_file).ok()?, - )); - } + if let Ok(logs_file) = std::fs::OpenOptions::new() + .write(true) + .create(true) + .append(true) + .open(get_logs_file_path()?) + { + return Some(( + gag::Redirect::stdout(logs_file.try_clone().ok()?).ok()?, + gag::Redirect::stderr(logs_file).ok()?, + )); } None } From d812f0af128d1835b31ae0719dda4441571dddec Mon Sep 17 00:00:00 2001 From: GyulyVGC Date: Sat, 7 Dec 2024 11:23:51 +0100 Subject: [PATCH 13/14] run cargo clippy --release in package.yml workflow --- .github/workflows/package.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/package.yml b/.github/workflows/package.yml index 77db9658..7c539abe 100644 --- a/.github/workflows/package.yml +++ b/.github/workflows/package.yml @@ -72,6 +72,9 @@ jobs: if: matrix.os == 'ubuntu' run: cargo install cross --git https://github.com/cross-rs/cross + - name: Clippy (release mode) + run: cargo clippy --release -- -D warnings + - name: Build binary if: matrix.os == 'ubuntu' run: cross build --release --target ${{ matrix.target }} From 711796a01ded33f74c1d3fd6d2eef3160cbdda49 Mon Sep 17 00:00:00 2001 From: GyulyVGC Date: Sat, 7 Dec 2024 14:20:26 +0100 Subject: [PATCH 14/14] remove unsafe code block --- src/cli/mod.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 7dfe9e0c..f8e617a2 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -41,13 +41,10 @@ pub fn handle_cli_args() -> Task { std::process::exit(0); } else { // truncate logs file - unsafe { - std::fs::OpenOptions::new() - .write(true) - .truncate(true) - .open(logs_file) - .unwrap_unchecked(); - } + let _ = std::fs::OpenOptions::new() + .write(true) + .truncate(true) + .open(logs_file); } }