Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graceful termination #420

Merged
merged 7 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ All Sniffnet releases with the relative changes are documented in this file.
- Added Uzbek translation 🇺🇿 ([#385](https://github.com/GyulyVGC/sniffnet/pull/385))
- Window size and position are now remembered, so that Sniffnet can reopen with the same window properties
- Users can now provide custom paths for MMDB files to allow using the commercial versions of the country and ASN databases (fixes [#243](https://github.com/GyulyVGC/sniffnet/issues/243))
- The app's configurations are now stored only on application close, instead of needlessly store them each time the settings popup is closed
- Added new command line option `--restore-default` to restore the default configurations of the app (settings, window properties, and device selected at startup)
- The app's configurations are now stored only on application close, instead of needlessly store them each time the settings popup is closed ([#420](https://github.com/GyulyVGC/sniffnet/pull/420))
- The textual output report is not generated anymore
- Settings "Language" tab has been removed. Language selection and other options are now included in a new settings tab "General" ([#365](https://github.com/GyulyVGC/sniffnet/pull/365))
- Updated Portuguese translation to v1.2 ([#398](https://github.com/GyulyVGC/sniffnet/pull/398))
Expand Down
101 changes: 80 additions & 21 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ rodio = { version = "0.17.3", default_features = false, features = ["mp3"] }
dns-lookup = "2.0.4"
toml = "0.8.8"
once_cell = "1.19.0"
ctrlc = { version = "3.4.2", features = ["termination"] }

[target.'cfg(not(target_arch = "powerpc64"))'.dependencies]
reqwest = { version = "0.11.23", default-features = false, features = ["json", "blocking", "rustls-tls"] }
Expand All @@ -59,6 +60,7 @@ reqwest = { version = "0.11.23", features = ["json", "blocking"] }
[dev-dependencies]
serde_test = "1.0.176"
rstest = "0.18.2"
serial_test = { version = "2.0.0", default_features = false }

#───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Expand Down
84 changes: 78 additions & 6 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::utils::formatted_strings::APP_VERSION;
use crate::{Configs, SNIFFNET_LOWERCASE};

/// Parse CLI arguments, and exit if `--help`, `--version`, or an
/// unknown argument was supplied
Expand All @@ -8,6 +9,7 @@ pub fn parse_cli_args() {
match arg.as_str() {
"--help" | "-h" => print_help(),
"--version" | "-v" => print_version(),
"--restore-default" => restore_default(),
_ => {
unknown_argument(&arg);
std::process::exit(1);
Expand All @@ -20,21 +22,91 @@ pub fn parse_cli_args() {
fn print_help() {
println!(
"Application to comfortably monitor your Internet traffic\n\
Usage: sniffnet [OPTIONS]\n\
Usage: {SNIFFNET_LOWERCASE} [OPTIONS]\n\
Options:\n\
\t-h, --help Print help\n\
\t-v, --version Print version info\n\
\t-h, --help Print help\n\
\t--restore-default Restore default settings\n\
\t-v, --version Print version info\n\
(Run without options to start the app)"
);
}

fn print_version() {
println!("sniffnet {APP_VERSION}");
println!("{SNIFFNET_LOWERCASE} {APP_VERSION}");
}

fn restore_default() {
Configs::default().store();
println!("Default settings have been restored");
}

fn unknown_argument(arg: &str) {
eprintln!(
"sniffnet: unknown option '{arg}'\n\
For more information, try 'sniffnet --help'"
"{SNIFFNET_LOWERCASE}: unknown option '{arg}'\n\
For more information, try '{SNIFFNET_LOWERCASE} --help'"
);
}

#[cfg(test)]
mod tests {
use std::sync::{Arc, Mutex};

use serial_test::serial;

use crate::gui::styles::types::custom_palette::ExtraStyles;
use crate::gui::styles::types::gradient_type::GradientType;
use crate::notifications::types::notifications::Notifications;
use crate::{ConfigDevice, ConfigSettings, ConfigWindow, Language, Sniffer, StyleType};

use super::*;

#[test]
#[serial]
fn test_restore_default_configs() {
// initial configs stored are the default ones
assert_eq!(Configs::load(), Configs::default());
let modified_configs = Configs {
settings: ConfigSettings {
color_gradient: GradientType::Wild,
language: Language::ZH,
scale_factor: 0.65,
mmdb_country: "countrymmdb".to_string(),
mmdb_asn: "asnmmdb".to_string(),
style_path: format!(
"{}/resources/themes/catppuccin.toml",
env!("CARGO_MANIFEST_DIR")
),
notifications: Notifications {
volume: 100,
packets_notification: Default::default(),
bytes_notification: Default::default(),
favorite_notification: Default::default(),
},
style: StyleType::Custom(ExtraStyles::DraculaDark),
},
device: ConfigDevice {
device_name: "hey-hey".to_string(),
},
window: ConfigWindow {
position: (440, 99),
size: (452, 870),
},
};
// we want to be sure that modified config is different from defaults
assert_ne!(Configs::default(), modified_configs);
//store modified configs
modified_configs.clone().store();
// assert they've been stored
assert_eq!(Configs::load(), modified_configs);
// restore defaults
restore_default();
// assert that defaults are stored
assert_eq!(Configs::load(), Configs::default());

// only needed because it will delete config files via its Drop implementation
Sniffer::new(
&Arc::new(Mutex::new(Configs::default())),
Arc::new(Mutex::new(Some(true))),
);
}
}
Loading
Loading