GithubMetaProvider.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using Microsoft.Extensions.DependencyInjection;
  2. using Microsoft.Extensions.Logging;
  3. using Microsoft.Extensions.Options;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Net.Http;
  8. using System.Net.Http.Json;
  9. using System.Net.Sockets;
  10. using System.Text.Json.Serialization;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. namespace FastGithub.Scanner.LookupProviders
  14. {
  15. /// <summary>
  16. /// Github公开的域名与ip关系提供者
  17. /// </summary>
  18. [Service(ServiceLifetime.Singleton, ServiceType = typeof(IGithubLookupProvider))]
  19. sealed class GithubMetaProvider : IGithubLookupProvider
  20. {
  21. private readonly IOptionsMonitor<GithubMetaProviderOptions> options;
  22. private readonly IHttpClientFactory httpClientFactory;
  23. private readonly ILogger<GithubMetaProvider> logger;
  24. private const string META_URI = "https://api.github.com/meta";
  25. /// <summary>
  26. /// 获取排序
  27. /// </summary>
  28. public int Order => int.MaxValue;
  29. /// <summary>
  30. /// Github公开的域名与ip关系提供者
  31. /// </summary>
  32. /// <param name="options"></param>
  33. /// <param name="logger"></param>
  34. public GithubMetaProvider(
  35. IOptionsMonitor<GithubMetaProviderOptions> options,
  36. IHttpClientFactory httpClientFactory,
  37. ILogger<GithubMetaProvider> logger)
  38. {
  39. this.options = options;
  40. this.httpClientFactory = httpClientFactory;
  41. this.logger = logger;
  42. }
  43. /// <summary>
  44. /// 查找域名与ip关系
  45. /// </summary>
  46. /// <param name="domains"></param>
  47. /// <param name="cancellationToken"></param>
  48. /// <returns></returns>
  49. public async Task<IEnumerable<DomainAddress>> LookupAsync(IEnumerable<string> domains, CancellationToken cancellationToken)
  50. {
  51. var setting = this.options.CurrentValue;
  52. if (setting.Enable == false)
  53. {
  54. return Enumerable.Empty<DomainAddress>();
  55. }
  56. try
  57. {
  58. var httpClient = this.httpClientFactory.CreateClient(nameof(Scanner));
  59. var meta = await GetMetaAsync(httpClient, setting.MetaUri, cancellationToken);
  60. if (meta != null)
  61. {
  62. return meta.ToDomainAddresses(domains);
  63. }
  64. }
  65. catch (Exception ex)
  66. {
  67. cancellationToken.ThrowIfCancellationRequested();
  68. this.logger.LogWarning($"加载远程的ip列表异常:{ex.Message}");
  69. }
  70. return Enumerable.Empty<DomainAddress>();
  71. }
  72. /// <summary>
  73. /// 尝试获取meta
  74. /// </summary>
  75. /// <param name="httpClient"></param>
  76. /// <param name="metaUri"></param>
  77. /// <returns></returns>
  78. private async Task<Meta?> GetMetaAsync(HttpClient httpClient, Uri metaUri, CancellationToken cancellationToken)
  79. {
  80. try
  81. {
  82. return await httpClient.GetFromJsonAsync<Meta>(META_URI, cancellationToken);
  83. }
  84. catch (Exception)
  85. {
  86. cancellationToken.ThrowIfCancellationRequested();
  87. this.logger.LogWarning($"使用{metaUri}的副本数据");
  88. return await httpClient.GetFromJsonAsync<Meta>(metaUri, cancellationToken);
  89. }
  90. }
  91. /// <summary>
  92. /// github的meta结构
  93. /// </summary>
  94. private class Meta
  95. {
  96. [JsonPropertyName("web")]
  97. public string[] Web { get; set; } = Array.Empty<string>();
  98. [JsonPropertyName("api")]
  99. public string[] Api { get; set; } = Array.Empty<string>();
  100. /// <summary>
  101. /// 转换为域名与ip关系
  102. /// </summary>
  103. /// <returns></returns>
  104. public IEnumerable<DomainAddress> ToDomainAddresses(IEnumerable<string> domains)
  105. {
  106. const string github = "github.com";
  107. const string apiGithub = "api.github.com";
  108. if (domains.Contains(github) == true)
  109. {
  110. foreach (var range in IPAddressRange.From(this.Web).OrderBy(item => item.Size))
  111. {
  112. if (range.AddressFamily == AddressFamily.InterNetwork)
  113. {
  114. foreach (var address in range)
  115. {
  116. yield return new DomainAddress(github, address);
  117. }
  118. }
  119. }
  120. }
  121. if (domains.Contains(apiGithub) == true)
  122. {
  123. foreach (var range in IPAddressRange.From(this.Api).OrderBy(item => item.Size))
  124. {
  125. if (range.AddressFamily == AddressFamily.InterNetwork)
  126. {
  127. foreach (var address in range)
  128. {
  129. yield return new DomainAddress(apiGithub, address);
  130. }
  131. }
  132. }
  133. }
  134. }
  135. }
  136. }
  137. }