GithubMetaProvider.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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(FastGithub));
  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 static 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. return await httpClient.GetFromJsonAsync<Meta>(metaUri, cancellationToken);
  88. }
  89. }
  90. /// <summary>
  91. /// github的meta结构
  92. /// </summary>
  93. private class Meta
  94. {
  95. [JsonPropertyName("web")]
  96. public string[] Web { get; set; } = Array.Empty<string>();
  97. [JsonPropertyName("api")]
  98. public string[] Api { get; set; } = Array.Empty<string>();
  99. /// <summary>
  100. /// 转换为域名与ip关系
  101. /// </summary>
  102. /// <returns></returns>
  103. public IEnumerable<DomainAddress> ToDomainAddresses(IEnumerable<string> domains)
  104. {
  105. const string github = "github.com";
  106. const string apiGithub = "api.github.com";
  107. if (domains.Contains(github) == true)
  108. {
  109. foreach (var range in IPAddressRange.From(this.Web).OrderBy(item => item.Size))
  110. {
  111. if (range.AddressFamily == AddressFamily.InterNetwork)
  112. {
  113. foreach (var address in range)
  114. {
  115. yield return new DomainAddress(github, address);
  116. }
  117. }
  118. }
  119. }
  120. if (domains.Contains(apiGithub) == true)
  121. {
  122. foreach (var range in IPAddressRange.From(this.Api).OrderBy(item => item.Size))
  123. {
  124. if (range.AddressFamily == AddressFamily.InterNetwork)
  125. {
  126. foreach (var address in range)
  127. {
  128. yield return new DomainAddress(apiGithub, address);
  129. }
  130. }
  131. }
  132. }
  133. }
  134. }
  135. }
  136. }