using System;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
namespace FastGithub.Dns
{
    /// 
    /// 系统域名服务工具
    /// 
    [SupportedOSPlatform("windows")]
    static class SystemDnsUtil
    {
        /// 
        /// www.baidu.com的ip
        /// 
        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);
        /// 
        /// 通过远程地址查找匹配的网络适接口
        /// 
        /// 
        /// 
        private static NetworkInterface? GetBestNetworkInterface(IPAddress remoteAddress)
        {
            var dwBestIfIndex = 0u;
            var dwDestAddr = BitConverter.ToUInt32(remoteAddress.GetAddressBytes());
            var errorCode = GetBestInterface(dwDestAddr, ref dwBestIfIndex);
            return errorCode != 0
                ? throw new NetworkInformationException(errorCode)
                : NetworkInterface
                .GetAllNetworkInterfaces()
                .Where(item => item.GetIPProperties().GetIPv4Properties().Index == dwBestIfIndex)
                .FirstOrDefault();
        }
        /// 
        /// 设置域名服务
        /// 
        /// 
        /// 
        /// 
        /// 未设置之前的记录
        public static IPAddress[] SetNameServers(params IPAddress[] nameServers)
        {
            var networkInterface = GetBestNetworkInterface(www_baidu_com);
            if (networkInterface == null)
            {
                throw new NotSupportedException("找不到网络适配器用来设置dns");
            }
            var dnsAddresses = networkInterface.GetIPProperties().DnsAddresses.ToArray();
            Netsh($@"interface ipv4 delete dns ""{networkInterface.Name}"" all");
            foreach (var address in nameServers)
            {
                Netsh($@"interface ipv4 add dns ""{networkInterface.Name}"" {address} validate=no");
            }
            return dnsAddresses;
        }
        /// 
        /// 执行Netsh
        /// 
        /// 
        private 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();
        }
    }
}