public void TestBlacklistDns() { IPBanConfig config = IPBanConfig.LoadFromXml("<?xml version='1.0'?><configuration>" + "<appSettings><add key='Blacklist' value='test.com' /></appSettings></configuration>", this); Assert.IsFalse(config.IsWhitelisted("99.88.77.66")); Assert.IsTrue(config.IsBlackListed("99.88.77.66")); }
public void TestListComments() { IPBanConfig config = IPBanConfig.LoadFromXml("<?xml version='1.0'?><configuration>" + "<appSettings><add key='Whitelist' value='99.99.99.99?TestIP?2020-05-25," + "88.88.88.88?TestIP2?2020-05-24' /></appSettings></configuration>", DefaultDnsLookup.Instance); Assert.AreEqual(string.Join(",", config.Whitelist.OrderBy(i => i)), "88.88.88.88,99.99.99.99"); Assert.IsTrue(config.IsWhitelisted("99.99.99.99")); Assert.IsTrue(config.IsWhitelisted("88.88.88.88")); Assert.IsFalse(config.IsWhitelisted("77.77.77.77")); }
public async Task TestDefaultConfig() { // ensure config file is read properly IPBanService service = IPBanService.CreateAndStartIPBanTestService <IPBanService>(); try { IPBanConfig cfg = service.Config; Assert.IsNotNull(cfg); Assert.AreEqual(TimeSpan.FromDays(1.0), cfg.BanTimes.First()); Assert.AreEqual(1, cfg.BanTimes.Length); Assert.IsEmpty(cfg.BlacklistFilter.IPAddressRanges); Assert.IsTrue(string.IsNullOrEmpty(cfg.BlacklistFilter.Regex?.ToString())); Assert.IsFalse(cfg.ClearBannedIPAddressesOnRestart); Assert.IsFalse(cfg.ClearFailedLoginsOnSuccessfulLogin); Assert.IsFalse(cfg.ProcessInternalIPAddresses); Assert.AreEqual(TimeSpan.FromSeconds(15.0), cfg.CycleTime); Assert.AreEqual(TimeSpan.FromDays(1.0), cfg.ExpireTime); Assert.AreEqual("https://checkip.amazonaws.com/", cfg.ExternalIPAddressUrl); Assert.AreEqual(5, cfg.FailedLoginAttemptsBeforeBan); Assert.AreEqual(20, cfg.FailedLoginAttemptsBeforeBanUserNameWhitelist); Assert.AreEqual("IPBan_", cfg.FirewallRulePrefix); Assert.AreEqual(TimeSpan.FromSeconds(1.0), cfg.MinimumTimeBetweenFailedLoginAttempts); Assert.IsEmpty(cfg.ProcessToRunOnBan); Assert.IsEmpty(cfg.ProcessToRunOnUnban); Assert.IsFalse(cfg.ResetFailedLoginCountForUnbannedIPAddresses); Assert.IsTrue(cfg.UseDefaultBannedIPAddressHandler); Assert.IsEmpty(cfg.UserNameWhitelist); Assert.IsEmpty(cfg.UserNameWhitelistRegex); Assert.IsEmpty(cfg.WhitelistFilter.IPAddressRanges); Assert.IsTrue(string.IsNullOrEmpty(cfg.WhitelistFilter.Regex?.ToString())); Assert.AreEqual(0, cfg.ExtraRules.Count); Assert.AreEqual(cfg.FirewallUriRules.Trim(), "EmergingThreats,01:00:00:00,https://rules.emergingthreats.net/fwrules/emerging-Block-IPs.txt"); AssertLogFilesToParse(cfg); AssertEventViewer(cfg); string xml = await service.ConfigReaderWriter.ReadConfigAsync(); IPBanConfig prod = IPBanConfig.LoadFromXml(xml); Assert.IsTrue(prod.UseDefaultBannedIPAddressHandler); } finally { IPBanService.DisposeIPBanTestService(service); } }
public async Task TestDefaultConfig() { // ensure config file is read properly IPBanService service = IPBanService.CreateAndStartIPBanTestService <IPBanService>(); try { IPBanConfig cfg = service.Config; Assert.IsNotNull(cfg); Assert.AreEqual(TimeSpan.FromDays(1.0), cfg.BanTimes.First()); Assert.AreEqual(1, cfg.BanTimes.Length); Assert.IsEmpty(cfg.BlackList); Assert.IsEmpty(cfg.BlackListRegex); Assert.IsFalse(cfg.ClearBannedIPAddressesOnRestart); Assert.AreEqual(TimeSpan.FromSeconds(15.0), cfg.CycleTime); Assert.AreEqual(TimeSpan.FromDays(1.0), cfg.ExpireTime); Assert.AreEqual("https://checkip.amazonaws.com/", cfg.ExternalIPAddressUrl); Assert.AreEqual(5, cfg.FailedLoginAttemptsBeforeBan); Assert.AreEqual(20, cfg.FailedLoginAttemptsBeforeBanUserNameWhitelist); Assert.AreEqual(1, cfg.FirewallOSAndType.Count); Assert.AreEqual("*:Default", cfg.FirewallOSAndType.Keys.First() + ":" + cfg.FirewallOSAndType.Values.First()); Assert.AreEqual("IPBan_", cfg.FirewallRulePrefix); Assert.AreEqual(TimeSpan.FromSeconds(1.0), cfg.MinimumTimeBetweenFailedLoginAttempts); Assert.IsEmpty(cfg.ProcessToRunOnBan); Assert.IsFalse(cfg.ResetFailedLoginCountForUnbannedIPAddresses); Assert.IsTrue(cfg.UseDefaultBannedIPAddressHandler); Assert.IsEmpty(cfg.UserNameWhitelist); Assert.IsEmpty(cfg.UserNameWhitelistRegex); Assert.IsEmpty(cfg.WhiteList); Assert.IsEmpty(cfg.WhiteListRegex); Assert.AreEqual(0, cfg.ExtraRules.Count); AssertLogFilesToParse(cfg); AssertEventViewer(cfg); string xml = await service.ReadConfigAsync(); IPBanConfig prod = IPBanConfig.LoadFromXml(xml, null); Assert.IsTrue(prod.UseDefaultBannedIPAddressHandler); } finally { IPBanService.DisposeIPBanTestService(service); } }
/// <summary> /// Config from ConfigXml /// </summary> public IPBanConfig GetConfig() => config ??= IPBanConfig.LoadFromXml(ConfigXml);