1 using System.Collections;
2 using System.Collections.Generic;
3 using System.Text.RegularExpressions;
8 class AutoWebProxyScriptEngine
10 public AutoWebProxyScriptEngine (WebProxy proxy, bool useRegistry)
14 public Uri AutomaticConfigurationScript { get; set; }
15 public bool AutomaticallyDetectSettings { get; set; }
17 public bool GetProxies (Uri destination, out IList<string> proxyList)
20 return GetProxies (destination, out proxyList, ref i);
23 public bool GetProxies(Uri destination, out IList<string> proxyList, ref int syncStatus)
33 public void Abort (ref int syncStatus)
37 public void CheckForChanges ()
42 public WebProxyData GetWebProxyData ()
46 // TODO: Could re-use some pieces from _AutoWebProxyScriptEngine.cs
48 data = InitializeRegistryGlobalProxy ();
53 data = ReadEnvVariables ();
54 return data ?? new WebProxyData ();
57 WebProxyData ReadEnvVariables ()
59 string address = Environment.GetEnvironmentVariable ("http_proxy") ?? Environment.GetEnvironmentVariable ("HTTP_PROXY");
61 if (address != null) {
63 if (!address.StartsWith ("http://"))
64 address = "http://" + address;
66 Uri uri = new Uri (address);
69 if (IPAddress.TryParse (uri.Host, out ip)) {
70 if (IPAddress.Any.Equals (ip)) {
71 UriBuilder builder = new UriBuilder (uri);
72 builder.Host = "127.0.0.1";
74 } else if (IPAddress.IPv6Any.Equals (ip)) {
75 UriBuilder builder = new UriBuilder (uri);
76 builder.Host = "[::1]";
81 bool bBypassOnLocal = false;
82 ArrayList al = new ArrayList ();
83 string bypass = Environment.GetEnvironmentVariable ("no_proxy") ?? Environment.GetEnvironmentVariable ("NO_PROXY");
86 string[] bypassList = bypass.Split (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
88 foreach (string str in bypassList) {
92 bBypassOnLocal = true;
96 return new WebProxyData {
98 bypassOnLocal = bBypassOnLocal,
99 bypassList = CreateBypassList (al)
101 } catch (UriFormatException) {
108 static bool IsWindows ()
110 return (int) Environment.OSVersion.Platform < 4;
113 WebProxyData InitializeRegistryGlobalProxy ()
115 int iProxyEnable = (int)Microsoft.Win32.Registry.GetValue ("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", "ProxyEnable", 0);
117 if (iProxyEnable > 0) {
118 string strHttpProxy = "";
119 bool bBypassOnLocal = false;
120 ArrayList al = new ArrayList ();
122 string strProxyServer = (string)Microsoft.Win32.Registry.GetValue ("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", "ProxyServer", null);
123 string strProxyOverrride = (string)Microsoft.Win32.Registry.GetValue ("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", "ProxyOverride", null);
125 if (strProxyServer.Contains ("=")) {
126 foreach (string strEntry in strProxyServer.Split (new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
127 if (strEntry.StartsWith ("http=")) {
128 strHttpProxy = strEntry.Substring (5);
131 } else strHttpProxy = strProxyServer;
133 if (strProxyOverrride != null) {
134 string[] bypassList = strProxyOverrride.Split (new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
136 foreach (string str in bypassList) {
137 if (str != "<local>")
140 bBypassOnLocal = true;
144 return new WebProxyData {
145 proxyAddress = ToUri (strHttpProxy),
146 bypassOnLocal = bBypassOnLocal,
147 bypassList = CreateBypassList (al)
154 static Uri ToUri (string address)
159 if (address.IndexOf ("://", StringComparison.Ordinal) == -1)
160 address = "http://" + address;
162 return new Uri (address);
165 // Takes an ArrayList of fileglob-formatted strings and returns an array of Regex-formatted strings
166 static ArrayList CreateBypassList (ArrayList al)
168 string[] result = al.ToArray (typeof (string)) as string[];
169 for (int c = 0; c < result.Length; c++)
172 Regex.Escape (result [c]).Replace (@"\*", ".*").Replace (@"\?", ".") +
175 return new ArrayList (result);