Skip to content

Commit 46e66bd

Browse files
author
e1732a364fed
committed
auto extract dll for download_wintun
1 parent 493e948 commit 46e66bd

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

crates/ruci-cmd/src/utils/mod.rs

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ pub enum Commands {
2626
/// download Country.mmdb
2727
Mmdb,
2828

29-
/// download wintun.zip
29+
/// download wintun.zip and auto extract the wintun.dll
3030
Wintun,
3131

32-
/// download ruci-webui's dist folder
32+
/// download ruci-webui's dist folder and extract it.
3333
Webui,
3434

3535
/// calculate trojan hash for a plain text password
@@ -398,7 +398,50 @@ async fn download_mmdb() -> anyhow::Result<()> {
398398

399399
async fn download_wintun() -> anyhow::Result<()> {
400400
const WINTUN_ZIP: &str = "wintun.zip";
401-
dl_url(WINTUN_DOWNLOAD_LINK, Some(WINTUN_ZIP)).await?;
401+
const WINTUN_DLL: &str = "wintun.dll";
402+
if std::fs::exists(WINTUN_DLL).unwrap_or(false) {
403+
info!("wintun.dll already exists!");
404+
return Ok(());
405+
}
406+
407+
if !std::fs::exists(WINTUN_ZIP).unwrap_or(false) {
408+
dl_url(WINTUN_DOWNLOAD_LINK, Some(WINTUN_ZIP)).await?;
409+
}
410+
411+
let data = std::fs::read(WINTUN_ZIP)?;
412+
413+
use anyhow::Context;
414+
use rucimp::zip::ZipArchive;
415+
use std::io::Cursor;
416+
use std::io::Read;
417+
use std::io::Write;
418+
let cursor = Cursor::new(data);
419+
let mut archive = ZipArchive::new(cursor).context("Failed to open ZIP archive")?;
420+
use std::fs::File;
421+
422+
let arch_str = match std::env::consts::ARCH {
423+
"aarch64" => "arm64",
424+
"arm" => "arm",
425+
"x86" => "x86",
426+
"x86_64" => "amd64",
427+
_ => std::env::consts::ARCH,
428+
};
429+
430+
let mut file = archive
431+
.by_name(&format!("wintun/bin/{arch_str}/wintun.dll"))
432+
.context("Failed to find the file in the archive")?;
433+
434+
let mut out_file = File::create(WINTUN_DLL).context("Failed to create output file")?;
435+
let mut buffer = Vec::new();
436+
file.read_to_end(&mut buffer)
437+
.context("Failed to read file from archive")?;
438+
439+
out_file
440+
.write_all(&buffer)
441+
.context("Failed to write to output file")?;
442+
443+
info!("wintun.dll extracted successfully!");
444+
402445
Ok(())
403446
}
404447

rucimp/src/api/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ pub async fn app_working_dir() -> String {
430430
format!("{r:?}")
431431
}
432432

433+
#[cfg(feature = "file_server")]
433434
fn serve_folder_by_tar_data_source_base64(
434435
mut app: Router,
435436
file_server_tar_data_source_base64: String,

rucimp/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub mod api;
1919
pub use base64;
2020
pub use serde_json;
2121
pub use strum;
22+
pub use zip;
2223

2324
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
2425

0 commit comments

Comments
 (0)