From f45b880232a5e7a258af6cf2b827b4b7786d2864 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com> Date: Thu, 24 Jun 2021 13:53:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=9C=AC=E6=9C=BA=E5=88=B7?= =?UTF-8?q?=E6=96=B0=E5=9F=9F=E5=90=8D=E7=9A=84dns=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.Scanner/GithubDnsFlushService.cs | 47 +++++++++++++++++++++ FastGithub.Scanner/GithubScanService.cs | 5 ++- 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 FastGithub.Scanner/GithubDnsFlushService.cs diff --git a/FastGithub.Scanner/GithubDnsFlushService.cs b/FastGithub.Scanner/GithubDnsFlushService.cs new file mode 100644 index 0000000..45a8e28 --- /dev/null +++ b/FastGithub.Scanner/GithubDnsFlushService.cs @@ -0,0 +1,47 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using System; +using System.Runtime.InteropServices; +using System.Runtime.Versioning; + +namespace FastGithub.Scanner +{ + [Service(ServiceLifetime.Singleton)] + sealed class GithubDnsFlushService + { + private readonly ILogger logger; + private readonly IOptionsMonitor options; + + [SupportedOSPlatform("windows")] + [DllImport("dnsapi.dll", EntryPoint = "DnsFlushResolverCacheEntry_A", CharSet = CharSet.Ansi)] + private static extern int DnsFlushResolverCacheEntry(string hostName); + + public GithubDnsFlushService( + ILogger logger, + IOptionsMonitor options) + { + this.logger = logger; + this.options = options; + } + + public void FlushGithubResolverCache() + { + if (OperatingSystem.IsWindows()) + { + try + { + foreach (var domain in this.options.CurrentValue.Domains) + { + DnsFlushResolverCacheEntry(domain); + } + this.logger.LogInformation($"刷新本机相关域名的dns缓存成功"); + } + catch (Exception ex) + { + this.logger.LogWarning($"刷新本机相关域名的dns缓存失败:{ex.Message}"); + } + } + } + } +} diff --git a/FastGithub.Scanner/GithubScanService.cs b/FastGithub.Scanner/GithubScanService.cs index ec276f1..5efd383 100644 --- a/FastGithub.Scanner/GithubScanService.cs +++ b/FastGithub.Scanner/GithubScanService.cs @@ -17,6 +17,7 @@ namespace FastGithub.Scanner { private readonly GithubLookupFacotry lookupFactory; private readonly GithubScanResults scanResults; + private readonly GithubDnsFlushService dnsFlushService; private readonly ILoggerFactory loggerFactory; private readonly ILogger logger; @@ -33,12 +34,14 @@ namespace FastGithub.Scanner public GithubScanService( GithubLookupFacotry lookupFactory, GithubScanResults scanResults, + GithubDnsFlushService dnsFlushService, IServiceProvider appService, ILoggerFactory loggerFactory, ILogger logger) { this.lookupFactory = lookupFactory; this.scanResults = scanResults; + this.dnsFlushService = dnsFlushService; this.loggerFactory = loggerFactory; this.logger = logger; @@ -156,7 +159,7 @@ namespace FastGithub.Scanner domainLogger.LogWarning(context.ToStatisticsString()); } } - + this.dnsFlushService.FlushGithubResolverCache(); this.logger.LogInformation($"结果扫描结束,共扫描{results.Length}条记录"); } }