Ejemplo n.º 1
0
        internal async Task UpdateConfiguration()
        {
            try
            {
                ConfigFilePath = (!File.Exists(ConfigFilePath) ? Path.Combine(AppContext.BaseDirectory, IPBanService.ConfigFileName) : ConfigFilePath);
                string newXml = await ConfigReaderWriter.CheckForConfigChange();

                if (!string.IsNullOrWhiteSpace(newXml))
                {
                    IPBanConfig oldConfig = Config;
                    IPBanConfig newConfig = IPBanConfig.LoadFromXml(newXml, DnsLookup);
                    ConfigChanged?.Invoke(newConfig);
                    whitelistChanged = (Config is null || Config.Whitelist != newConfig.Whitelist || Config.WhitelistRegex != newConfig.WhitelistRegex);
                    Config           = newConfig;
                    LoadFirewall(oldConfig);
                    ParseAndAddUriFirewallRules(newConfig);
                    Logger.Info("Config file changed");
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }

            if (Config is null)
            {
                throw new ApplicationException("Configuration failed to load, make sure to check for XML errors or unblock all the files.");
            }
            if (Firewall is null)
            {
                throw new ApplicationException("Firewall failed to load, check that your firewall is enabled and setup in configuration properly");
            }

            // set or unset default banned ip address handler based on config
            if (Config.UseDefaultBannedIPAddressHandler && BannedIPAddressHandler is null)
            {
                BannedIPAddressHandler = new DefaultBannedIPAddressHandler();
            }
            else if (!Config.UseDefaultBannedIPAddressHandler && BannedIPAddressHandler != null && BannedIPAddressHandler is DefaultBannedIPAddressHandler)
            {
                BannedIPAddressHandler = NullBannedIPAddressHandler.Instance;
            }

            // will only execute once
            UpdateBannedIPAddressesOnStart();

            // will only execute once
            SetupWindowsEventViewer();
        }
Ejemplo n.º 2
0
        internal async Task UpdateConfiguration()
        {
            try
            {
                ConfigFilePath = (!File.Exists(ConfigFilePath) ? Path.Combine(AppContext.BaseDirectory, IPBanConfig.DefaultFileName) : ConfigFilePath);
                var configChange = await ConfigReaderWriter.CheckForConfigChange();

                if (!string.IsNullOrWhiteSpace(configChange.Item1))
                {
                    IPBanConfig oldConfig = Config;
                    IPBanConfig newConfig = IPBanConfig.LoadFromXml(configChange.Item1, DnsLookup, DnsList, RequestMaker);
                    ConfigChanged?.Invoke(newConfig);
                    whitelistChanged = (Config is null || Config.Whitelist != newConfig.Whitelist || Config.WhitelistRegex != newConfig.WhitelistRegex);
                    Config           = newConfig;
                    LoadFirewall(oldConfig);
                    ParseAndAddUriFirewallRules(newConfig);

                    // if the config change was not a force refresh with no actual config values changed, log it
                    if (!configChange.Item2)
                    {
                        Logger.Info("Config file changed");
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }

            if (Config is null)
            {
                throw new ApplicationException("Configuration failed to load, make sure to check for XML errors or unblock all the files.");
            }
            if (Firewall is null)
            {
                throw new ApplicationException("Firewall failed to load, check that your firewall is enabled and setup in configuration properly");
            }

            // set or unset default banned ip address handler based on config
            if (Config.UseDefaultBannedIPAddressHandler && BannedIPAddressHandler is null)
            {
                BannedIPAddressHandler = new DefaultBannedIPAddressHandler();
            }
            else if (!Config.UseDefaultBannedIPAddressHandler && BannedIPAddressHandler != null && BannedIPAddressHandler is DefaultBannedIPAddressHandler)
            {
                BannedIPAddressHandler = NullBannedIPAddressHandler.Instance;
            }
        }
Ejemplo n.º 3
0
        internal async Task ReadAppSettings()
        {
            try
            {
                ConfigFilePath = (!File.Exists(ConfigFilePath) ? Path.Combine(AppDomain.CurrentDomain.BaseDirectory, IPBanService.ConfigFileName) : ConfigFilePath);
                string newXml = await ConfigReaderWriter.CheckForConfigChange();

                if (!string.IsNullOrWhiteSpace(newXml))
                {
                    IPBanConfig oldConfig = Config;
                    IPBanConfig newConfig = IPBanConfig.LoadFromXml(newXml, DnsLookup);
                    UpdateLogFiles(newConfig);
                    whitelistChanged = (Config == null || Config.WhiteList != newConfig.WhiteList || Config.WhiteListRegex != newConfig.WhiteListRegex);
                    Config           = newConfig;
                    LoadFirewall(oldConfig);
                }
            }
            catch (Exception ex)
            {
                IPBanLog.Error(ex);

                if (Config == null)
                {
                    throw new ApplicationException("Configuration failed to load, make sure to check for XML errors or unblock all the files.", ex);
                }
            }

            // set or unset default banned ip address handler based on config
            if (Config.UseDefaultBannedIPAddressHandler && BannedIPAddressHandler == null)
            {
                BannedIPAddressHandler = new DefaultBannedIPAddressHandler();
            }
            else if (!Config.UseDefaultBannedIPAddressHandler && BannedIPAddressHandler != null && BannedIPAddressHandler is DefaultBannedIPAddressHandler)
            {
                BannedIPAddressHandler = null;
            }
        }