<# .SYNOPSIS Installs Octelium binaries for Windows. .EXAMPLE .\install.ps1 .\install.ps1 -Version "v1.2.3" .\install.ps1 -InstallDir "C:\MyCustomPath\bin" .\install.ps1 -UsePackageManager false #> [CmdletBinding()] param( [string]$Version = "", [string]$InstallDir = "$env:USERPROFILE\.local\bin", [ValidateSet("auto", "true", "false")] [string]$UsePackageManager = $(if ($env:USE_PACKAGE_MANAGER) { $env:USE_PACKAGE_MANAGER } else { "auto" }), [int]$MaxRetries = 3 ) $ErrorActionPreference = 'Stop' [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $DownloadRepo = "https://github.com/octelium/octelium/releases" $LatestReleaseUrl = "https://api.github.com/repos/octelium/octelium/releases/latest" $GitHubApiVersion = "2026-03-10" $Binaries = @("octelium", "octeliumctl", "octops") $Libraries = @("wireguard.dll", "wintun.dll") $ChocolateyPackage = "octelium" $ChocolateySuccess = @(0, 2, 1641, 3010) $InstallDirSet = [bool]($PSBoundParameters.Keys -contains 'InstallDir') if (-not [Environment]::Is64BitOperatingSystem) { throw "ERROR: 32-bit architectures are not supported." } $Arch = switch ($env:PROCESSOR_ARCHITECTURE) { "ARM64" { "arm64" } "AMD64" { "amd64" } default { throw "ERROR: Unsupported architecture: $($env:PROCESSOR_ARCHITECTURE)" } } function Invoke-Download { param([string]$Uri, [string]$OutFile) for ($i = 1; $i -le $MaxRetries; $i++) { try { Invoke-WebRequest -Uri $Uri -OutFile $OutFile -UseBasicParsing -ErrorAction Stop return } catch { if ($i -eq $MaxRetries) { throw "Failed to download $Uri after $MaxRetries attempts: $_" } Write-Warning " Attempt $i failed, retrying..." Start-Sleep -Seconds (2 * $i) } } } function Test-Administrator { $Principal = [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent() return $Principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) } function Test-ChocolateyPackage { return [bool]($env:ChocolateyInstall -and (Test-Path (Join-Path $env:ChocolateyInstall "lib\$ChocolateyPackage"))) } function Get-PackageManagerSkipReason { if ($UsePackageManager -eq "false") { return "UsePackageManager is false" } if ($Version) { return "a version was requested" } if ($InstallDirSet) { return "InstallDir is set" } if (-not (Get-Command choco -ErrorAction SilentlyContinue)) { return "Chocolatey is not installed" } if (-not (Test-Administrator)) { if (Test-ChocolateyPackage) { throw "ERROR: Octelium is installed with Chocolatey. Update it from an elevated PowerShell with: choco upgrade $ChocolateyPackage" } return "Chocolatey requires an elevated PowerShell" } return "" } function Update-SessionPath { foreach ($Target in @([EnvironmentVariableTarget]::Machine, [EnvironmentVariableTarget]::User)) { $Value = [Environment]::GetEnvironmentVariable('Path', $Target) if (-not $Value) { continue } foreach ($Dir in $Value -split ';') { if ($Dir -and -not (";$env:Path;".ToLower().Contains(";$($Dir.ToLower());"))) { $env:Path += ";$Dir" } } } } function Remove-ReplacedBinaries { foreach ($Name in @($Binaries | ForEach-Object { "$_.exe" }) + $Libraries) { $Path = Join-Path $InstallDir $Name if (-not (Test-Path -LiteralPath $Path)) { continue } Write-Host "Removing $Path, it is replaced by the $ChocolateyPackage package" Remove-Item -LiteralPath $Path -Force -ErrorAction SilentlyContinue if (Test-Path -LiteralPath $Path) { Write-Warning "Could not remove $Path, it may be in use." } } } function Install-WithChocolatey { Write-Host "Installing $ChocolateyPackage with Chocolatey" & choco upgrade $ChocolateyPackage -y --no-progress | Out-Host if ($ChocolateySuccess -notcontains $LASTEXITCODE) { return $false } Update-SessionPath Remove-ReplacedBinaries return $true } function Install-Binaries { if ([string]::IsNullOrWhiteSpace($Version)) { try { $Release = Invoke-RestMethod ` -Uri $LatestReleaseUrl ` -Headers @{ "Accept" = "application/vnd.github+json" "X-GitHub-Api-Version" = $GitHubApiVersion } ` -UseBasicParsing ` -ErrorAction Stop $Version = ([string]$Release.tag_name).Trim() } catch { throw "ERROR: Failed to fetch the latest release tag from GitHub: $_" } if ([string]::IsNullOrWhiteSpace($Version)) { throw "ERROR: GitHub returned an empty release tag." } } else { Write-Host "Using requested version: $Version" } if ($Version -notmatch '^v\d+\.\d+\.\d+') { throw "ERROR: Version '$Version' does not look valid (expected vX.Y.Z)." } $DownloadUrlPrefix = "$DownloadRepo/download/$Version" $TmpDir = Join-Path ([IO.Path]::GetTempPath()) "octelium-install-$([guid]::NewGuid().ToString().Substring(0,8))" New-Item -ItemType Directory -Path $TmpDir -Force | Out-Null try { $ChecksumsFile = Join-Path $TmpDir "SHA256SUMS" $HasChecksums = $false try { Invoke-Download -Uri "$DownloadUrlPrefix/SHA256SUMS" -OutFile $ChecksumsFile $HasChecksums = $true } catch { Write-Warning "Could not download checksums file — skipping verification." } if (-not (Test-Path $InstallDir)) { New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null } foreach ($Bin in $Binaries) { $FileName = "$Bin-windows-$Arch.zip" $FilePath = Join-Path $TmpDir $FileName Write-Host "Downloading $Bin..." Invoke-Download -Uri "$DownloadUrlPrefix/$FileName" -OutFile $FilePath Unblock-File -Path $FilePath -ErrorAction SilentlyContinue if ($HasChecksums) { $ChecksumLine = Get-Content $ChecksumsFile | Where-Object { $_ -match [regex]::Escape($FileName) } | Select-Object -First 1 if ($ChecksumLine) { $ExpectedHash = ($ChecksumLine -split '\s+')[0].Trim().ToUpper() $ActualHash = (Get-FileHash -Path $FilePath -Algorithm SHA256).Hash.ToUpper() if ($ExpectedHash -ne $ActualHash) { throw "ERROR: Checksum mismatch for $FileName!`n Expected: $ExpectedHash`n Got: $ActualHash" } } else { Write-Warning " No checksum entry found for $FileName — skipping." } } $ExtractDir = Join-Path $TmpDir "$Bin-extracted" Expand-Archive -Path $FilePath -DestinationPath $ExtractDir -Force $ExtractedFiles = Get-ChildItem -Path $ExtractDir -Include "*.exe", "*.dll" -Recurse -File -ErrorAction SilentlyContinue if (-not $ExtractedFiles) { throw "ERROR: No executables or DLLs found inside $FileName." } foreach ($File in $ExtractedFiles) { $TargetDest = Join-Path $InstallDir $File.Name Move-Item -Path $File.FullName -Destination $TargetDest -Force } $TargetExe = Join-Path $InstallDir "$Bin.exe" if (-not (Test-Path $TargetExe)) { throw "ERROR: '$Bin.exe' not found inside $FileName." } $TmpCompletion = Join-Path $TmpDir "_$Bin.ps1" try { $output = & $TargetExe completion powershell 2>&1 if ($LASTEXITCODE -eq 0 -and $output) { $output | Out-File -FilePath $TmpCompletion -Encoding UTF8 -Force Move-Item -Path $TmpCompletion -Destination (Join-Path $InstallDir "_$Bin.ps1") -Force } else { Write-Warning " Completion generation returned no output for $Bin." } } catch { Write-Warning " Could not generate completions for $Bin : $_" } } $User = [EnvironmentVariableTarget]::User $CurrentPath = [Environment]::GetEnvironmentVariable('Path', $User) if ($null -eq $CurrentPath) { $CurrentPath = "" } if (-not (";$CurrentPath;".ToLower() -like "*;$($InstallDir.ToLower());*")) { [Environment]::SetEnvironmentVariable('Path', "$CurrentPath;$InstallDir", $User) $env:Path += ";$InstallDir" } Write-Host "Octelium $Version installed to $InstallDir" } finally { Remove-Item -Path $TmpDir -Recurse -Force -ErrorAction SilentlyContinue } } $Method = "binary" $Reason = Get-PackageManagerSkipReason if (-not $Reason) { if (Install-WithChocolatey) { $Method = "package" } elseif ($UsePackageManager -eq "true") { throw "ERROR: Installation with Chocolatey failed." } else { Write-Warning "Installation with Chocolatey failed, installing the binaries from GitHub instead." } } elseif ($UsePackageManager -eq "true") { throw "ERROR: Cannot install with Chocolatey: $Reason." } else { Write-Host "Installing the binaries from GitHub since $Reason." } if ($Method -eq "package") { Write-Host "Octelium has been successfully installed with Chocolatey" Write-Host " Updates are installed by Chocolatey: choco upgrade $ChocolateyPackage" } else { Install-Binaries } Write-Host " Docs: https://octelium.com/docs" Write-Host " Repo: https://github.com/octelium/octelium"