GithubScanService.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 lookupFactory;
  17. private readonly GithubContextCollection scanResults;
  18. private readonly ILoggerFactory loggerFactory;
  19. private readonly ILogger<GithubScanService> logger;
  20. private readonly InvokeDelegate<GithubContext> fullScanDelegate;
  21. private readonly InvokeDelegate<GithubContext> resultScanDelegate;
  22. /// <summary>
  23. /// github扫描服务
  24. /// </summary>
  25. /// <param name="lookupFactory"></param>
  26. /// <param name="scanResults"></param>
  27. /// <param name="appService"></param>
  28. /// <param name="logger"></param>
  29. public GithubScanService(
  30. GithubLookupFacotry lookupFactory,
  31. GithubContextCollection scanResults,
  32. IServiceProvider appService,
  33. ILoggerFactory loggerFactory,
  34. ILogger<GithubScanService> logger)
  35. {
  36. this.lookupFactory = lookupFactory;
  37. this.scanResults = scanResults;
  38. this.loggerFactory = loggerFactory;
  39. this.logger = logger;
  40. this.fullScanDelegate = new PipelineBuilder<GithubContext>(appService, ctx => Task.CompletedTask)
  41. .Use<ConcurrentMiddleware>()
  42. .Use<StatisticsMiddleware>()
  43. .Use<TcpScanMiddleware>()
  44. .Use<HttpsScanMiddleware>()
  45. .Build();
  46. this.resultScanDelegate = new PipelineBuilder<GithubContext>(appService, ctx => Task.CompletedTask)
  47. .Use<StatisticsMiddleware>()
  48. .Use<HttpsScanMiddleware>()
  49. .Build();
  50. }
  51. /// <summary>
  52. /// 扫描所有的ip
  53. /// </summary>
  54. /// <returns></returns>
  55. public async Task ScanAllAsync(CancellationToken cancellationToken)
  56. {
  57. this.logger.LogInformation("完整扫描开始..");
  58. var domainAddresses = await this.lookupFactory.LookupAsync(cancellationToken);
  59. var scanTasks = domainAddresses
  60. .Select(item => new GithubContext(item.Domain, item.Address, cancellationToken))
  61. .Select(ctx => ScanAsync(ctx));
  62. var results = await Task.WhenAll(scanTasks);
  63. var successCount = results.Count(item => item);
  64. this.logger.LogInformation($"完整扫描结束,成功{successCount}条共{results.Length}条");
  65. async Task<bool> ScanAsync(GithubContext context)
  66. {
  67. await this.fullScanDelegate(context);
  68. if (context.Available && this.scanResults.Add(context))
  69. {
  70. this.logger.LogInformation($"扫描到{context}");
  71. }
  72. return context.Available;
  73. }
  74. }
  75. /// <summary>
  76. /// 扫描曾经扫描到的结果
  77. /// </summary>
  78. /// <returns></returns>
  79. public async Task ScanResultAsync()
  80. {
  81. this.logger.LogInformation("结果扫描开始..");
  82. var results = this.scanResults.ToArray();
  83. var contexts = results
  84. .OrderBy(item => item.Domain)
  85. .ThenByDescending(item => item.History.AvailableRate)
  86. .ThenBy(item => item.History.AvgElapsed);
  87. foreach (var context in contexts)
  88. {
  89. await this.resultScanDelegate(context);
  90. var domainLogger = this.loggerFactory.CreateLogger(context.Domain);
  91. if (context.Available == true)
  92. {
  93. domainLogger.LogInformation(context.ToStatisticsString());
  94. }
  95. else
  96. {
  97. domainLogger.LogWarning(context.ToStatisticsString());
  98. }
  99. }
  100. this.logger.LogInformation($"结果扫描结束,共扫描{results.Length}条记录");
  101. }
  102. }
  103. }