刷新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 DNS.Protocol;
using FastGithub.Configuration; using FastGithub.Configuration;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System; using System;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
@ -29,13 +28,10 @@ namespace FastGithub.Dns
/// <param name="logger"></param> /// <param name="logger"></param>
public DnsOverUdpServer( public DnsOverUdpServer(
RequestResolver requestResolver, RequestResolver requestResolver,
ILogger<DnsOverUdpServer> logger, ILogger<DnsOverUdpServer> logger)
IOptionsMonitor<FastGithubOptions> options)
{ {
this.requestResolver = requestResolver; this.requestResolver = requestResolver;
this.logger = logger; this.logger = logger;
options.OnChange(opt => SystemDnsUtil.FlushResolverCache());
} }
/// <summary> /// <summary>
@ -64,7 +60,6 @@ namespace FastGithub.Dns
try try
{ {
SystemDnsUtil.SetAsPrimitiveDns(); SystemDnsUtil.SetAsPrimitiveDns();
SystemDnsUtil.FlushResolverCache();
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -141,10 +136,6 @@ namespace FastGithub.Dns
{ {
this.logger.LogWarning(ex.Message); this.logger.LogWarning(ex.Message);
} }
finally
{
SystemDnsUtil.FlushResolverCache();
}
} }
} }
} }

View File

@ -6,6 +6,7 @@ using PacketDotNet;
using System; using System;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Runtime.InteropServices;
using System.Runtime.Versioning; using System.Runtime.Versioning;
using System.Threading; using System.Threading;
using WinDivertSharp; using WinDivertSharp;
@ -22,6 +23,13 @@ namespace FastGithub.Dns
private readonly ILogger<DnsPoisoningServer> logger; 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> /// <summary>
/// dns投毒后台服务 /// dns投毒后台服务
/// </summary> /// </summary>
@ -53,7 +61,7 @@ namespace FastGithub.Dns
using var winDivertBuffer = new WinDivertBuffer(packetBuffer); using var winDivertBuffer = new WinDivertBuffer(packetBuffer);
var winDivertAddress = new WinDivertAddress(); var winDivertAddress = new WinDivertAddress();
SystemDnsUtil.FlushResolverCache(); DnsFlushResolverCache();
while (cancellationToken.IsCancellationRequested == false) while (cancellationToken.IsCancellationRequested == false)
{ {
if (WinDivert.WinDivertRecv(handle, winDivertBuffer, ref winDivertAddress, ref packetLength)) if (WinDivert.WinDivertRecv(handle, winDivertBuffer, ref winDivertAddress, ref packetLength))
@ -73,6 +81,7 @@ namespace FastGithub.Dns
} }
WinDivert.WinDivertClose(handle); WinDivert.WinDivertClose(handle);
DnsFlushResolverCache();
} }
/// <summary> /// <summary>

View File

@ -1,12 +1,8 @@
using FastGithub.Configuration; using FastGithub.Configuration;
using System; using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Net.NetworkInformation; using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
namespace FastGithub.Dns namespace FastGithub.Dns
{ {
@ -15,24 +11,6 @@ namespace FastGithub.Dns
/// </summary> /// </summary>
static class SystemDnsUtil 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> /// <summary>
/// 设置为主dns /// 设置为主dns
/// </summary> /// </summary>
@ -50,12 +28,7 @@ namespace FastGithub.Dns
if (firstRecord == null || LocalMachine.ContainsIPAddress(firstRecord) == false) if (firstRecord == null || LocalMachine.ContainsIPAddress(firstRecord) == false)
{ {
var primitive = IPAddress.Loopback; var primitive = IPAddress.Loopback;
var nameServers = dnsAddresses.Prepend(primitive); if (OperatingSystem.IsLinux())
if (OperatingSystem.IsWindows())
{
SetNameServers(@interface, nameServers);
}
else if (OperatingSystem.IsLinux())
{ {
throw new FastGithubException($"不支持自动设置本机DNS请手工添加{primitive}做为/etc/resolv.conf的第一条记录"); throw new FastGithubException($"不支持自动设置本机DNS请手工添加{primitive}做为/etc/resolv.conf的第一条记录");
} }
@ -82,12 +55,7 @@ namespace FastGithub.Dns
var firstRecord = dnsAddresses.FirstOrDefault(); var firstRecord = dnsAddresses.FirstOrDefault();
if (firstRecord != null && LocalMachine.ContainsIPAddress(firstRecord)) if (firstRecord != null && LocalMachine.ContainsIPAddress(firstRecord))
{ {
var nameServers = dnsAddresses.Skip(1); if (OperatingSystem.IsLinux())
if (OperatingSystem.IsWindows())
{
SetNameServers(@interface, nameServers);
}
else if (OperatingSystem.IsLinux())
{ {
throw new FastGithubException($"不支持自动移除本机主DNS请手工移除/etc/resolv.conf的第一条记录"); 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))) .Where(item => item.GetIPProperties().UnicastAddresses.Any(a => a.Address.Equals(address)))
.FirstOrDefault(); .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();
}
}
} }
} }