From 1ab313b61685a6f487d6bec12a50ec35c3cebf8b Mon Sep 17 00:00:00 2001 From: ospab Date: Fri, 15 May 2026 01:09:15 +0300 Subject: [PATCH] Fix: Overhaul Windows UAC elevation to preserve CWD and CLI arguments, preventing instant crash on relaunch; apply UseBasicParsing to fix background downloader hangs. --- ostp-client/src/runner.rs | 27 +++++++++++++++++---- ostp-client/src/tunnel/wintun_downloader.rs | 4 +-- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/ostp-client/src/runner.rs b/ostp-client/src/runner.rs index a7f2623..d1e3b01 100644 --- a/ostp-client/src/runner.rs +++ b/ostp-client/src/runner.rs @@ -45,12 +45,29 @@ fn is_admin() -> bool { #[cfg(target_os = "windows")] fn relaunch_as_admin() -> Result<()> { let current_exe = std::env::current_exe()?; - let exe_str = current_exe.to_string_lossy(); + let exe_str = current_exe.to_string_lossy().replace('\'', "''"); + + let current_dir = std::env::current_dir()?; + let dir_str = current_dir.to_string_lossy().replace('\'', "''"); + + let mut arg_parts = Vec::new(); + for arg in std::env::args().skip(1) { + arg_parts.push(format!("'{}'", arg.replace('\'', "''"))); + } + + let arg_list_str = if !arg_parts.is_empty() { + format!("-ArgumentList {}", arg_parts.join(",")) + } else { + String::new() + }; + + let ps_script = format!( + "Start-Process -FilePath '{}' {} -WorkingDirectory '{}' -Verb RunAs", + exe_str, arg_list_str, dir_str + ); + let _ = std::process::Command::new("powershell") - .args([ - "-Command", - &format!("Start-Process -FilePath '{}' -Verb RunAs", exe_str), - ]) + .args(["-Command", &ps_script]) .spawn()?; std::process::exit(0); } diff --git a/ostp-client/src/tunnel/wintun_downloader.rs b/ostp-client/src/tunnel/wintun_downloader.rs index 7b7917a..1ed1e96 100644 --- a/ostp-client/src/tunnel/wintun_downloader.rs +++ b/ostp-client/src/tunnel/wintun_downloader.rs @@ -20,7 +20,7 @@ pub fn download_wintun_dll(debug: bool) -> Result<()> { let dll_dest = dll_path.to_string_lossy().replace('\\', "/"); let ps_script = format!( - "Invoke-WebRequest -Uri 'https://www.wintun.net/builds/wintun-0.14.1.zip' -OutFile '{}' -ErrorAction Stop; \ + "Invoke-WebRequest -Uri 'https://www.wintun.net/builds/wintun-0.14.1.zip' -OutFile '{}' -UseBasicParsing -ErrorAction Stop; \ Expand-Archive -Path '{}' -DestinationPath '{}' -Force; \ Get-ChildItem -Path '{}' -Filter 'wintun.dll' -Recurse | Copy-Item -Destination '{}' -Force; \ Remove-Item '{}', '{}' -Recurse -Force", @@ -66,7 +66,7 @@ pub fn download_tun2socks(debug: bool) -> Result<()> { let url = "https://github.com/xjasonlyu/tun2socks/releases/download/v2.5.2/tun2socks-windows-amd64.zip"; let ps_script = format!( - "Invoke-WebRequest -Uri '{}' -OutFile '{}' -ErrorAction Stop; \ + "Invoke-WebRequest -Uri '{}' -OutFile '{}' -UseBasicParsing -ErrorAction Stop; \ Expand-Archive -Path '{}' -DestinationPath '{}' -Force; \ Get-ChildItem -Path '{}' -Filter '*.exe' -Recurse | Copy-Item -Destination '{}' -Force; \ Remove-Item '{}', '{}' -Recurse -Force",