From af5e0b1f0f190174e92de6fc641ca29a458df8d2 Mon Sep 17 00:00:00 2001 From: shichao Date: Tue, 10 Dec 2024 19:17:35 +0000 Subject: [PATCH] [cipd] support proxy for .cipd_impl.ps1 Until in PowerShell 7.0, Invoke-WebRequest supports proxy configuration defined by environment variables. see: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-7.4 Change-Id: Id7d8d09163c2397b50c1684451827aca2a802317 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5919901 Reviewed-by: Scott Lee Commit-Queue: Scott Lee --- .cipd_impl.ps1 | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/.cipd_impl.ps1 b/.cipd_impl.ps1 index 511ed84d1..a8bd40182 100644 --- a/.cipd_impl.ps1 +++ b/.cipd_impl.ps1 @@ -102,6 +102,31 @@ function Retry-Command { } } +# Check is url in the NO_PROXY of Environment? +function Is-UrlInNoProxy { + [CmdletBinding()] + param ( + [Parameter(Mandatory)] + [string]$Url + ) + $NoProxy = $Env:NO_PROXY + if ([string]::IsNullOrEmpty($NoProxy)) { + return $false + } + $NoProxyList = $NoProxy -split ',' | ForEach-Object { $_.Trim().ToLower() } + $UriHost = ([uri]$Url).Host.ToLower() + foreach ($entry in $NoProxyList) { + if ($entry.StartsWith('.')) { + if ($UriHost.EndsWith($entry.Substring(1))) { + return $true + } + } elseif ($entry -eq $UriHost) { + return $true + } + } + return $false +} + $ExpectedSHA256 = Get-Expected-SHA256 $Platform $Version = (Get-Content $VersionFile).Trim() $URL = "$BackendURL/client?platform=$Platform&version=$Version" @@ -110,9 +135,31 @@ $URL = "$BackendURL/client?platform=$Platform&version=$Version" $TmpPath = $CipdBinary + ".tmp." + $PID try { Write-Output "Downloading CIPD client for $Platform from $URL..." + + $Parameters = @{ + UserAgent = $UserAgent + Uri = $URL + OutFile = $TmpPath + } + + if ($Env:HTTPS_PROXY) { + $Proxy = $Env:HTTPS_PROXY + } elseif ($Env:HTTP_PROXY) { + $Proxy = $Env:HTTP_PROXY + } elseif ($Env:ALL_PROXY) { + $Proxy = $Env:ALL_PROXY + } else { + $Proxy = $null + } + $UrlNotInNoProxy = -not (Is-UrlInNoProxy -Url $URL) + if ($UrlNotInNoProxy -and $Proxy) { + Write-Output "Using Proxy $Proxy..." + $Parameters.Proxy = $Proxy + } + Retry-Command { $ProgressPreference = "SilentlyContinue" - Invoke-WebRequest -UserAgent $UserAgent -Uri $URL -OutFile $TmpPath + Invoke-WebRequest @Parameters } $ActualSHA256 = (Get-FileHash -Path $TmpPath -Algorithm "SHA256").Hash.toLower()