EnvironmentProxyDetector.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #if !BESTHTTP_DISABLE_PROXY && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Linq;
  4. using BestHTTP.Connections;
  5. // Examples on proxy strings:
  6. // https://gist.github.com/yougg/5d2b3353fc5e197a0917aae0b3287d64
  7. namespace BestHTTP.Proxies.Autodetect
  8. {
  9. /// <summary>
  10. /// Based on https://curl.se/docs/manual.html "Environment Variables" section.
  11. /// </summary>
  12. public sealed class EnvironmentProxyDetector : IProxyDetector
  13. {
  14. private Proxy _cachedProxy;
  15. Proxy IProxyDetector.GetProxy(HTTPRequest request)
  16. {
  17. if (this._cachedProxy != null)
  18. return this._cachedProxy;
  19. string proxyUrl = null;
  20. if (HTTPProtocolFactory.IsSecureProtocol(request.CurrentUri))
  21. {
  22. proxyUrl = GetEnv("HTTPS_PROXY");
  23. HTTPManager.Logger.Information(nameof(EnvironmentProxyDetector), $"{nameof(IProxyDetector.GetProxy)} - HTTPS_PROXY: '{proxyUrl}'", request.Context);
  24. }
  25. else
  26. {
  27. proxyUrl = GetEnv("HTTP_PROXY");
  28. HTTPManager.Logger.Information(nameof(EnvironmentProxyDetector), $"{nameof(IProxyDetector.GetProxy)} - HTTP_PROXY: '{proxyUrl}'", request.Context);
  29. }
  30. if (proxyUrl == null)
  31. {
  32. proxyUrl = GetEnv("ALL_PROXY");
  33. }
  34. else
  35. HTTPManager.Logger.Information(nameof(EnvironmentProxyDetector), $"{nameof(IProxyDetector.GetProxy)} - ALL_PROXY: '{proxyUrl}'", request.Context);
  36. if (string.IsNullOrEmpty(proxyUrl))
  37. return null;
  38. // if the url is just a host[:port], add the http:// part too. Checking for :// should keep and treat the socks:// scheme too.
  39. if (proxyUrl.IndexOf("://") == -1 && !proxyUrl.StartsWith("http", StringComparison.OrdinalIgnoreCase))
  40. proxyUrl = "http://" + proxyUrl;
  41. string exceptionList = null;
  42. try
  43. {
  44. var proxyUri = new Uri(proxyUrl);
  45. Proxy proxy = null;
  46. if (proxyUri.Scheme.StartsWith("socks", StringComparison.OrdinalIgnoreCase))
  47. proxy = new SOCKSProxy(proxyUri, null);
  48. else
  49. proxy = new HTTPProxy(proxyUri);
  50. // A comma-separated list of host names that should not go through any proxy is set in (only an asterisk, * matches all hosts)
  51. exceptionList = GetEnv("NO_PROXY");
  52. if (!string.IsNullOrEmpty(exceptionList))
  53. proxy.Exceptions = exceptionList.Split(';').ToList<string>();
  54. return this._cachedProxy = proxy;
  55. }
  56. catch (Exception ex)
  57. {
  58. HTTPManager.Logger.Exception(nameof(EnvironmentProxyDetector), $"GetProxy - proxyUrl: '{proxyUrl}', exceptionList: '{exceptionList}'", ex, request.Context);
  59. }
  60. return null;
  61. }
  62. string GetEnv(string key) => System.Environment.GetEnvironmentVariable(key) ?? System.Environment.GetEnvironmentVariable(key.ToLowerInvariant());
  63. }
  64. }
  65. #endif