using System.Net;

namespace DahuaIPCRename
{
    internal class Program
    {
        public static Dictionary<string, string> listIPC = new Dictionary<string, string>
        {
            {"172.16.54.18","Ворота 35"},
            {"172.16.54.20","Ворота 36"},
            {"172.16.54.21","Ворота 31"},
            {"172.16.54.22","Ворота 32"},
            {"172.16.54.23","Ворота 33"},
            {"172.16.54.24","Ворота 34"},
            {"172.16.54.28","Ворота 16"},
            {"172.16.54.29","Ворота 26"},
            {"172.16.54.30","Ворота 20"},
            {"172.16.54.31","Ворота 22"},
            {"172.16.54.32","Ворота 18"},
            {"172.16.54.33","Ворота 21"},
            {"172.16.54.34","Ворота 24"},
            {"172.16.54.35","Ворота 27"},
            {"172.16.54.36","Ворота 29"},
            {"172.16.54.37","Ворота 25"},
            {"172.16.54.38","Ворота 23"},
            {"172.16.54.39","Ворота 15"},
            {"172.16.54.40","Ворота 17"},
            {"172.16.54.41","Ворота 37"},
            {"172.16.54.42","Ворота 19"},
            {"172.16.54.43","Ворота 28"}
        };

        static async Task Main(string[] args)
        {
            foreach (var ipc in listIPC)
            {
                string baseUrl = $"http://{ipc.Key}";
                var credCache = new CredentialCache();
                credCache.Add(new Uri(baseUrl), "Digest", new NetworkCredential("admin", "4NUDZhJ7"));
                using (var handler = new HttpClientHandler
                {
                    Credentials = credCache,
                    ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true
                })
                using (var httpClient = new HttpClient(handler))
                {
                    string requestUrl = $"{baseUrl}/cgi-bin/configManager.cgi?action=setConfig&ChannelTitle[0].Name={ipc.Value}";
                    var response = await httpClient.GetAsync(requestUrl);
                    try
                    {
                        response.EnsureSuccessStatusCode();
                        Console.WriteLine($"{ipc.Key}\tOK\t{ipc.Value}");
                    }
                    catch (Exception)
                    {
                        Console.WriteLine($"{ipc.Key}\tNO\t{ipc.Value}");
                        return;
                    }
                }
            }
            Console.ReadKey();
        }
    }
}
