刷新dns缓存
This commit is contained in:
parent
e37c5592a4
commit
7187e4302a
@ -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>
|
||||||
@ -63,8 +59,7 @@ namespace FastGithub.Dns
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
SystemDnsUtil.SetAsPrimitiveDns();
|
SystemDnsUtil.SetAsPrimitiveDns();
|
||||||
SystemDnsUtil.FlushResolverCache();
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@ -140,11 +135,7 @@ namespace FastGithub.Dns
|
|||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
this.logger.LogWarning(ex.Message);
|
this.logger.LogWarning(ex.Message);
|
||||||
}
|
}
|
||||||
finally
|
|
||||||
{
|
|
||||||
SystemDnsUtil.FlushResolverCache();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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;
|
||||||
@ -20,7 +21,14 @@ namespace FastGithub.Dns
|
|||||||
const string DNS_FILTER = "udp.DstPort == 53";
|
const string DNS_FILTER = "udp.DstPort == 53";
|
||||||
private readonly FastGithubConfig fastGithubConfig;
|
private readonly FastGithubConfig fastGithubConfig;
|
||||||
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投毒后台服务
|
||||||
@ -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>
|
||||||
|
|||||||
@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user