Skip to content

Commit d9537a6

Browse files
committed
Fix build script on Windows
1 parent 552d982 commit d9537a6

File tree

2 files changed

+31
-34
lines changed

2 files changed

+31
-34
lines changed

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ cargo-insta = "1.31.0"
4949
packet-builder = { version = "0.7.0", git = "https://github.com/cyqsimon/packet_builder.git", branch = "patch-update" }
5050
regex = "1"
5151

52-
[target.'cfg(target_os="windows")'.build-dependencies]
52+
# [target.'cfg(target_os="windows")'.build-dependencies]
53+
[build-dependencies]
5354
http_req = "0.9.2"
5455
zip = "0.6.6"

build.rs

+29-33
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,50 @@
11
fn main() {
22
#[cfg(target_os = "windows")]
3-
download_winpcap_sdk()
3+
download_windows_pcap_sdk()
44
}
55

66
#[cfg(target_os = "windows")]
7-
fn download_winpcap_sdk() {
7+
fn download_windows_pcap_sdk() {
8+
use std::{
9+
env, fs,
10+
io::{self, Write},
11+
};
12+
813
use http_req::request;
9-
use std::env;
10-
use std::fs::File;
11-
use std::io::prelude::*;
14+
use zip::ZipArchive;
1215

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

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

17-
let mut reader = Vec::new();
18-
let _res = request::get(
19-
"https://nmap.org/npcap/dist/npcap-sdk-1.05.zip",
20-
&mut reader,
21-
)
22-
.unwrap();
23-
24-
let mut pcapzip = File::create(format!("{}{}", out_dir, "/npcap.zip")).unwrap();
25-
pcapzip.write_all(reader.as_slice()).unwrap();
26-
pcapzip.flush().unwrap();
27-
28-
pcapzip = File::open(format!("{}{}", out_dir, "/npcap.zip")).unwrap();
29-
20+
let mut pcap_zip = Vec::new();
21+
let res = request::get("https://npcap.com/dist/npcap-sdk-1.13.zip", &mut pcap_zip).unwrap();
22+
eprintln!("{:?}", res);
23+
24+
let lib_dir = if cfg!(target_arch = "aarch64") {
25+
"Lib/ARM64"
26+
} else if cfg!(target_arch = "x86_64") {
27+
"Lib/x64"
28+
} else if cfg!(target_arch = "x86") {
29+
"Lib"
30+
} else {
31+
panic!("Unsupported target!")
32+
};
3033
let lib_name = "Packet.lib";
31-
#[cfg(target_arch = "x86_64")]
32-
let lib_dir = "Lib/x64";
33-
#[cfg(target_arch = "x86")]
34-
let lib_dir = "Lib";
35-
3634
let lib_path = format!("{}/{}", lib_dir, lib_name);
37-
let mut zip_archive = zip::ZipArchive::new(pcapzip).unwrap();
38-
let mut pcaplib = match zip_archive.by_name(lib_path.as_str()) {
39-
Ok(pcaplib) => pcaplib,
35+
36+
let mut archive = ZipArchive::new(io::Cursor::new(pcap_zip)).unwrap();
37+
let mut pcap_lib = match archive.by_name(&lib_path) {
38+
Ok(lib) => lib,
4039
Err(err) => {
4140
panic!("{}", err);
4241
}
4342
};
4443

45-
let mut pcaplib_bytes = Vec::new();
46-
pcaplib.read_to_end(&mut pcaplib_bytes).unwrap();
47-
48-
std::fs::create_dir_all(format!("{}/{}", out_dir, lib_dir)).unwrap();
49-
let mut pcaplib_file = File::create(format!("{}/{}", out_dir, lib_path)).unwrap();
50-
pcaplib_file.write_all(pcaplib_bytes.as_slice()).unwrap();
51-
pcaplib_file.flush().unwrap();
44+
fs::create_dir_all(format!("{}/{}", out_dir, lib_dir)).unwrap();
45+
let mut pcap_lib_file = fs::File::create(format!("{}/{}", out_dir, lib_path)).unwrap();
46+
io::copy(&mut pcap_lib, &mut pcap_lib_file).unwrap();
47+
pcap_lib_file.flush().unwrap();
5248

5349
println!("cargo:rustc-link-search=native={}/{}", out_dir, lib_dir);
5450
}

0 commit comments

Comments
 (0)