Skip to content

Commit

Permalink
handle custom report path (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
GyulyVGC committed Dec 6, 2023
1 parent 0bf99f7 commit 7786097
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 79 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
</details>

## Troubleshooting
Expand Down
9 changes: 3 additions & 6 deletions src/configs/types/config_advanced_settings.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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(),
}
}
}
24 changes: 6 additions & 18 deletions src/gui/pages/settings_advanced_page.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::path::PathBuf;
use std::sync::Arc;

use iced::advanced::widget::Text;
Expand Down Expand Up @@ -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};

Expand Down Expand Up @@ -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<StyleType>> {
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(
Expand Down
4 changes: 2 additions & 2 deletions src/gui/types/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
6 changes: 2 additions & 4 deletions src/gui/types/sniffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -391,7 +389,7 @@ impl Sniffer {

fn reset(&mut self) -> Command<Message> {
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;
Expand Down
24 changes: 0 additions & 24 deletions src/translations/translations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!",
Expand Down
55 changes: 30 additions & 25 deletions src/utils/formatted_strings.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
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};

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)]
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 7786097

Please sign in to comment.