private/New-WebClient.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
function New-WebClient {
    Param (
        [Parameter(Position = 0)]
        [string] $Uri 
    )
    
    $client = new-object System.Net.WebClient
    $ignoreProxy = $Env:ChocolateyIgnoreProxy
    $defaultCreds = [System.Net.CredentialCache]::DefaultCredentials

    if (!$defaultCreds) {
        $client.Credentials = $defaultCreds
    }
    
    
    if (!$ignoreProxy -and $ignoreProxy -eq 'true') {
        Write-Debug "Explicitly bypassing proxy due to user environment variable"
        $client.Proxy = [System.Net.GlobalProxySelection]::GetEmptyWebProxy()
    
    } else {
        # check if a proxy is required
        $explicitProxy = $env:chocolateyProxyLocation
        $explicitProxyUser = $env:chocolateyProxyUser
        $explicitProxyPassword = $env:chocolateyProxyPassword
        
        if (! [string]::IsNullOrWhiteSpace($explicitProxy)) {
            
            # explicit proxy
            $proxy = New-Object System.Net.WebProxy($explicitProxy, $true)
            if (![string]::IsNullOrWhiteSpace($explicitProxyPassword)) {
                $passwd = ConvertTo-SecureString $explicitProxyPassword -AsPlainText -Force
                $proxy.Credentials = New-Object System.Management.Automation.PSCredential ($explicitProxyUser, $passwd)
            }
    
            Write-Debug "Using explicit proxy server '$explicitProxy'."
            $client.Proxy = $proxy
    
        } elseif (!$client.Proxy.IsBypassed($Uri)) {
            
            # system proxy (pass through)
            $creds = $defaultCreds
          
            if (!$creds) {
                
                Write-Debug "Default credentials were null. Attempting backup method"
                $cred = get-credential
                $creds = $cred.GetNetworkCredential();
            }
    
            $proxyaddress = $client.Proxy.GetProxy($Uri).Authority
            Write-Debug "Using system proxy server '$proxyaddress'."
          
            $proxy = New-Object System.Net.WebProxy($proxyaddress)
            $proxy.Credentials = $creds
            $client.Proxy = $proxy
        }
    }
    
    return $client;
}