private void ReloadResolver(DnsCryptProxyType dnsCryptProxyType)
 {
     if (dnsCryptProxyType == DnsCryptProxyType.Primary)
     {
         if (_primaryResolver != null)
         {
             PrimaryDnsCryptProxyManager.DnsCryptProxy.Parameter = ConvertProxyEntryToParameter(
                 _primaryResolver, DnsCryptProxyType.Primary);
             if (PrimaryDnsCryptProxyManager.WriteRegistry(DnsCryptProxyType.Primary))
             {
                 if (PrimaryDnsCryptProxyManager.IsDnsCryptProxyInstalled())
                 {
                     RestartService(DnsCryptProxyType.Primary);
                 }
             }
         }
     }
     else
     {
         if (_secondaryResolver != null)
         {
             SecondaryDnsCryptProxyManager.DnsCryptProxy.Parameter =
                 ConvertProxyEntryToParameter(_secondaryResolver, DnsCryptProxyType.Secondary);
             if (SecondaryDnsCryptProxyManager.WriteRegistry(DnsCryptProxyType.Secondary))
             {
                 if (SecondaryDnsCryptProxyManager.IsDnsCryptProxyInstalled())
                 {
                     RestartService(DnsCryptProxyType.Secondary);
                 }
             }
         }
     }
 }
        /// <summary>
        ///	Add quotation marks for existing installations.
        /// This fixes a vulnerability, where the dnscrypt-proxy
        /// path contains spaces.
        /// </summary>
        /// <remarks>Thanks to @rugk!</remarks>
        /// <param name="dnsCryptProxyType"></param>
        public void FixImagePath(DnsCryptProxyType dnsCryptProxyType)
        {
            var proxyName = Global.PrimaryResolverServiceName;

            if (dnsCryptProxyType != DnsCryptProxyType.Primary)
            {
                proxyName = Global.SecondaryResolverServiceName;
            }

            var localMachine = Registry.LocalMachine;
            var main         = localMachine.OpenSubKey(
                @"SYSTEM\\CurrentControlSet\\Services\\" + proxyName, true);

            if (main == null)
            {
                return;
            }
            var imagePath = string.Empty;

            foreach (var mainName in main.GetValueNames())
            {
                switch (mainName)
                {
                case "ImagePath":
                    imagePath = (string)main.GetValue(mainName);
                    break;
                }
            }

            if (string.IsNullOrEmpty(imagePath))
            {
                return;
            }

            if (!imagePath.StartsWith("\""))
            {
                imagePath = "\"" + imagePath;
            }

            if (!imagePath.EndsWith("\""))
            {
                imagePath = imagePath + "\"";
            }
            main.SetValue("ImagePath", imagePath, RegistryValueKind.ExpandString);
        }
        /// <summary>
        ///     Write the data to the registry.
        /// </summary>
        /// <param name="dnsCryptProxyType">Type of the proxy (primary or secondary)</param>
        /// <returns><c>true</c> on success, otherwise <c>false</c></returns>
        public bool WriteRegistry(DnsCryptProxyType dnsCryptProxyType)
        {
            try
            {
                var proxyName = Global.PrimaryResolverServiceName;
                var proxyPort = DnsCryptProxy.Parameter.LocalPort;
                if (dnsCryptProxyType != DnsCryptProxyType.Primary)
                {
                    proxyName = Global.SecondaryResolverServiceName;
                    proxyPort = Global.SecondaryResolverPort;
                }

                var localMachine = Registry.LocalMachine;
                var parameters   = localMachine.OpenSubKey(
                    @"SYSTEM\\CurrentControlSet\\Services\\" + proxyName + "\\Parameters", true);

                if (parameters == null)
                {
                    localMachine.CreateSubKey(@"SYSTEM\\CurrentControlSet\\Services\\" + proxyName + "\\Parameters");
                    parameters = localMachine.OpenSubKey(
                        @"SYSTEM\\CurrentControlSet\\Services\\" + proxyName + "\\Parameters", true);
                }

                parameters.SetValue("ResolverName", DnsCryptProxy.Parameter.ResolverName, RegistryValueKind.String);
                parameters.SetValue("Plugins", DnsCryptProxy.Parameter.Plugins, RegistryValueKind.MultiString);
                parameters.SetValue("LocalAddress", DnsCryptProxy.Parameter.LocalAddress + ":" + proxyPort,
                                    RegistryValueKind.String);
                parameters.SetValue("ProviderKey", DnsCryptProxy.Parameter.ProviderKey, RegistryValueKind.String);
                parameters.SetValue("ResolversList", DnsCryptProxy.Parameter.ResolversList, RegistryValueKind.String);
                parameters.SetValue("ProviderName", DnsCryptProxy.Parameter.ProviderName, RegistryValueKind.String);
                parameters.SetValue("ResolverAddress", DnsCryptProxy.Parameter.ResolverAddress, RegistryValueKind.String);
                parameters.SetValue("EphemeralKeys", Convert.ToInt32(DnsCryptProxy.Parameter.EphemeralKeys),
                                    RegistryValueKind.DWord);
                parameters.SetValue("TCPOnly", Convert.ToInt32(DnsCryptProxy.Parameter.TcpOnly), RegistryValueKind.DWord);
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
        /// <summary>
        ///	Add quotation marks for existing installations.
        /// This fixes a vulnerability, where the dnscrypt-proxy 
        /// path contains spaces.
        /// </summary>
        /// <remarks>Thanks to @rugk!</remarks>
        /// <param name="dnsCryptProxyType"></param>
        public void FixImagePath(DnsCryptProxyType dnsCryptProxyType)
        {
            var proxyName = Global.PrimaryResolverServiceName;
            if (dnsCryptProxyType != DnsCryptProxyType.Primary)
            {
                proxyName = Global.SecondaryResolverServiceName;
            }

            var localMachine = Registry.LocalMachine;
            var main = localMachine.OpenSubKey(
                @"SYSTEM\\CurrentControlSet\\Services\\" + proxyName, true);

            if (main == null)
            {
                return;
            }
            var imagePath = string.Empty;
            foreach (var mainName in main.GetValueNames())
            {
                switch (mainName)
                {
                    case "ImagePath":
                        imagePath = (string)main.GetValue(mainName);
                        break;
                }
            }

            if (string.IsNullOrEmpty(imagePath)) return;

            if (!imagePath.StartsWith("\""))
            {
                imagePath = "\"" + imagePath;
            }

            if (!imagePath.EndsWith("\""))
            {
                imagePath = imagePath + "\"";
            }
            main.SetValue("ImagePath", imagePath, RegistryValueKind.ExpandString);
        }
        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 DnsCryptProxyParameter ConvertProxyEntryToParameter(DnsCryptProxyEntry dnsCryptProxyEntry,
                                                                    DnsCryptProxyType dnsCryptProxyType)
        {
            var dnsCryptProxyParameter = new DnsCryptProxyParameter
            {
                ProviderKey     = dnsCryptProxyEntry.ProviderPublicKey,
                Plugins         = Plugins.ToArray(),
                ProviderName    = dnsCryptProxyEntry.ProviderName,
                ResolverAddress = dnsCryptProxyEntry.ResolverAddress,
                ResolverName    = dnsCryptProxyEntry.Name,
                LocalPort       = dnsCryptProxyEntry.LocalPort,
                ResolversList   =
                    Path.Combine(Directory.GetCurrentDirectory(), Global.DnsCryptProxyFolder,
                                 Global.DnsCryptProxyResolverListName),
                EphemeralKeys = true,
                TcpOnly       = UseTcpOnly
            };

            if (dnsCryptProxyType == DnsCryptProxyType.Primary)
            {
                if (ActAsGlobalGateway)
                {
                    dnsCryptProxyParameter.LocalAddress = Global.GlobalGatewayAddress;
                }
                else
                {
                    dnsCryptProxyParameter.LocalAddress = Global.PrimaryResolverAddress;
                }
            }
            else
            {
                dnsCryptProxyParameter.LocalAddress = Global.SecondaryResolverAddress;
            }

            return(dnsCryptProxyParameter);
        }
 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;
     }
 }
 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 DnsCryptProxyParameter ConvertProxyEntryToParameter(DnsCryptProxyEntry dnsCryptProxyEntry,
            DnsCryptProxyType dnsCryptProxyType)
        {
            var dnsCryptProxyParameter = new DnsCryptProxyParameter
            {
                ProviderKey = dnsCryptProxyEntry.ProviderPublicKey,
                Plugins = Plugins.ToArray(),
                ProviderName = dnsCryptProxyEntry.ProviderName,
                ResolverAddress = dnsCryptProxyEntry.ResolverAddress,
                ResolverName = dnsCryptProxyEntry.Name,
				LocalPort = dnsCryptProxyEntry.LocalPort,
                ResolversList =
                    Path.Combine(Directory.GetCurrentDirectory(), Global.DnsCryptProxyFolder,
                        Global.DnsCryptProxyResolverListName),
                EphemeralKeys = true,
                TcpOnly = UseTcpOnly
            };

            if (dnsCryptProxyType == DnsCryptProxyType.Primary)
            {
                if (ActAsGlobalGateway)
                {
                    dnsCryptProxyParameter.LocalAddress = Global.GlobalGatewayAddress;
                }
                else
                {
                    dnsCryptProxyParameter.LocalAddress = Global.PrimaryResolverAddress;
                }
            }
            else
            {
                dnsCryptProxyParameter.LocalAddress = Global.SecondaryResolverAddress;
            }

            return dnsCryptProxyParameter;
        }
		private void ReloadResolver(DnsCryptProxyType dnsCryptProxyType)
        {
            if (dnsCryptProxyType == DnsCryptProxyType.Primary)
            {
                if (_primaryResolver != null)
                {
                    PrimaryDnsCryptProxyManager.DnsCryptProxy.Parameter = ConvertProxyEntryToParameter(
                        _primaryResolver, DnsCryptProxyType.Primary);
                    if (PrimaryDnsCryptProxyManager.WriteRegistry(DnsCryptProxyType.Primary))
                    {
                        if (PrimaryDnsCryptProxyManager.IsDnsCryptProxyInstalled())
                        {
                            RestartService(DnsCryptProxyType.Primary);
                        }
                    }
                }
            }
            else
            {
                if (_secondaryResolver != null)
                {
                    SecondaryDnsCryptProxyManager.DnsCryptProxy.Parameter =
                        ConvertProxyEntryToParameter(_secondaryResolver, DnsCryptProxyType.Secondary);
                    if (SecondaryDnsCryptProxyManager.WriteRegistry(DnsCryptProxyType.Secondary))
                    {
                        if (SecondaryDnsCryptProxyManager.IsDnsCryptProxyInstalled())
                        {
                            RestartService(DnsCryptProxyType.Secondary);
                        }
                    }
                }
            }
        }
Beispiel #11
0
        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);

                        if (IsDebugModeEnabled)
                        {
                            if (installResult.Success)
                            {
                                _windowManager.ShowMetroMessageBox(
                                    installResult.StandardOutput,
                                    LocalizationEx.GetUiString("dialog_success_title",
                                        Thread.CurrentThread.CurrentCulture),
                                    MessageBoxButton.OK, BoxType.Default);
                            }
                            else
                            {
                                _windowManager.ShowMetroMessageBox(
                                    installResult.StandardOutput,
                                    LocalizationEx.GetUiString("dialog_error_title", Thread.CurrentThread.CurrentCulture),
                                    MessageBoxButton.OK, BoxType.Error);
                            }
                        }
                    }
                }
                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);

                        if (IsDebugModeEnabled)
                        {
                            if (installResult.Success)
                            {
                                _windowManager.ShowMetroMessageBox(
                                    installResult.StandardOutput,
                                    LocalizationEx.GetUiString("dialog_success_title",
                                        Thread.CurrentThread.CurrentCulture),
                                    MessageBoxButton.OK, BoxType.Default);
                            }
                            else
                            {
                                _windowManager.ShowMetroMessageBox(
                                    installResult.StandardOutput,
                                    LocalizationEx.GetUiString("dialog_error_title", Thread.CurrentThread.CurrentCulture),
                                    MessageBoxButton.OK, BoxType.Error);
                            }
                        }
                    }
                }
                IsWorkingOnSecondaryService = false;
            }
        }
 /// <summary>
 ///     Initialize a new DnsCryptProxyManager instance.
 /// </summary>
 /// <param name="dnsCryptProxyType"></param>
 public DnsCryptProxyManager(DnsCryptProxyType dnsCryptProxyType = DnsCryptProxyType.Primary)
 {
     DnsCryptProxy = new DnsCryptProxy(dnsCryptProxyType);
     ReadRegistry(dnsCryptProxyType);
 }
 /// <summary>
 ///     Initialize a new DnsCryptProxyManager instance.
 /// </summary>
 /// <param name="dnsCryptProxyType"></param>
 public DnsCryptProxyManager(DnsCryptProxyType dnsCryptProxyType = DnsCryptProxyType.Primary)
 {
     DnsCryptProxy = new DnsCryptProxy(dnsCryptProxyType);
     ReadRegistry(dnsCryptProxyType);
 }
        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;
            }
        }
        /// <summary>
        ///     Read the current registry values.
        /// </summary>
        /// <param name="dnsCryptProxyType">Type of the proxy (primary or secondary)</param>
        public void ReadRegistry(DnsCryptProxyType dnsCryptProxyType)
        {
            try
            {
                FixImagePath(dnsCryptProxyType);
                var proxyName = Global.PrimaryResolverServiceName;
                if (dnsCryptProxyType != DnsCryptProxyType.Primary)
                {
                    proxyName = Global.SecondaryResolverServiceName;
                }

                var localMachine = Registry.LocalMachine;
                var main         = localMachine.OpenSubKey(
                    @"SYSTEM\\CurrentControlSet\\Services\\" + proxyName, false);

                if (main == null)
                {
                    DnsCryptProxy.IsReady = false;
                    return;
                }

                foreach (var mainName in main.GetValueNames())
                {
                    switch (mainName)
                    {
                    case "DisplayName":
                        DnsCryptProxy.DisplayName = (string)main.GetValue(mainName);
                        break;

                    case "ImagePath":
                        DnsCryptProxy.ImagePath = (string)main.GetValue(mainName);
                        break;
                    }
                }
                var parameters = localMachine.OpenSubKey(
                    @"SYSTEM\\CurrentControlSet\\Services\\" + proxyName + "\\Parameters", false);
                foreach (var parameterName in parameters.GetValueNames())
                {
                    switch (parameterName)
                    {
                    case "EphemeralKeys":
                        DnsCryptProxy.Parameter.EphemeralKeys = Convert.ToBoolean(parameters.GetValue(parameterName));
                        break;

                    case "LocalAddress":
                        // the LocalAddress is stored in the following format: 127.0.0.1:53
                        var mergedAddress = ((string)parameters.GetValue(parameterName)).Split(':');
                        DnsCryptProxy.Parameter.LocalAddress = mergedAddress[0];
                        DnsCryptProxy.Parameter.LocalPort    = Convert.ToInt32(mergedAddress[1]);
                        break;

                    case "Plugins":
                        DnsCryptProxy.Parameter.Plugins = (string[])parameters.GetValue(parameterName);
                        break;

                    case "ProviderName":
                        DnsCryptProxy.Parameter.ProviderName = (string)parameters.GetValue(parameterName);
                        break;

                    case "ProviderKey":
                        DnsCryptProxy.Parameter.ProviderKey = (string)parameters.GetValue(parameterName);
                        break;

                    case "ResolversList":
                        DnsCryptProxy.Parameter.ResolversList = (string)parameters.GetValue(parameterName);
                        break;

                    case "ResolverAddress":
                        DnsCryptProxy.Parameter.ResolverAddress = (string)parameters.GetValue(parameterName);
                        break;

                    case "ResolverName":
                        DnsCryptProxy.Parameter.ResolverName = (string)parameters.GetValue(parameterName);
                        break;

                    case "TCPOnly":
                        DnsCryptProxy.Parameter.TcpOnly = Convert.ToBoolean(parameters.GetValue(parameterName));
                        break;

                    case "EDNSPayloadSize":
                        // not yet supported, will be ignored
                        break;

                    case "MaxActiveRequests":
                        // not yet supported, will be ignored
                        break;

                    case "ClientKeyFile":
                        // not yet supported, will be ignored
                        break;
                    }
                }
                DnsCryptProxy.IsReady = true;
            }
            catch (Exception)
            {
                DnsCryptProxy.IsReady = false;
            }
        }
        /// <summary>
        ///     Read the current registry values.
        /// </summary>
        /// <param name="dnsCryptProxyType">Type of the proxy (primary or secondary)</param>
        public void ReadRegistry(DnsCryptProxyType dnsCryptProxyType)
        {
            try
            {
                var proxyName = Global.PrimaryResolverServiceName;
                if (dnsCryptProxyType != DnsCryptProxyType.Primary)
                {
                    proxyName = Global.SecondaryResolverServiceName;
                }

                var localMachine = Registry.LocalMachine;
                var main = localMachine.OpenSubKey(
                    @"SYSTEM\\CurrentControlSet\\Services\\" + proxyName, false);

                if (main == null)
                {
                    DnsCryptProxy.IsReady = false;
                    return;
                }

                foreach (var mainName in main.GetValueNames())
                {
                    switch (mainName)
                    {
                        case "DisplayName":
                            DnsCryptProxy.DisplayName = (string) main.GetValue(mainName);
                            break;
                        case "ImagePath":
                            DnsCryptProxy.ImagePath = (string) main.GetValue(mainName);
                            break;
                    }
                }
                var parameters = localMachine.OpenSubKey(
                    @"SYSTEM\\CurrentControlSet\\Services\\" + proxyName + "\\Parameters", false);
                foreach (var parameterName in parameters.GetValueNames())
                {
                    switch (parameterName)
                    {
                        case "EphemeralKeys":
                            DnsCryptProxy.Parameter.EphemeralKeys = Convert.ToBoolean(parameters.GetValue(parameterName));
                            break;
                        case "LocalAddress":
                            // the LocalAddress is stored in the following format: 127.0.0.1:53
                            var mergedAddress = ((string) parameters.GetValue(parameterName)).Split(':');
                            DnsCryptProxy.Parameter.LocalAddress = mergedAddress[0];
                            DnsCryptProxy.Parameter.LocalPort = Convert.ToInt32(mergedAddress[1]);
                            break;
                        case "Plugins":
                            DnsCryptProxy.Parameter.Plugins = (string[]) parameters.GetValue(parameterName);
                            break;
                        case "ProviderName":
                            DnsCryptProxy.Parameter.ProviderName = (string) parameters.GetValue(parameterName);
                            break;
                        case "ProviderKey":
                            DnsCryptProxy.Parameter.ProviderKey = (string) parameters.GetValue(parameterName);
                            break;
                        case "ResolversList":
                            DnsCryptProxy.Parameter.ResolversList = (string) parameters.GetValue(parameterName);
                            break;
                        case "ResolverAddress":
                            DnsCryptProxy.Parameter.ResolverAddress = (string) parameters.GetValue(parameterName);
                            break;
                        case "ResolverName":
                            DnsCryptProxy.Parameter.ResolverName = (string) parameters.GetValue(parameterName);
                            break;
                        case "TCPOnly":
                            DnsCryptProxy.Parameter.TcpOnly = Convert.ToBoolean(parameters.GetValue(parameterName));
                            break;
                        case "EDNSPayloadSize":
                            // not yet supported, will be ignored
                            break;
                        case "MaxActiveRequests":
                            // not yet supported, will be ignored
                            break;
                        case "ClientKeyFile":
                            // not yet supported, will be ignored
                            break;
                    }
                }
                DnsCryptProxy.IsReady = true;
            }
            catch (Exception)
            {
                DnsCryptProxy.IsReady = false;
            }
        }
Beispiel #17
0
 /// <summary>
 ///     Initialize a new DnsCryptProxy instance.
 /// </summary>
 /// <param name="dnsCryptProxyType"></param>
 public DnsCryptProxy(DnsCryptProxyType dnsCryptProxyType)
 {
     Type      = dnsCryptProxyType;
     IsReady   = false;
     Parameter = new DnsCryptProxyParameter();
 }
        /// <summary>
        ///     Write the data to the registry.
        /// </summary>
        /// <param name="dnsCryptProxyType">Type of the proxy (primary or secondary)</param>
        /// <returns><c>true</c> on success, otherwise <c>false</c></returns>
        public bool WriteRegistry(DnsCryptProxyType dnsCryptProxyType)
        {
            try
            {
                var proxyName = Global.PrimaryResolverServiceName;
                var proxyPort = DnsCryptProxy.Parameter.LocalPort;
                if (dnsCryptProxyType != DnsCryptProxyType.Primary)
                {
                    proxyName = Global.SecondaryResolverServiceName;
                    proxyPort = Global.SecondaryResolverPort;
                }

                var localMachine = Registry.LocalMachine;
                var parameters = localMachine.OpenSubKey(
                    @"SYSTEM\\CurrentControlSet\\Services\\" + proxyName + "\\Parameters", true);

                if (parameters == null)
                {
                    localMachine.CreateSubKey(@"SYSTEM\\CurrentControlSet\\Services\\" + proxyName + "\\Parameters");
                    parameters = localMachine.OpenSubKey(
                        @"SYSTEM\\CurrentControlSet\\Services\\" + proxyName + "\\Parameters", true);
                }

                parameters.SetValue("ResolverName", DnsCryptProxy.Parameter.ResolverName, RegistryValueKind.String);
                parameters.SetValue("Plugins", DnsCryptProxy.Parameter.Plugins, RegistryValueKind.MultiString);
                parameters.SetValue("LocalAddress", DnsCryptProxy.Parameter.LocalAddress + ":" + proxyPort,
                    RegistryValueKind.String);
                parameters.SetValue("ProviderKey", DnsCryptProxy.Parameter.ProviderKey, RegistryValueKind.String);
                parameters.SetValue("ResolversList", DnsCryptProxy.Parameter.ResolversList, RegistryValueKind.String);
                parameters.SetValue("ProviderName", DnsCryptProxy.Parameter.ProviderName, RegistryValueKind.String);
                parameters.SetValue("ResolverAddress", DnsCryptProxy.Parameter.ResolverAddress, RegistryValueKind.String);
                parameters.SetValue("EphemeralKeys", Convert.ToInt32(DnsCryptProxy.Parameter.EphemeralKeys),
                    RegistryValueKind.DWord);
                parameters.SetValue("TCPOnly", Convert.ToInt32(DnsCryptProxy.Parameter.TcpOnly), RegistryValueKind.DWord);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
 /// <summary>
 ///     Initialize a new DnsCryptProxy instance.
 /// </summary>
 /// <param name="dnsCryptProxyType"></param>
 public DnsCryptProxy(DnsCryptProxyType dnsCryptProxyType)
 {
     Type = dnsCryptProxyType;
     IsReady = false;
     Parameter = new DnsCryptProxyParameter();
 }