こんにちは。
Windows の .NET 系のSystem.Net.WebClient
で、下記の認証proxyの環境変数設定を使うようにするために1 2、PowerShell用に Get-NewWebClient
を作ってみました。
>> Get-Content .\Get-NewWebClient.ps1
function Get-NewWebClient {
param()
$wc = new-object System.Net.WebClient
$proxyaddress = [Environment]::GetEnvironmentVariable('HTTP_PROXY') -replace 'http://', ''
if ($proxyaddress -nq $null) {
$p = $proxyaddress.Split("@")
$proxy = new-object System.Net.WebProxy($p[$p.Length-1])
if ($p.Length -eq 2) {
$creds = $p[0].Split(":")
$proxy.credentials = new-object System.Net.NetworkCredential($creds[0],$creds[1])
}
$wc.proxy = $proxy
}
$wc
}
>> $env:HTTP_PROXY
http://<USERNAME>:<PASSWORD>@<HOSTNAME>:<PORTNUMBER>
###Chocolatey
これを使って、Chocolatey をインストールする例は、
>> Set-ExecutionPolicy RemoteSigned
>> set HTTP_PROXY=http://<USERNAME>:<PASSWORD>@<HOSTNAME>:<PORTNUMBER>
>> .\Get-NewWebClient
>> (Get-NewWebClient).DownloadString('https://chocolatey.org/install.ps1') > install.ps1
>> set ChocolateyInstall=C:\ProgramData\chocolatey
>> .\install.ps1
>> clist -lo
chocolatey 0.9.10.3
この install.ps1 内でも Get-NewWebClient
を使うように修正。
$ diff -u install.ps1.orig install.ps1
--- install.ps1.orig
+++ install.ps1
@@ -75,47 +75,10 @@
function Get-Downloader {
param (
[string]$url
)
- $downloader = new-object System.Net.WebClient
-
- $defaultCreds = [System.Net.CredentialCache]::DefaultCredentials
- if ($defaultCreds -ne $null) {
- $downloader.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'."
- $downloader.Proxy = $proxy
-
- } elseif (!$downloader.Proxy.IsBypassed($url))
- {
- # system proxy (pass through)
- $creds = $defaultCreds
- if ($creds -eq $null) {
- Write-Debug "Default credentials were null. Attempting backup method"
- $cred = get-credential
- $creds = $cred.GetNetworkCredential();
- }
-
- $proxyaddress = $downloader.Proxy.GetProxy($url).Authority
- Write-Debug "Using system proxy server '$proxyaddress'."
- $proxy = New-Object System.Net.WebProxy($proxyaddress)
- $proxy.Credentials = $creds
- $downloader.Proxy = $proxy
- }
-
+ $downloader = Get-NewWebClient
+
return $downloader
}
なお Chocolatey 0.9.9 以降では、下記の環境変数を読み取るそうです。
>> $env:chocolateyProxyLocation = "<HOSTNAME>:<PORTNUMBER>"
>> $env:chocolateyProxyUser = "<USERNAME>"
>> $env:chocolateyProxyPassword = "<PASSWORD>"
Chocolatey 0.9.9.9 以降では、明示的に設定するそうです。
>> choco config set proxy <HOSTNAME>:<PORTNUMBER>
>> choco config set proxyUser <USERNAME>
>> choco config set proxyPassword <PASSWORD>
-
NuGet (および clist コマンド)はこの環境変数設定を使ってproxyを通ります。 ↩