private async void HandleGlobalResolver(bool actAsGlobalGateway)
        {
            IsWorkingOnPrimaryService = true;
            if (actAsGlobalGateway)
            {
                PrimaryDnsCryptProxyManager.DnsCryptProxy.Parameter.LocalAddress = Global.GlobalGatewayAddress;
                PrimaryDnsCryptProxyManager.DnsCryptProxy.Parameter.LocalPort    = Global.PrimaryResolverPort;
                PrimaryDnsCryptProxyManager.WriteRegistry(DnsCryptProxyType.Primary);
                await Task.Run(() => { PrimaryDnsCryptProxyManager.Restart(); }).ConfigureAwait(false);

                Thread.Sleep(Global.ServiceRestartTime);
                _isPrimaryResolverRunning = PrimaryDnsCryptProxyManager.IsDnsCryptProxyRunning();
                NotifyOfPropertyChange(() => IsPrimaryResolverRunning);
                PrimaryResolverTitle = string.Format("{0} ({1}:{2})",
                                                     LocalizationEx.GetUiString("default_settings_primary_header", Thread.CurrentThread.CurrentCulture),
                                                     Global.GlobalGatewayAddress, Global.PrimaryResolverPort);
            }
            else
            {
                PrimaryDnsCryptProxyManager.DnsCryptProxy.Parameter.LocalAddress = Global.PrimaryResolverAddress;
                PrimaryDnsCryptProxyManager.DnsCryptProxy.Parameter.LocalPort    = Global.PrimaryResolverPort;
                PrimaryDnsCryptProxyManager.WriteRegistry(DnsCryptProxyType.Primary);
                await Task.Run(() => { PrimaryDnsCryptProxyManager.Restart(); }).ConfigureAwait(false);

                Thread.Sleep(Global.ServiceRestartTime);
                _isPrimaryResolverRunning = PrimaryDnsCryptProxyManager.IsDnsCryptProxyRunning();
                NotifyOfPropertyChange(() => IsPrimaryResolverRunning);
                PrimaryResolver.LocalPort = Global.PrimaryResolverPort;                 // reset the resolver port
                PrimaryResolverTitle      = string.Format("{0}",
                                                          LocalizationEx.GetUiString("default_settings_primary_header", Thread.CurrentThread.CurrentCulture));
            }
            IsWorkingOnPrimaryService = false;
        }
 /// <summary>
 ///     Click event for the network cards.
 /// </summary>
 /// <param name="localNetworkInterface">The clicked network card.</param>
 public void NetworkCardClicked(LocalNetworkInterface localNetworkInterface)
 {
     if (localNetworkInterface == null)
     {
         return;
     }
     if (localNetworkInterface.UseDnsCrypt)
     {
         var status = LocalNetworkInterfaceManager.SetNameservers(localNetworkInterface, new List <string>(),
                                                                  NetworkInterfaceComponent.IPv4);
         LocalNetworkInterfaceManager.SetNameservers(localNetworkInterface, new List <string>(),
                                                     NetworkInterfaceComponent.IPv6);
         localNetworkInterface.UseDnsCrypt = !status;
     }
     else
     {
         var dns4 = new List <string>();
         var dns6 = new List <string>();
         if (PrimaryResolver != null)
         {
             if (!string.IsNullOrEmpty(PrimaryResolver.ProviderPublicKey))
             {
                 // only add the local address if the proxy is running
                 if (PrimaryDnsCryptProxyManager.DnsCryptProxy.IsReady && PrimaryDnsCryptProxyManager.IsDnsCryptProxyRunning())
                 {
                     dns4.Add(Global.PrimaryResolverAddress);
                     if (_userData.UseIpv6)
                     {
                         dns6.Add(Global.PrimaryResolverAddress6);
                     }
                 }
             }
         }
         if (SecondaryResolver != null)
         {
             if (!string.IsNullOrEmpty(SecondaryResolver.ProviderPublicKey))
             {
                 // only add the local address if the proxy is running
                 if (SecondaryDnsCryptProxyManager.DnsCryptProxy.IsReady && SecondaryDnsCryptProxyManager.IsDnsCryptProxyRunning())
                 {
                     dns4.Add(Global.SecondaryResolverAddress);
                     if (_userData.UseIpv6)
                     {
                         dns6.Add(Global.SecondaryResolverAddress6);
                     }
                 }
             }
         }
         var status = LocalNetworkInterfaceManager.SetNameservers(localNetworkInterface, dns4, NetworkInterfaceComponent.IPv4);
         if (_userData.UseIpv6)
         {
             LocalNetworkInterfaceManager.SetNameservers(localNetworkInterface, dns6, NetworkInterfaceComponent.IPv6);
         }
         localNetworkInterface.UseDnsCrypt = status;
     }
     LoadNetworkCards();
 }
        /// <summary>
        ///     Uninstall all installed dnscrypt-proxy services.
        /// </summary>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        /// <exception cref="NetworkInformationException"></exception>
        /// <exception cref="ArgumentNullException"></exception>
        public async void UninstallServices()
        {
            var result = _windowManager.ShowMetroMessageBox(
                LocalizationEx.GetUiString("dialog_message_uninstall", Thread.CurrentThread.CurrentCulture),
                LocalizationEx.GetUiString("dialog_uninstall_title", Thread.CurrentThread.CurrentCulture),
                MessageBoxButton.YesNo, BoxType.Default);

            if (result == MessageBoxResult.Yes)
            {
                IsUninstallingServices = true;
                await Task.Run(() =>
                {
                    PrimaryDnsCryptProxyManager.Uninstall();
                    SecondaryDnsCryptProxyManager.Uninstall();
                }).ConfigureAwait(false);

                Thread.Sleep(Global.ServiceUninstallTime);
                IsUninstallingServices = false;
            }

            _isPrimaryResolverRunning = PrimaryDnsCryptProxyManager.IsDnsCryptProxyRunning();
            NotifyOfPropertyChange(() => IsPrimaryResolverRunning);
            _isSecondaryResolverRunning = SecondaryDnsCryptProxyManager.IsDnsCryptProxyRunning();
            NotifyOfPropertyChange(() => IsSecondaryResolverRunning);

            // recover the network interfaces (also the hidden and down cards)
            foreach (var nic in LocalNetworkInterfaceManager.GetLocalNetworkInterfaces(true, false))
            {
                if (!nic.UseDnsCrypt)
                {
                    continue;
                }
                var status = LocalNetworkInterfaceManager.SetNameservers(nic, new List <string>(), NetworkInterfaceComponent.IPv4);
                var card   = _localNetworkInterfaces.SingleOrDefault(n => n.Description.Equals(nic.Description));
                if (card != null)
                {
                    card.UseDnsCrypt = !status;
                }
            }
        }
        private async void RestartService(DnsCryptProxyType dnsCryptProxyType)
        {
            if (dnsCryptProxyType == DnsCryptProxyType.Primary)
            {
                IsWorkingOnPrimaryService = true;
                await Task.Run(() => { PrimaryDnsCryptProxyManager.Restart(); }).ConfigureAwait(false);

                Thread.Sleep(Global.ServiceRestartTime);
                _isPrimaryResolverRunning = PrimaryDnsCryptProxyManager.IsDnsCryptProxyRunning();
                NotifyOfPropertyChange(() => IsPrimaryResolverRunning);
                IsWorkingOnPrimaryService = false;
            }
            else
            {
                IsWorkingOnSecondaryService = true;
                await Task.Run(() => { SecondaryDnsCryptProxyManager.Restart(); }).ConfigureAwait(false);

                Thread.Sleep(Global.ServiceRestartTime);
                _isSecondaryResolverRunning = SecondaryDnsCryptProxyManager.IsDnsCryptProxyRunning();
                NotifyOfPropertyChange(() => IsSecondaryResolverRunning);
                IsWorkingOnSecondaryService = false;
            }
        }
        private async void HandleService(DnsCryptProxyType dnsCryptProxyType)
        {
            if (dnsCryptProxyType == DnsCryptProxyType.Primary)
            {
                IsWorkingOnPrimaryService = true;
                if (IsPrimaryResolverRunning)
                {
                    // service is running, stop it
                    await Task.Run(() => { PrimaryDnsCryptProxyManager.Stop(); }).ConfigureAwait(false);

                    Thread.Sleep(Global.ServiceStopTime);
                    _isPrimaryResolverRunning = PrimaryDnsCryptProxyManager.IsDnsCryptProxyRunning();
                    NotifyOfPropertyChange(() => IsPrimaryResolverRunning);
                }
                else
                {
                    if (PrimaryDnsCryptProxyManager.IsDnsCryptProxyInstalled())
                    {
                        // service is installed, just start them
                        await Task.Run(() => { PrimaryDnsCryptProxyManager.Start(); }).ConfigureAwait(false);

                        Thread.Sleep(Global.ServiceStartTime);
                        _isPrimaryResolverRunning = PrimaryDnsCryptProxyManager.IsDnsCryptProxyRunning();
                        NotifyOfPropertyChange(() => IsPrimaryResolverRunning);
                    }
                    else
                    {
                        //install and start the service
                        var installResult =
                            await
                            Task.Run(() => { return(PrimaryDnsCryptProxyManager.Install()); }).ConfigureAwait(false);

                        Thread.Sleep(Global.ServiceStartTime);
                        _isPrimaryResolverRunning = PrimaryDnsCryptProxyManager.IsDnsCryptProxyRunning();
                        NotifyOfPropertyChange(() => IsPrimaryResolverRunning);
                    }
                }
                IsWorkingOnPrimaryService = false;
            }
            else
            {
                IsWorkingOnSecondaryService = true;
                if (IsSecondaryResolverRunning)
                {
                    // service is running, stop it
                    await Task.Run(() => { SecondaryDnsCryptProxyManager.Stop(); }).ConfigureAwait(false);

                    Thread.Sleep(Global.ServiceStopTime);
                    _isSecondaryResolverRunning = SecondaryDnsCryptProxyManager.IsDnsCryptProxyRunning();
                    NotifyOfPropertyChange(() => IsSecondaryResolverRunning);
                }
                else
                {
                    if (SecondaryDnsCryptProxyManager.IsDnsCryptProxyInstalled())
                    {
                        // service is installed, just start them
                        await Task.Run(() => { SecondaryDnsCryptProxyManager.Start(); }).ConfigureAwait(false);

                        Thread.Sleep(Global.ServiceStartTime);
                        _isSecondaryResolverRunning = SecondaryDnsCryptProxyManager.IsDnsCryptProxyRunning();
                        NotifyOfPropertyChange(() => IsSecondaryResolverRunning);
                    }
                    else
                    {
                        //install and start the service
                        var installResult =
                            await
                            Task.Run(() => { return(SecondaryDnsCryptProxyManager.Install()); })
                            .ConfigureAwait(false);

                        Thread.Sleep(Global.ServiceStartTime);
                        _isSecondaryResolverRunning = SecondaryDnsCryptProxyManager.IsDnsCryptProxyRunning();
                        NotifyOfPropertyChange(() => IsSecondaryResolverRunning);
                    }
                }
                IsWorkingOnSecondaryService = false;
            }
        }