ProxyConflictValidtor.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using FastGithub.Configuration;
  2. using Microsoft.Extensions.Logging;
  3. using Microsoft.Extensions.Options;
  4. using System;
  5. using System.Net.Http;
  6. using System.Threading.Tasks;
  7. namespace FastGithub.Dns
  8. {
  9. /// <summary>
  10. /// 代理冲突验证
  11. /// </summary>
  12. sealed class ProxyConflictValidtor : IConflictValidator
  13. {
  14. private readonly IOptions<FastGithubOptions> options;
  15. private readonly ILogger<ProxyConflictValidtor> logger;
  16. public ProxyConflictValidtor(
  17. IOptions<FastGithubOptions> options,
  18. ILogger<ProxyConflictValidtor> logger)
  19. {
  20. this.options = options;
  21. this.logger = logger;
  22. }
  23. /// <summary>
  24. /// 验证冲突
  25. /// </summary>
  26. /// <returns></returns>
  27. public Task ValidateAsync()
  28. {
  29. try
  30. {
  31. this.ValidateSystemProxy();
  32. }
  33. catch (Exception)
  34. {
  35. }
  36. return Task.CompletedTask;
  37. }
  38. /// <summary>
  39. /// 验证代理
  40. /// </summary>
  41. private void ValidateSystemProxy()
  42. {
  43. var systemProxy = HttpClient.DefaultProxy;
  44. if (systemProxy == null)
  45. {
  46. return;
  47. }
  48. foreach (var domain in this.options.Value.DomainConfigs.Keys)
  49. {
  50. var destination = new Uri($"https://{domain.Replace('*', 'a')}");
  51. var proxyServer = systemProxy.GetProxy(destination);
  52. if (proxyServer != null)
  53. {
  54. this.logger.LogError($"由于系统配置了{proxyServer}代理{domain},{nameof(FastGithub)}无法加速相关域名");
  55. }
  56. }
  57. }
  58. }
  59. }