EnvironmentProxyDetector.cs 3.1 KB

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