@@ -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
399399async 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
0 commit comments