public void StampDecodeTest1()
        {
            const string stamp  = "sdns://AQcAAAAAAAAADjIxMi40Ny4yMjguMTM2IOgBuE6mBr-wusDOQ0RbsV66ZLAvo8SqMa4QY2oHkDJNHzIuZG5zY3J5cHQtY2VydC5mci5kbnNjcnlwdC5vcmc";
            var          result = StampTools.Decode(stamp);

            Assert.Equal(StampProtocol.DnsCrypt, result.Protocol);
            Assert.Equal("2.dnscrypt-cert.fr.dnscrypt.org", result.ProviderName);
        }
        public void StampDecodeTest3()
        {
            const string stamp  = "sdns://AgUAAAAAAAAAACDyXGrcc5eNecJ8nomJCJ-q6eCLTEn6bHic0hWGUwYQaA5kbnMuZ29vZ2xlLmNvbQ0vZXhwZXJpbWVudGFs";
            var          result = StampTools.Decode(stamp);

            Assert.Equal(StampProtocol.DoH, result.Protocol);
            Assert.Equal("dns.google.com", result.Hostname);
        }
        public void StampDecodeTestRelay()
        {
            const string stamp  = "sdns://gQ84My43Ny44NS43Ojg0NDM";
            var          result = StampTools.Decode(stamp);

            Assert.Equal(StampProtocol.DNSCryptRelay, result.Protocol);
            Assert.Equal("83.77.85.7", result.Address);
        }
        public void StampDecodeTest2()
        {
            const string stamp  = "sdns://AgcAAAAAAAAADTM3LjU5LjIzOC4yMTMgwzRA_TfjYt0RwSHqBHwj7OM-D_x-CDgqIHeJHIoN1P0UZG9oLmZyLmRuc2NyeXB0LmluZm8KL2Rucy1xdWVyeQ";
            var          result = StampTools.Decode(stamp);

            Assert.Equal(StampProtocol.DoH, result.Protocol);
            Assert.Equal("doh.fr.dnscrypt.info", result.Hostname);
        }
 private async void CustomStampAdded(Proxy proxy)
 {
     try
     {
         if (proxy != null)
         {
             if (!string.IsNullOrEmpty(proxy.Stamp))
             {
                 var decodedStamp = StampTools.Decode(proxy.Stamp);
                 if (decodedStamp != null)
                 {
                     var addStamp = false;
                     if (decodedStamp.Protocol == DnsCrypt.Models.StampProtocol.DnsCrypt)
                     {
                         //simple check if the stamp is a valid cleanbrowsing stamp
                         if (decodedStamp.ProviderName.Equals(Global.ValidCleanBrowsingDnsCryptStamp))
                         {
                             addStamp = 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))
                         {
                             addStamp = true;
                         }
                     }
                     else
                     {
                         //unsupported stamp
                         addStamp = false;
                     }
                     if (addStamp)
                     {
                         _appConfiguration.Proxies.Add(proxy);
                         _appConfigurationService.Configuration = _appConfiguration;
                         _logger.Log($"{proxy.Name} - {proxy.Stamp} added", Category.Info, Priority.Medium);
                         MessageQueue.Enqueue("New custom filter added");
                         await HandleDnsCrypt(Global.DefaultCustomFilterKey);
                     }
                     else
                     {
                         _logger.Log($"invalid custom stamp", Category.Warn, Priority.Medium);
                     }
                 }
                 else
                 {
                     _logger.Log($"invalid custom stamp", Category.Warn, Priority.Medium);
                 }
             }
         }
     }
     catch (Exception exception)
     {
         _logger.Log($"CustomStampAdded {exception.Message}", Category.Exception, Priority.High);
     }
 }
        public async void MeasureTest1()
        {
            const string stamp       = "sdns://AQcAAAAAAAAADjIxMi40Ny4yMjguMTM2IOgBuE6mBr-wusDOQ0RbsV66ZLAvo8SqMa4QY2oHkDJNHzIuZG5zY3J5cHQtY2VydC5mci5kbnNjcnlwdC5vcmc";
            var          result      = StampTools.Decode(stamp);
            var          measurement = await MeasurementTools.Proxy(result).ConfigureAwait(false);

            Assert.NotNull(measurement);
            Assert.False(measurement.Failed);
        }
        public async void MeasureTest2()
        {
            const string stamp       = "sdns://AQcAAAAAAAAAEzIwOS4yNTAuMjM1LjE3MDo0NDMgz0wbvISl_NVCSe0wDJMS79BAFZoWth1djmhuzv_n3KAiMi5kbnNjcnlwdC1jZXJ0LmRlLmRuc21hc2NoaW5lLm5ldA";
            var          result      = StampTools.Decode(stamp);
            var          measurement = await MeasurementTools.Proxy(result).ConfigureAwait(false);

            Assert.NotNull(measurement);
            Assert.False(measurement.Failed);
        }
        private void SaveStamp()
        {
            if (!string.IsNullOrEmpty(Stamp))
            {
                var decodedStamp = StampTools.Decode(Stamp.Trim());
                if (decodedStamp != null)
                {
                    var addStamp = false;
                    if (decodedStamp.Protocol == DnsCrypt.Models.StampProtocol.DnsCrypt)
                    {
                        //simple check if the stamp is a valid cleanbrowsing stamp
                        if (decodedStamp.ProviderName.Equals(Global.ValidCleanBrowsingDnsCryptStamp))
                        {
                            _logger.Log("valid DnsCrypt stamp", Category.Info, Priority.Low);
                            addStamp = 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))
                        {
                            _logger.Log("valid DoH stamp", Category.Info, Priority.Low);
                            addStamp = true;
                        }
                    }
                    else
                    {
                        //unsupported stamp
                        _logger.Log("unsupported stamp type", Category.Warn, Priority.Medium);
                        addStamp = false;
                    }

                    if (addStamp)
                    {
                        _eventAggregator.GetEvent <StampAddedEvent>().Publish(new Proxy
                        {
                            Name  = Global.DefaultCustomFilterKey,
                            Stamp = Stamp.Trim()
                        });
                        NavigateToMain();
                    }
                    else
                    {
                        MessageQueue.Enqueue("not a valid stamp://");
                    }
                }
                else
                {
                    MessageQueue.Enqueue("not a valid stamp://");
                }
            }
        }
 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);
 }
        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;
        }