Skip to content

Commit 098343e

Browse files
committed
Workaround for proxy support in Windows 7
Windows7 supports proxy configuration through Autounattend.xml Unattended install sets everything needed to have a system-wide proxy configuration. The problem *seems* to be that it fails to notify wininet subsystem (sometimes or always, it depends, I don't really know why/how exactly) Anyways, this patch allow chocolatey to work reliably behind a proxy
1 parent f086475 commit 098343e

7 files changed

Lines changed: 78 additions & 13 deletions

File tree

malboxes/installconfig/windows7/Autounattend.xml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
</settings>
7373
<settings pass="oobeSystem">
7474
{% if proxy %}
75-
<component name="Microsoft-Windows-IE-ClientNetworkProtocolImplementation" processorArchitecture="x86" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State">
75+
<component name="Microsoft-Windows-IE-ClientNetworkProtocolImplementation" processorArchitecture="x86" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
7676
<POLICYProxySettingsPerUser>0</POLICYProxySettingsPerUser>
7777
<HKLMProxyEnable>true</HKLMProxyEnable>
7878
<HKLMProxyServer>{{ proxy }}</HKLMProxyServer>
@@ -130,9 +130,17 @@
130130
<CommandLine>cmd.exe /c wmic useraccount where &quot;name=&apos;{{ username }}&apos;&quot; set PasswordExpires=FALSE</CommandLine>
131131
<Description>Disable password expiration for user {{ username }}</Description>
132132
<RequiresUserInput>true</RequiresUserInput>
133-
</SynchronousCommand>
133+
</SynchronousCommand>
134+
{% if proxy %}
135+
<SynchronousCommand wcm:action="add">
136+
<Order>4</Order>
137+
<CommandLine>cmd.exe /c powershell -File &quot;A:\refresh-proxy.ps1&quot;</CommandLine>
138+
<Description>Refresh Proxy Settings</Description>
139+
<RequiresUserInput>true</RequiresUserInput>
140+
</SynchronousCommand>
141+
{% endif %}
134142
<SynchronousCommand wcm:action="add">
135-
<Order>4</Order>
143+
<Order>5</Order>
136144
<CommandLine>cmd.exe /c powershell -File &quot;A:\enablewinrm.ps1&quot;</CommandLine>
137145
<Description>Enable WinRM for Packer/Vagrant communicator</Description>
138146
<RequiresUserInput>true</RequiresUserInput>

malboxes/installconfig/windows7_64/Autounattend.xml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
</settings>
7373
<settings pass="oobeSystem">
7474
{% if proxy %}
75-
<component name="Microsoft-Windows-IE-ClientNetworkProtocolImplementation" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State">
75+
<component name="Microsoft-Windows-IE-ClientNetworkProtocolImplementation" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
7676
<POLICYProxySettingsPerUser>0</POLICYProxySettingsPerUser>
7777
<HKLMProxyEnable>true</HKLMProxyEnable>
7878
<HKLMProxyServer>{{ proxy }}</HKLMProxyServer>
@@ -131,8 +131,16 @@
131131
<Description>Disable password expiration for user {{ username }}</Description>
132132
<RequiresUserInput>true</RequiresUserInput>
133133
</SynchronousCommand>
134+
{% if proxy %}
135+
<SynchronousCommand wcm:action="add">
136+
<Order>4</Order>
137+
<CommandLine>cmd.exe /c powershell -File &quot;A:\refresh-proxy.ps1&quot;</CommandLine>
138+
<Description>Refresh Proxy Settings</Description>
139+
<RequiresUserInput>true</RequiresUserInput>
140+
</SynchronousCommand>
141+
{% endif %}
134142
<SynchronousCommand wcm:action="add">
135-
<Order>4</Order>
143+
<Order>5</Order>
136144
<CommandLine>cmd.exe /c powershell -File &quot;A:\enablewinrm.ps1&quot;</CommandLine>
137145
<Description>Enable WinRM for Packer/Vagrant communicator</Description>
138146
<RequiresUserInput>true</RequiresUserInput>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Windows...
2+
#
3+
# Here is a note to someone wondering what we're doing _here_
4+
# (And why we're doing it here and now)
5+
#
6+
# Windows7 supports proxy configuration through Autounattend.xml
7+
# Unattended install sets everything needed to have a system-wide proxy
8+
# configuration.
9+
#
10+
# The problem seems to be that it fails to notify wininet subsystems of the
11+
# configuration. Which can prevent us from accessing the Internet.
12+
# (sometimes or always, it depends, I don't really know)
13+
#
14+
# This piece of code can't be run through WinRM for some reason. Running it
15+
# through WinRM will result in Windows removing `ProxyEnable` and `ProxyServer`
16+
# keys from the registry <--- O_o
17+
#
18+
# Anyway, in order to MakeItWork(tm) we need to run this interactively.
19+
#
20+
# These dark spells of black witchcraftery should be enough to allow
21+
# Windows/IE/Wininet/Whatever to get the right memo and use the configured
22+
# proxy
23+
#
24+
25+
echo "Proxy is: $([Net.GlobalProxySelection]::Select.Address.Host)"
26+
27+
$signature = @'
28+
[DllImport("wininet.dll", SetLastError = true, CharSet=CharSet.Auto)]
29+
public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
30+
'@
31+
$wininet = Add-Type -MemberDefinition $signature -Name wininet -Namespace pinvoke -PassThru
32+
33+
function Refresh-System
34+
{
35+
$INTERNET_OPTION_SETTINGS_CHANGED = 39
36+
$INTERNET_OPTION_REFRESH = 37
37+
$INTERNET_OPTION_PROXY_SETTINGS_CHANGED = 95
38+
$a = $wininet::InternetSetOption(0, $INTERNET_OPTION_SETTINGS_CHANGED, 0, 0)
39+
$b = $wininet::InternetSetOption(0, $INTERNET_OPTION_PROXY_SETTINGS_CHANGED, 0, 0)
40+
$c = $wininet::InternetSetOption(0, $INTERNET_OPTION_REFRESH, 0, 0)
41+
}
42+
43+
function Test-Connection
44+
{
45+
$ie = New-Object -comobject InternetExplorer.Application;
46+
$ie.visible=$False;
47+
$ie.navigate('http://google.com');
48+
start-sleep -s 5;
49+
$ie.quit();
50+
}
51+
52+
Refresh-System
53+
Test-Connection
54+
55+
echo "Proxy is: $([Net.GlobalProxySelection]::Select.Address.Host)"

malboxes/templates/snippets/provision_powershell.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
{
1616
"type": "windows-shell",
1717
"inline": [
18-
{% if proxy %}
19-
{# Sometimes, choco decide to ignore the proxy... #}
20-
"choco config set proxy {{ proxy }}",
21-
{% endif %}
2218
"choco install npcap --package-parameters '/winpcap_mode=yes' -y",
2319
"choco install {{ choco_packages }} -y"
2420
]

malboxes/templates/snippets/provision_powershell_win7.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@
3434
{
3535
"type": "windows-shell",
3636
"inline": [
37-
{% if proxy %}
38-
{# Sometimes, choco decide to ignore the proxy... #}
39-
"choco config set proxy {{ proxy }}",
40-
{% endif %}
4137
"choco install npcap --package-parameters '/winpcap_mode=yes' -y",
4238
"choco install {{ choco_packages }} -y"
4339
]

malboxes/templates/win7_32_analyst.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
"floppy_files": [
2020
"{{ cache_dir }}/Autounattend.xml",
21+
{% if proxy %}"{{ dir }}/scripts/windows/refresh-proxy.ps1",{% endif %}
2122
"{{ dir }}/installconfig/windows7/enablewinrm.ps1"
2223
]
2324
}],

malboxes/templates/win7_64_analyst.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
"floppy_files": [
2020
"{{ cache_dir }}/Autounattend.xml",
21+
{% if proxy %}"{{ dir }}/scripts/windows/refresh-proxy.ps1",{% endif %}
2122
"{{ dir }}/installconfig/windows7_64/enablewinrm.ps1"
2223
]
2324
}],

0 commit comments

Comments
 (0)