Skip to content

Commit

Permalink
feat: add wifiscanner local lib, update win support code
Browse files Browse the repository at this point in the history
  • Loading branch information
Chleba committed Oct 21, 2024
1 parent 0b1d76e commit ad5d74e
Show file tree
Hide file tree
Showing 17 changed files with 1,633 additions and 271 deletions.
386 changes: 192 additions & 194 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ strum = "0.26.3"
surge-ping = "0.8.1"
tokio = { version = "1.40.0", features = ["full"] }
tokio-util = "0.7.12"
tokio-wifiscanner = { git = "https://github.com/EphemeralSapient/tokio-wifiscanner" }
tokio-wifiscanner = { path = "./libs/wifiscanner" }
tracing = "0.1.40"
tracing-error = "0.2.0"
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "serde"] }
Expand Down
153 changes: 77 additions & 76 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,79 +49,80 @@ fn main() {
println!("cargo:rustc-env=_GIT_INFO={}", git_describe);
}

#[cfg(target_os = "windows")]
fn download_windows_npcap_sdk() -> anyhow::Result<()> {

use anyhow::anyhow;

use std::{
fs,
io::{self, Write},
env,
path::PathBuf
};

use http_req::request;
use zip::ZipArchive;

println!("cargo:rerun-if-changed=build.rs");

// get npcap SDK
const NPCAP_SDK: &str = "npcap-sdk-1.13.zip";

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 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/Packet.lib"
} else if cfg!(target_arch = "x86") {
"Lib/Packet.lib"
} else {
panic!("Unsupported target!")
};
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)?;

println!(
"cargo:rustc-link-search=native={}",
lib_dir
.to_str()
.ok_or(anyhow!("{lib_dir:?} is not valid UTF-8"))?
);

Ok(())
}
// -- not working on win10, also I don't like it when app is downloading third party *
// #[cfg(target_os = "windows")]
// fn download_windows_npcap_sdk() -> anyhow::Result<()> {

// use anyhow::anyhow;

// use std::{
// fs,
// io::{self, Write},
// env,
// path::PathBuf
// };

// use http_req::request;
// use zip::ZipArchive;

// println!("cargo:rerun-if-changed=build.rs");

// // get npcap SDK
// const NPCAP_SDK: &str = "npcap-sdk-1.13.zip";

// 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 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/Packet.lib"
// } else if cfg!(target_arch = "x86") {
// "Lib/Packet.lib"
// } else {
// panic!("Unsupported target!")
// };
// 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)?;

// println!(
// "cargo:rustc-link-search=native={}",
// lib_dir
// .to_str()
// .ok_or(anyhow!("{lib_dir:?} is not valid UTF-8"))?
// );

// Ok(())
// }
2 changes: 2 additions & 0 deletions libs/wifiscanner/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
/.idea
Loading

0 comments on commit ad5d74e

Please sign in to comment.