ProxyDetector.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #if !BESTHTTP_DISABLE_PROXY && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using BestHTTP.Core;
  4. namespace BestHTTP.Proxies.Autodetect
  5. {
  6. public interface IProxyDetector
  7. {
  8. Proxy GetProxy(HTTPRequest request);
  9. }
  10. public enum ProxyDetectionMode
  11. {
  12. /// <summary>
  13. /// In Continouos mode the ProxyDetector will check for a proxy for every request.
  14. /// </summary>
  15. Continouos,
  16. /// <summary>
  17. /// This mode will cache the first Proxy found and use it for consecutive requests.
  18. /// </summary>
  19. CacheFirstFound
  20. }
  21. public sealed class ProxyDetector
  22. {
  23. public static IProxyDetector[] GetDefaultDetectors() => new IProxyDetector[] {
  24. // HTTPManager.Proxy has the highest priority
  25. new ProgrammaticallyAddedProxyDetector(),
  26. // then comes the environment set
  27. new EnvironmentProxyDetector(),
  28. // .net framework's detector
  29. new FrameworkProxyDetector(),
  30. #if UNITY_ANDROID && !UNITY_EDITOR
  31. new AndroidProxyDetector(),
  32. #endif
  33. };
  34. private IProxyDetector[] _proxyDetectors;
  35. private ProxyDetectionMode _detectionMode;
  36. private bool _attached;
  37. public ProxyDetector()
  38. : this(ProxyDetectionMode.CacheFirstFound, GetDefaultDetectors())
  39. { }
  40. public ProxyDetector(ProxyDetectionMode detectionMode)
  41. :this(detectionMode, GetDefaultDetectors())
  42. { }
  43. public ProxyDetector(ProxyDetectionMode detectionMode, IProxyDetector[] proxyDetectors)
  44. {
  45. this._detectionMode = detectionMode;
  46. this._proxyDetectors = proxyDetectors;
  47. if (this._proxyDetectors != null)
  48. Reattach();
  49. }
  50. public void Reattach()
  51. {
  52. HTTPManager.Logger.Information(nameof(ProxyDetector), $"{nameof(Reattach)}({this._attached})");
  53. if (!this._attached)
  54. {
  55. RequestEventHelper.OnEvent += OnRequestEvent;
  56. this._attached = true;
  57. }
  58. }
  59. /// <summary>
  60. /// Call Detach() to disable ProxyDetector's logic to find and set a proxy.
  61. /// </summary>
  62. public void Detach()
  63. {
  64. HTTPManager.Logger.Information(nameof(ProxyDetector), $"{nameof(Detach)}({this._attached})");
  65. if (this._attached)
  66. {
  67. RequestEventHelper.OnEvent -= OnRequestEvent;
  68. this._attached = false;
  69. }
  70. }
  71. private void OnRequestEvent(RequestEventInfo @event)
  72. {
  73. // The Resend event is raised for every request when it's queued up (sent or redirected).
  74. if (@event.Event == RequestEvents.Resend && @event.SourceRequest.Proxy == null)
  75. {
  76. Uri uri = @event.SourceRequest.CurrentUri;
  77. HTTPManager.Logger.Information(nameof(ProxyDetector), $"OnRequestEvent(RequestEvents.Resend) uri: '{uri}'", @event.SourceRequest.Context);
  78. try
  79. {
  80. foreach (var detector in this._proxyDetectors)
  81. {
  82. if (detector == null)
  83. continue;
  84. if (HTTPManager.Logger.Level == Logger.Loglevels.All)
  85. HTTPManager.Logger.Verbose(nameof(ProxyDetector), $"Calling {detector.GetType().Name}'s GetProxy", @event.SourceRequest.Context);
  86. var proxy = detector.GetProxy(@event.SourceRequest);
  87. if (proxy != null && proxy.UseProxyForAddress(uri))
  88. {
  89. if (HTTPManager.Logger.Level == Logger.Loglevels.All)
  90. HTTPManager.Logger.Verbose(nameof(ProxyDetector), $"[{detector.GetType().Name}] Proxy found: {proxy.Address} ", @event.SourceRequest.Context);
  91. switch (this._detectionMode)
  92. {
  93. case ProxyDetectionMode.Continouos:
  94. @event.SourceRequest.Proxy = proxy;
  95. break;
  96. case ProxyDetectionMode.CacheFirstFound:
  97. HTTPManager.Proxy = @event.SourceRequest.Proxy = proxy;
  98. HTTPManager.Logger.Verbose(nameof(ProxyDetector), $"Proxy cached in HTTPManager.Proxy!", @event.SourceRequest.Context);
  99. Detach();
  100. break;
  101. }
  102. return;
  103. }
  104. }
  105. HTTPManager.Logger.Information(nameof(ProxyDetector), $"No Proxy for '{uri}'.", @event.SourceRequest.Context);
  106. }
  107. catch (Exception ex)
  108. {
  109. if (HTTPManager.Logger.Level == BestHTTP.Logger.Loglevels.All)
  110. HTTPManager.Logger.Exception(nameof(ProxyDetector), $"GetProxyFor({@event.SourceRequest.CurrentUri})", ex, @event.SourceRequest.Context);
  111. }
  112. }
  113. }
  114. }
  115. }
  116. #endif