GithubResolver.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using Microsoft.Extensions.DependencyInjection;
  2. using Microsoft.Extensions.Options;
  3. using System.Net;
  4. namespace FastGithub.Scanner
  5. {
  6. /// <summary>
  7. /// github解析器
  8. /// </summary>
  9. [Service(ServiceLifetime.Singleton, ServiceType = typeof(IGithubResolver))]
  10. sealed class GithubResolver : IGithubResolver
  11. {
  12. private readonly GithubScanResults githubScanResults;
  13. private readonly IOptionsMonitor<GithubLookupFactoryOptions> options;
  14. /// <summary>
  15. /// github解析器
  16. /// </summary>
  17. /// <param name="githubScanResults"></param>
  18. /// <param name="options"></param>
  19. public GithubResolver(
  20. GithubScanResults githubScanResults,
  21. IOptionsMonitor<GithubLookupFactoryOptions> options)
  22. {
  23. this.githubScanResults = githubScanResults;
  24. this.options = options;
  25. }
  26. /// <summary>
  27. /// 是否支持指定的域名
  28. /// </summary>
  29. /// <param name="domain"></param>
  30. /// <returns></returns>
  31. public bool IsSupported(string domain)
  32. {
  33. return this.options.CurrentValue.Domains.Contains(domain);
  34. }
  35. /// <summary>
  36. /// 解析指定的域名
  37. /// </summary>
  38. /// <param name="domain"></param>
  39. /// <returns></returns>
  40. public IPAddress? Resolve(string domain)
  41. {
  42. return this.IsSupported(domain) ? this.githubScanResults.FindBestAddress(domain) : default;
  43. }
  44. }
  45. }