Fix: Mitigate Invoke-WebRequest hangs via UseBasicParsing, and implement aggressive child/parent directory hierarchy scans for zero-friction binary discoveries.

This commit is contained in:
ospab 2026-05-15 00:57:54 +03:00
parent fb32ca29de
commit a118e45cf1
1 changed files with 44 additions and 29 deletions

View File

@ -1,18 +1,32 @@
$ErrorActionPreference = "Stop" $ErrorActionPreference = "Stop"
$repo = "ospab/ostp" $repo = "ospab/ostp"
# 1. Smart Installation Path Resolution # 1. Smart & Aggressive Installation Path Resolution
$InstallDir = "C:\opt\ostp" # Standard default fallback $InstallDir = "C:\opt\ostp" # Standard default fallback
if (Test-Path "config.json") { if (Test-Path "config.json") {
# If current directory already has a configuration, keep it here # Config is in active current directory
$InstallDir = (Get-Item .).FullName $InstallDir = (Get-Item .).FullName
} elseif (Test-Path "ostp.exe") { } elseif (Test-Path "ostp.exe") {
# If binary already exists in current working directory, use it # Binary is in active current directory
$InstallDir = (Get-Item .).FullName $InstallDir = (Get-Item .).FullName
} elseif ($cmd = Get-Command "ostp" -ErrorAction SilentlyContinue) { } elseif ($cmd = Get-Command "ostp" -ErrorAction SilentlyContinue) {
# If binary is already in the system PATH, find where it lives and update it there # Binary is registered in system PATH
$InstallDir = Split-Path $cmd.Path $InstallDir = Split-Path $cmd.Path
} else {
# Aggressive search in current directory tree (excluding compiler target and Git directories)
$found = Get-ChildItem -Filter "ostp.exe" -Recurse -File -ErrorAction SilentlyContinue |
Where-Object { $_.FullName -notlike "*\target\*" -and $_.FullName -notlike "*\.git\*" } |
Select-Object -First 1
if ($found) {
$InstallDir = Split-Path $found.FullName
} else {
# Scan parent directory as fallback
$parentFound = Get-ChildItem -Path .. -Filter "ostp.exe" -File -ErrorAction SilentlyContinue | Select-Object -First 1
if ($parentFound) {
$InstallDir = Split-Path $parentFound.FullName
}
}
} }
Write-Host "========================================================" Write-Host "========================================================"
@ -20,14 +34,14 @@ Write-Host " Installing Ospab Stealth Transport Protocol (OSTP)"
Write-Host "========================================================" Write-Host "========================================================"
Write-Host "Target deployment location: $InstallDir" Write-Host "Target deployment location: $InstallDir"
# 2. Check Write Access & Verify Elevation # 2. Check Write Access & Elevation Status
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) $isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not (Test-Path $InstallDir)) { if (-not (Test-Path $InstallDir)) {
try { try {
New-Item -ItemType Directory -Path $InstallDir -ErrorAction Stop | Out-Null New-Item -ItemType Directory -Path $InstallDir -ErrorAction Stop | Out-Null
} catch { } catch {
Write-Error "Access Denied: Cannot create target directory '$InstallDir'. Run as Administrator to proceed." Write-Error "Access Denied: Cannot create target directory '$InstallDir'. Run as Administrator."
exit 1 exit 1
} }
} else { } else {
@ -36,25 +50,26 @@ if (-not (Test-Path $InstallDir)) {
"test" | Set-Content $testFile -ErrorAction Stop "test" | Set-Content $testFile -ErrorAction Stop
Remove-Item $testFile -Force Remove-Item $testFile -Force
} catch { } catch {
Write-Error "Access Denied: Target directory '$InstallDir' is write-protected. Run as Administrator to proceed." Write-Error "Access Denied: Directory '$InstallDir' is read-only. Run as Administrator to update."
exit 1 exit 1
} }
} }
# 3. Detect System Architecture # 3. Detect Architecture
$arch = "amd64" $arch = "amd64"
if ([System.Environment]::Is64BitOperatingSystem -and ($Env:PROCESSOR_ARCHITECTURE -eq "ARM64" -or $Env:PROCESSOR_ARCHITEW6432 -eq "ARM64")) { if ([System.Environment]::Is64BitOperatingSystem -and ($Env:PROCESSOR_ARCHITECTURE -eq "ARM64" -or $Env:PROCESSOR_ARCHITEW6432 -eq "ARM64")) {
$arch = "arm64" $arch = "arm64"
} }
# 4. Fetch Stable Version Asset # 4. Fetch Stable Version Asset (with -UseBasicParsing to prevent Internet Explorer hangs)
Write-Host "Fetching the latest stable version from the repository..." Write-Host "Fetching latest stable version from the repository..."
try { try {
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$api = Invoke-RestMethod -Uri "https://api.github.com/repos/$repo/releases/latest" # Explicitly set -UseBasicParsing to guarantee execution doesn't hang on headless servers
$api = Invoke-RestMethod -Uri "https://api.github.com/repos/$repo/releases/latest" -UseBasicParsing
$tag = $api.tag_name $tag = $api.tag_name
} catch { } catch {
Write-Host "[Notice] Failed to automatically retrieve latest version tag." Write-Host "[Notice] Failed to retrieve tag automatically."
$tag = Read-Host "Enter release version tag manually (e.g., v0.1.23)" $tag = Read-Host "Enter release version tag manually (e.g., v0.1.23)"
if (-not $tag) { exit 1 } if (-not $tag) { exit 1 }
} }
@ -65,35 +80,35 @@ $zipPath = Join-Path $env:TEMP "ostp_temp_$($PID).zip"
$extractPath = Join-Path $env:TEMP "ostp_extract_$($PID)" $extractPath = Join-Path $env:TEMP "ostp_extract_$($PID)"
Write-Host "Downloading asset windows-${arch}: $url ..." Write-Host "Downloading asset windows-${arch}: $url ..."
Invoke-WebRequest -Uri $url -OutFile $zipPath # Explicitly set -UseBasicParsing to prevent any engine initialization stalls
Invoke-WebRequest -Uri $url -OutFile $zipPath -UseBasicParsing
if (-not (Test-Path $zipPath)) { if (-not (Test-Path $zipPath)) {
Write-Error "Failed to download zip archive." Write-Error "Failed to download zip package."
exit 1 exit 1
} }
# Overwrite logic with active process suspension # Overwrite and clear file locks
if (Test-Path $extractPath) { Remove-Item $extractPath -Recurse -Force } if (Test-Path $extractPath) { Remove-Item $extractPath -Recurse -Force }
Expand-Archive -Path $zipPath -DestinationPath $extractPath -Force Expand-Archive -Path $zipPath -DestinationPath $extractPath -Force
$exeFile = Get-ChildItem -Path $extractPath -Filter "*.exe" -Recurse | Select-Object -First 1 $exeFile = Get-ChildItem -Path $extractPath -Filter "*.exe" -Recurse | Select-Object -First 1
if ($exeFile) { if ($exeFile) {
Write-Host "Stopping any running instances of ostp to unlock binary..." Write-Host "Stopping any active instances of ostp to unlock binary target..."
Stop-Process -Name "ostp" -ErrorAction SilentlyContinue Stop-Process -Name "ostp" -ErrorAction SilentlyContinue
Start-Sleep -Seconds 1 Start-Sleep -Seconds 1
Copy-Item -Path $exeFile.FullName -Destination (Join-Path $InstallDir "ostp.exe") -Force Copy-Item -Path $exeFile.FullName -Destination (Join-Path $InstallDir "ostp.exe") -Force
Write-Host "Executable successfully deployed to $(Join-Path $InstallDir 'ostp.exe')." Write-Host "Executable successfully deployed to $(Join-Path $InstallDir 'ostp.exe')."
} else { } else {
Write-Error "Executable ostp.exe not found in package." Write-Error "Binary file ostp.exe not found in archive package."
exit 1 exit 1
} }
# Cleanup temp cache
Remove-Item $zipPath -Force Remove-Item $zipPath -Force
Remove-Item $extractPath -Recurse -Force Remove-Item $extractPath -Recurse -Force
# 5. Zero-Interactive Smart Updater # 5. Smart Auto-Updater Mode
$configPath = Join-Path $InstallDir "config.json" $configPath = Join-Path $InstallDir "config.json"
if (Test-Path $configPath) { if (Test-Path $configPath) {
Write-Host "--------------------------------------------------------" Write-Host "--------------------------------------------------------"
@ -104,13 +119,13 @@ if (Test-Path $configPath) {
exit 0 exit 0
} }
# 6. Interactive Matrix Setup Menu # 6. Interactive Setup
Write-Host "--------------------------------------------------------" Write-Host "--------------------------------------------------------"
Write-Host "Select configuration mode:" Write-Host "Select configuration mode:"
Write-Host "1) Configure Server" Write-Host "1) Configure Server"
Write-Host "2) Configure Client" Write-Host "2) Configure Client"
Write-Host "--------------------------------------------------------" Write-Host "--------------------------------------------------------"
$mode = Read-Host "Enter selection choice [1-2]" $mode = Read-Host "Enter choice [1-2]"
Push-Location $InstallDir Push-Location $InstallDir
@ -126,13 +141,13 @@ if ($mode -eq "1") {
if (-not $keyCount) { $keyCount = 1 } if (-not $keyCount) { $keyCount = 1 }
if ([int]$keyCount -gt 1) { if ([int]$keyCount -gt 1) {
Write-Host "Generating additional telemetry keys..." Write-Host "Generating additional security keys..."
$keys = & .\ostp.exe -g -c $keyCount $keys = & .\ostp.exe -g -c $keyCount
$config.access_keys = $keys -split "`r`n" | Where-Object { $_ -ne "" } $config.access_keys = $keys -split "`r`n" | Where-Object { $_ -ne "" }
} }
$config | ConvertTo-Json -Depth 10 | Set-Content "config.json" $config | ConvertTo-Json -Depth 10 | Set-Content "config.json"
Write-Host "Server deployment completed. Config file: $(Join-Path $InstallDir 'config.json')" Write-Host "Server configuration completed. Config written to $(Join-Path $InstallDir 'config.json')"
} elseif ($mode -eq "2") { } elseif ($mode -eq "2") {
Write-Host "Initializing client configuration..." Write-Host "Initializing client configuration..."
@ -153,16 +168,16 @@ if ($mode -eq "1") {
if ($socks) { $config.socks5_bind = $socks } if ($socks) { $config.socks5_bind = $socks }
$config | ConvertTo-Json -Depth 10 | Set-Content "config.json" $config | ConvertTo-Json -Depth 10 | Set-Content "config.json"
Write-Host "Client deployment completed. Config file: $(Join-Path $InstallDir 'config.json')" Write-Host "Client configuration completed. Config written to $(Join-Path $InstallDir 'config.json')"
} else { } else {
Write-Error "Invalid configuration selection." Write-Error "Invalid configuration choice."
Pop-Location Pop-Location
exit 1 exit 1
} }
Pop-Location Pop-Location
# 7. Adaptive Environment PATH Injection # 7. Environment PATH Registration
Write-Host "--------------------------------------------------------" Write-Host "--------------------------------------------------------"
Write-Host "Registering binary route in Environment PATH..." Write-Host "Registering binary route in Environment PATH..."
$targetScope = if ($isAdmin) { [EnvironmentVariableTarget]::Machine } else { [EnvironmentVariableTarget]::User } $targetScope = if ($isAdmin) { [EnvironmentVariableTarget]::Machine } else { [EnvironmentVariableTarget]::User }
@ -172,11 +187,11 @@ if ($sysPath -notlike "*$InstallDir*") {
[Environment]::SetEnvironmentVariable("Path", $newPath, $targetScope) [Environment]::SetEnvironmentVariable("Path", $newPath, $targetScope)
Write-Host "Environment PATH updated successfully ($($targetScope.ToString()) scope)." Write-Host "Environment PATH updated successfully ($($targetScope.ToString()) scope)."
} else { } else {
Write-Host "$InstallDir is already mapped in PATH." Write-Host "$InstallDir is already registered in PATH."
} }
Write-Host "--------------------------------------------------------" Write-Host "--------------------------------------------------------"
Write-Host "Deployment completed successfully!" Write-Host "Deployment completed successfully!"
Write-Host "OSTP binary can now be run globally by typing: ostp" Write-Host "Binary can be executed globally by typing: ostp"
Write-Host "Configuration location: $(Join-Path $InstallDir 'config.json')" Write-Host "Config location: $(Join-Path $InstallDir 'config.json')"
Write-Host "--------------------------------------------------------" Write-Host "--------------------------------------------------------"