Пример #1
0
        public static bool Patch()
        {
            var version = VersionHelper.PublishVersion;

            if (!DnscryptProxyConfigurationManager.LoadConfiguration())
            {
                return(false);
            }
            if (version.Equals("0.6.5"))
            {
                //added: netprobe_address = '255.255.255.0:53'
                //changed: netprobe_timeout = 0
                DnscryptProxyConfigurationManager.DnscryptProxyConfiguration.netprobe_address = "255.255.255.0:53";
                DnscryptProxyConfigurationManager.DnscryptProxyConfiguration.netprobe_timeout = 0;
                return(DnscryptProxyConfigurationManager.SaveConfiguration());
            }
            if (version.Equals("0.6.6"))
            {
                //changed: netprobe_address = '9.9.9.9:53'
                //changed: netprobe_timeout = 60
                DnscryptProxyConfigurationManager.DnscryptProxyConfiguration.netprobe_address = "9.9.9.9:53";
                DnscryptProxyConfigurationManager.DnscryptProxyConfiguration.netprobe_timeout = 60;
                return(DnscryptProxyConfigurationManager.SaveConfiguration());
            }
            return(false);
        }
Пример #2
0
        public static bool Patch()
        {
            var version = VersionHelper.PublishVersion;

            if (!DnscryptProxyConfigurationManager.LoadConfiguration())
            {
                return(false);
            }
            if (version.Equals("0.6.5"))
            {
                //added: netprobe_address = '255.255.255.0:53'
                //changed: netprobe_timeout = 0
                DnscryptProxyConfigurationManager.DnscryptProxyConfiguration.netprobe_address = "255.255.255.0:53";
                DnscryptProxyConfigurationManager.DnscryptProxyConfiguration.netprobe_timeout = 0;
                return(DnscryptProxyConfigurationManager.SaveConfiguration());
            }
            if (version.Equals("0.6.6"))
            {
                //changed: netprobe_address = '9.9.9.9:53'
                //changed: netprobe_timeout = 60
                DnscryptProxyConfigurationManager.DnscryptProxyConfiguration.netprobe_address = "9.9.9.9:53";
                DnscryptProxyConfigurationManager.DnscryptProxyConfiguration.netprobe_timeout = 60;
                return(DnscryptProxyConfigurationManager.SaveConfiguration());
            }
            if (version.Equals("0.6.8") || version.Equals("0.6.9"))
            {
                //changed: timeout = 5000
                //added: reject_ttl = 600
                //changed: cache_size = 1024
                //changed: cache_min_ttl = 2400
                //added: cache_neg_min_ttl = 60
                //added: cache_neg_max_ttl = 600
                DnscryptProxyConfigurationManager.DnscryptProxyConfiguration.timeout           = 5000;
                DnscryptProxyConfigurationManager.DnscryptProxyConfiguration.reject_ttl        = 600;
                DnscryptProxyConfigurationManager.DnscryptProxyConfiguration.cache_size        = 1024;
                DnscryptProxyConfigurationManager.DnscryptProxyConfiguration.cache_min_ttl     = 2400;
                DnscryptProxyConfigurationManager.DnscryptProxyConfiguration.cache_neg_min_ttl = 60;
                DnscryptProxyConfigurationManager.DnscryptProxyConfiguration.cache_neg_max_ttl = 600;
                var sources = DnscryptProxyConfigurationManager.DnscryptProxyConfiguration.sources;
                if (!sources.ContainsKey("relays"))
                {
                    sources.Add("relays", new Models.Source
                    {
                        urls          = new string[] { "https://github.com/DNSCrypt/dnscrypt-resolvers/raw/master/v2/relays.md", "https://download.dnscrypt.info/resolvers-list/v2/relays.md" },
                        cache_file    = "relays.md",
                        minisign_key  = "RWQf6LRCGA9i53mlYecO4IzT51TGPpvWucNSCh1CBM0QTaLn73Y7GFO3",
                        refresh_delay = 72,
                        prefix        = ""
                    });
                    DnscryptProxyConfigurationManager.DnscryptProxyConfiguration.sources = sources;
                }
                return(DnscryptProxyConfigurationManager.SaveConfiguration());
            }
            return(false);
        }
 private bool SetStaticStamp(string filter)
 {
     try
     {
         if (_dnscryptProxyConfiguration.Static != null)
         {
             if (_dnscryptProxyConfiguration.Static.ContainsKey(filter))
             {
                 return(true);
             }
             else
             {
                 RemoveStaticStamps();
                 DnscryptProxyConfigurationManager.LoadConfiguration();
                 _dnscryptProxyConfiguration = DnscryptProxyConfigurationManager.DnscryptProxyConfiguration;
                 var stamp = _appConfiguration.Proxies.Where(p => p.Name.Equals(filter)).FirstOrDefault();
                 if (stamp != null)
                 {
                     if (!string.IsNullOrEmpty(stamp.Stamp))
                     {
                         var decodedStamp = StampTools.Decode(stamp.Stamp);
                         if (decodedStamp != null)
                         {
                             _dnscryptProxyConfiguration.Static.Add(filter, new Static
                             {
                                 stamp = stamp.Stamp
                             });
                         }
                     }
                 }
                 DnscryptProxyConfigurationManager.DnscryptProxyConfiguration = _dnscryptProxyConfiguration;
                 DnscryptProxyConfigurationManager.SaveConfiguration();
                 return(true);
             }
         }
     }
     catch (Exception exception)
     {
         _logger.Log($"SetStaticStamp {exception.Message}", Category.Exception, Priority.High);
         return(false);
     }
     return(false);
 }
Пример #4
0
        public static bool Patch()
        {
            var version = VersionHelper.PublishVersion;

            if (!DnscryptProxyConfigurationManager.LoadConfiguration())
            {
                return(false);
            }
            if (version.Equals("0.6.5"))
            {
                //added: netprobe_address = '255.255.255.0:53'
                //changed: netprobe_timeout = 0
                DnscryptProxyConfigurationManager.DnscryptProxyConfiguration.netprobe_address = "255.255.255.0:53";
                DnscryptProxyConfigurationManager.DnscryptProxyConfiguration.netprobe_timeout = 0;
                return(DnscryptProxyConfigurationManager.SaveConfiguration());
            }
            if (version.Equals("0.6.6"))
            {
                //changed: netprobe_address = '9.9.9.9:53'
                //changed: netprobe_timeout = 60
                DnscryptProxyConfigurationManager.DnscryptProxyConfiguration.netprobe_address = "9.9.9.9:53";
                DnscryptProxyConfigurationManager.DnscryptProxyConfiguration.netprobe_timeout = 60;
                return(DnscryptProxyConfigurationManager.SaveConfiguration());
            }
            if (version.Equals("0.6.8"))
            {
                //changed: timeout = 5000
                //added: reject_ttl = 600
                //changed: cache_size = 1024
                //changed: cache_min_ttl = 2400
                //added: cache_neg_min_ttl = 60
                //added: cache_neg_max_ttl = 600
                DnscryptProxyConfigurationManager.DnscryptProxyConfiguration.timeout           = 5000;
                DnscryptProxyConfigurationManager.DnscryptProxyConfiguration.reject_ttl        = 600;
                DnscryptProxyConfigurationManager.DnscryptProxyConfiguration.cache_size        = 1024;
                DnscryptProxyConfigurationManager.DnscryptProxyConfiguration.cache_min_ttl     = 2400;
                DnscryptProxyConfigurationManager.DnscryptProxyConfiguration.cache_neg_min_ttl = 60;
                DnscryptProxyConfigurationManager.DnscryptProxyConfiguration.cache_neg_max_ttl = 600;
                return(DnscryptProxyConfigurationManager.SaveConfiguration());
            }
            return(false);
        }
 private bool RemoveStaticStamps()
 {
     try
     {
         if (_dnscryptProxyConfiguration.Static != null)
         {
             foreach (var key in _dnscryptProxyConfiguration.Static.Keys)
             {
                 _dnscryptProxyConfiguration.Static.Remove(key);
             }
             DnscryptProxyConfigurationManager.DnscryptProxyConfiguration = _dnscryptProxyConfiguration;
             DnscryptProxyConfigurationManager.SaveConfiguration();
             return(true);
         }
     }
     catch (Exception exception)
     {
         _logger.Log($"RemoveStaticStamps {exception.Message}", Category.Exception, Priority.High);
         return(false);
     }
     return(false);
 }
Пример #6
0
        private async void InitializeApplication()
        {
            try
            {
                if (IsAdministrator())
                {
                    ProgressText =
                        LocalizationEx.GetUiString("loader_administrative_rights_available", Thread.CurrentThread.CurrentCulture);
                }
                else
                {
                    ProgressText =
                        LocalizationEx.GetUiString("loader_administrative_rights_missing", Thread.CurrentThread.CurrentCulture);
                    await Task.Delay(3000).ConfigureAwait(false);

                    Process.GetCurrentProcess().Kill();
                }

                ProgressText = LocalizationEx.GetUiString("loader_redistributable_package_check", Thread.CurrentThread.CurrentCulture);
                if (PrerequisiteHelper.IsRedistributablePackageInstalled())
                {
                    ProgressText = LocalizationEx.GetUiString("loader_redistributable_package_already_installed", Thread.CurrentThread.CurrentCulture);
                }
                else
                {
                    ProgressText = LocalizationEx.GetUiString("loader_redistributable_package_installing", Thread.CurrentThread.CurrentCulture);
                    //minisign needs this (to verify the installer with libsodium)
                    await PrerequisiteHelper.DownloadAndInstallRedistributablePackage();

                    if (PrerequisiteHelper.IsRedistributablePackageInstalled())
                    {
                        ProgressText = LocalizationEx.GetUiString("loader_redistributable_package_ready", Thread.CurrentThread.CurrentCulture);
                        await Task.Delay(1000).ConfigureAwait(false);
                    }
                }

                if (Properties.Settings.Default.AutoUpdate)
                {
                    ProgressText = LocalizationEx.GetUiString("loader_checking_version", Thread.CurrentThread.CurrentCulture);
                    //TODO: remove in future version (for now we only have one channel)
                    Properties.Settings.Default.MinUpdateType = 2;
                    Properties.Settings.Default.Save();
                    var minUpdateType = (UpdateType)Properties.Settings.Default.MinUpdateType;
                    var update        = await ApplicationUpdater.CheckForRemoteUpdateAsync(minUpdateType).ConfigureAwait(false);

                    if (update.CanUpdate)
                    {
                        ProgressText =
                            string.Format(LocalizationEx.GetUiString("loader_new_version_found", Thread.CurrentThread.CurrentCulture),
                                          update.Update.Version);
                        await Task.Delay(200).ConfigureAwait(false);

                        var installer = await StartRemoteUpdateDownload(update).ConfigureAwait(false);

                        if (!string.IsNullOrEmpty(installer) && File.Exists(installer))
                        {
                            ProgressText = LocalizationEx.GetUiString("loader_starting_update", Thread.CurrentThread.CurrentCulture);
                            await Task.Delay(200).ConfigureAwait(false);

                            Process.Start(installer);
                            Process.GetCurrentProcess().Kill();
                        }
                        else
                        {
                            await Task.Delay(500).ConfigureAwait(false);

                            ProgressText = LocalizationEx.GetUiString("loader_update_failed", Thread.CurrentThread.CurrentCulture);
                        }
                    }
                    else
                    {
                        ProgressText = LocalizationEx.GetUiString("loader_latest_version", Thread.CurrentThread.CurrentCulture);
                    }
                }
                else
                {
                    await Task.Delay(500).ConfigureAwait(false);
                }

                ProgressText =
                    string.Format(LocalizationEx.GetUiString("loader_validate_folder", Thread.CurrentThread.CurrentCulture),
                                  Global.DnsCryptProxyFolder);
                var validatedFolder = ValidateDnsCryptProxyFolder();
                if (validatedFolder.Count == 0)
                {
                    ProgressText = LocalizationEx.GetUiString("loader_all_files_available", Thread.CurrentThread.CurrentCulture);
                }
                else
                {
                    var fileErrors = "";
                    foreach (var pair in validatedFolder)
                    {
                        fileErrors += $"{pair.Key}: {pair.Value}\n";
                    }

                    ProgressText =
                        string.Format(
                            LocalizationEx.GetUiString("loader_missing_files", Thread.CurrentThread.CurrentCulture).Replace("\\n", "\n"),
                            Global.DnsCryptProxyFolder, fileErrors, Global.ApplicationName);
                    await Task.Delay(5000).ConfigureAwait(false);

                    Process.GetCurrentProcess().Kill();
                }

                ProgressText = string.Format(LocalizationEx.GetUiString("loader_loading", Thread.CurrentThread.CurrentCulture),
                                             Global.DnsCryptConfigurationFile);
                if (DnscryptProxyConfigurationManager.LoadConfiguration())
                {
                    ProgressText =
                        string.Format(LocalizationEx.GetUiString("loader_successfully_loaded", Thread.CurrentThread.CurrentCulture),
                                      Global.DnsCryptConfigurationFile);
                    _mainViewModel.DnscryptProxyConfiguration = DnscryptProxyConfigurationManager.DnscryptProxyConfiguration;
                }
                else
                {
                    ProgressText =
                        string.Format(LocalizationEx.GetUiString("loader_failed_loading", Thread.CurrentThread.CurrentCulture),
                                      Global.DnsCryptConfigurationFile);
                    await Task.Delay(5000).ConfigureAwait(false);

                    Process.GetCurrentProcess().Kill();
                }

                ProgressText = LocalizationEx.GetUiString("loader_loading_network_cards", Thread.CurrentThread.CurrentCulture);

                List <LocalNetworkInterface> localNetworkInterfaces;
                if (DnscryptProxyConfigurationManager.DnscryptProxyConfiguration.listen_addresses.Contains(Global.GlobalResolver))
                {
                    var dnsServer = new List <string>
                    {
                        Global.DefaultResolverIpv4,
                        Global.DefaultResolverIpv6
                    };
                    localNetworkInterfaces = LocalNetworkInterfaceManager.GetLocalNetworkInterfaces(dnsServer);
                }
                else
                {
                    localNetworkInterfaces = LocalNetworkInterfaceManager.GetLocalNetworkInterfaces(
                        DnscryptProxyConfigurationManager.DnscryptProxyConfiguration.listen_addresses.ToList());
                }
                _mainViewModel.LocalNetworkInterfaces = new BindableCollection <LocalNetworkInterface>();
                _mainViewModel.LocalNetworkInterfaces.AddRange(localNetworkInterfaces);
                _mainViewModel.Initialize();
                ProgressText = LocalizationEx.GetUiString("loader_starting", Thread.CurrentThread.CurrentCulture);

                if (Properties.Settings.Default.TrayMode)
                {
                    Execute.OnUIThread(() => _windowManager.ShowWindow(_systemTrayViewModel));
                    Execute.OnUIThread(() => _systemTrayViewModel.ShowWindow());
                }
                else
                {
                    Execute.OnUIThread(() => _windowManager.ShowWindow(_mainViewModel));
                }

                TryClose(true);
            }
            catch (Exception exception)
            {
                Log.Error(exception);
            }
        }
Пример #7
0
        private async void InitializeApplication()
        {
            try
            {
                if (IsAdministrator())
                {
                    ProgressText =
                        LocalizationEx.GetUiString("loader_administrative_rights_available", Thread.CurrentThread.CurrentCulture);
                }
                else
                {
                    ProgressText =
                        LocalizationEx.GetUiString("loader_administrative_rights_missing", Thread.CurrentThread.CurrentCulture);
                    await Task.Delay(3000).ConfigureAwait(false);

                    Process.GetCurrentProcess().Kill();
                }

                ProgressText = LocalizationEx.GetUiString("loader_checking_version", Thread.CurrentThread.CurrentCulture);
                var update = await ApplicationUpdater.CheckForRemoteUpdateAsync().ConfigureAwait(false);

                if (update.CanUpdate)
                {
                    ProgressText =
                        string.Format(LocalizationEx.GetUiString("loader_new_version_found", Thread.CurrentThread.CurrentCulture),
                                      update.Update.Version);
                    await Task.Delay(200).ConfigureAwait(false);

                    var installer = await StartRemoteUpdateDownload(update).ConfigureAwait(false);

                    if (!string.IsNullOrEmpty(installer) && File.Exists(installer))
                    {
                        ProgressText = LocalizationEx.GetUiString("loader_starting_update", Thread.CurrentThread.CurrentCulture);
                        await Task.Delay(200).ConfigureAwait(false);

                        Process.Start(installer);
                        Process.GetCurrentProcess().Kill();
                    }
                    else
                    {
                        await Task.Delay(500).ConfigureAwait(false);

                        ProgressText = LocalizationEx.GetUiString("loader_update_failed", Thread.CurrentThread.CurrentCulture);
                    }
                }
                else
                {
                    ProgressText = LocalizationEx.GetUiString("loader_latest_version", Thread.CurrentThread.CurrentCulture);
                }

                ProgressText =
                    string.Format(LocalizationEx.GetUiString("loader_validate_folder", Thread.CurrentThread.CurrentCulture),
                                  Global.DnsCryptProxyFolder);
                var validatedFolder = ValidateDnsCryptProxyFolder();
                if (validatedFolder.Count == 0)
                {
                    ProgressText = LocalizationEx.GetUiString("loader_all_files_available", Thread.CurrentThread.CurrentCulture);
                }
                else
                {
                    var fileErrors = "";
                    foreach (var pair in validatedFolder)
                    {
                        fileErrors += $"{pair.Key}: {pair.Value}\n";
                    }

                    ProgressText =
                        string.Format(
                            LocalizationEx.GetUiString("loader_missing_files", Thread.CurrentThread.CurrentCulture).Replace("\\n", "\n"),
                            Global.DnsCryptProxyFolder, fileErrors, Global.ApplicationName);
                    await Task.Delay(5000).ConfigureAwait(false);

                    Process.GetCurrentProcess().Kill();
                }

                ProgressText = string.Format(LocalizationEx.GetUiString("loader_loading", Thread.CurrentThread.CurrentCulture),
                                             Global.DnsCryptConfigurationFile);
                if (DnscryptProxyConfigurationManager.LoadConfiguration())
                {
                    ProgressText =
                        string.Format(LocalizationEx.GetUiString("loader_successfully_loaded", Thread.CurrentThread.CurrentCulture),
                                      Global.DnsCryptConfigurationFile);
                    _mainViewModel.DnscryptProxyConfiguration = DnscryptProxyConfigurationManager.DnscryptProxyConfiguration;
                }
                else
                {
                    ProgressText =
                        string.Format(LocalizationEx.GetUiString("loader_failed_loading", Thread.CurrentThread.CurrentCulture),
                                      Global.DnsCryptConfigurationFile);
                    await Task.Delay(5000).ConfigureAwait(false);

                    Process.GetCurrentProcess().Kill();
                }

                ProgressText = LocalizationEx.GetUiString("loader_loading_network_cards", Thread.CurrentThread.CurrentCulture);
                var localNetworkInterfaces = LocalNetworkInterfaceManager.GetLocalNetworkInterfaces(
                    DnscryptProxyConfigurationManager.DnscryptProxyConfiguration.listen_addresses.ToList());
                _mainViewModel.LocalNetworkInterfaces = new BindableCollection <LocalNetworkInterface>();
                _mainViewModel.LocalNetworkInterfaces.AddRange(localNetworkInterfaces);
                _mainViewModel.Initialize();
                ProgressText = LocalizationEx.GetUiString("loader_starting", Thread.CurrentThread.CurrentCulture);
                Execute.OnUIThread(() => _windowManager.ShowWindow(_mainViewModel));
                TryClose(true);
            }
            catch (Exception exception)
            {
                Log.Error(exception);
            }
        }
        public MainWindowViewModel(
            ILoggerFacade logger,
            IRegionManager regionManager,
            IEventAggregator eventAggregator,
            ISnackbarMessageQueue snackbarMessageQueue,
            IAppConfigurationService appConfigurationService)
        {
            _logger                  = logger ?? throw new ArgumentNullException(nameof(logger));
            _eventAggregator         = eventAggregator ?? throw new ArgumentNullException(nameof(eventAggregator));
            _regionManager           = regionManager ?? throw new ArgumentNullException(nameof(regionManager));
            _messageQueue            = snackbarMessageQueue ?? throw new ArgumentNullException(nameof(snackbarMessageQueue));
            _appConfigurationService = appConfigurationService ?? throw new ArgumentNullException(nameof(appConfigurationService));
            try
            {
                _appConfiguration = _appConfigurationService.Configuration;
                _eventAggregator.GetEvent <StampAddedEvent>().Subscribe(CustomStampAdded);
                NavigateToAboutViewCommand    = new DelegateCommand(NavigateToAboutView);
                NavigateToStampViewCommand    = new DelegateCommand(NavigateToStampView);
                NavigateToSettingsViewCommand = new DelegateCommand(NavigateToSettingsView);
                OpenWebCommand         = new DelegateCommand <string>(OpenUrl);
                CheckForUpdatesCommand = new DelegateCommand(CheckForUpdates);

                HandleCustomFilter = new DelegateCommand(HandleCustomStamp);
                HandleFamilyFilter = new DelegateCommand(async() => await HandleDnsCrypt(Global.DefaultFamilyFilterKey));
                HandleAdultFilter  = new DelegateCommand(async() => await HandleDnsCrypt(Global.DefaultAdultFilterKey));

                IsWorking             = true;
                IsCustomFilterEnabled = false;
                IsFamilyFilterEnabled = false;
                IsAdultFilterEnabled  = false;

                var year = Global.CopyrightYear.ToString();
                if (DateTime.UtcNow.Year != Global.CopyrightYear)
                {
                    year = $"{Global.CopyrightYear} - {DateTime.UtcNow.Year}";
                }
                Footer = $"Copyright © {year} {Global.CompanyName}. {VersionHelper.PublishVersion} {VersionHelper.PublishBuild}";
                ((App)Application.Current).LoggingLevelSwitch.MinimumLevel = (Serilog.Events.LogEventLevel)_appConfiguration.LogLevel;
                _logger.Log($"LogLevel: {_appConfiguration.LogLevel}", Category.Debug, Priority.Low);
                _logger.Log($"{Global.ApplicationName} {VersionHelper.PublishVersion} {VersionHelper.PublishBuild} started", Category.Info, Priority.Medium);
                // check application configuration
                _logger.Log($"checking {Global.AppConfigurationFile}", Category.Info, Priority.Medium);

                var resetApplicationConfig = false;
                if (_appConfiguration != null)
                {
                    if (_appConfiguration.Proxies != null)
                    {
                        foreach (var proxy in _appConfiguration.Proxies)
                        {
                            var decodedStamp = StampTools.Decode(proxy.Stamp);
                            if (decodedStamp != null)
                            {
                                if (decodedStamp.Protocol == DnsCrypt.Models.StampProtocol.DnsCrypt)
                                {
                                    //simple check if the stamp is a valid cleanbrowsing stamp
                                    if (!decodedStamp.ProviderName.Equals(Global.ValidCleanBrowsingDnsCryptStamp))
                                    {
                                        resetApplicationConfig = true;
                                    }
                                }
                                else if (decodedStamp.Protocol == DnsCrypt.Models.StampProtocol.DoH)
                                {
                                    //simple check if the stamp is a valid cleanbrowsing stamp
                                    if (!decodedStamp.Hostname.Equals(Global.ValidCleanBrowsingDohStamp))
                                    {
                                        resetApplicationConfig = true;
                                    }
                                }
                                else
                                {
                                    //unsupported stamp
                                    resetApplicationConfig = true;
                                }
                            }
                            else
                            {
                                resetApplicationConfig = true;
                            }
                            _logger.Log($"{proxy.Name} loaded", Category.Info, Priority.Medium);
                        }
                        _logger.Log($"{Global.AppConfigurationFile} loaded", Category.Info, Priority.Medium);
                    }
                    else
                    {
                        resetApplicationConfig = true;
                    }
                }
                else
                {
                    resetApplicationConfig = true;
                    _logger.Log($"failed to load {Global.AppConfigurationFile}", Category.Warn, Priority.Medium);
                }

                if (resetApplicationConfig)
                {
                    _logger.Log($"reset {Global.AppConfigurationFile} to default", Category.Warn, Priority.Medium);
                    _appConfigurationService.Reset();
                    _appConfiguration = _appConfigurationService.Configuration;
                    if (_appConfiguration == null)
                    {
                        _logger.Log($"failed to reset {Global.AppConfigurationFile}", Category.Exception, Priority.High);
                        Environment.Exit(-1);
                    }
                    else
                    {
                        //no validation this time, just go on
                    }
                }

                _logger.Log($"checking {Global.DnsCryptProxyFolder} folder", Category.Info, Priority.Medium);
                foreach (var proxyFile in Global.DnsCryptProxyFiles)
                {
                    var proxyFilePath = Path.Combine(Directory.GetCurrentDirectory(), Global.DnsCryptProxyFolder, proxyFile);
                    if (!File.Exists(proxyFilePath))
                    {
                        _logger.Log($"missing {proxyFile}", Category.Warn, Priority.Medium);
                    }
                    else
                    {
                        _logger.Log($"found {proxyFile}", Category.Info, Priority.Low);
                    }
                }

                var isValidConfiguration = false;
                _logger.Log($"checking {Global.DnsCryptConfigurationFile}", Category.Info, Priority.Medium);

                var configurationCheck = DnsCryptProxyManager.IsConfigurationFileValid();
                if (configurationCheck.Success)
                {
                    isValidConfiguration = true;
                    _logger.Log($"{Global.DnsCryptConfigurationFile} is valid", Category.Info, Priority.Medium);
                }
                else
                {
                    if (configurationCheck.StandardError.Contains("[FATAL] No servers configured"))
                    {
                        isValidConfiguration = true;
                        _logger.Log($"{Global.DnsCryptConfigurationFile} is valid (but no servers)", Category.Info, Priority.Medium);
                    }
                }

                if (isValidConfiguration)
                {
                    var version = DnsCryptProxyManager.GetVersion();
                    if (!string.IsNullOrEmpty(version))
                    {
                        Title = $"{Global.ApplicationName} (dnscrypt-proxy {version})";
                        _logger.Log($"dnscrypt-proxy version: {version}", Category.Info, Priority.Medium);
                    }
                    else
                    {
                        Title = $"{Global.ApplicationName} (dnscrypt-proxy unknown)";
                        _logger.Log("dnscrypt-proxy version: unknown", Category.Warn, Priority.Medium);
                    }
                    _logger.Log($"loading {Global.DnsCryptConfigurationFile}", Category.Info, Priority.Medium);
                    if (DnscryptProxyConfigurationManager.LoadConfiguration())
                    {
                        _logger.Log($"{Global.DnsCryptConfigurationFile} loaded", Category.Info, Priority.Medium);
                        _dnscryptProxyConfiguration = DnscryptProxyConfigurationManager.DnscryptProxyConfiguration;
                        if (_dnscryptProxyConfiguration.Static != null && _dnscryptProxyConfiguration.Static.Count > 0)
                        {
                            if (_dnscryptProxyConfiguration.Static.ContainsKey(Global.DefaultCustomFilterKey))
                            {
                                _logger.Log($"found {Global.DefaultCustomFilterKey} filter", Category.Info, Priority.Medium);
                                IsCustomFilterEnabled = true;
                            }
                            else if (_dnscryptProxyConfiguration.Static.ContainsKey(Global.DefaultAdultFilterKey))
                            {
                                _logger.Log($"found {Global.DefaultAdultFilterKey} filter", Category.Info, Priority.Medium);
                                IsAdultFilterEnabled = true;
                            }
                            else if (_dnscryptProxyConfiguration.Static.ContainsKey(Global.DefaultFamilyFilterKey))
                            {
                                _logger.Log($"found {Global.DefaultFamilyFilterKey} filter", Category.Info, Priority.Medium);
                                IsFamilyFilterEnabled = true;
                            }
                        }
                        else
                        {
                            _logger.Log("no static filter configured", Category.Info, Priority.Medium);
                        }

                        if (IsCustomFilterEnabled || IsFamilyFilterEnabled || IsAdultFilterEnabled)
                        {
                            if (!DnsCryptProxyManager.IsDnsCryptProxyInstalled())
                            {
                                _logger.Log($"dnscrypt-proxy service is not installed, try install", Category.Info, Priority.High);
                                //install
                                Task.Run(() => { DnsCryptProxyManager.Install(); }).ConfigureAwait(false);
                                Task.Delay(Global.ServiceInstallTime).ConfigureAwait(false);
                            }
                            else
                            {
                                _logger.Log($"dnscrypt-proxy service is already installed", Category.Info, Priority.Medium);
                            }

                            if (!DnsCryptProxyManager.IsDnsCryptProxyRunning())
                            {
                                _logger.Log($"dnscrypt-proxy service is not running, try start", Category.Info, Priority.High);
                                Task.Run(() => { DnsCryptProxyManager.Start(); }).ConfigureAwait(false);
                                Task.Delay(Global.ServiceStartTime).ConfigureAwait(false);
                            }
                            else
                            {
                                _logger.Log($"dnscrypt-proxy service is already running", Category.Info, Priority.Medium);
                            }

                            if (DnsCryptProxyManager.IsDnsCryptProxyRunning())
                            {
                                _logger.Log($"checking dns servers on network interfaces", Category.Info, Priority.High);
                                if (!HandleNetworkInterfaces(true))
                                {
                                    _logger.Log($"could not update dns servers on network interfaces", Category.Warn, Priority.High);
                                }
                            }
                            else
                            {
                                _logger.Log($"could not start dnscrypt-proxy", Category.Warn, Priority.High);
                            }
                        }
                    }
                    else
                    {
                        _logger.Log($"could not load configuration: {Global.DnsCryptConfigurationFile}", Category.Warn, Priority.High);
                        Environment.Exit(-1);
                    }
                }
                else
                {
                    _logger.Log($"invalid {Global.DnsCryptConfigurationFile}", Category.Warn, Priority.High);
                    Environment.Exit(-1);
                }
            }
            catch (Exception ex)
            {
                _logger.Log(ex.Message, Category.Exception, Priority.High);
                Environment.Exit(-1);
            }
            MessageQueue.Enqueue("🐞 You are using a preview version! May contain bugs! 🐞");
            IsWorking = false;
        }
Пример #9
0
        private async void InitializeApplication()
        {
            try
            {
                if (IsAdministrator())
                {
                    ProgressText =
                        LocalizationEx.GetUiString("loader_administrative_rights_available", Thread.CurrentThread.CurrentCulture);
                }
                else
                {
                    ProgressText =
                        LocalizationEx.GetUiString("loader_administrative_rights_missing", Thread.CurrentThread.CurrentCulture);
                    await Task.Delay(3000).ConfigureAwait(false);

                    Process.GetCurrentProcess().Kill();
                }

                ProgressText = LocalizationEx.GetUiString("loader_redistributable_package_check", Thread.CurrentThread.CurrentCulture);
                if (PrerequisiteHelper.IsRedistributablePackageInstalled())
                {
                    ProgressText = LocalizationEx.GetUiString("loader_redistributable_package_already_installed", Thread.CurrentThread.CurrentCulture);
                }
                else
                {
                    //Note: if this is disabled, the auto update may not work
                    if (Properties.Settings.Default.InstallRedistributablePackage)
                    {
                        ProgressText = LocalizationEx.GetUiString("loader_redistributable_package_installing",
                                                                  Thread.CurrentThread.CurrentCulture);
                        //minisign needs this (to verify the installer with libsodium)
                        await PrerequisiteHelper.DownloadAndInstallRedistributablePackage();

                        if (PrerequisiteHelper.IsRedistributablePackageInstalled())
                        {
                            ProgressText = LocalizationEx.GetUiString("loader_redistributable_package_ready",
                                                                      Thread.CurrentThread.CurrentCulture);
                            await Task.Delay(1000).ConfigureAwait(false);
                        }
                    }
                }

                ProgressText =
                    string.Format(LocalizationEx.GetUiString("loader_validate_folder", Thread.CurrentThread.CurrentCulture),
                                  Global.DnsCryptProxyFolder);
                var validatedFolder = ValidateDnsCryptProxyFolder();
                if (validatedFolder.Count == 0)
                {
                    ProgressText = LocalizationEx.GetUiString("loader_all_files_available", Thread.CurrentThread.CurrentCulture);
                }
                else if (validatedFolder.Count == 1 && validatedFolder.Single().Key == Global.DnsCryptConfigurationFile)
                {
                    File.Copy(
                        Path.Combine(Directory.GetCurrentDirectory(), Global.DnsCryptProxyFolder, Global.DnsCryptExampleConfigurationFile),
                        Path.Combine(Directory.GetCurrentDirectory(), Global.DnsCryptProxyFolder, Global.DnsCryptConfigurationFile),
                        false);
                }
                else
                {
                    var fileErrors = "";
                    foreach (var pair in validatedFolder)
                    {
                        fileErrors += $"{pair.Key}: {pair.Value}\n";
                    }

                    ProgressText =
                        string.Format(
                            LocalizationEx.GetUiString("loader_missing_files", Thread.CurrentThread.CurrentCulture).Replace("\\n", "\n"),
                            Global.DnsCryptProxyFolder, fileErrors, Global.ApplicationName);
                    await Task.Delay(5000).ConfigureAwait(false);

                    Process.GetCurrentProcess().Kill();
                }

                if (Properties.Settings.Default.BackupAndRestoreConfigOnUpdate)
                {
                    var tmpConfigPath = Path.Combine(Path.GetTempPath(), Global.DnsCryptConfigurationFile + ".bak");
                    if (File.Exists(tmpConfigPath))
                    {
                        ProgressText = string.Format(LocalizationEx.GetUiString("loader_restore_config", Thread.CurrentThread.CurrentCulture),
                                                     Global.DnsCryptConfigurationFile);
                        var configFile = Path.Combine(Directory.GetCurrentDirectory(), Global.DnsCryptProxyFolder, Global.DnsCryptConfigurationFile);
                        if (File.Exists(configFile))
                        {
                            if (File.Exists(configFile + ".bak"))
                            {
                                File.Delete(configFile + ".bak");
                            }
                            File.Move(configFile, configFile + ".bak");
                        }
                        File.Move(tmpConfigPath, configFile);
                        // update the configuration file
                        if (PatchHelper.Patch())
                        {
                            await Task.Delay(500).ConfigureAwait(false);
                        }
                    }
                }

                ProgressText = string.Format(LocalizationEx.GetUiString("loader_loading", Thread.CurrentThread.CurrentCulture),
                                             Global.DnsCryptConfigurationFile);
                if (DnscryptProxyConfigurationManager.LoadConfiguration())
                {
                    ProgressText =
                        string.Format(LocalizationEx.GetUiString("loader_successfully_loaded", Thread.CurrentThread.CurrentCulture),
                                      Global.DnsCryptConfigurationFile);
                    _mainViewModel.DnscryptProxyConfiguration = DnscryptProxyConfigurationManager.DnscryptProxyConfiguration;
                }
                else
                {
                    ProgressText =
                        string.Format(LocalizationEx.GetUiString("loader_failed_loading", Thread.CurrentThread.CurrentCulture),
                                      Global.DnsCryptConfigurationFile);
                    await Task.Delay(5000).ConfigureAwait(false);

                    Process.GetCurrentProcess().Kill();
                }

                ProgressText = LocalizationEx.GetUiString("loader_loading_network_cards", Thread.CurrentThread.CurrentCulture);

                List <LocalNetworkInterface> localNetworkInterfaces;
                if (DnscryptProxyConfigurationManager.DnscryptProxyConfiguration.listen_addresses.Contains(Global.GlobalResolver))
                {
                    var dnsServer = new List <string>
                    {
                        Global.DefaultResolverIpv4,
                        Global.DefaultResolverIpv6
                    };
                    localNetworkInterfaces = LocalNetworkInterfaceManager.GetLocalNetworkInterfaces(dnsServer);
                }
                else
                {
                    localNetworkInterfaces = LocalNetworkInterfaceManager.GetLocalNetworkInterfaces(
                        DnscryptProxyConfigurationManager.DnscryptProxyConfiguration.listen_addresses.ToList());
                }
                _mainViewModel.LocalNetworkInterfaces = new BindableCollection <LocalNetworkInterface>();
                _mainViewModel.LocalNetworkInterfaces.AddRange(localNetworkInterfaces);
                _mainViewModel.Initialize();
                ProgressText = LocalizationEx.GetUiString("loader_starting", Thread.CurrentThread.CurrentCulture);

                if (Properties.Settings.Default.TrayMode)
                {
                    await Execute.OnUIThreadAsync(() => _windowManager.ShowWindowAsync(_systemTrayViewModel));

                    if (Properties.Settings.Default.StartInTray)
                    {
                        Execute.OnUIThread(() => _systemTrayViewModel.HideWindow());
                    }
                    else
                    {
                        await Execute.OnUIThreadAsync(() => _windowManager.ShowWindowAsync(_mainViewModel));
                    }
                }
                else
                {
                    await Execute.OnUIThreadAsync(() => _windowManager.ShowWindowAsync(_mainViewModel));
                }

                await TryCloseAsync(true);
            }
            catch (Exception exception)
            {
                Log.Error(exception);
                throw;
            }
        }