1
1
using System ;
2
2
using System . Runtime . InteropServices ;
3
3
using Microsoft . Win32 ;
4
+ using System . Text . RegularExpressions ;
5
+ using System . Collections . Generic ;
6
+ using System . Linq ;
4
7
5
8
namespace Titanium . Web . Proxy . Helpers
6
9
{
@@ -11,55 +14,203 @@ internal static extern bool InternetSetOption(IntPtr hInternet, int dwOption, In
11
14
int dwBufferLength ) ;
12
15
}
13
16
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
+
14
32
public static class SystemProxyHelper
15
33
{
16
34
public const int InternetOptionSettingsChanged = 39 ;
17
35
public const int InternetOptionRefresh = 37 ;
18
- private static object _prevProxyServer ;
19
- private static object _prevProxyEnable ;
20
36
21
- public static void EnableProxyHttp ( string hostname , int port )
37
+ public static void SetHttpProxy ( string hostname , int port )
22
38
{
23
39
var reg = Registry . CurrentUser . OpenSubKey (
24
40
"Software\\ Microsoft\\ Windows\\ CurrentVersion\\ Internet Settings" , true ) ;
25
41
if ( reg != null )
26
42
{
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
+
29
75
reg . SetValue ( "ProxyEnable" , 1 ) ;
30
- reg . SetValue ( "ProxyServer" , "http=" + hostname + ":" + port + ";" ) ;
76
+ reg . SetValue ( "ProxyServer" , String . Join ( ";" , existingSystemProxyValues . Select ( x => x . ToString ( ) ) . ToArray ( ) ) ) ;
31
77
}
78
+
32
79
Refresh ( ) ;
33
80
}
34
81
35
- public static void EnableProxyHttps ( string hostname , int port )
82
+ public static void SetHttpsProxy ( string hostname , int port )
36
83
{
37
84
var reg = Registry . CurrentUser . OpenSubKey (
38
85
"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 ) ;
39
113
if ( reg != null )
40
114
{
115
+ var exisitingContent = reg . GetValue ( "ProxyServer" ) as string ;
116
+
117
+ var existingSystemProxyValues = GetSystemProxyValues ( exisitingContent ) ;
118
+ existingSystemProxyValues . RemoveAll ( x => x . IsSecure ) ;
119
+
120
+
41
121
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 ( ) ) ) ;
43
123
}
124
+
44
125
Refresh ( ) ;
45
126
}
46
127
47
128
public static void DisableAllProxy ( )
48
129
{
49
130
var reg = Registry . CurrentUser . OpenSubKey (
50
131
"Software\\ Microsoft\\ Windows\\ CurrentVersion\\ Internet Settings" , true ) ;
132
+
51
133
if ( reg != null )
52
134
{
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 ) ;
56
137
}
138
+
57
139
Refresh ( ) ;
58
140
}
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
+
59
210
60
211
private static void Refresh ( )
61
212
{
62
- NativeMethods . InternetSetOption ( IntPtr . Zero , InternetOptionSettingsChanged , IntPtr . Zero , 0 ) ;
213
+ NativeMethods . InternetSetOption ( IntPtr . Zero , InternetOptionSettingsChanged , IntPtr . Zero , 0 ) ;
63
214
NativeMethods . InternetSetOption ( IntPtr . Zero , InternetOptionRefresh , IntPtr . Zero , 0 ) ;
64
215
}
65
216
}
0 commit comments