1 using System
.Collections
;
2 using System
.Collections
.Generic
;
3 using System
.Text
.RegularExpressions
;
10 class AutoWebProxyScriptEngine
12 public AutoWebProxyScriptEngine (WebProxy proxy
, bool useRegistry
)
16 public Uri AutomaticConfigurationScript { get; set; }
17 public bool AutomaticallyDetectSettings { get; set; }
19 public bool GetProxies (Uri destination
, out IList
<string> proxyList
)
22 return GetProxies (destination
, out proxyList
, ref i
);
25 public bool GetProxies(Uri destination
, out IList
<string> proxyList
, ref int syncStatus
)
35 public void Abort (ref int syncStatus
)
39 public void CheckForChanges ()
44 public WebProxyData
GetWebProxyData ()
48 // TODO: Could re-use some pieces from _AutoWebProxyScriptEngine.cs
50 data
= InitializeRegistryGlobalProxy ();
55 data
= ReadEnvVariables ();
56 return data
?? new WebProxyData ();
59 WebProxyData
ReadEnvVariables ()
61 string address
= Environment
.GetEnvironmentVariable ("http_proxy") ?? Environment
.GetEnvironmentVariable ("HTTP_PROXY");
63 if (address
!= null) {
65 if (!address
.StartsWith ("http://"))
66 address
= "http://" + address
;
68 Uri uri
= new Uri (address
);
71 if (IPAddress
.TryParse (uri
.Host
, out ip
)) {
72 if (IPAddress
.Any
.Equals (ip
)) {
73 UriBuilder builder
= new UriBuilder (uri
);
74 builder
.Host
= "127.0.0.1";
76 } else if (IPAddress
.IPv6Any
.Equals (ip
)) {
77 UriBuilder builder
= new UriBuilder (uri
);
78 builder
.Host
= "[::1]";
83 bool bBypassOnLocal
= false;
84 ArrayList al
= new ArrayList ();
85 string bypass
= Environment
.GetEnvironmentVariable ("no_proxy") ?? Environment
.GetEnvironmentVariable ("NO_PROXY");
88 string[] bypassList
= bypass
.Split (new char[] { ',' }
, StringSplitOptions
.RemoveEmptyEntries
);
90 foreach (string str
in bypassList
) {
94 bBypassOnLocal
= true;
98 return new WebProxyData
{
100 bypassOnLocal
= bBypassOnLocal
,
101 bypassList
= CreateBypassList (al
)
103 } catch (UriFormatException
) {
110 static bool IsWindows ()
112 return (int) Environment
.OSVersion
.Platform
< 4;
115 WebProxyData
InitializeRegistryGlobalProxy ()
117 int iProxyEnable
= (int)Microsoft
.Win32
.Registry
.GetValue ("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", "ProxyEnable", 0);
119 if (iProxyEnable
> 0) {
120 string strHttpProxy
= "";
121 bool bBypassOnLocal
= false;
122 ArrayList al
= new ArrayList ();
124 string strProxyServer
= (string)Microsoft
.Win32
.Registry
.GetValue ("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", "ProxyServer", null);
125 string strProxyOverrride
= (string)Microsoft
.Win32
.Registry
.GetValue ("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", "ProxyOverride", null);
127 if (strProxyServer
.Contains ("=")) {
128 foreach (string strEntry
in strProxyServer
.Split (new char[] { ';' }
, StringSplitOptions
.RemoveEmptyEntries
))
129 if (strEntry
.StartsWith ("http=")) {
130 strHttpProxy
= strEntry
.Substring (5);
133 } else strHttpProxy
= strProxyServer
;
135 if (strProxyOverrride
!= null) {
136 string[] bypassList
= strProxyOverrride
.Split (new char[] { ';' }
, StringSplitOptions
.RemoveEmptyEntries
);
138 foreach (string str
in bypassList
) {
139 if (str
!= "<local>")
142 bBypassOnLocal
= true;
146 return new WebProxyData
{
147 proxyAddress
= ToUri (strHttpProxy
),
148 bypassOnLocal
= bBypassOnLocal
,
149 bypassList
= CreateBypassList (al
)
156 static Uri
ToUri (string address
)
161 if (address
.IndexOf ("://", StringComparison
.Ordinal
) == -1)
162 address
= "http://" + address
;
164 return new Uri (address
);
167 // Takes an ArrayList of fileglob-formatted strings and returns an array of Regex-formatted strings
168 static ArrayList
CreateBypassList (ArrayList al
)
170 string[] result
= al
.ToArray (typeof (string)) as string[];
171 for (int c
= 0; c
< result
.Length
; c
++)
174 Regex
.Escape (result
[c
]).Replace (@"\*", ".*").Replace (@"\?", ".") +
177 return new ArrayList (result
);