From 0cb2cb9493c97b28119f52efe5deda2b3b89a7e2 Mon Sep 17 00:00:00 2001 From: Nbiba Bedis Date: Wed, 30 Aug 2023 07:35:41 +0100 Subject: [PATCH 1/3] fix(windows): don't send key twice --- src/main.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 6361d119d..84bbb6684 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,7 +19,7 @@ use std::{ }; use crossterm::{ - event::{Event, KeyCode, KeyEvent, KeyModifiers}, + event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers}, terminal, }; use display::{elapsed_time, RawTerminalBackend, Ui}; @@ -229,11 +229,13 @@ where Event::Key(KeyEvent { modifiers: KeyModifiers::CONTROL, code: KeyCode::Char('c'), + kind: KeyEventKind::Press, .. }) | Event::Key(KeyEvent { modifiers: KeyModifiers::NONE, code: KeyCode::Char('q'), + kind: KeyEventKind::Press, .. }) => { running.store(false, Ordering::Release); @@ -253,6 +255,7 @@ where Event::Key(KeyEvent { modifiers: KeyModifiers::NONE, code: KeyCode::Char(' '), + kind: KeyEventKind::Press, .. }) => { let restarting = paused.fetch_xor(true, Ordering::SeqCst); @@ -272,6 +275,7 @@ where Event::Key(KeyEvent { modifiers: KeyModifiers::NONE, code: KeyCode::Tab, + kind: KeyEventKind::Press, .. }) => { let paused = paused.load(Ordering::SeqCst); From 51aa29308e2d1d9d4b6776e82bd1c023cfe61473 Mon Sep 17 00:00:00 2001 From: cyqsimon <28627918+cyqsimon@users.noreply.github.com> Date: Thu, 31 Aug 2023 20:07:16 +0800 Subject: [PATCH 2/3] Cache npcap SDK when building on Windows (#281) * Cache npcap SDK on Windows * Call build function correctly * Log when local cache of SDK is found * Fix clippy warnings * Log to STDERR --- .gitignore | 2 +- Cargo.toml | 1 + build.rs | 78 +++++++++++++++++++++++++++++++++++++----------------- 3 files changed, 55 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index d81dc02e8..28fb351f8 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,4 @@ debian/* !debian/control !debian/copyright !debian/rules -!debian/source \ No newline at end of file +!debian/source diff --git a/Cargo.toml b/Cargo.toml index 2162d082b..b649190e8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,5 +50,6 @@ pnet_base = "0.34.0" packet-builder = { version = "0.7.0", git = "https://github.com/cyqsimon/packet_builder.git", branch = "patch-update" } [target.'cfg(target_os="windows")'.build-dependencies] +anyhow = "1.0.75" http_req = "0.9.2" zip = "0.6.6" diff --git a/build.rs b/build.rs index f30602106..d20e5b568 100644 --- a/build.rs +++ b/build.rs @@ -1,50 +1,78 @@ fn main() { #[cfg(target_os = "windows")] - download_windows_pcap_sdk() + download_windows_npcap_sdk().unwrap(); } #[cfg(target_os = "windows")] -fn download_windows_pcap_sdk() { +fn download_windows_npcap_sdk() -> anyhow::Result<()> { use std::{ env, fs, io::{self, Write}, + path::PathBuf, }; + use anyhow::anyhow; use http_req::request; use zip::ZipArchive; println!("cargo:rerun-if-changed=build.rs"); - let out_dir = env::var("OUT_DIR").unwrap(); + // get npcap SDK + const NPCAP_SDK: &str = "npcap-sdk-1.13.zip"; - let mut pcap_zip = Vec::new(); - let res = request::get("https://npcap.com/dist/npcap-sdk-1.13.zip", &mut pcap_zip).unwrap(); - eprintln!("{res:?}"); + let npcap_sdk_download_url = format!("https://npcap.com/dist/{NPCAP_SDK}"); + let cache_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?).join("target"); + let npcap_sdk_cache_path = cache_dir.join(NPCAP_SDK); - let lib_dir = if cfg!(target_arch = "aarch64") { - "Lib/ARM64" + let npcap_zip = match fs::read(&npcap_sdk_cache_path) { + // use cached + Ok(zip_data) => { + eprintln!("Found cached npcap SDK"); + zip_data + } + // download SDK + Err(_) => { + eprintln!("Downloading npcap SDK"); + + // download + let mut zip_data = vec![]; + let _res = request::get(npcap_sdk_download_url, &mut zip_data)?; + + // write cache + fs::create_dir_all(cache_dir)?; + let mut cache = fs::File::create(npcap_sdk_cache_path)?; + cache.write_all(&zip_data)?; + + zip_data + } + }; + + // extract DLL + let lib_path = if cfg!(target_arch = "aarch64") { + "Lib/ARM64/Packet.lib" } else if cfg!(target_arch = "x86_64") { - "Lib/x64" + "Lib/x64/Packet.lib" } else if cfg!(target_arch = "x86") { - "Lib" + "Lib/Packet.lib" } else { panic!("Unsupported target!") }; - let lib_name = "Packet.lib"; - let lib_path = format!("{lib_dir}/{lib_name}"); - - let mut archive = ZipArchive::new(io::Cursor::new(pcap_zip)).unwrap(); - let mut pcap_lib = match archive.by_name(&lib_path) { - Ok(lib) => lib, - Err(err) => { - panic!("{err}"); - } - }; + let mut archive = ZipArchive::new(io::Cursor::new(npcap_zip))?; + let mut npcap_lib = archive.by_name(lib_path)?; + + // write DLL + let lib_dir = PathBuf::from(env::var("OUT_DIR")?).join("npcap_sdk"); + let lib_path = lib_dir.join("Packet.lib"); + fs::create_dir_all(&lib_dir)?; + let mut lib_file = fs::File::create(lib_path)?; + io::copy(&mut npcap_lib, &mut lib_file)?; - fs::create_dir_all(format!("{out_dir}/{lib_dir}")).unwrap(); - let mut pcap_lib_file = fs::File::create(format!("{out_dir}/{lib_path}")).unwrap(); - io::copy(&mut pcap_lib, &mut pcap_lib_file).unwrap(); - pcap_lib_file.flush().unwrap(); + println!( + "cargo:rustc-link-search=native={}", + lib_dir + .to_str() + .ok_or(anyhow!("{lib_dir:?} is not valid UTF-8"))? + ); - println!("cargo:rustc-link-search=native={out_dir}/{lib_dir}"); + Ok(()) } From 8fc1b219bf35cad7efbb41743d01ca02b5f02bfc Mon Sep 17 00:00:00 2001 From: cyqsimon <28627918+cyqsimon@users.noreply.github.com> Date: Thu, 31 Aug 2023 21:58:32 +0800 Subject: [PATCH 3/3] Disable tests on Windows for PRs --- .github/workflows/ci.yaml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index a2d37480d..3e12c46e4 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -52,7 +52,8 @@ jobs: run: cargo clippy --all-targets --all-features -- -D warnings - name: Install npcap on Windows - if: matrix.os == 'windows-latest' + # PRs cannot not be trusted with repository secrets + if: (matrix.os == 'windows-latest') && (github.event_name != 'pull_request') env: NPCAP_OEM_URL: ${{ secrets.NPCAP_OEM_URL }} run: | @@ -62,6 +63,9 @@ jobs: & "$env:TEMP/npcap-oem.exe" /S - name: Run tests + # npcap is needed to run tests on Windows, so this is an unfortunate + # sacrifice we have to make in the name of security + if: (matrix.os != 'windows-latest') || (github.event_name != 'pull_request') run: cargo test --verbose - name: Upload unix binary