Exemple #1
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);
                        }
                    }
                }

                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);

                            if (Properties.Settings.Default.AutoUpdateSilent)
                            {
                                // auto install
                                const string arguments = "/qb /passive /norestart";
                                var          startInfo = new ProcessStartInfo(installer)
                                {
                                    Arguments       = arguments,
                                    UseShellExecute = true
                                };
                                Process.Start(startInfo);
                            }
                            else
                            {
                                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();
                }

                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)
                {
                    Execute.OnUIThread(() => _windowManager.ShowWindow(_systemTrayViewModel));
                    if (Properties.Settings.Default.StartInTray)
                    {
                        Execute.OnUIThread(() => _systemTrayViewModel.HideWindow());
                    }
                    else
                    {
                        Execute.OnUIThread(() => _windowManager.ShowWindow(_mainViewModel));
                    }
                }
                else
                {
                    Execute.OnUIThread(() => _windowManager.ShowWindow(_mainViewModel));
                }

                TryClose(true);
            }
            catch (Exception exception)
            {
                Log.Error(exception);
            }
        }
        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;
            }
        }