Skip to content

Commit

Permalink
(chocolateyGH-603) Get-FileName should set proxy if required
Browse files Browse the repository at this point in the history
As the other two web calls check/use proxy and network credentials,
Get-FileName should also use a proxy if required.
  • Loading branch information
ferventcoder committed Feb 4, 2016
1 parent 08909a6 commit 6dffaff
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/chocolatey.resources/helpers/functions/Get-FileName.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,47 @@ param(
return $originalFileName
}

$defaultCreds = [System.Net.CredentialCache]::DefaultCredentials
if ($defaultCreds -ne $null) {
$request.Credentials = $defaultCreds
}

$client = New-Object System.Net.WebClient
if ($defaultCreds -ne $null) {
$client.Credentials = $defaultCreds
}

# check if a proxy is required
$explicitProxy = $env:chocolateyProxyLocation
$explicitProxyUser = $env:chocolateyProxyUser
$explicitProxyPassword = $env:chocolateyProxyPassword
if ($explicitProxy -ne $null) {
# explicit proxy
$proxy = New-Object System.Net.WebProxy($explicitProxy, $true)
if ($explicitProxyPassword -ne $null) {
$passwd = ConvertTo-SecureString $explicitProxyPassword -AsPlainText -Force
$proxy.Credentials = New-Object System.Management.Automation.PSCredential ($explicitProxyUser, $passwd)
}

Write-Debug "Using explicit proxy server '$explicitProxy'."
$request.Proxy = $proxy

} elseif (!$client.Proxy.IsBypassed($url))
{
# system proxy (pass through)
$creds = [Net.CredentialCache]::DefaultCredentials
if ($creds -eq $null) {
Write-Debug "Default credentials were null. Attempting backup method"
$cred = Get-Credential
$creds = $cred.GetNetworkCredential();
}
$proxyAddress = $client.Proxy.GetProxy($url).Authority
Write-Debug "Using system proxy server '$proxyaddress'."
$proxy = New-Object System.Net.WebProxy($proxyAddress)
$proxy.Credentials = $creds
$request.Proxy = $proxy
}

$request.Method = "GET"
$request.Accept = '*/*'
$request.AllowAutoRedirect = $true
Expand Down

0 comments on commit 6dffaff

Please sign in to comment.