/// <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); }
/// <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); } }
/// <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); } }
/// <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); }
public BanPeriod Ban(BanPeriod period, Guid userId) { return(period); }