|
@@ -12,8 +12,7 @@ namespace FastGithub.Dns
|
|
{
|
|
{
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 系统域名服务工具
|
|
/// 系统域名服务工具
|
|
- /// </summary>
|
|
|
|
- [SupportedOSPlatform("windows")]
|
|
|
|
|
|
+ /// </summary>
|
|
static class SystemDnsUtil
|
|
static class SystemDnsUtil
|
|
{
|
|
{
|
|
/// <summary>
|
|
/// <summary>
|
|
@@ -21,54 +20,53 @@ namespace FastGithub.Dns
|
|
/// </summary>
|
|
/// </summary>
|
|
private static readonly IPAddress www_baidu_com = IPAddress.Parse("183.232.231.172");
|
|
private static readonly IPAddress www_baidu_com = IPAddress.Parse("183.232.231.172");
|
|
|
|
|
|
- [DllImport("iphlpapi")]
|
|
|
|
- private static extern int GetBestInterface(uint dwDestAddr, ref uint pdwBestIfIndex);
|
|
|
|
-
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 刷新DNS缓存
|
|
/// 刷新DNS缓存
|
|
/// </summary>
|
|
/// </summary>
|
|
|
|
+ [SupportedOSPlatform("windows")]
|
|
[DllImport("dnsapi.dll", EntryPoint = "DnsFlushResolverCache", SetLastError = true)]
|
|
[DllImport("dnsapi.dll", EntryPoint = "DnsFlushResolverCache", SetLastError = true)]
|
|
- public static extern void DnsFlushResolverCache();
|
|
|
|
-
|
|
|
|
|
|
+ private static extern void DnsFlushResolverCache();
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
- /// 通过远程地址查找匹配的网络适接口
|
|
|
|
|
|
+ /// 刷新DNS缓存
|
|
/// </summary>
|
|
/// </summary>
|
|
- /// <param name="remoteAddress"></param>
|
|
|
|
- /// <returns></returns>
|
|
|
|
- private static NetworkInterface GetBestNetworkInterface(IPAddress remoteAddress)
|
|
|
|
|
|
+ public static void FlushResolverCache()
|
|
{
|
|
{
|
|
- var dwBestIfIndex = 0u;
|
|
|
|
- var dwDestAddr = BitConverter.ToUInt32(remoteAddress.GetAddressBytes());
|
|
|
|
- var errorCode = GetBestInterface(dwDestAddr, ref dwBestIfIndex);
|
|
|
|
- if (errorCode != 0)
|
|
|
|
|
|
+ if (OperatingSystem.IsWindows())
|
|
{
|
|
{
|
|
- throw new NetworkInformationException(errorCode);
|
|
|
|
|
|
+ DnsFlushResolverCache();
|
|
}
|
|
}
|
|
-
|
|
|
|
- var @interface = NetworkInterface
|
|
|
|
- .GetAllNetworkInterfaces()
|
|
|
|
- .Where(item => item.GetIPProperties().GetIPv4Properties().Index == dwBestIfIndex)
|
|
|
|
- .FirstOrDefault();
|
|
|
|
-
|
|
|
|
- return @interface ?? throw new FastGithubException("找不到网络适配器用来设置dns");
|
|
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 设置主dns
|
|
/// 设置主dns
|
|
/// </summary>
|
|
/// </summary>
|
|
/// <param name="primitive"></param>
|
|
/// <param name="primitive"></param>
|
|
- /// <exception cref="NetworkInformationException"></exception>
|
|
|
|
- /// <exception cref="NotSupportedException"></exception>
|
|
|
|
- public static void DnsSetPrimitive(IPAddress primitive)
|
|
|
|
|
|
+ /// <exception cref="FastGithubException"></exception>
|
|
|
|
+ public static void SetPrimitiveDns(IPAddress primitive)
|
|
{
|
|
{
|
|
- var @interface = GetBestNetworkInterface(www_baidu_com);
|
|
|
|
|
|
+ var @interface = GetOutboundNetworkInterface();
|
|
|
|
+ if (@interface == null)
|
|
|
|
+ {
|
|
|
|
+ throw new FastGithubException($"找不到匹配的网络适配器来设置主DNS值:{primitive}");
|
|
|
|
+ }
|
|
|
|
+
|
|
var dnsAddresses = @interface.GetIPProperties().DnsAddresses;
|
|
var dnsAddresses = @interface.GetIPProperties().DnsAddresses;
|
|
if (primitive.Equals(dnsAddresses.FirstOrDefault()) == false)
|
|
if (primitive.Equals(dnsAddresses.FirstOrDefault()) == false)
|
|
{
|
|
{
|
|
var nameServers = dnsAddresses.Prepend(primitive);
|
|
var nameServers = dnsAddresses.Prepend(primitive);
|
|
- SetNameServers(@interface, nameServers);
|
|
|
|
|
|
+ if (OperatingSystem.IsWindows())
|
|
|
|
+ {
|
|
|
|
+ SetNameServers(@interface, nameServers);
|
|
|
|
+ }
|
|
|
|
+ else if (OperatingSystem.IsLinux())
|
|
|
|
+ {
|
|
|
|
+ throw new FastGithubException($"不支持自动设置本机DNS,请手工添加{primitive}做为/etc/resolv.conf的第一条记录");
|
|
|
|
+ }
|
|
|
|
+ else if (OperatingSystem.IsMacOS())
|
|
|
|
+ {
|
|
|
|
+ throw new FastGithubException($"不支持自动设置本机DNS,请手工添加{primitive}做为连接网络的DNS的第一条记录");
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -76,24 +74,53 @@ namespace FastGithub.Dns
|
|
/// 移除主dns
|
|
/// 移除主dns
|
|
/// </summary>
|
|
/// </summary>
|
|
/// <param name="primitive"></param>
|
|
/// <param name="primitive"></param>
|
|
- /// <exception cref="NetworkInformationException"></exception>
|
|
|
|
- /// <exception cref="NotSupportedException"></exception>
|
|
|
|
- public static void DnsRemovePrimitive(IPAddress primitive)
|
|
|
|
|
|
+ /// <exception cref="FastGithubException"></exception>
|
|
|
|
+ public static void RemovePrimitiveDns(IPAddress primitive)
|
|
{
|
|
{
|
|
- var @interface = GetBestNetworkInterface(www_baidu_com);
|
|
|
|
|
|
+ var @interface = GetOutboundNetworkInterface();
|
|
|
|
+ if (@interface == null)
|
|
|
|
+ {
|
|
|
|
+ throw new FastGithubException($"找不到匹配的网络适配器来移除主DNS值:{primitive}");
|
|
|
|
+ }
|
|
|
|
+
|
|
var dnsAddresses = @interface.GetIPProperties().DnsAddresses;
|
|
var dnsAddresses = @interface.GetIPProperties().DnsAddresses;
|
|
if (primitive.Equals(dnsAddresses.FirstOrDefault()))
|
|
if (primitive.Equals(dnsAddresses.FirstOrDefault()))
|
|
{
|
|
{
|
|
var nameServers = dnsAddresses.Skip(1);
|
|
var nameServers = dnsAddresses.Skip(1);
|
|
- SetNameServers(@interface, nameServers);
|
|
|
|
|
|
+ if (OperatingSystem.IsWindows())
|
|
|
|
+ {
|
|
|
|
+ SetNameServers(@interface, nameServers);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 查找出口的网络适器
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ private static NetworkInterface? GetOutboundNetworkInterface()
|
|
|
|
+ {
|
|
|
|
+ var remoteEndPoint = new IPEndPoint(www_baidu_com, 443);
|
|
|
|
+ var address = LocalMachine.GetLocalIPAddress(remoteEndPoint);
|
|
|
|
+ if (address == null)
|
|
|
|
+ {
|
|
|
|
+ return default;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ return NetworkInterface
|
|
|
|
+ .GetAllNetworkInterfaces()
|
|
|
|
+ .Where(item => item.GetIPProperties().UnicastAddresses.Any(a => a.Address.Equals(address)))
|
|
|
|
+ .FirstOrDefault();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 设置网口的dns
|
|
/// 设置网口的dns
|
|
/// </summary>
|
|
/// </summary>
|
|
/// <param name="interface"></param>
|
|
/// <param name="interface"></param>
|
|
/// <param name="nameServers"></param>
|
|
/// <param name="nameServers"></param>
|
|
|
|
+ [SupportedOSPlatform("windows")]
|
|
private static void SetNameServers(NetworkInterface @interface, IEnumerable<IPAddress> nameServers)
|
|
private static void SetNameServers(NetworkInterface @interface, IEnumerable<IPAddress> nameServers)
|
|
{
|
|
{
|
|
Netsh($@"interface ipv4 delete dns ""{@interface.Name}"" all");
|
|
Netsh($@"interface ipv4 delete dns ""{@interface.Name}"" all");
|