Skip to content

Commit

Permalink
Fix build script on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
cyqsimon committed Aug 25, 2023
1 parent 552d982 commit bcb6905
Showing 1 changed file with 29 additions and 33 deletions.
62 changes: 29 additions & 33 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,54 +1,50 @@
fn main() {
#[cfg(target_os = "windows")]
download_winpcap_sdk()
download_windows_pcap_sdk()
}

#[cfg(target_os = "windows")]
fn download_winpcap_sdk() {
fn download_windows_pcap_sdk() {
use std::{
env, fs,
io::{self, Write},
};

use http_req::request;
use std::env;
use std::fs::File;
use std::io::prelude::*;
use zip::ZipArchive;

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

let out_dir = env::var("OUT_DIR").unwrap();

let mut reader = Vec::new();
let _res = request::get(
"https://nmap.org/npcap/dist/npcap-sdk-1.05.zip",
&mut reader,
)
.unwrap();

let mut pcapzip = File::create(format!("{}{}", out_dir, "/npcap.zip")).unwrap();
pcapzip.write_all(reader.as_slice()).unwrap();
pcapzip.flush().unwrap();

pcapzip = File::open(format!("{}{}", out_dir, "/npcap.zip")).unwrap();

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 lib_dir = if cfg!(target_arch = "aarch64") {
"Lib/ARM64"
} else if cfg!(target_arch = "x86_64") {
"Lib/x64"
} else if cfg!(target_arch = "x86") {
"Lib"
} else {
panic!("Unsupported target!")
};
let lib_name = "Packet.lib";
#[cfg(target_arch = "x86_64")]
let lib_dir = "Lib/x64";
#[cfg(target_arch = "x86")]
let lib_dir = "Lib";

let lib_path = format!("{}/{}", lib_dir, lib_name);
let mut zip_archive = zip::ZipArchive::new(pcapzip).unwrap();
let mut pcaplib = match zip_archive.by_name(lib_path.as_str()) {
Ok(pcaplib) => pcaplib,

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 pcaplib_bytes = Vec::new();
pcaplib.read_to_end(&mut pcaplib_bytes).unwrap();

std::fs::create_dir_all(format!("{}/{}", out_dir, lib_dir)).unwrap();
let mut pcaplib_file = File::create(format!("{}/{}", out_dir, lib_path)).unwrap();
pcaplib_file.write_all(pcaplib_bytes.as_slice()).unwrap();
pcaplib_file.flush().unwrap();
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={}/{}", out_dir, lib_dir);
}

0 comments on commit bcb6905

Please sign in to comment.