Exemplo n.º 1
0
        private void UpdateBannedIPAddressesOnStart()
        {
            if (updateBannedIPAddressesOnStartCalled)
            {
                return;
            }
            updateBannedIPAddressesOnStartCalled = true;

            if (Config.ClearBannedIPAddressesOnRestart)
            {
                Logger.Warn("Clearing all banned ip addresses on start because ClearBannedIPAddressesOnRestart is set");
                Firewall.Truncate();
                ipDB.Truncate(true);
            }
            else
            {
                DateTime now    = UtcNow;
                DateTime banEnd = now + Config.BanTimes.First();

                Logger.Warn("Syncing firewall and {0} database...", IPBanDB.FileName);

                // bring all firewall ip into the database, if they already exist they will be ignored
                ipDB.SetBannedIPAddresses(Firewall.EnumerateBannedIPAddresses().Select(i => new Tuple <string, DateTime, DateTime>(i, now, banEnd)), UtcNow);

                // remove any rows where the ip address was going to be removed
                ipDB.DeletePendingRemoveIPAddresses();

                // ensure firewall is up to date with all the correct ip addresses, if any ip are in the db but not in the firewall, they will
                // get synced up here
                Firewall.BlockIPAddresses(null, ipDB.EnumerateBannedIPAddresses()).Sync();

                // set firewall update flag, if any deltas are lingering in the db (state = add pending or remove pending) they will get
                // processed on the next cycle
                firewallNeedsBlockedIPAddressesUpdate = true;

                // report on initial count
                int count = ipDB.GetIPAddressCount();
                Logger.Warn("{0} total ip addresses in the {1} database", count, IPBanDB.FileName);
            }
        }