From 8f74f5bf354a643ef0ac7c610e6c4faab86f29fb Mon Sep 17 00:00:00 2001 From: GyulyVGC Date: Sun, 24 Nov 2024 13:39:33 +0100 Subject: [PATCH 1/5] add CLI argument --adapter to allow immediately start the capture from a given interface --- Cargo.lock | 240 ++++++++++++++++++++++++++++++++++++------------- Cargo.toml | 1 + src/cli/mod.rs | 74 +++++++-------- src/main.rs | 7 +- 4 files changed, 217 insertions(+), 105 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 585613a2..b5040d69 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -142,6 +142,55 @@ dependencies = [ "libc", ] +[[package]] +name = "anstream" +version = "0.6.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" + +[[package]] +name = "anstyle-parse" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" +dependencies = [ + "windows-sys 0.59.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2109dbce0e72be3ec00bed26e6a7479ca384ad226efdd66db8fa2e3a38c83125" +dependencies = [ + "anstyle", + "windows-sys 0.59.0", +] + [[package]] name = "approx" version = "0.5.1" @@ -316,7 +365,7 @@ checksum = "3b43422f69d8ff38f95f1b2bb76517c91589a924d1559a0e935d7c8ce0274c11" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -351,7 +400,7 @@ checksum = "721cae7de5c34fbb2acd27e21e6d2cf7b886dce0c27388d46c4e6c47ea4318dd" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -402,7 +451,7 @@ dependencies = [ "regex", "rustc-hash 1.1.0", "shlex", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -498,7 +547,7 @@ checksum = "bcfcc3cd946cb52f0bbfdbbcfa2f4e24f75ebb6c0e1002f7c25904fada18b9ec" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -606,6 +655,46 @@ dependencies = [ "libloading 0.8.5", ] +[[package]] +name = "clap" +version = "4.5.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb3b4b9e5a7c7514dfa52869339ee98b3156b0bfb4e8a77c4ff4babb64b1604f" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b17a95aa67cc7b5ebd32aa5370189aa0d79069ef1c64ce893bd30fb24bff20ec" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ac6a0c7b1a9e9a5186361f67dfa1b88213572f427fb9ab038efb2bd8c582dab" +dependencies = [ + "heck 0.5.0", + "proc-macro2", + "quote", + "syn 2.0.89", +] + +[[package]] +name = "clap_lex" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "afb84c814227b90d6895e01398aee0d8033c00e7466aca416fb6a8e0eb19d8a7" + [[package]] name = "clipboard-win" version = "5.4.0" @@ -661,6 +750,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" +[[package]] +name = "colorchoice" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" + [[package]] name = "com" version = "0.6.0" @@ -865,9 +960,9 @@ dependencies = [ [[package]] name = "cpufeatures" -version = "0.2.15" +version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ca741a962e1b0bff6d724a1a0958b686406e853bb14061f218562e1896f95e6" +checksum = "16b80225097f2e5ae4e7179dd2266824648f3e2f49d9134d584b76389d31c4c3" dependencies = [ "libc", ] @@ -1060,7 +1155,7 @@ checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -1186,7 +1281,7 @@ checksum = "de0d48a183585823424a4ce1aa132d174a6a81bd540895822eb4c8373a8e49e8" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -1409,7 +1504,7 @@ checksum = "1a5c6c585bc94aaf2c7b51dd4c2ba22680844aba4c687be581871a6f518c5742" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -1503,7 +1598,7 @@ checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -1772,6 +1867,12 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "hermit-abi" version = "0.3.9" @@ -2231,7 +2332,7 @@ checksum = "1ec89e9337638ecdc08744df490b221a7399bf8d164eb52a665454e60e075ad6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -2295,6 +2396,12 @@ dependencies = [ "serde", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + [[package]] name = "itertools" version = "0.10.5" @@ -2474,9 +2581,9 @@ checksum = "2a385b1be4e5c3e362ad2ffa73c392e53f031eaa5b7d648e64cd87f27f6063d7" [[package]] name = "litemap" -version = "0.7.3" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "643cb0b8d4fcc284004d5fd0d67ccf61dfffadb7f75e1e71bc420f4688a3a704" +checksum = "4ee93343901ab17bd981295f2cf0026d4ad018c7c31ba84549a4ddbb47a45104" [[package]] name = "lock_api" @@ -2776,7 +2883,7 @@ checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -2817,7 +2924,7 @@ dependencies = [ "proc-macro-crate", "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -3103,7 +3210,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -3176,12 +3283,12 @@ version = "0.18.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39b0deead1528fd0e5947a8546a9642a9777c25f6e1e26f34c97b204bbb465bd" dependencies = [ - "heck", + "heck 0.4.1", "itertools 0.12.1", "proc-macro2", "proc-macro2-diagnostics", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -3214,7 +3321,7 @@ dependencies = [ "by_address", "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -3338,7 +3445,7 @@ dependencies = [ "phf_shared", "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -3373,7 +3480,7 @@ checksum = "3c0f5fad0874fc7abcd4d750e76917eaebbecaa2c20bde22e1dbeeba8beb758c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -3496,9 +3603,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.89" +version = "1.0.92" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e" +checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" dependencies = [ "unicode-ident", ] @@ -3511,7 +3618,7 @@ checksum = "af066a9c399a26e020ada66a034357a868728e72cd426f3adcd35f80d88d88c8" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", "version_check", "yansi", ] @@ -3902,7 +4009,7 @@ dependencies = [ "regex", "relative-path", "rustc_version", - "syn 2.0.87", + "syn 2.0.89", "unicode-ident", ] @@ -3958,9 +4065,9 @@ dependencies = [ [[package]] name = "rustls" -version = "0.23.17" +version = "0.23.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f1a745511c54ba6d4465e8d5dfbd81b45791756de28d4981af70d6dca128f1e" +checksum = "9c9cc1d47e243d655ace55ed38201c19ae02c148ae56412ab8750e8f0166ab7f" dependencies = [ "once_cell", "ring", @@ -4148,7 +4255,7 @@ checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -4171,7 +4278,7 @@ checksum = "6c64451ba24fc7a6a2d60fc75dd9c83c90903b19028d4eff35e88fc1e86564e9" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -4224,7 +4331,7 @@ checksum = "5d69265a08751de7844521fd15003ae0a888e035773ba05695c5c759a6f89eef" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -4364,6 +4471,7 @@ name = "sniffnet" version = "1.3.1" dependencies = [ "chrono", + "clap", "confy", "ctrlc", "dns-lookup", @@ -4474,6 +4582,12 @@ dependencies = [ "float-cmp", ] +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + [[package]] name = "subtle" version = "2.6.1" @@ -4569,9 +4683,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.87" +version = "2.0.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d" +checksum = "44d46482f1c1c87acd84dea20c1bf5ebff4c757009ed6bf19cfd36fb10e92c4e" dependencies = [ "proc-macro2", "quote", @@ -4595,7 +4709,7 @@ checksum = "c8af7666ab7b6390ab78131fb5b0fce11d6b7a6951602017c35fa82800708971" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -4676,7 +4790,7 @@ checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -4687,7 +4801,7 @@ checksum = "f077553d607adc1caf65430528a576c757a71ed73944b66ebb58ef2bbd243568" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -4871,7 +4985,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -5007,9 +5121,9 @@ checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" [[package]] name = "url" -version = "2.5.3" +version = "2.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d157f1b96d14500ffdc1f10ba712e780825526c03d9a49b4d0324b0d9113ada" +checksum = "32f8b686cadd1473f4bd0117a5d28d36b1ade384ea9b5069a1c40aefed7fda60" dependencies = [ "form_urlencoded", "idna", @@ -5062,6 +5176,12 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "vcpkg" version = "0.2.15" @@ -5121,7 +5241,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", "wasm-bindgen-shared", ] @@ -5155,7 +5275,7 @@ checksum = "26c6ab57572f7a24a4985830b120de1594465e5d500f24afe89e16b4e833ef68" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -5312,9 +5432,9 @@ dependencies = [ [[package]] name = "webpki-roots" -version = "0.26.6" +version = "0.26.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "841c67bff177718f1d4dfefde8d8f0e78f9b6589319ba88312f567fc5841a958" +checksum = "5d642ff16b7e79272ae451b7322067cdc17cadf68c23264be9d94a32319efe7e" dependencies = [ "rustls-pki-types", ] @@ -6002,9 +6122,9 @@ checksum = "c94451ac9513335b5e23d7a8a2b61a7102398b8cca5160829d313e84c9d98be1" [[package]] name = "yoke" -version = "0.7.4" +version = "0.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c5b1314b079b0930c31e3af543d8ee1757b1951ae1e1565ec704403a7240ca5" +checksum = "120e6aef9aa629e3d4f52dc8cc43a015c7724194c97dfaf45180d2daf2b77f40" dependencies = [ "serde", "stable_deref_trait", @@ -6014,13 +6134,13 @@ dependencies = [ [[package]] name = "yoke-derive" -version = "0.7.4" +version = "0.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28cc31741b18cb6f1d5ff12f5b7523e3d6eb0852bbbad19d73905511d9849b95" +checksum = "2380878cad4ac9aac1e2435f3eb4020e8374b5f13c296cb75b4620ff8e229154" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", "synstructure", ] @@ -6107,7 +6227,7 @@ dependencies = [ "proc-macro-crate", "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", "zvariant_utils 2.1.0", ] @@ -6120,7 +6240,7 @@ dependencies = [ "proc-macro-crate", "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", "zbus_names 4.1.0", "zvariant 5.1.0", "zvariant_utils 3.0.2", @@ -6173,27 +6293,27 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] name = "zerofrom" -version = "0.1.4" +version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91ec111ce797d0e0784a1116d0ddcdbea84322cd79e5d5ad173daeba4f93ab55" +checksum = "cff3ee08c995dee1859d998dea82f7374f2826091dd9cd47def953cae446cd2e" dependencies = [ "zerofrom-derive", ] [[package]] name = "zerofrom-derive" -version = "0.1.4" +version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ea7b4a3637ea8669cedf0f1fd5c286a17f3de97b8dd5a70a6c167a1730e63a5" +checksum = "595eed982f7d355beb85837f651fa22e90b3c044842dc7f2c2842c086f295808" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", "synstructure", ] @@ -6222,7 +6342,7 @@ checksum = "6eafa6dfb17584ea3e2bd6e76e0cc15ad7af12b09abdd1ca55961bed9b1063c6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -6263,7 +6383,7 @@ dependencies = [ "proc-macro-crate", "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", "zvariant_utils 2.1.0", ] @@ -6276,7 +6396,7 @@ dependencies = [ "proc-macro-crate", "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", "zvariant_utils 3.0.2", ] @@ -6288,7 +6408,7 @@ checksum = "c51bcff7cc3dbb5055396bcf774748c3dab426b4b8659046963523cee4808340" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -6301,6 +6421,6 @@ dependencies = [ "quote", "serde", "static_assertions", - "syn 2.0.87", + "syn 2.0.89", "winnow", ] diff --git a/Cargo.toml b/Cargo.toml index 44002eaf..56f8a702 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,6 +53,7 @@ rfd = "0.15.1" phf = "0.11.2" phf_shared = "0.11.2" splines = "4.4.0" +clap = { version = "4.5.21", features = ["derive"] } [target.'cfg(not(target_arch = "powerpc64"))'.dependencies] reqwest = { version = "0.12.9", default-features = false, features = ["json", "blocking", "rustls-tls"] } diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 21a594db..e165687e 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -1,50 +1,42 @@ +use crate::gui::types::message::Message; use crate::utils::formatted_strings::APP_VERSION; -use crate::{Configs, SNIFFNET_LOWERCASE}; +use crate::Configs; +use crate::SNIFFNET_LOWERCASE; +use clap::Parser; +use iced::{window, Task}; -/// Parse CLI arguments, and exit if `--help`, `--version`, or an -/// unknown argument was supplied -pub fn parse_cli_args() { - let mut args = std::env::args().skip(1); - if let Some(arg) = args.next() { - match arg.as_str() { - "--help" | "-h" => print_help(), - "--version" | "-v" => print_version(), - "--restore-default" => restore_default(), - _ => { - unknown_argument(&arg); - std::process::exit(1); - } - } - std::process::exit(0); - } +#[derive(Parser, Debug)] +#[command( + name = SNIFFNET_LOWERCASE, + bin_name = SNIFFNET_LOWERCASE, + version = APP_VERSION, + about = "Application to comfortably monitor your network traffic" +)] +struct Args { + /// Start sniffing packets from the supplied network adapter + #[arg(short, long, value_name = "NAME", default_missing_value = "", num_args = 0..=1)] + adapter: Option, + /// Restore default settings + #[arg(short, long)] + restore_default: bool, } -fn print_help() { - println!( - "Application to comfortably monitor your Internet traffic\n\ - Usage: {SNIFFNET_LOWERCASE} [OPTIONS]\n\ - Options:\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)" - ); -} +pub fn parse_cli_args() -> Task { + let mut boot_task_chain = window::get_latest().map(Message::WindowId); -fn print_version() { - println!("{SNIFFNET_LOWERCASE} {APP_VERSION}"); -} + let args = Args::parse(); -fn restore_default() { - Configs::default().store(); - println!("Default settings have been restored"); -} + if args.restore_default { + Configs::default().store(); + } + + if let Some(adapter) = args.adapter { + boot_task_chain = boot_task_chain + .chain(Task::done(Message::AdapterSelection(adapter))) + .chain(Task::done(Message::Start)); + } -fn unknown_argument(arg: &str) { - eprintln!( - "{SNIFFNET_LOWERCASE}: unknown option '{arg}'\n\ - For more information, try '{SNIFFNET_LOWERCASE} --help'" - ); + boot_task_chain } #[cfg(test)] @@ -101,7 +93,7 @@ mod tests { // assert they've been stored assert_eq!(Configs::load(), modified_configs); // restore defaults - restore_default(); + Configs::default().store(); // assert that defaults are stored assert_eq!(Configs::load(), Configs::default()); diff --git a/src/main.rs b/src/main.rs index 3e289d26..521e93fa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ use std::{panic, process, thread}; #[cfg(target_os = "linux")] use iced::window::settings::PlatformSpecific; -use iced::{application, window, Font, Pixels, Settings, Task}; +use iced::{application, window, Font, Pixels, Settings}; use chart::types::chart_type::ChartType; use chart::types::traffic_chart::TrafficChart; @@ -33,7 +33,6 @@ use crate::configs::types::config_window::{ConfigWindow, ToPosition, ToSize}; use crate::configs::types::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::message::Message; use crate::secondary_threads::check_updates::set_newer_release_status; mod chart; @@ -56,7 +55,7 @@ pub const SNIFFNET_TITLECASE: &str = "Sniffnet"; /// /// It initializes shared variables and loads configuration parameters pub fn main() -> iced::Result { - parse_cli_args(); + let boot_task_chain = parse_cli_args(); let configs1 = Arc::new(Mutex::new(Configs::load())); let configs2 = configs1.clone(); @@ -127,7 +126,7 @@ pub fn main() -> iced::Result { .run_with(move || { ( Sniffer::new(&configs1, newer_release_available1), - Task::none().chain(window::get_latest().map(Message::WindowId)), + boot_task_chain, ) }) } From d87b38a6e4c14aa659d5f01e1fedc2f3e5e16835 Mon Sep 17 00:00:00 2001 From: GyulyVGC Date: Sun, 24 Nov 2024 14:08:32 +0100 Subject: [PATCH 2/5] update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index daad39df..0636d8bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ 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)) - 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 92276d54fba7ec5c61bf00619bef13aab16ca82a Mon Sep 17 00:00:00 2001 From: GyulyVGC Date: Sun, 24 Nov 2024 14:46:40 +0100 Subject: [PATCH 3/5] use correct default value for --adapter --- src/cli/mod.rs | 3 ++- src/configs/types/configs.rs | 6 +++++- src/main.rs | 4 ++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/cli/mod.rs b/src/cli/mod.rs index e165687e..6e74a36d 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -1,6 +1,7 @@ use crate::gui::types::message::Message; use crate::utils::formatted_strings::APP_VERSION; use crate::Configs; +use crate::CONFIGS; use crate::SNIFFNET_LOWERCASE; use clap::Parser; use iced::{window, Task}; @@ -14,7 +15,7 @@ use iced::{window, Task}; )] struct Args { /// Start sniffing packets from the supplied network adapter - #[arg(short, long, value_name = "NAME", default_missing_value = "", num_args = 0..=1)] + #[arg(short, long, value_name = "NAME", default_missing_value = CONFIGS.device.device_name.as_str(), num_args = 0..=1)] adapter: Option, /// Restore default settings #[arg(short, long)] diff --git a/src/configs/types/configs.rs b/src/configs/types/configs.rs index 76588bb7..8725935e 100644 --- a/src/configs/types/configs.rs +++ b/src/configs/types/configs.rs @@ -1,4 +1,7 @@ use crate::{ConfigDevice, ConfigSettings, ConfigWindow}; +use once_cell::sync::Lazy; + +pub static CONFIGS: Lazy = Lazy::new(|| Configs::load()); #[derive(Default, Clone, PartialEq, Debug)] pub struct Configs { @@ -8,7 +11,8 @@ pub struct Configs { } impl Configs { - pub fn load() -> Self { + /// This shouldn't be used directly, use `CONFIGS` instead + fn load() -> Self { Configs { settings: ConfigSettings::load(), device: ConfigDevice::load(), diff --git a/src/main.rs b/src/main.rs index 521e93fa..b9c2ea4b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,7 +30,7 @@ use translations::types::language::Language; use utils::formatted_strings::print_cli_welcome_message; use crate::configs::types::config_window::{ConfigWindow, ToPosition, ToSize}; -use crate::configs::types::configs::Configs; +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::secondary_threads::check_updates::set_newer_release_status; @@ -57,7 +57,7 @@ pub const SNIFFNET_TITLECASE: &str = "Sniffnet"; pub fn main() -> iced::Result { let boot_task_chain = parse_cli_args(); - let configs1 = Arc::new(Mutex::new(Configs::load())); + let configs1 = Arc::new(Mutex::new(CONFIGS.clone())); let configs2 = configs1.clone(); let newer_release_available1 = Arc::new(Mutex::new(None)); From 07fad71314426bac4020b3be888641720533c96c Mon Sep 17 00:00:00 2001 From: GyulyVGC Date: Sun, 24 Nov 2024 14:52:18 +0100 Subject: [PATCH 4/5] fix tests --- src/configs/types/configs.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/configs/types/configs.rs b/src/configs/types/configs.rs index 8725935e..cc9e061a 100644 --- a/src/configs/types/configs.rs +++ b/src/configs/types/configs.rs @@ -11,8 +11,8 @@ pub struct Configs { } impl Configs { - /// This shouldn't be used directly, use `CONFIGS` instead - fn load() -> Self { + /// This shouldn't be used directly outside tests, use `CONFIGS` instead + pub fn load() -> Self { Configs { settings: ConfigSettings::load(), device: ConfigDevice::load(), From 32772648ad5a6995747a0380da93981d6b72e1e7 Mon Sep 17 00:00:00 2001 From: GyulyVGC Date: Sun, 24 Nov 2024 15:06:41 +0100 Subject: [PATCH 5/5] fix clippy::pedantic lint --- src/configs/types/configs.rs | 5 +++-- src/main.rs | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/configs/types/configs.rs b/src/configs/types/configs.rs index cc9e061a..79a79351 100644 --- a/src/configs/types/configs.rs +++ b/src/configs/types/configs.rs @@ -1,7 +1,7 @@ use crate::{ConfigDevice, ConfigSettings, ConfigWindow}; use once_cell::sync::Lazy; -pub static CONFIGS: Lazy = Lazy::new(|| Configs::load()); +pub static CONFIGS: Lazy = Lazy::new(Configs::load); #[derive(Default, Clone, PartialEq, Debug)] pub struct Configs { @@ -11,7 +11,8 @@ pub struct Configs { } impl Configs { - /// This shouldn't be used directly outside tests, use `CONFIGS` instead + /// This should only be used directly to load fresh configs; + /// use `CONFIGS` instead to access the initial instance pub fn load() -> Self { Configs { settings: ConfigSettings::load(), diff --git a/src/main.rs b/src/main.rs index b9c2ea4b..9811672d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -55,9 +55,11 @@ pub const SNIFFNET_TITLECASE: &str = "Sniffnet"; /// /// It initializes shared variables and loads configuration parameters pub fn main() -> iced::Result { + let configs = CONFIGS.clone(); + let boot_task_chain = parse_cli_args(); - let configs1 = Arc::new(Mutex::new(CONFIGS.clone())); + let configs1 = Arc::new(Mutex::new(configs)); let configs2 = configs1.clone(); let newer_release_available1 = Arc::new(Mutex::new(None));