コード例 #1
0
        public async Task HandleDnsCrypt(string filter)
        {
            try
            {
                IsWorking = true;
                if (filter.Equals(Global.DefaultCustomFilterKey))
                {
                    if (_isCustomFilterEnabled)
                    {
                        //disable
                        if (RemoveStaticStamps())
                        {
                            if (DnsCryptProxyManager.IsDnsCryptProxyRunning())
                            {
                                await Task.Run(() => { DnsCryptProxyManager.Stop(); }).ConfigureAwait(false);

                                await Task.Delay(Global.ServiceStopTime).ConfigureAwait(false);
                            }
                            if (DnsCryptProxyManager.IsDnsCryptProxyInstalled())
                            {
                                await Task.Run(() => { DnsCryptProxyManager.Uninstall(); }).ConfigureAwait(false);

                                await Task.Delay(Global.ServiceUninstallTime).ConfigureAwait(false);
                            }
                            HandleNetworkInterfaces(false);

                            //remove stamp from config
                            var stampIndex = _appConfiguration.Proxies.FindIndex(p => p.Name.Equals(filter));
                            if (stampIndex != -1)
                            {
                                _appConfiguration.Proxies.RemoveAt(stampIndex);
                                _appConfigurationService.Configuration = _appConfiguration;
                            }
                        }
                        IsCustomFilterEnabled = false;
                        IsAdultFilterEnabled  = false;
                        IsFamilyFilterEnabled = false;
                    }
                    else
                    {
                        //enable
                        if (SetStaticStamp(filter))
                        {
                            if (!DnsCryptProxyManager.IsDnsCryptProxyInstalled())
                            {
                                await Task.Run(() => { DnsCryptProxyManager.Install(); }).ConfigureAwait(false);

                                await Task.Delay(Global.ServiceInstallTime).ConfigureAwait(false);
                            }
                            if (DnsCryptProxyManager.IsDnsCryptProxyRunning())
                            {
                                await Task.Run(() => { DnsCryptProxyManager.Restart(); }).ConfigureAwait(false);

                                await Task.Delay(Global.ServiceRestartTime).ConfigureAwait(false);
                            }
                            else
                            {
                                await Task.Run(() => { DnsCryptProxyManager.Start(); }).ConfigureAwait(false);

                                await Task.Delay(Global.ServiceStartTime).ConfigureAwait(false);
                            }
                            HandleNetworkInterfaces(true);
                            IsCustomFilterEnabled = true;
                            IsAdultFilterEnabled  = false;
                            IsFamilyFilterEnabled = false;
                        }
                    }
                }
                else if (filter.Equals(Global.DefaultAdultFilterKey))
                {
                    if (_isAdultFilterEnabled)
                    {
                        //disable
                        if (RemoveStaticStamps())
                        {
                            if (DnsCryptProxyManager.IsDnsCryptProxyRunning())
                            {
                                await Task.Run(() => { DnsCryptProxyManager.Stop(); }).ConfigureAwait(false);

                                await Task.Delay(Global.ServiceStopTime).ConfigureAwait(false);
                            }
                            if (DnsCryptProxyManager.IsDnsCryptProxyInstalled())
                            {
                                await Task.Run(() => { DnsCryptProxyManager.Uninstall(); }).ConfigureAwait(false);

                                await Task.Delay(Global.ServiceUninstallTime).ConfigureAwait(false);
                            }
                            HandleNetworkInterfaces(false);
                        }
                        IsCustomFilterEnabled = false;
                        IsAdultFilterEnabled  = false;
                        IsFamilyFilterEnabled = false;
                    }
                    else
                    {
                        //enable
                        if (SetStaticStamp(filter))
                        {
                            if (!DnsCryptProxyManager.IsDnsCryptProxyInstalled())
                            {
                                await Task.Run(() => { DnsCryptProxyManager.Install(); }).ConfigureAwait(false);

                                await Task.Delay(Global.ServiceInstallTime).ConfigureAwait(false);
                            }
                            if (DnsCryptProxyManager.IsDnsCryptProxyRunning())
                            {
                                await Task.Run(() => { DnsCryptProxyManager.Restart(); }).ConfigureAwait(false);

                                await Task.Delay(Global.ServiceRestartTime).ConfigureAwait(false);
                            }
                            else
                            {
                                await Task.Run(() => { DnsCryptProxyManager.Start(); }).ConfigureAwait(false);

                                await Task.Delay(Global.ServiceStartTime).ConfigureAwait(false);
                            }
                            HandleNetworkInterfaces(true);
                            IsAdultFilterEnabled  = true;
                            IsCustomFilterEnabled = false;
                            IsFamilyFilterEnabled = false;
                        }
                    }
                }
                else if (filter.Equals(Global.DefaultFamilyFilterKey))
                {
                    if (_isFamilyFilterEnabled)
                    {
                        //disable
                        if (RemoveStaticStamps())
                        {
                            if (DnsCryptProxyManager.IsDnsCryptProxyRunning())
                            {
                                await Task.Run(() => { DnsCryptProxyManager.Stop(); }).ConfigureAwait(false);

                                await Task.Delay(Global.ServiceStopTime).ConfigureAwait(false);
                            }
                            if (DnsCryptProxyManager.IsDnsCryptProxyInstalled())
                            {
                                await Task.Run(() => { DnsCryptProxyManager.Uninstall(); }).ConfigureAwait(false);

                                await Task.Delay(Global.ServiceUninstallTime).ConfigureAwait(false);
                            }
                            HandleNetworkInterfaces(false);
                        }
                        IsCustomFilterEnabled = false;
                        IsFamilyFilterEnabled = false;
                        IsAdultFilterEnabled  = false;
                    }
                    else
                    {
                        //enable
                        if (SetStaticStamp(filter))
                        {
                            if (!DnsCryptProxyManager.IsDnsCryptProxyInstalled())
                            {
                                await Task.Run(() => { DnsCryptProxyManager.Install(); }).ConfigureAwait(false);

                                await Task.Delay(Global.ServiceInstallTime).ConfigureAwait(false);
                            }
                            if (DnsCryptProxyManager.IsDnsCryptProxyRunning())
                            {
                                await Task.Run(() => { DnsCryptProxyManager.Restart(); }).ConfigureAwait(false);

                                await Task.Delay(Global.ServiceRestartTime).ConfigureAwait(false);
                            }
                            else
                            {
                                await Task.Run(() => { DnsCryptProxyManager.Start(); }).ConfigureAwait(false);

                                await Task.Delay(Global.ServiceStartTime).ConfigureAwait(false);
                            }
                            HandleNetworkInterfaces(true);
                            IsFamilyFilterEnabled = true;
                            IsCustomFilterEnabled = false;
                            IsAdultFilterEnabled  = false;
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                _logger.Log($"HandleDnsCrypt {exception.Message}", Category.Exception, Priority.High);
            }
            finally
            {
                IsWorking = false;
            }
        }
コード例 #2
0
        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;
        }