mirror of
https://github.com/GyulyVGC/sniffnet.git
synced 2025-12-23 22:29:01 -05:00
fix Conf trait impls
This commit is contained in:
@@ -1,11 +1,9 @@
|
||||
use clap::Parser;
|
||||
use iced::{Task, window};
|
||||
|
||||
use crate::CONFIGS;
|
||||
use crate::Configs;
|
||||
use crate::SNIFFNET_LOWERCASE;
|
||||
use crate::gui::types::conf::{CONF, Conf};
|
||||
use crate::gui::types::message::Message;
|
||||
use crate::utils::formatted_strings::APP_VERSION;
|
||||
use clap::Parser;
|
||||
use iced::{Task, window};
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(
|
||||
@@ -16,7 +14,7 @@
|
||||
)]
|
||||
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)]
|
||||
#[arg(short, long, value_name = "NAME", default_missing_value = CONF.device.device_name.as_str(), num_args = 0..=1)]
|
||||
adapter: Option<String>,
|
||||
#[cfg(all(windows, not(debug_assertions)))]
|
||||
/// Show the logs (stdout and stderr) of the most recent application run
|
||||
@@ -50,7 +48,7 @@ pub fn handle_cli_args() -> Task<Message> {
|
||||
}
|
||||
|
||||
if args.restore_default {
|
||||
if Configs::default().store().is_ok() {
|
||||
if Conf::default().store().is_ok() {
|
||||
println!("Restored default settings");
|
||||
}
|
||||
std::process::exit(0);
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
pub mod types;
|
||||
@@ -1,32 +0,0 @@
|
||||
use crate::ConfigWindow;
|
||||
use crate::gui::types::settings::Settings;
|
||||
use crate::networking::types::config_device::ConfigDevice;
|
||||
use confy::ConfyError;
|
||||
|
||||
pub static CONFIGS: std::sync::LazyLock<Configs> = std::sync::LazyLock::new(Configs::load);
|
||||
|
||||
#[derive(Default, Clone, PartialEq, Debug)]
|
||||
pub struct Configs {
|
||||
pub settings: Settings,
|
||||
pub device: ConfigDevice,
|
||||
pub window: ConfigWindow,
|
||||
}
|
||||
|
||||
impl Configs {
|
||||
/// This should only be used directly to load fresh configs;
|
||||
/// use `CONFIGS` instead to access the initial instance
|
||||
pub fn load() -> Self {
|
||||
Configs {
|
||||
settings: Settings::load(),
|
||||
device: ConfigDevice::load(),
|
||||
window: ConfigWindow::load(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn store(self) -> Result<(), ConfyError> {
|
||||
self.settings.store()?;
|
||||
self.device.store()?;
|
||||
self.window.store()?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
pub mod configs;
|
||||
@@ -3,11 +3,13 @@
|
||||
use crate::translations::translations_3::general_translation;
|
||||
use crate::utils::types::icon::Icon;
|
||||
use crate::{Language, StyleType};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// This enum defines the current settings page.
|
||||
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
|
||||
#[derive(PartialEq, Eq, Clone, Copy, Debug, Serialize, Deserialize, Default)]
|
||||
pub enum SettingsPage {
|
||||
/// Settings Notifications page.
|
||||
#[default]
|
||||
Notifications,
|
||||
/// Settings Appearance page.
|
||||
Appearance,
|
||||
|
||||
@@ -89,7 +89,6 @@ pub struct Sniffer {
|
||||
/// Reports if a newer release of the software is available on GitHub
|
||||
pub newer_release_available: Option<bool>,
|
||||
/// Network device to be analyzed, or PCAP file to be imported
|
||||
/// TODO: Conf???
|
||||
pub capture_source: CaptureSource,
|
||||
/// List of network devices
|
||||
pub my_devices: Vec<MyDevice>,
|
||||
@@ -132,7 +131,7 @@ pub fn new(conf: Conf) -> Self {
|
||||
mmdb_asn,
|
||||
..
|
||||
} = conf.settings.clone();
|
||||
let device = conf.device.to_my_device();
|
||||
let capture_source = CaptureSource::from_conf(&conf);
|
||||
Self {
|
||||
conf,
|
||||
current_capture_rx: (0, None),
|
||||
@@ -141,7 +140,7 @@ pub fn new(conf: Conf) -> Self {
|
||||
favorite_hosts: HashSet::new(),
|
||||
logged_notifications: (VecDeque::new(), 0),
|
||||
newer_release_available: None,
|
||||
capture_source: CaptureSource::Device(device),
|
||||
capture_source,
|
||||
my_devices: Vec::new(),
|
||||
pcap_error: None,
|
||||
dots_pulse: (".".to_string(), 0),
|
||||
|
||||
@@ -7,9 +7,14 @@
|
||||
use crate::networking::types::config_device::ConfigDevice;
|
||||
use crate::report::types::report_sort_type::ReportSortType;
|
||||
use crate::report::types::sort_type::SortType;
|
||||
use crate::utils::error_logger::{ErrorLogger, Location};
|
||||
use crate::{SNIFFNET_LOWERCASE, location};
|
||||
use confy::ConfyError;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
|
||||
pub static CONF: std::sync::LazyLock<Conf> = std::sync::LazyLock::new(Conf::load);
|
||||
|
||||
#[derive(Serialize, Deserialize, Default, Clone, PartialEq, Debug)]
|
||||
pub struct Conf {
|
||||
/// Parameters from settings pages
|
||||
pub settings: Settings,
|
||||
@@ -34,3 +39,46 @@ pub struct Conf {
|
||||
/// Import path for PCAP file
|
||||
pub import_pcap_path: String,
|
||||
}
|
||||
|
||||
impl Conf {
|
||||
const FILE_NAME: &'static str = "conf";
|
||||
|
||||
/// This should only be used directly to load fresh configurations;
|
||||
/// use `CONF` instead to access the initial instance
|
||||
#[cfg(not(test))]
|
||||
pub fn load() -> Self {
|
||||
if let Ok(conf) = confy::load::<Conf>(SNIFFNET_LOWERCASE, Self::FILE_NAME) {
|
||||
conf
|
||||
} else {
|
||||
let _ = confy::store(SNIFFNET_LOWERCASE, Self::FILE_NAME, Conf::default())
|
||||
.log_err(location!());
|
||||
Conf::default()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(test))]
|
||||
pub fn store(self) -> Result<(), ConfyError> {
|
||||
confy::store(SNIFFNET_LOWERCASE, Self::FILE_NAME, self).log_err(location!())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::Settings;
|
||||
use crate::gui::types::conf::Conf;
|
||||
|
||||
impl Conf {
|
||||
pub fn test_path() -> String {
|
||||
format!("{}/{}.toml", env!("CARGO_MANIFEST_DIR"), Self::FILE_NAME)
|
||||
}
|
||||
|
||||
pub fn load() -> Self {
|
||||
confy::load_path::<Settings>(Settings::test_path())
|
||||
.unwrap_or_else(|_| Settings::default())
|
||||
}
|
||||
|
||||
pub fn store(self) -> Result<(), confy::ConfyError> {
|
||||
confy::store_path(Settings::test_path(), self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
#[cfg(not(test))]
|
||||
use crate::utils::error_logger::{ErrorLogger, Location};
|
||||
#[cfg(not(test))]
|
||||
use crate::{SNIFFNET_LOWERCASE, location};
|
||||
use iced::window::Position;
|
||||
use iced::{Point, Size};
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -30,23 +26,6 @@ impl ConfigWindow {
|
||||
const MIN_SIZE_X: f32 = 100.0;
|
||||
const MIN_SIZE_Y: f32 = 100.0;
|
||||
|
||||
const FILE_NAME: &'static str = "window";
|
||||
#[cfg(not(test))]
|
||||
pub fn load() -> Self {
|
||||
if let Ok(window) = confy::load::<ConfigWindow>(SNIFFNET_LOWERCASE, Self::FILE_NAME) {
|
||||
window
|
||||
} else {
|
||||
let _ = confy::store(SNIFFNET_LOWERCASE, Self::FILE_NAME, ConfigWindow::default())
|
||||
.log_err(location!());
|
||||
ConfigWindow::default()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(test))]
|
||||
pub fn store(self) -> Result<(), confy::ConfyError> {
|
||||
confy::store(SNIFFNET_LOWERCASE, Self::FILE_NAME, self).log_err(location!())
|
||||
}
|
||||
|
||||
pub fn thumbnail_size(factor: f64) -> SizeTuple {
|
||||
Self::THUMBNAIL_SIZE.scale_and_check(factor)
|
||||
}
|
||||
@@ -142,23 +121,3 @@ fn scale_and_check(self, factor: f64) -> PositionTuple {
|
||||
PositionTuple(x, y)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::ConfigWindow;
|
||||
|
||||
impl ConfigWindow {
|
||||
pub fn test_path() -> String {
|
||||
format!("{}/{}.toml", env!("CARGO_MANIFEST_DIR"), Self::FILE_NAME)
|
||||
}
|
||||
|
||||
pub fn load() -> Self {
|
||||
confy::load_path::<ConfigWindow>(ConfigWindow::test_path())
|
||||
.unwrap_or_else(|_| ConfigWindow::default())
|
||||
}
|
||||
|
||||
pub fn store(self) -> Result<(), confy::ConfyError> {
|
||||
confy::store_path(ConfigWindow::test_path(), self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
|
||||
pub struct ExportPcap {
|
||||
enabled: bool,
|
||||
file_name: String,
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
#[derive(Default)]
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug, Default)]
|
||||
pub struct Filters {
|
||||
expanded: bool,
|
||||
bpf: String,
|
||||
|
||||
@@ -2,11 +2,7 @@
|
||||
|
||||
use crate::gui::styles::types::gradient_type::GradientType;
|
||||
use crate::notifications::types::notifications::Notifications;
|
||||
#[cfg(not(test))]
|
||||
use crate::utils::error_logger::{ErrorLogger, Location};
|
||||
use crate::{Language, StyleType};
|
||||
#[cfg(not(test))]
|
||||
use crate::{SNIFFNET_LOWERCASE, location};
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
|
||||
pub struct Settings {
|
||||
@@ -21,26 +17,6 @@ pub struct Settings {
|
||||
pub style: StyleType,
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
const FILE_NAME: &'static str = "settings";
|
||||
|
||||
#[cfg(not(test))]
|
||||
pub fn load() -> Self {
|
||||
if let Ok(settings) = confy::load::<Settings>(SNIFFNET_LOWERCASE, Self::FILE_NAME) {
|
||||
settings
|
||||
} else {
|
||||
let _ = confy::store(SNIFFNET_LOWERCASE, Self::FILE_NAME, Settings::default())
|
||||
.log_err(location!());
|
||||
Settings::default()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(test))]
|
||||
pub fn store(self) -> Result<(), confy::ConfyError> {
|
||||
confy::store(SNIFFNET_LOWERCASE, Self::FILE_NAME, self).log_err(location!())
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Settings {
|
||||
fn default() -> Self {
|
||||
Settings {
|
||||
@@ -55,23 +31,3 @@ fn default() -> Self {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::Settings;
|
||||
|
||||
impl Settings {
|
||||
pub fn test_path() -> String {
|
||||
format!("{}/{}.toml", env!("CARGO_MANIFEST_DIR"), Self::FILE_NAME)
|
||||
}
|
||||
|
||||
pub fn load() -> Self {
|
||||
confy::load_path::<Settings>(Settings::test_path())
|
||||
.unwrap_or_else(|_| Settings::default())
|
||||
}
|
||||
|
||||
pub fn store(self) -> Result<(), confy::ConfyError> {
|
||||
confy::store_path(Settings::test_path(), self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,14 +24,13 @@
|
||||
use translations::types::language::Language;
|
||||
use utils::formatted_strings::print_cli_welcome_message;
|
||||
|
||||
use crate::configs::types::configs::{CONFIGS, Configs};
|
||||
use crate::gui::sniffer::FONT_FAMILY_NAME;
|
||||
use crate::gui::styles::style_constants::{ICONS_BYTES, SARASA_MONO_BOLD_BYTES, SARASA_MONO_BYTES};
|
||||
use crate::gui::types::conf::CONF;
|
||||
use crate::gui::types::config_window::{ConfigWindow, ToPosition, ToSize};
|
||||
|
||||
mod chart;
|
||||
mod cli;
|
||||
mod configs;
|
||||
mod countries;
|
||||
mod gui;
|
||||
mod mmdb;
|
||||
@@ -60,7 +59,7 @@ pub fn main() -> iced::Result {
|
||||
_gag2 = gag2;
|
||||
}
|
||||
|
||||
let configs = CONFIGS.clone();
|
||||
let conf = CONF.clone();
|
||||
let boot_task_chain = handle_cli_args();
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
@@ -76,7 +75,7 @@ pub fn main() -> iced::Result {
|
||||
|
||||
print_cli_welcome_message();
|
||||
|
||||
let ConfigWindow { size, position, .. } = configs.window;
|
||||
let ConfigWindow { size, position, .. } = conf.window;
|
||||
|
||||
application(SNIFFNET_TITLECASE, Sniffer::update, Sniffer::view)
|
||||
.settings(Settings {
|
||||
@@ -112,5 +111,5 @@ pub fn main() -> iced::Result {
|
||||
.subscription(Sniffer::subscription)
|
||||
.theme(Sniffer::theme)
|
||||
.scale_factor(Sniffer::scale_factor)
|
||||
.run_with(move || (Sniffer::new(configs), boot_task_chain))
|
||||
.run_with(move || (Sniffer::new(conf), boot_task_chain))
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::gui::types::conf::Conf;
|
||||
use crate::gui::types::filters::Filters;
|
||||
use crate::networking::types::my_device::MyDevice;
|
||||
use crate::networking::types::my_link_type::MyLinkType;
|
||||
@@ -5,6 +6,7 @@
|
||||
use crate::translations::translations_4::capture_file_translation;
|
||||
use crate::translations::types::language::Language;
|
||||
use pcap::{Active, Address, Capture, Error, Packet, Savefile, Stat};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub enum CaptureContext {
|
||||
Live(Live),
|
||||
@@ -155,6 +157,19 @@ pub enum CaptureSource {
|
||||
}
|
||||
|
||||
impl CaptureSource {
|
||||
pub fn from_conf(conf: &Conf) -> Self {
|
||||
match conf.capture_source_picklist {
|
||||
CaptureSourcePicklist::Device => {
|
||||
let device = conf.device.to_my_device();
|
||||
Self::Device(device)
|
||||
}
|
||||
CaptureSourcePicklist::File => {
|
||||
let path = conf.import_pcap_path.clone();
|
||||
Self::File(MyPcapImport::new(path))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn title(&self, language: Language) -> &str {
|
||||
match self {
|
||||
Self::Device(_) => network_adapter_translation(language),
|
||||
@@ -222,7 +237,7 @@ pub fn new(path: String) -> Self {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Eq, PartialEq, Debug, Copy, Default)]
|
||||
#[derive(Clone, Eq, PartialEq, Debug, Copy, Default, Serialize, Deserialize)]
|
||||
pub enum CaptureSourcePicklist {
|
||||
#[default]
|
||||
Device,
|
||||
|
||||
@@ -1,8 +1,4 @@
|
||||
use crate::networking::types::my_device::MyDevice;
|
||||
#[cfg(not(test))]
|
||||
use crate::utils::error_logger::{ErrorLogger, Location};
|
||||
#[cfg(not(test))]
|
||||
use crate::{SNIFFNET_LOWERCASE, location};
|
||||
use pcap::{Device, DeviceFlags};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@@ -28,24 +24,6 @@ fn default() -> Self {
|
||||
}
|
||||
|
||||
impl ConfigDevice {
|
||||
const FILE_NAME: &'static str = "device";
|
||||
|
||||
#[cfg(not(test))]
|
||||
pub fn load() -> Self {
|
||||
if let Ok(device) = confy::load::<ConfigDevice>(SNIFFNET_LOWERCASE, Self::FILE_NAME) {
|
||||
device
|
||||
} else {
|
||||
let _ = confy::store(SNIFFNET_LOWERCASE, Self::FILE_NAME, ConfigDevice::default())
|
||||
.log_err(location!());
|
||||
ConfigDevice::default()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(test))]
|
||||
pub fn store(self) -> Result<(), confy::ConfyError> {
|
||||
confy::store(SNIFFNET_LOWERCASE, Self::FILE_NAME, self).log_err(location!())
|
||||
}
|
||||
|
||||
pub fn to_my_device(&self) -> MyDevice {
|
||||
for device in Device::list().unwrap_or_default() {
|
||||
if device.name.eq(&self.device_name) {
|
||||
@@ -61,23 +39,3 @@ pub fn to_my_device(&self) -> MyDevice {
|
||||
MyDevice::from_pcap_device(standard_device)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::networking::types::config_device::ConfigDevice;
|
||||
|
||||
impl ConfigDevice {
|
||||
pub fn test_path() -> String {
|
||||
format!("{}/{}.toml", env!("CARGO_MANIFEST_DIR"), Self::FILE_NAME)
|
||||
}
|
||||
|
||||
pub fn load() -> Self {
|
||||
confy::load_path::<ConfigDevice>(ConfigDevice::test_path())
|
||||
.unwrap_or_else(|_| ConfigDevice::default())
|
||||
}
|
||||
|
||||
pub fn store(self) -> Result<(), confy::ConfyError> {
|
||||
confy::store_path(ConfigDevice::test_path(), self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
use std::fmt::Debug;
|
||||
|
||||
use iced::widget::Text;
|
||||
|
||||
use crate::gui::styles::button::ButtonType;
|
||||
use crate::gui::styles::types::style_type::StyleType;
|
||||
use crate::report::types::sort_type::SortType;
|
||||
use iced::widget::Text;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Struct representing the possible kinds of sort for displayed relevant connections.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
|
||||
pub struct ReportSortType {
|
||||
pub data_sort: SortType,
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
use iced::widget::Text;
|
||||
|
||||
use crate::gui::styles::button::ButtonType;
|
||||
use crate::gui::styles::types::style_type::StyleType;
|
||||
use crate::utils::types::icon::Icon;
|
||||
use iced::widget::Text;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
|
||||
pub enum SortType {
|
||||
Ascending,
|
||||
Descending,
|
||||
|
||||
Reference in New Issue
Block a user