刷新dns缓存

This commit is contained in:
陈国伟 2021-09-14 14:11:53 +08:00
parent e37c5592a4
commit 7187e4302a
3 changed files with 17 additions and 78 deletions

View File

@ -1,7 +1,6 @@
using DNS.Protocol;
using FastGithub.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Net;
using System.Net.Sockets;
@ -29,13 +28,10 @@ namespace FastGithub.Dns
/// <param name="logger"></param>
public DnsOverUdpServer(
RequestResolver requestResolver,
ILogger<DnsOverUdpServer> logger,
IOptionsMonitor<FastGithubOptions> options)
ILogger<DnsOverUdpServer> logger)
{
this.requestResolver = requestResolver;
this.logger = logger;
options.OnChange(opt => SystemDnsUtil.FlushResolverCache());
this.logger = logger;
}
/// <summary>
@ -63,8 +59,7 @@ namespace FastGithub.Dns
try
{
SystemDnsUtil.SetAsPrimitiveDns();
SystemDnsUtil.FlushResolverCache();
SystemDnsUtil.SetAsPrimitiveDns();
}
catch (Exception ex)
{
@ -140,11 +135,7 @@ namespace FastGithub.Dns
catch (Exception ex)
{
this.logger.LogWarning(ex.Message);
}
finally
{
SystemDnsUtil.FlushResolverCache();
}
}
}
}
}

View File

@ -6,6 +6,7 @@ using PacketDotNet;
using System;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Threading;
using WinDivertSharp;
@ -20,7 +21,14 @@ namespace FastGithub.Dns
const string DNS_FILTER = "udp.DstPort == 53";
private readonly FastGithubConfig fastGithubConfig;
private readonly ILogger<DnsPoisoningServer> logger;
private readonly TimeSpan ttl = TimeSpan.FromSeconds(10d);
private readonly TimeSpan ttl = TimeSpan.FromSeconds(10d);
/// <summary>
/// 刷新DNS缓存
/// </summary>
[SupportedOSPlatform("windows")]
[DllImport("dnsapi.dll", EntryPoint = "DnsFlushResolverCache", SetLastError = true)]
private static extern void DnsFlushResolverCache();
/// <summary>
/// dns投毒后台服务
@ -53,7 +61,7 @@ namespace FastGithub.Dns
using var winDivertBuffer = new WinDivertBuffer(packetBuffer);
var winDivertAddress = new WinDivertAddress();
SystemDnsUtil.FlushResolverCache();
DnsFlushResolverCache();
while (cancellationToken.IsCancellationRequested == false)
{
if (WinDivert.WinDivertRecv(handle, winDivertBuffer, ref winDivertAddress, ref packetLength))
@ -73,6 +81,7 @@ namespace FastGithub.Dns
}
WinDivert.WinDivertClose(handle);
DnsFlushResolverCache();
}
/// <summary>

View File

@ -1,12 +1,8 @@
using FastGithub.Configuration;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
namespace FastGithub.Dns
{
@ -15,24 +11,6 @@ namespace FastGithub.Dns
/// </summary>
static class SystemDnsUtil
{
/// <summary>
/// 刷新DNS缓存
/// </summary>
[SupportedOSPlatform("windows")]
[DllImport("dnsapi.dll", EntryPoint = "DnsFlushResolverCache", SetLastError = true)]
private static extern void DnsFlushResolverCache();
/// <summary>
/// 刷新DNS缓存
/// </summary>
public static void FlushResolverCache()
{
if (OperatingSystem.IsWindows())
{
DnsFlushResolverCache();
}
}
/// <summary>
/// 设置为主dns
/// </summary>
@ -50,12 +28,7 @@ namespace FastGithub.Dns
if (firstRecord == null || LocalMachine.ContainsIPAddress(firstRecord) == false)
{
var primitive = IPAddress.Loopback;
var nameServers = dnsAddresses.Prepend(primitive);
if (OperatingSystem.IsWindows())
{
SetNameServers(@interface, nameServers);
}
else if (OperatingSystem.IsLinux())
if (OperatingSystem.IsLinux())
{
throw new FastGithubException($"不支持自动设置本机DNS请手工添加{primitive}做为/etc/resolv.conf的第一条记录");
}
@ -82,12 +55,7 @@ namespace FastGithub.Dns
var firstRecord = dnsAddresses.FirstOrDefault();
if (firstRecord != null && LocalMachine.ContainsIPAddress(firstRecord))
{
var nameServers = dnsAddresses.Skip(1);
if (OperatingSystem.IsWindows())
{
SetNameServers(@interface, nameServers);
}
else if (OperatingSystem.IsLinux())
if (OperatingSystem.IsLinux())
{
throw new FastGithubException($"不支持自动移除本机主DNS请手工移除/etc/resolv.conf的第一条记录");
}
@ -117,34 +85,5 @@ namespace FastGithub.Dns
.Where(item => item.GetIPProperties().UnicastAddresses.Any(a => a.Address.Equals(address)))
.FirstOrDefault();
}
/// <summary>
/// 设置网口的dns
/// </summary>
/// <param name="interface"></param>
/// <param name="nameServers"></param>
[SupportedOSPlatform("windows")]
private static void SetNameServers(NetworkInterface @interface, IEnumerable<IPAddress> nameServers)
{
Netsh($@"interface ipv4 delete dns ""{@interface.Name}"" all");
foreach (var address in nameServers)
{
Netsh($@"interface ipv4 add dns ""{@interface.Name}"" {address} validate=no");
}
static void Netsh(string arguments)
{
var netsh = new ProcessStartInfo
{
FileName = "netsh.exe",
Arguments = arguments,
CreateNoWindow = true,
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden
};
Process.Start(netsh)?.WaitForExit();
}
}
}
}