Skip to content

Commit 1e334ed

Browse files
committed
Also unset PAC script, added settings. Small fixes. Updated nuget packages. Updated to v1.70
1 parent 1cce6af commit 1e334ed

14 files changed

+259
-101
lines changed

ProxyUnsetter/App.config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
<setting name="LastReleaseCheck" serializeAs="String">
3232
<value>1753-01-01</value>
3333
</setting>
34+
<setting name="UnsetPac" serializeAs="String">
35+
<value>True</value>
36+
</setting>
3437
</ProxyUnsetter.Properties.Settings>
3538
</userSettings>
3639
<system.net>

ProxyUnsetter/Helpers/ProxyHelper.cs

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,48 +14,70 @@ namespace ProxyUnsetter.Helpers
1414
internal static class ProxyHelper
1515
{
1616
[DllImport("wininet.dll")]
17-
private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
17+
private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer,
18+
int dwBufferLength);
1819

1920
// ReSharper disable InconsistentNaming
2021
private const int INTERNET_OPTION_SETTINGS_CHANGED = 39;
22+
2123
private const int INTERNET_OPTION_REFRESH = 37;
2224
// ReSharper restore InconsistentNaming
2325

2426
// Computer\HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings
2527
private const string ProxyRegistryKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
2628

2729
public static string ManuallySetProxyServer = "127.0.0.1:8080";
30+
31+
public static ProxyState LastProxyState;
32+
33+
/// <summary>
34+
/// Current proxy server or auto-config url
35+
/// </summary>
2836
public static string CurrentProxyServer = string.Empty;
37+
2938
public static ProxyState GetCurrentProxyState()
3039
{
3140
RegistryKey registry = Registry.CurrentUser.OpenSubKey(ProxyRegistryKey, true);
3241
if (registry == null)
3342
{
34-
MessageBox.Show(@"Couldn't find the registry key to update proxy settings.", @"Error", MessageBoxButtons.OK,
43+
MessageBox.Show(@"Couldn't find the registry key to update proxy settings.", @"Error",
44+
MessageBoxButtons.OK,
3545
MessageBoxIcon.Error);
3646
return ProxyState.Unknown;
3747
}
48+
3849
var proxyEnableValue = registry.GetValue("ProxyEnable");
39-
var proxyServer = (string)registry.GetValue("ProxyServer");
50+
var proxyServer = (string) registry.GetValue("ProxyServer");
51+
var autoConfigUrl = (string) registry.GetValue("AutoConfigURL"); // Proxy auto-config (PAC) url
4052

41-
if (proxyEnableValue != null && (int)proxyEnableValue != 0)
53+
if (proxyEnableValue != null && (int) proxyEnableValue != 0)
4254
{
4355
if (string.IsNullOrWhiteSpace(proxyServer))
4456
{
4557
return ProxyState.Unknown; // weird stuff, proxy enabled, but no proxy address
4658
}
59+
4760
if (proxyServer == ManuallySetProxyServer)
4861
{
4962
CurrentProxyServer = proxyServer;
5063
return ProxyState.ManuallySet;
5164
}
65+
5266
if (IpIsWhiteListed())
5367
{
5468
CurrentProxyServer = proxyServer;
5569
return ProxyState.IgnoredBecauseOfWhitelistedIp;
5670
}
71+
5772
return ProxyState.AutomaticallySet;
5873
}
74+
75+
if (autoConfigUrl != null)
76+
{
77+
CurrentProxyServer = autoConfigUrl;
78+
return ProxyState.AutomaticallySetWithAutoconfigScript;
79+
}
80+
5981
return ProxyState.AutomaticallyUnset;
6082
}
6183

@@ -70,11 +92,19 @@ public static void UnsetProxy()
7092
RegistryKey registry = Registry.CurrentUser.OpenSubKey(ProxyRegistryKey, true);
7193
if (registry == null)
7294
{
73-
MessageBox.Show(@"Couldn't find the registry key to update proxy settings.", @"Error", MessageBoxButtons.OK,
95+
MessageBox.Show(@"Couldn't find the registry key to update proxy settings.", @"Error",
96+
MessageBoxButtons.OK,
7497
MessageBoxIcon.Error);
7598
return;
7699
}
100+
77101
registry.SetValue("ProxyEnable", 0);
102+
103+
if (Settings.Default.UnsetPac)
104+
{
105+
registry.DeleteValue("AutoConfigURL", false);
106+
}
107+
78108
RefreshProxySettings();
79109
}
80110

@@ -83,10 +113,12 @@ public static void SetProxy()
83113
RegistryKey registry = Registry.CurrentUser.OpenSubKey(ProxyRegistryKey, true);
84114
if (registry == null)
85115
{
86-
MessageBox.Show(@"Couldn't find the registry key to update proxy settings.", @"Error", MessageBoxButtons.OK,
116+
MessageBox.Show(@"Couldn't find the registry key to update proxy settings.", @"Error",
117+
MessageBoxButtons.OK,
87118
MessageBoxIcon.Error);
88119
return;
89120
}
121+
90122
registry.SetValue("ProxyEnable", 1);
91123
registry.SetValue("ProxyServer", ManuallySetProxyServer);
92124
RefreshProxySettings();
@@ -98,11 +130,13 @@ public static bool IpIsWhiteListed()
98130
{
99131
return false;
100132
}
133+
101134
var localIpAddress = LocalIpAddress();
102135
if (Equals(localIpAddress, IPAddress.None))
103136
{
104137
return false;
105138
}
139+
106140
foreach (var ipRange in Settings.Default.IpWhitelist)
107141
{
108142
var range = IPAddressRange.Parse(ipRange);
@@ -111,6 +145,7 @@ public static bool IpIsWhiteListed()
111145
return true;
112146
}
113147
}
148+
114149
return false;
115150
}
116151

@@ -130,7 +165,8 @@ public static IPAddress LocalIpAddress()
130165
}
131166
}
132167
}
168+
133169
return IPAddress.None;
134170
}
135171
}
136-
}
172+
}

ProxyUnsetter/Helpers/ProxyState.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ internal enum ProxyState
55
Unknown,
66
AutomaticallyUnset,
77
AutomaticallySet,
8+
AutomaticallySetWithAutoconfigScript,
89
ManuallySet,
9-
IgnoredBecauseOfWhitelistedIp
10+
IgnoredBecauseOfWhitelistedIp
1011
}
1112
}

ProxyUnsetter/Helpers/SettingsHelper.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ internal static class SettingsHelper
99
{
1010
public static event EventHandler SettingsChanged;
1111
public const string AppName = "ProxyUnsetter";
12+
1213
public static bool GetLaunchAtWindowsStartupState()
1314
{
1415
var registryKey = Registry.CurrentUser.OpenSubKey
@@ -20,6 +21,7 @@ public static bool GetLaunchAtWindowsStartupState()
2021
@"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
2122
return false;
2223
}
24+
2325
return registryKey.GetValue(AppName) != null;
2426
}
2527

@@ -44,6 +46,7 @@ public static void ToggleWindowsStartup(bool set)
4446
@"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
4547
return;
4648
}
49+
4750
if (set)
4851
registryKey.SetValue("ProxyUnsetter", Application.ExecutablePath);
4952
else
@@ -62,10 +65,16 @@ public static void ToggleAutomaticProxyUnset(bool set)
6265
Settings.Default.Save();
6366
}
6467

68+
public static void ToggleUnsetPac(bool set)
69+
{
70+
Settings.Default.UnsetPac = set;
71+
Settings.Default.Save();
72+
}
73+
6574
public static string[] GetIpAddressAndNetmaskFromSetting(string setting)
6675
{
6776
var split = setting.Split('/');
68-
return new[] { split[0], split[1] };
77+
return new[] {split[0], split[1]};
6978
}
7079

7180
public static void SetManuallySetProxy(string manualProxy)
@@ -76,4 +85,4 @@ public static void SetManuallySetProxy(string manualProxy)
7685
SettingsChanged?.Invoke(null, EventArgs.Empty);
7786
}
7887
}
79-
}
88+
}

ProxyUnsetter/Program.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@ public static void Main()
2121
Application.Run(new ProxyUnsetterApplicationContext());
2222
}
2323

24-
private static void ApplicationOnThreadException(object sender, ThreadExceptionEventArgs threadExceptionEventArgs)
24+
private static void ApplicationOnThreadException(object sender,
25+
ThreadExceptionEventArgs threadExceptionEventArgs)
2526
{
2627
CrashHelper.ReportCrash(threadExceptionEventArgs.Exception);
2728
}
2829

29-
private static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs)
30+
private static void CurrentDomainOnUnhandledException(object sender,
31+
UnhandledExceptionEventArgs unhandledExceptionEventArgs)
3032
{
31-
CrashHelper.ReportCrash((Exception)unhandledExceptionEventArgs.ExceptionObject);
33+
CrashHelper.ReportCrash((Exception) unhandledExceptionEventArgs.ExceptionObject);
3234
}
3335
}
34-
}
36+
}

ProxyUnsetter/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.60.*")]
35+
[assembly: AssemblyVersion("1.70.*")]

ProxyUnsetter/Properties/Settings.Designer.cs

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ProxyUnsetter/Properties/Settings.settings

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,8 @@
2121
<Setting Name="LastReleaseCheck" Type="System.DateTime" Scope="User">
2222
<Value Profile="(Default)">1753-01-01</Value>
2323
</Setting>
24+
<Setting Name="UnsetPac" Type="System.Boolean" Scope="User">
25+
<Value Profile="(Default)">True</Value>
26+
</Setting>
2427
</Settings>
2528
</SettingsFile>

ProxyUnsetter/ProxyUnsetter.csproj

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="..\packages\ILMerge.3.0.18\build\ILMerge.props" Condition="Exists('..\packages\ILMerge.3.0.18\build\ILMerge.props')" />
34
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
45
<PropertyGroup>
56
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -30,6 +31,8 @@
3031
<UseApplicationTrust>false</UseApplicationTrust>
3132
<PublishWizardCompleted>true</PublishWizardCompleted>
3233
<BootstrapperEnabled>true</BootstrapperEnabled>
34+
<NuGetPackageImportStamp>
35+
</NuGetPackageImportStamp>
3336
</PropertyGroup>
3437
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
3538
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -66,8 +69,8 @@
6669
<SignManifests>false</SignManifests>
6770
</PropertyGroup>
6871
<ItemGroup>
69-
<Reference Include="IPAddressRange, Version=2.1.1.0, Culture=neutral, processorArchitecture=MSIL">
70-
<HintPath>..\packages\IPAddressRange.2.1.1\lib\net45\IPAddressRange.dll</HintPath>
72+
<Reference Include="IPAddressRange, Version=3.2.0.0, Culture=neutral, PublicKeyToken=578e3c3d17e7c751, processorArchitecture=MSIL">
73+
<HintPath>..\packages\IPAddressRange.3.2.0\lib\net45\IPAddressRange.dll</HintPath>
7174
</Reference>
7275
<Reference Include="System" />
7376
<Reference Include="System.configuration" />
@@ -187,6 +190,12 @@
187190
</ItemGroup>
188191
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
189192
<PropertyGroup>
190-
<PostBuildEvent>"C:\Program Files (x86)\Microsoft\ILMerge\ILMerge.exe" /target:winexe /targetplatform:"v4,C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1" /out:ProxyUnsetter.exe ProxyUnsetterMain.exe IPAddressRange.dll</PostBuildEvent>
193+
<PostBuildEvent>$(ILMergeConsolePath) /target:winexe /targetplatform:"v4,C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1" /out:ProxyUnsetter.exe ProxyUnsetterMain.exe IPAddressRange.dll</PostBuildEvent>
191194
</PropertyGroup>
195+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
196+
<PropertyGroup>
197+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
198+
</PropertyGroup>
199+
<Error Condition="!Exists('..\packages\ILMerge.3.0.18\build\ILMerge.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\ILMerge.3.0.18\build\ILMerge.props'))" />
200+
</Target>
192201
</Project>

0 commit comments

Comments
 (0)