PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/System/ReferenceSources/AutoWebProxyScriptEngine.cs

https://github.com/pruiz/mono
C# | 181 lines | 147 code | 32 blank | 2 comment | 27 complexity | a29efb655ebfcecf4d4c2b142758a8d8 MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4. #if !MONOTOUCH_WATCH
  5. using Mono.Net;
  6. #endif
  7. namespace System.Net
  8. {
  9. class AutoWebProxyScriptEngine
  10. {
  11. public AutoWebProxyScriptEngine (WebProxy proxy, bool useRegistry)
  12. {
  13. }
  14. public Uri AutomaticConfigurationScript { get; set; }
  15. public bool AutomaticallyDetectSettings { get; set; }
  16. public bool GetProxies (Uri destination, out IList<string> proxyList)
  17. {
  18. int i = 0;
  19. return GetProxies (destination, out proxyList, ref i);
  20. }
  21. public bool GetProxies(Uri destination, out IList<string> proxyList, ref int syncStatus)
  22. {
  23. proxyList = null;
  24. return false;
  25. }
  26. public void Close ()
  27. {
  28. }
  29. public void Abort (ref int syncStatus)
  30. {
  31. }
  32. public void CheckForChanges ()
  33. {
  34. }
  35. #if !MOBILE
  36. public WebProxyData GetWebProxyData ()
  37. {
  38. WebProxyData data;
  39. // TODO: Could re-use some pieces from _AutoWebProxyScriptEngine.cs
  40. if (IsWindows ()) {
  41. data = InitializeRegistryGlobalProxy ();
  42. if (data != null)
  43. return data;
  44. }
  45. data = ReadEnvVariables ();
  46. return data ?? new WebProxyData ();
  47. }
  48. WebProxyData ReadEnvVariables ()
  49. {
  50. string address = Environment.GetEnvironmentVariable ("http_proxy") ?? Environment.GetEnvironmentVariable ("HTTP_PROXY");
  51. if (address != null) {
  52. try {
  53. if (!address.StartsWith ("http://"))
  54. address = "http://" + address;
  55. Uri uri = new Uri (address);
  56. IPAddress ip;
  57. if (IPAddress.TryParse (uri.Host, out ip)) {
  58. if (IPAddress.Any.Equals (ip)) {
  59. UriBuilder builder = new UriBuilder (uri);
  60. builder.Host = "127.0.0.1";
  61. uri = builder.Uri;
  62. } else if (IPAddress.IPv6Any.Equals (ip)) {
  63. UriBuilder builder = new UriBuilder (uri);
  64. builder.Host = "[::1]";
  65. uri = builder.Uri;
  66. }
  67. }
  68. bool bBypassOnLocal = false;
  69. ArrayList al = new ArrayList ();
  70. string bypass = Environment.GetEnvironmentVariable ("no_proxy") ?? Environment.GetEnvironmentVariable ("NO_PROXY");
  71. if (bypass != null) {
  72. string[] bypassList = bypass.Split (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  73. foreach (string str in bypassList) {
  74. if (str != "*.local")
  75. al.Add (str);
  76. else
  77. bBypassOnLocal = true;
  78. }
  79. }
  80. return new WebProxyData {
  81. proxyAddress = uri,
  82. bypassOnLocal = bBypassOnLocal,
  83. bypassList = CreateBypassList (al)
  84. };
  85. } catch (UriFormatException) {
  86. }
  87. }
  88. return null;
  89. }
  90. static bool IsWindows ()
  91. {
  92. return (int) Environment.OSVersion.Platform < 4;
  93. }
  94. WebProxyData InitializeRegistryGlobalProxy ()
  95. {
  96. int iProxyEnable = (int)Microsoft.Win32.Registry.GetValue ("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", "ProxyEnable", 0);
  97. if (iProxyEnable > 0) {
  98. string strHttpProxy = "";
  99. bool bBypassOnLocal = false;
  100. ArrayList al = new ArrayList ();
  101. string strProxyServer = (string)Microsoft.Win32.Registry.GetValue ("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", "ProxyServer", null);
  102. string strProxyOverrride = (string)Microsoft.Win32.Registry.GetValue ("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", "ProxyOverride", null);
  103. if (strProxyServer.Contains ("=")) {
  104. foreach (string strEntry in strProxyServer.Split (new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
  105. if (strEntry.StartsWith ("http=")) {
  106. strHttpProxy = strEntry.Substring (5);
  107. break;
  108. }
  109. } else strHttpProxy = strProxyServer;
  110. if (strProxyOverrride != null) {
  111. string[] bypassList = strProxyOverrride.Split (new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
  112. foreach (string str in bypassList) {
  113. if (str != "<local>")
  114. al.Add (str);
  115. else
  116. bBypassOnLocal = true;
  117. }
  118. }
  119. return new WebProxyData {
  120. proxyAddress = ToUri (strHttpProxy),
  121. bypassOnLocal = bBypassOnLocal,
  122. bypassList = CreateBypassList (al)
  123. };
  124. }
  125. return null;
  126. }
  127. static Uri ToUri (string address)
  128. {
  129. if (address == null)
  130. return null;
  131. if (address.IndexOf ("://", StringComparison.Ordinal) == -1)
  132. address = "http://" + address;
  133. return new Uri (address);
  134. }
  135. // Takes an ArrayList of fileglob-formatted strings and returns an array of Regex-formatted strings
  136. static ArrayList CreateBypassList (ArrayList al)
  137. {
  138. string[] result = al.ToArray (typeof (string)) as string[];
  139. for (int c = 0; c < result.Length; c++)
  140. {
  141. result [c] = "^" +
  142. Regex.Escape (result [c]).Replace (@"\*", ".*").Replace (@"\?", ".") +
  143. "$";
  144. }
  145. return new ArrayList (result);
  146. }
  147. #endif
  148. }
  149. }