GithubScanService.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using FastGithub.Scanner.ScanMiddlewares;
  2. using Microsoft.Extensions.DependencyInjection;
  3. using Microsoft.Extensions.Logging;
  4. using System;
  5. using System.Linq;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace FastGithub.Scanner
  9. {
  10. /// <summary>
  11. /// github扫描服务
  12. /// </summary>
  13. [Service(ServiceLifetime.Singleton)]
  14. sealed class GithubScanService
  15. {
  16. private readonly GithubLookupFacotry domainAddressFactory;
  17. private readonly GithubContextCollection scanResults;
  18. private readonly ILogger<GithubScanService> logger;
  19. private readonly InvokeDelegate<GithubContext> fullScanDelegate;
  20. private readonly InvokeDelegate<GithubContext> resultScanDelegate;
  21. /// <summary>
  22. /// github扫描服务
  23. /// </summary>
  24. /// <param name="domainAddressFactory"></param>
  25. /// <param name="scanResults"></param>
  26. /// <param name="appService"></param>
  27. /// <param name="logger"></param>
  28. public GithubScanService(
  29. GithubLookupFacotry domainAddressFactory,
  30. GithubContextCollection scanResults,
  31. IServiceProvider appService,
  32. ILogger<GithubScanService> logger)
  33. {
  34. this.domainAddressFactory = domainAddressFactory;
  35. this.scanResults = scanResults;
  36. this.logger = logger;
  37. this.fullScanDelegate = new PipelineBuilder<GithubContext>(appService, ctx => Task.CompletedTask)
  38. .Use<ConcurrentMiddleware>()
  39. .Use<StatisticsMiddleware>()
  40. .Use<TcpScanMiddleware>()
  41. .Use<HttpsScanMiddleware>()
  42. .Build();
  43. this.resultScanDelegate = new PipelineBuilder<GithubContext>(appService, ctx => Task.CompletedTask)
  44. .Use<StatisticsMiddleware>()
  45. .Use<HttpsScanMiddleware>()
  46. .Build();
  47. }
  48. /// <summary>
  49. /// 扫描所有的ip
  50. /// </summary>
  51. /// <returns></returns>
  52. public async Task ScanAllAsync(CancellationToken cancellationToken)
  53. {
  54. this.logger.LogInformation("完整扫描开始..");
  55. var domainAddresses = await this.domainAddressFactory.CreateDomainAddressesAsync(cancellationToken);
  56. var scanTasks = domainAddresses
  57. .Select(item => new GithubContext(item.Domain, item.Address, cancellationToken))
  58. .Select(ctx => ScanAsync(ctx));
  59. var results = await Task.WhenAll(scanTasks);
  60. var successCount = results.Count(item => item);
  61. this.logger.LogInformation($"完整扫描结束,成功{successCount}条共{results.Length}条");
  62. async Task<bool> ScanAsync(GithubContext context)
  63. {
  64. await this.fullScanDelegate(context);
  65. if (context.Available == true)
  66. {
  67. this.scanResults.Add(context);
  68. }
  69. return context.Available;
  70. }
  71. }
  72. /// <summary>
  73. /// 扫描曾经扫描到的结果
  74. /// </summary>
  75. /// <returns></returns>
  76. public async Task ScanResultAsync()
  77. {
  78. this.logger.LogInformation("结果扫描开始..");
  79. var results = this.scanResults.ToArray();
  80. var contexts = results
  81. .OrderByDescending(item => item.History.AvailableRate)
  82. .ThenBy(item => item.History.AvgElapsed);
  83. foreach (var context in contexts)
  84. {
  85. await this.resultScanDelegate(context);
  86. }
  87. this.logger.LogInformation($"结果扫描结束,共扫描{results.Length}条记录");
  88. }
  89. }
  90. }