From a8ba6573e8eb68fe28ca5499bc7d780f4fc0487e Mon Sep 17 00:00:00 2001 From: Giuliano Bellini s294739 Date: Mon, 30 Jan 2023 11:45:27 +0100 Subject: [PATCH] added CI/CD for npcap (Windows) --- .github/workflows/rust.yml | 55 ++++++++++++++++++++++------ .gitignore | 7 ++-- src/enums/app_protocol.rs | 2 +- src/enums/ip_version.rs | 2 +- src/enums/trans_protocol.rs | 2 +- src/gui/gui_initial_page.rs | 6 +-- src/gui/gui_run_page.rs | 13 +++---- src/structs/traffic_chart.rs | 2 +- src/utility/get_formatted_strings.rs | 7 ++-- 9 files changed, 62 insertions(+), 34 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 7664a806..8673fe44 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -7,6 +7,10 @@ on: pull_request: branches: - '*' + workflow_call: + secrets: + NPCAP_OEM_URL: + required: true env: CARGO_TERM_COLOR: always @@ -15,7 +19,7 @@ env: jobs: fmt: name: Rustfmt - runs-on: ubuntu-latest + runs-on: macos-latest steps: - uses: actions/checkout@v2 - uses: actions-rs/toolchain@v1 @@ -31,7 +35,7 @@ jobs: clippy: name: Clippy - runs-on: ubuntu-latest + runs-on: macos-latest steps: - uses: actions/checkout@v2 - uses: actions-rs/toolchain@v1 @@ -48,20 +52,47 @@ jobs: name: Mac Tests runs-on: macos-latest steps: - - uses: actions/checkout@v3 - - name: Build - run: cargo build --verbose - - name: Run tests - run: cargo test --verbose + - uses: actions/checkout@v3 + - name: Build + run: cargo build --verbose + - name: Run tests + run: cargo test --verbose - Linux: + Linux: name: Linux Tests runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Install libpcap + run: sudo apt-get install libpcap-dev + - name: Install dependency for alsa-sys + run: sudo apt-get install libasound2-dev + - name: Build + run: cargo build --verbose + - name: Run tests + run: cargo test --verbose + + Windows: + name: Windows Tests + runs-on: windows-latest steps: - uses: actions/checkout@v3 - - name: Install libpcap - run: sudo apt-get install libpcap-dev - - name: Build + - name: Install npcap sdk + run: | + Invoke-WebRequest -Uri "https://npcap.com/dist/npcap-sdk-1.13.zip" -OutFile "C:/npcap-sdk.zip" + Expand-Archive -LiteralPath C:/npcap-sdk.zip -DestinationPath C:/npcap-sdk + echo "LIB=C:/npcap-sdk/Lib/x64" >> $env:GITHUB_ENV + - name: Install npcap dll + # Secrets are not passed to workflows that are triggered by a pull request from a fork. + # https://docs.github.com/actions/automating-your-workflow-with-github-actions/creating-and-using-encrypted-secrets + if: github.event_name != 'pull_request' + run: | + $OEM_URL = ConvertTo-SecureString "${{ secrets.NPCAP_OEM_URL }}" -AsPlainText -Force + Invoke-WebRequest -Uri $OEM_URL -OutFile C:/npcap-oem.exe + C:/npcap-oem.exe /S + - name: Build + if: github.event_name != 'pull_request' run: cargo build --verbose - - name: Run tests + - name: Run tests + if: github.event_name != 'pull_request' run: cargo test --verbose diff --git a/.gitignore b/.gitignore index 5587f27b..968c0e34 100644 --- a/.gitignore +++ b/.gitignore @@ -217,11 +217,10 @@ $RECYCLE.BIN/ ##################################################################### -# report file generated by the application +# report file generated by the application and country database *.txt - -#folder with gif and images -/img +*.csv +/country_db #folder with reports /sniffnet_* diff --git a/src/enums/app_protocol.rs b/src/enums/app_protocol.rs index d0a9266a..73e255cc 100644 --- a/src/enums/app_protocol.rs +++ b/src/enums/app_protocol.rs @@ -113,7 +113,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { if self.eq(&AppProtocol::Other) { write!(f, "All protocols") } else { - write!(f, "{:?}", self) + write!(f, "{self:?}") } } } diff --git a/src/enums/ip_version.rs b/src/enums/ip_version.rs index fbb6ecc1..02c5c8c4 100644 --- a/src/enums/ip_version.rs +++ b/src/enums/ip_version.rs @@ -13,6 +13,6 @@ pub enum IpVersion { impl fmt::Display for IpVersion { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{:?}", self) + write!(f, "{self:?}") } } diff --git a/src/enums/trans_protocol.rs b/src/enums/trans_protocol.rs index ad54d279..f6bd7da5 100644 --- a/src/enums/trans_protocol.rs +++ b/src/enums/trans_protocol.rs @@ -14,6 +14,6 @@ pub enum TransProtocol { impl fmt::Display for TransProtocol { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{:?}", self) + write!(f, "{self:?}") } } diff --git a/src/gui/gui_initial_page.rs b/src/gui/gui_initial_page.rs index 83bf801b..4f2e6d49 100644 --- a/src/gui/gui_initial_page.rs +++ b/src/gui/gui_initial_page.rs @@ -105,7 +105,7 @@ pub fn initial_page(sniffer: &mut Sniffer) -> Column { } Some(description) => { #[cfg(not(target_os = "windows"))] - dev_str.push_str(&format!("{}\n", name)); + dev_str.push_str(&format!("{name}\n")); dev_str.push_str(&description); } } @@ -122,7 +122,7 @@ pub fn initial_page(sniffer: &mut Sniffer) -> Column { for addr in dev.addresses { let address_string = addr.addr.to_string(); - dev_str.push_str(&format!("\n {}", address_string)); + dev_str.push_str(&format!("\n {address_string}")); } dev_str.push_str("\n "); dev_str_list.push((name, dev_str)); @@ -329,7 +329,7 @@ pub fn initial_page(sniffer: &mut Sniffer) -> Column { let footer_row = Row::new() .align_items(Alignment::Center) .push( - Text::new(format!("Sniffnet {} - by Giuliano Bellini ", APP_VERSION)) + Text::new(format!("Sniffnet {APP_VERSION} - by Giuliano Bellini ")) .size(FONT_SIZE_FOOTER) .font(font_footer), ) diff --git a/src/gui/gui_run_page.rs b/src/gui/gui_run_page.rs index 85252cf0..b27ce21f 100644 --- a/src/gui/gui_run_page.rs +++ b/src/gui/gui_run_page.rs @@ -145,13 +145,13 @@ pub fn run_page(sniffer: &mut Sniffer) -> Column { { (Text::new(sniffer.waiting.len().to_string()).font(ICONS).size(60), Text::new(format!("No traffic has been observed yet. Waiting for network packets...\n\n\ - Network adapter: {}\n\n\ - Are you sure you are connected to the internet and you have selected the right adapter?", adapter_name)).font(font)) + Network adapter: {adapter_name}\n\n\ + Are you sure you are connected to the internet and you have selected the right adapter?")).font(font)) } else { (Text::new('T'.to_string()).font(ICONS).size(60), Text::new(format!("No traffic can be observed because the adapter you selected has no active addresses...\n\n\ - Network adapter: {}\n\n\ - If you are sure you are connected to the internet, try choosing a different adapter.", adapter_name)).font(font)) + Network adapter: {adapter_name}\n\n\ + If you are sure you are connected to the internet, try choosing a different adapter.")).font(font)) }; body = body .push(Row::new().height(Length::FillPortion(1))) @@ -410,8 +410,7 @@ pub fn run_page(sniffer: &mut Sniffer) -> Column { let error_text = Text::new(format!( "An error occurred! \n\n\ - {}", - err_string + {err_string}", )) .font(font); @@ -438,7 +437,7 @@ pub fn run_page(sniffer: &mut Sniffer) -> Column { let footer_row = Row::new() .align_items(Alignment::Center) .push( - Text::new(format!("Sniffnet {} - by Giuliano Bellini ", APP_VERSION)) + Text::new(format!("Sniffnet {APP_VERSION} - by Giuliano Bellini ")) .size(FONT_SIZE_FOOTER) .font(font_footer), ) diff --git a/src/structs/traffic_chart.rs b/src/structs/traffic_chart.rs index 5ca9e159..02e60b01 100644 --- a/src/structs/traffic_chart.rs +++ b/src/structs/traffic_chart.rs @@ -89,7 +89,7 @@ fn build_chart(&self, mut chart: ChartBuilder) { .label_style(("notosans", 15).into_font().color(&self.font_color)) .y_label_formatter(&|bytes| match bytes { 0..=999 | -999..=-1 => { - format!("{}", bytes) + format!("{bytes}") } 1000..=999_999 | -999_999..=-1000 => { format!("{:.1} {}", *bytes as f64 / 1_000_f64, "k") diff --git a/src/utility/get_formatted_strings.rs b/src/utility/get_formatted_strings.rs index d1062688..a393e7e7 100644 --- a/src/utility/get_formatted_strings.rs +++ b/src/utility/get_formatted_strings.rs @@ -129,8 +129,7 @@ pub fn get_app_count_string(app_count: HashMap, tot_packets: let spaces_string_2 = " ".to_string().repeat(10 - percentage_string.len()); ret_val.push_str(&format!( - " {}:{}{}{}{} \n", - app_proto_string, spaces_string_1, num_string, spaces_string_2, percentage_string + " {app_proto_string}:{spaces_string_1}{num_string}{spaces_string_2}{percentage_string} \n", )); } ret_val @@ -163,9 +162,9 @@ pub fn get_formatted_bytes_string(bytes: u128) -> String { if !multiple_transmitted.is_empty() { // with multiple - format!("{:.1} {}B", n, multiple_transmitted) + format!("{n:.1} {multiple_transmitted}B") } else { // no multiple - format!("{} B", n) + format!("{n} B") } }