private void ProcessBanFileOnStart() { lock (ipBlocker) { ipBlocker.Clear(); ipBlockerDate.Clear(); if (File.Exists(config.BanFile)) { string[] lines = File.ReadAllLines(config.BanFile); if (config.BanFileClearOnRestart) { File.Delete(config.BanFile); } else { IPAddress tmp; foreach (string ip in lines) { string ipTrimmed = ip.Trim(); if (IPAddress.TryParse(ipTrimmed, out tmp)) { IPBlockCount blockCount = new IPBlockCount(); blockCount.IncrementCount(); ipBlocker[ip] = blockCount; ipBlockerDate[ip] = DateTime.UtcNow; } } } } } IPBanWindowsFirewall.DeleteRules(); ExecuteBanScript(); }
private void Initialize() { ReadAppSettings(); IPBanWindowsFirewall.Initialize(config.RuleName); ProcessBanFileOnStart(); SetupEventLogWatcher(); LogInitialConfig(); }
private void ExecuteBanScript() { lock (ipBlocker) { IPBanWindowsFirewall.CreateRules(ipBlockerDate.Keys.ToArray()); File.WriteAllLines(config.BanFile, ipBlocker.Keys.ToArray()); } }
private void ExecuteBanScript() { lock (ipAddressesAndBlockCounts) { // create rules for all banned ip addresses IPBanWindowsFirewall.CreateRules(ipAddressesAndBanDate.Keys.ToArray()); // write all banned ip addresses using (StreamWriter writer = File.CreateText(config.BanFile)) { foreach (KeyValuePair <string, DateTime> ipAndBanDate in ipAddressesAndBanDate) { writer.WriteLine("{0}\t{1}", ipAndBanDate.Key, ipAndBanDate.Value.ToString("o")); } } } }