From 77860974b76f5040e9d06d545022a447970509d2 Mon Sep 17 00:00:00 2001 From: Giuliano Bellini s294739 Date: Wed, 6 Dec 2023 21:31:29 +0100 Subject: [PATCH] handle custom report path (WIP) --- README.md | 3 + src/configs/types/config_advanced_settings.rs | 9 +-- src/gui/pages/settings_advanced_page.rs | 24 ++------ src/gui/types/message.rs | 4 +- src/gui/types/sniffer.rs | 6 +- src/translations/translations.rs | 24 -------- src/utils/formatted_strings.rs | 55 ++++++++++--------- 7 files changed, 46 insertions(+), 79 deletions(-) diff --git a/README.md b/README.md index 92befd81..3cae022a 100644 --- a/README.md +++ b/README.md @@ -428,6 +428,9 @@ The currently usable hotkeys are reported in the following. ``` The example theme above uses colors from [Catppuccin Mocha](https://github.com/catppuccin/catppuccin). + + To use a custom theme for your instance of Sniffnet, specify the path of your TOML file in the application's + advanced settings (accessible from the top right corner of the app). ## Troubleshooting diff --git a/src/configs/types/config_advanced_settings.rs b/src/configs/types/config_advanced_settings.rs index 51b5793a..2eb323e4 100644 --- a/src/configs/types/config_advanced_settings.rs +++ b/src/configs/types/config_advanced_settings.rs @@ -1,19 +1,16 @@ //! Module defining the `ConfigAdvancedSettings` struct, which allows to save and reload //! the application advanced settings. -use std::path::PathBuf; - +use crate::utils::formatted_strings::get_default_report_file_path; use serde::{Deserialize, Serialize}; -use crate::utils::formatted_strings::get_default_report_directory; - #[derive(Serialize, Deserialize, Clone, PartialEq)] pub struct ConfigAdvancedSettings { pub scale_factor: f64, pub mmdb_country: String, pub mmdb_asn: String, pub style_path: String, - pub output_path: PathBuf, + pub output_path: String, } impl ConfigAdvancedSettings { @@ -45,7 +42,7 @@ impl Default for ConfigAdvancedSettings { mmdb_country: String::new(), mmdb_asn: String::new(), style_path: String::new(), - output_path: get_default_report_directory(), + output_path: get_default_report_file_path(), } } } diff --git a/src/gui/pages/settings_advanced_page.rs b/src/gui/pages/settings_advanced_page.rs index ba116a83..4b77df1b 100644 --- a/src/gui/pages/settings_advanced_page.rs +++ b/src/gui/pages/settings_advanced_page.rs @@ -1,4 +1,3 @@ -use std::path::PathBuf; use std::sync::Arc; use iced::advanced::widget::Text; @@ -27,7 +26,7 @@ use crate::translations::translations_3::{ info_mmdb_paths_translation, mmdb_paths_translation, params_not_editable_translation, restore_defaults_translation, scale_factor_translation, }; -use crate::utils::formatted_strings::get_default_report_directory; +use crate::utils::formatted_strings::get_default_report_file_path; use crate::utils::types::icon::Icon; use crate::{ConfigAdvancedSettings, Language, RunningPage, Sniffer, StyleType}; @@ -171,33 +170,22 @@ fn report_path_setting( is_editable: bool, language: Language, font: Font, - mut path: PathBuf, + custom_path: String, ) -> Row<'static, Message, Renderer> { - let default_directory = &get_default_report_directory().to_string_lossy().to_string(); - path.pop(); - let custom_directory = &path.to_string_lossy().to_string(); - // to be updated.........!!! - let is_error = !custom_directory.is_empty(); - - let mut input = TextInput::new(default_directory, custom_directory) + let mut input = TextInput::new(&get_default_report_file_path(), &custom_path) .padding([0, 5]) .font(font) - .width(Length::Fixed(200.0)) - .style(if is_error { - TextInputType::Error - } else { - TextInputType::Standard - }); + .width(Length::Fixed(500.0)) + .style(TextInputType::Standard); if is_editable { - input = input.on_input(Message::CustomReportDirectory); + input = input.on_input(Message::CustomReport); } Row::new() .push(Text::new(format!("{}:", file_path_translation(language))).font(font)) .push(horizontal_space(5)) .push(input) - .push(Text::new("/sniffnet.pcap").font(font)) } fn mmdb_settings( diff --git a/src/gui/types/message.rs b/src/gui/types/message.rs index 5cacc919..65a455de 100644 --- a/src/gui/types/message.rs +++ b/src/gui/types/message.rs @@ -101,8 +101,8 @@ pub enum Message { CustomCountryDb(String), /// The ASN MMDB custom path has been updated CustomAsnDb(String), - /// The directory for the output report has been updated - CustomReportDirectory(String), + /// The path for the output report has been updated + CustomReport(String), /// Save the configurations of the app and quit CloseRequested, } diff --git a/src/gui/types/sniffer.rs b/src/gui/types/sniffer.rs index 55c285d2..c817e66a 100644 --- a/src/gui/types/sniffer.rs +++ b/src/gui/types/sniffer.rs @@ -33,7 +33,6 @@ use crate::report::get_report_entries::get_searched_entries; use crate::report::types::report_sort_type::ReportSortType; use crate::secondary_threads::parse_packets::parse_packets; use crate::translations::types::language::Language; -use crate::utils::formatted_strings::push_pcap_file_name; use crate::utils::types::web_page::WebPage; use crate::{ ConfigAdvancedSettings, ConfigDevice, ConfigSettings, Configs, InfoTraffic, RunTimeData, @@ -285,8 +284,7 @@ impl Sniffer { self.advanced_settings.mmdb_asn = db.clone(); self.asn_mmdb_reader = Arc::new(MmdbReader::from(&db, ASN_MMDB)); } - Message::CustomReportDirectory(directory) => { - let path = push_pcap_file_name(PathBuf::from(directory)); + Message::CustomReport(path) => { self.advanced_settings.output_path = path; } Message::CloseRequested => { @@ -391,7 +389,7 @@ impl Sniffer { fn reset(&mut self) -> Command { self.running_page = RunningPage::Init; - *self.current_capture_id.lock().unwrap() += 1; //change capture id to kill previous capture and to rewrite output file + *self.current_capture_id.lock().unwrap() += 1; //change capture id to kill previous captures self.pcap_error = None; self.report_sort_type = ReportSortType::MostRecent; self.unread_notifications = 0; diff --git a/src/translations/translations.rs b/src/translations/translations.rs index 357fd7b6..01e58617 100644 --- a/src/translations/translations.rs +++ b/src/translations/translations.rs @@ -1633,30 +1633,6 @@ pub fn sound_translation(language: Language) -> &'static str { } } -pub fn open_report_translation(language: Language) -> &'static str { - match language { - Language::EN => "Open full report", - Language::IT => "Apri report completo", - Language::FR => "Ouvrir le rapport complet", - Language::ES => "Abrir el informe completo", - Language::PL => "Otwórz pełny raport", - Language::DE => "Kompletten Bericht öffnen", - Language::UK => "Відкрий повний рапорт", - Language::ZH => "打开完整报告", - Language::RO => "Deschideți raport complet", - Language::KO => "전체 보고서 열기", - Language::TR => "Tam raporu aç", - Language::RU => "Открыть полный отчёт", - Language::PT => "Abrir relatório completo", - Language::EL => "Άνοιγμα της πλήρους αναφοράς", - // Language::FA => "گزارش کامل را باز کن", - Language::SV => "Öppna fullständig rapport", - Language::FI => "Avaa koko raportti", - Language::JA => "詳細なレポートを開く", - Language::UZ => "To'liq hisobotni ochish", - } -} - pub fn bytes_exceeded_translation(language: Language) -> &'static str { match language { Language::EN => "Bytes threshold exceeded!", diff --git a/src/utils/formatted_strings.rs b/src/utils/formatted_strings.rs index 9c4e519d..a2e8344e 100644 --- a/src/utils/formatted_strings.rs +++ b/src/utils/formatted_strings.rs @@ -1,5 +1,6 @@ +use std::fs::File; use std::net::IpAddr; -use std::path::{Path, PathBuf}; +use std::path::PathBuf; use iced::widget::{Column, Text}; use iced::{Font, Renderer}; @@ -7,14 +8,14 @@ use iced::{Font, Renderer}; use crate::gui::styles::text::TextType; use crate::gui::types::message::Message; use crate::networking::types::filters::Filters; -use crate::translations::translations::{ - active_filters_translation, none_translation, open_report_translation, -}; +use crate::translations::translations::{active_filters_translation, none_translation}; use crate::{AppProtocol, IpVersion, Language, StyleType, TransProtocol}; /// Application version number (to be displayed in gui footer) pub const APP_VERSION: &str = env!("CARGO_PKG_VERSION"); +pub const PCAP_FILE_NAME: &str = "sniffnet.pcap"; + /// Computes the String representing the percentage of filtered bytes/packets pub fn get_percentage_string(observed: u128, filtered: u128) -> String { #[allow(clippy::cast_precision_loss)] @@ -102,30 +103,34 @@ pub fn get_formatted_bytes_string_with_b(bytes: u128) -> String { bytes_string } -/// Returns the default directory to use for the output report file -pub fn get_default_report_directory() -> PathBuf { - if let Ok(mut config_path) = confy::get_configuration_file_path("sniffnet", "file") { - config_path.pop(); - config_path +/// Returns the default report path +pub fn get_default_report_file_path() -> String { + return if let Ok(mut config_path) = + confy::get_configuration_file_path("sniffnet", PCAP_FILE_NAME) + { + config_path.to_string_lossy().to_string() } else { - PathBuf::from(std::env::var_os("HOME").unwrap()) - } -} - -pub fn push_pcap_file_name(mut directory: PathBuf) -> PathBuf { - directory.push("sniffnet.pcap"); - directory + std::env::var_os("HOME") + .unwrap() + .to_string_lossy() + .to_string() + }; } -pub fn get_open_report_tooltip(output_path: &Path, language: Language) -> String { - let open_report_translation = open_report_translation(language); - //open_report_translation.push_str(&format!(" [{}+O]", get_command_key())); - let string_path = output_path.to_string_lossy().to_string(); - format!( - "{:^len$}\n{string_path}", - open_report_translation, - len = string_path.len() - ) +/// Returns the file to use for the output PCAP report +/// It tries and fallbacks in the order: custom path, configs path, home directory path +// /// This function also updates the custom path text input +pub fn set_report_file_to_use(custom_path: &str) -> File { + if let Ok(custom_file) = File::create(custom_path) { + return custom_file; + } else if let Ok(mut config_path) = + confy::get_configuration_file_path("sniffnet", PCAP_FILE_NAME) + { + if let Ok(file) = File::create(config_path) { + return file; + } + } + File::create(PathBuf::from(std::env::var_os("HOME").unwrap())).unwrap() } pub fn print_cli_welcome_message() {