diff --git a/FastGithub.Dns/DnsOverUdpServer.cs b/FastGithub.Dns/DnsOverUdpServer.cs
index d044004..517d1d4 100644
--- a/FastGithub.Dns/DnsOverUdpServer.cs
+++ b/FastGithub.Dns/DnsOverUdpServer.cs
@@ -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
///
public DnsOverUdpServer(
RequestResolver requestResolver,
- ILogger logger,
- IOptionsMonitor options)
+ ILogger logger)
{
this.requestResolver = requestResolver;
- this.logger = logger;
-
- options.OnChange(opt => SystemDnsUtil.FlushResolverCache());
+ this.logger = logger;
}
///
@@ -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();
- }
+ }
}
}
}
diff --git a/FastGithub.Dns/DnsPoisoningServer.cs b/FastGithub.Dns/DnsPoisoningServer.cs
index b8c3494..c1403ce 100644
--- a/FastGithub.Dns/DnsPoisoningServer.cs
+++ b/FastGithub.Dns/DnsPoisoningServer.cs
@@ -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 logger;
- private readonly TimeSpan ttl = TimeSpan.FromSeconds(10d);
+ private readonly TimeSpan ttl = TimeSpan.FromSeconds(10d);
+
+ ///
+ /// 刷新DNS缓存
+ ///
+ [SupportedOSPlatform("windows")]
+ [DllImport("dnsapi.dll", EntryPoint = "DnsFlushResolverCache", SetLastError = true)]
+ private static extern void DnsFlushResolverCache();
///
/// 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();
}
///
diff --git a/FastGithub.Dns/SystemDnsUtil.cs b/FastGithub.Dns/SystemDnsUtil.cs
index 9fec28b..69a3c14 100644
--- a/FastGithub.Dns/SystemDnsUtil.cs
+++ b/FastGithub.Dns/SystemDnsUtil.cs
@@ -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
///
static class SystemDnsUtil
{
- ///
- /// 刷新DNS缓存
- ///
- [SupportedOSPlatform("windows")]
- [DllImport("dnsapi.dll", EntryPoint = "DnsFlushResolverCache", SetLastError = true)]
- private static extern void DnsFlushResolverCache();
-
- ///
- /// 刷新DNS缓存
- ///
- public static void FlushResolverCache()
- {
- if (OperatingSystem.IsWindows())
- {
- DnsFlushResolverCache();
- }
- }
-
///
/// 设置为主dns
///
@@ -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();
}
-
-
- ///
- /// 设置网口的dns
- ///
- ///
- ///
- [SupportedOSPlatform("windows")]
- private static void SetNameServers(NetworkInterface @interface, IEnumerable 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();
- }
- }
}
}
\ No newline at end of file