Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit e312b9b

Browse files
committed
cleanup proxy settings
1 parent 699956e commit e312b9b

File tree

2 files changed

+188
-20
lines changed

2 files changed

+188
-20
lines changed

Titanium.Web.Proxy/Helpers/SystemProxy.cs

+163-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System;
22
using System.Runtime.InteropServices;
33
using Microsoft.Win32;
4+
using System.Text.RegularExpressions;
5+
using System.Collections.Generic;
6+
using System.Linq;
47

58
namespace Titanium.Web.Proxy.Helpers
69
{
@@ -11,55 +14,203 @@ internal static extern bool InternetSetOption(IntPtr hInternet, int dwOption, In
1114
int dwBufferLength);
1215
}
1316

17+
internal class HttpSystemProxyValue
18+
{
19+
public string HostName { get; set; }
20+
public int Port { get; set; }
21+
public bool IsSecure { get; set; }
22+
23+
public override string ToString()
24+
{
25+
if (!IsSecure)
26+
return "http=" + HostName + ":" + Port;
27+
else
28+
return "https=" + HostName + ":" + Port;
29+
}
30+
}
31+
1432
public static class SystemProxyHelper
1533
{
1634
public const int InternetOptionSettingsChanged = 39;
1735
public const int InternetOptionRefresh = 37;
18-
private static object _prevProxyServer;
19-
private static object _prevProxyEnable;
2036

21-
public static void EnableProxyHttp(string hostname, int port)
37+
public static void SetHttpProxy(string hostname, int port)
2238
{
2339
var reg = Registry.CurrentUser.OpenSubKey(
2440
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
2541
if (reg != null)
2642
{
27-
_prevProxyEnable = reg.GetValue("ProxyEnable");
28-
_prevProxyServer = reg.GetValue("ProxyServer");
43+
prepareRegistry(reg);
44+
45+
var exisitingContent = reg.GetValue("ProxyServer") as string;
46+
var existingSystemProxyValues = GetSystemProxyValues(exisitingContent);
47+
existingSystemProxyValues.RemoveAll(x => !x.IsSecure);
48+
existingSystemProxyValues.Add(new HttpSystemProxyValue()
49+
{
50+
HostName = hostname,
51+
IsSecure = false,
52+
Port = port
53+
});
54+
55+
reg.SetValue("ProxyEnable", 1);
56+
reg.SetValue("ProxyServer", String.Join(";", existingSystemProxyValues.Select(x => x.ToString()).ToArray()));
57+
}
58+
59+
Refresh();
60+
}
61+
62+
63+
public static void RemoveHttpProxy()
64+
{
65+
var reg = Registry.CurrentUser.OpenSubKey(
66+
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
67+
if (reg != null)
68+
{
69+
var exisitingContent = reg.GetValue("ProxyServer") as string;
70+
71+
var existingSystemProxyValues = GetSystemProxyValues(exisitingContent);
72+
existingSystemProxyValues.RemoveAll(x => !x.IsSecure);
73+
74+
2975
reg.SetValue("ProxyEnable", 1);
30-
reg.SetValue("ProxyServer", "http=" + hostname + ":" + port + ";");
76+
reg.SetValue("ProxyServer", String.Join(";", existingSystemProxyValues.Select(x => x.ToString()).ToArray()));
3177
}
78+
3279
Refresh();
3380
}
3481

35-
public static void EnableProxyHttps(string hostname, int port)
82+
public static void SetHttpsProxy(string hostname, int port)
3683
{
3784
var reg = Registry.CurrentUser.OpenSubKey(
3885
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
86+
87+
if (reg != null)
88+
{
89+
prepareRegistry(reg);
90+
91+
var exisitingContent = reg.GetValue("ProxyServer") as string;
92+
93+
var existingSystemProxyValues = GetSystemProxyValues(exisitingContent);
94+
existingSystemProxyValues.RemoveAll(x => x.IsSecure);
95+
existingSystemProxyValues.Add(new HttpSystemProxyValue()
96+
{
97+
HostName = hostname,
98+
IsSecure = true,
99+
Port = port
100+
});
101+
102+
reg.SetValue("ProxyEnable", 1);
103+
reg.SetValue("ProxyServer", String.Join(";", existingSystemProxyValues.Select(x => x.ToString()).ToArray()));
104+
}
105+
106+
Refresh();
107+
}
108+
109+
public static void RemoveHttpsProxy()
110+
{
111+
var reg = Registry.CurrentUser.OpenSubKey(
112+
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
39113
if (reg != null)
40114
{
115+
var exisitingContent = reg.GetValue("ProxyServer") as string;
116+
117+
var existingSystemProxyValues = GetSystemProxyValues(exisitingContent);
118+
existingSystemProxyValues.RemoveAll(x => x.IsSecure);
119+
120+
41121
reg.SetValue("ProxyEnable", 1);
42-
reg.SetValue("ProxyServer", "http=" + hostname + ":" + port + ";https=" + hostname + ":" + port);
122+
reg.SetValue("ProxyServer", String.Join(";", existingSystemProxyValues.Select(x => x.ToString()).ToArray()));
43123
}
124+
44125
Refresh();
45126
}
46127

47128
public static void DisableAllProxy()
48129
{
49130
var reg = Registry.CurrentUser.OpenSubKey(
50131
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
132+
51133
if (reg != null)
52134
{
53-
reg.SetValue("ProxyEnable", _prevProxyEnable);
54-
if (_prevProxyServer != null)
55-
reg.SetValue("ProxyServer", _prevProxyServer);
135+
reg.SetValue("ProxyEnable", 0);
136+
reg.SetValue("ProxyServer", string.Empty);
56137
}
138+
57139
Refresh();
58140
}
141+
private static List<HttpSystemProxyValue> GetSystemProxyValues(string prevServerValue)
142+
{
143+
var result = new List<HttpSystemProxyValue>();
144+
145+
if (string.IsNullOrWhiteSpace(prevServerValue))
146+
return result;
147+
148+
var proxyValues = prevServerValue.Split(';');
149+
150+
if (proxyValues.Length > 0)
151+
{
152+
foreach (var value in proxyValues)
153+
{
154+
var parsedValue = parseProxyValue(value);
155+
if (parsedValue != null)
156+
result.Add(parsedValue);
157+
}
158+
}
159+
else
160+
{
161+
var parsedValue = parseProxyValue(prevServerValue);
162+
if (parsedValue != null)
163+
result.Add(parsedValue);
164+
}
165+
166+
return result;
167+
}
168+
169+
private static HttpSystemProxyValue parseProxyValue(string value)
170+
{
171+
var tmp = Regex.Replace(value, @"\s+", " ").Trim().ToLower();
172+
if (tmp.StartsWith("http="))
173+
{
174+
var endPoint = tmp.Substring(5);
175+
return new HttpSystemProxyValue()
176+
{
177+
HostName = endPoint.Split(':')[0],
178+
Port = int.Parse(endPoint.Split(':')[1]),
179+
IsSecure = false
180+
};
181+
}
182+
else if (tmp.StartsWith("https="))
183+
{
184+
var endPoint = tmp.Substring(5);
185+
return new HttpSystemProxyValue()
186+
{
187+
HostName = endPoint.Split(':')[0],
188+
Port = int.Parse(endPoint.Split(':')[1]),
189+
IsSecure = false
190+
};
191+
}
192+
return null;
193+
194+
}
195+
196+
private static void prepareRegistry(RegistryKey reg)
197+
{
198+
if (reg.GetValue("ProxyEnable") == null)
199+
{
200+
reg.SetValue("ProxyEnable", 0);
201+
}
202+
203+
if (reg.GetValue("ProxyServer") == null || reg.GetValue("ProxyEnable") as string == "0")
204+
{
205+
reg.SetValue("ProxyServer", string.Empty);
206+
}
207+
208+
}
209+
59210

60211
private static void Refresh()
61212
{
62-
NativeMethods.InternetSetOption(IntPtr.Zero, InternetOptionSettingsChanged, IntPtr.Zero,0);
213+
NativeMethods.InternetSetOption(IntPtr.Zero, InternetOptionSettingsChanged, IntPtr.Zero, 0);
63214
NativeMethods.InternetSetOption(IntPtr.Zero, InternetOptionRefresh, IntPtr.Zero, 0);
64215
}
65216
}

Titanium.Web.Proxy/ProxyServer.cs

+25-8
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public static void SetAsSystemHttpProxy(ExplicitProxyEndPoint endPoint)
9595
//clear any settings previously added
9696
ProxyEndPoints.OfType<ExplicitProxyEndPoint>().ToList().ForEach(x => x.IsSystemHttpProxy = false);
9797

98-
SystemProxyHelper.EnableProxyHttp(
98+
SystemProxyHelper.SetHttpProxy(
9999
Equals(endPoint.IpAddress, IPAddress.Any) | Equals(endPoint.IpAddress, IPAddress.Loopback) ? "127.0.0.1" : endPoint.IpAddress.ToString(), endPoint.Port);
100100

101101
endPoint.IsSystemHttpProxy = true;
@@ -106,6 +106,11 @@ public static void SetAsSystemHttpProxy(ExplicitProxyEndPoint endPoint)
106106

107107
}
108108

109+
public static void DisableSystemHttpProxy()
110+
{
111+
SystemProxyHelper.RemoveHttpProxy();
112+
}
113+
109114
public static void SetAsSystemHttpsProxy(ExplicitProxyEndPoint endPoint)
110115
{
111116
VerifyProxy(endPoint);
@@ -123,7 +128,7 @@ public static void SetAsSystemHttpsProxy(ExplicitProxyEndPoint endPoint)
123128
//If certificate was trusted by the machine
124129
if (certTrusted)
125130
{
126-
SystemProxyHelper.EnableProxyHttps(
131+
SystemProxyHelper.SetHttpsProxy(
127132
Equals(endPoint.IpAddress, IPAddress.Any) | Equals(endPoint.IpAddress, IPAddress.Loopback) ? "127.0.0.1" : endPoint.IpAddress.ToString(),
128133
endPoint.Port);
129134
}
@@ -136,6 +141,16 @@ public static void SetAsSystemHttpsProxy(ExplicitProxyEndPoint endPoint)
136141
Console.WriteLine("Set endpoint at Ip {1} and port: {2} as System HTTPS Proxy", endPoint.GetType().Name, endPoint.IpAddress, endPoint.Port);
137142
}
138143

144+
public static void DisableSystemHttpsProxy()
145+
{
146+
SystemProxyHelper.RemoveHttpsProxy();
147+
}
148+
149+
public static void DisableAllSystemProxies()
150+
{
151+
SystemProxyHelper.DisableAllProxy();
152+
}
153+
139154
public static void Start()
140155
{
141156
if (proxyRunning)
@@ -159,9 +174,9 @@ public static void Stop()
159174
if (!proxyRunning)
160175
throw new Exception("Proxy is not running.");
161176

162-
var SetAsSystemProxy = ProxyEndPoints.OfType<ExplicitProxyEndPoint>().Any(x => x.IsSystemHttpProxy || x.IsSystemHttpsProxy);
177+
var setAsSystemProxy = ProxyEndPoints.OfType<ExplicitProxyEndPoint>().Any(x => x.IsSystemHttpProxy || x.IsSystemHttpsProxy);
163178

164-
if (SetAsSystemProxy)
179+
if (setAsSystemProxy)
165180
{
166181
SystemProxyHelper.DisableAllProxy();
167182
#if !DEBUG
@@ -207,22 +222,24 @@ private static void VerifyProxy(ExplicitProxyEndPoint endPoint)
207222
private static void OnAcceptConnection(IAsyncResult asyn)
208223
{
209224
var endPoint = (ProxyEndPoint)asyn.AsyncState;
210-
var client = endPoint.listener.EndAcceptTcpClient(asyn);
211-
225+
212226
try
213227
{
228+
var client = endPoint.listener.EndAcceptTcpClient(asyn);
214229
if (endPoint.GetType() == typeof(TransparentProxyEndPoint))
215230
Task.Factory.StartNew(() => HandleClient(endPoint as TransparentProxyEndPoint, client));
216231
else
217232
Task.Factory.StartNew(() => HandleClient(endPoint as ExplicitProxyEndPoint, client));
233+
234+
// Get the listener that handles the client request.
235+
endPoint.listener.BeginAcceptTcpClient(OnAcceptConnection, endPoint);
218236
}
219237
catch
220238
{
221239
// ignored
222240
}
223241

224-
// Get the listener that handles the client request.
225-
endPoint.listener.BeginAcceptTcpClient(OnAcceptConnection, endPoint);
242+
226243
}
227244

228245
}

0 commit comments

Comments
 (0)