11using System ;
22using System . Runtime . InteropServices ;
33using Microsoft . Win32 ;
4+ using System . Text . RegularExpressions ;
5+ using System . Collections . Generic ;
6+ using System . Linq ;
47
58namespace 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 }
0 commit comments