示例#1
0
        /// <summary>
        /// Create a new ban for the given ip lasting for the given period.
        /// </summary>
        public static void Ban(string ip, TimeSpan @for, string reason)
        {
            // Never ban an internal IP
            if (Current.IsPrivateIP(ip))
            {
                return;
            }

            var db = Current.WriteDB;

            var now = Current.Now;

            var newBan =
                new IPBan
            {
                CreationDate   = now,
                ExpirationDate = now + @for,
                IP             = ip,
                Reason         = reason
            };

            db.IPBans.InsertOnSubmit(newBan);
            db.SubmitChanges();

            var period = new BanPeriod {
                CreationDate = newBan.CreationDate, ExpirationDate = newBan.ExpirationDate
            };

            BannedIPCache.AddOrUpdate(ip, period, (string key, BanPeriod old) => period);
        }
示例#2
0
        /// <summary>
        /// Call to periodically slurp down new bans and the like.
        /// 
        /// We want this stuff in the DB for persistence (so an App cycle doesn't lift bans),
        /// but we don't want an extra DB hit on every page either.
        /// 
        /// Thus, we maintain a copy of the "important" parts of the IPBans table in memory.
        /// </summary>
        private static void TryRefreshCache()
        {
            if (Current.Now < NextRefresh) return;

            lock (UpdateLock)
            {
                if (Current.Now < NextRefresh) return;

                var now = Current.Now;

                var getNewBansFrom = BannedIPCache.Count == 0 ? SqlDateTime.MinValue.Value : BannedIPCache.Max(i => i.Value.CreationDate);

                var newBans = Current.ReadDB.IPBans.Where(b => b.CreationDate > getNewBansFrom);

                foreach(var b in newBans){
                    var period = new BanPeriod{ CreationDate = b.CreationDate, ExpirationDate = b.ExpirationDate};
                    BannedIPCache.AddOrUpdate(b.IP, period, (string key, BanPeriod old) => period);
                }

                var expiredBans = BannedIPCache.Where(i => i.Value.ExpirationDate < now).Select(i => i.Key);

                BanPeriod ignored;
                foreach (var expired in expiredBans) BannedIPCache.TryRemove(expired, out ignored);

                NextRefresh = Current.Now + TimeSpan.FromMinutes(5);
            }
        }
示例#3
0
        /// <summary>
        /// Call to periodically slurp down new bans and the like.
        ///
        /// We want this stuff in the DB for persistence (so an App cycle doesn't lift bans),
        /// but we don't want an extra DB hit on every page either.
        ///
        /// Thus, we maintain a copy of the "important" parts of the IPBans table in memory.
        /// </summary>
        private static void TryRefreshCache()
        {
            if (Current.Now < NextRefresh)
            {
                return;
            }

            lock (UpdateLock)
            {
                if (Current.Now < NextRefresh)
                {
                    return;
                }

                var now = Current.Now;

                var getNewBansFrom = BannedIPCache.Count == 0 ? SqlDateTime.MinValue.Value : BannedIPCache.Max(i => i.Value.CreationDate);

                var newBans = Current.ReadDB.IPBans.Where(b => b.CreationDate > getNewBansFrom);

                foreach (var b in newBans)
                {
                    var period = new BanPeriod {
                        CreationDate = b.CreationDate, ExpirationDate = b.ExpirationDate
                    };
                    BannedIPCache.AddOrUpdate(b.IP, period, (string key, BanPeriod old) => period);
                }

                var expiredBans = BannedIPCache.Where(i => i.Value.ExpirationDate < now).Select(i => i.Key);

                BanPeriod ignored;
                foreach (var expired in expiredBans)
                {
                    BannedIPCache.TryRemove(expired, out ignored);
                }

                NextRefresh = Current.Now + TimeSpan.FromMinutes(5);
            }
        }
示例#4
0
        /// <summary>
        /// Create a new ban for the given ip lasting for the given period.
        /// </summary>
        public static void Ban(string ip, TimeSpan @for, string reason)
        {
            // Never ban an internal IP
            if (Current.IsPrivateIP(ip)) return;

            var db = Current.WriteDB;

            var now = Current.Now;

            var newBan =
                new IPBan
                {
                    CreationDate = now,
                    ExpirationDate = now + @for,
                    IP = ip,
                    Reason = reason
                };

            db.IPBans.InsertOnSubmit(newBan);
            db.SubmitChanges();

            var period = new BanPeriod { CreationDate = newBan.CreationDate, ExpirationDate = newBan.ExpirationDate };
            BannedIPCache.AddOrUpdate(ip, period, (string key, BanPeriod old) => period);
        }
示例#5
0
 public BanPeriod Ban(BanPeriod period, Guid userId)
 {
     return(period);
 }