/// <summary>Delete all CIDR ranges matched up with an IP from database</summary> /// <returns>List of CIDR ranges removed from database</returns> public List <string> DelCIDRBanByIP(string ip) { try { // check all delete canditates List <CIDRBan> banlist = GetCIDRBanList(); List <string> removelist = new List <string>(); foreach (CIDRBan ban in banlist) { if (CIDRBan.Check(ip, ban.CIDR)) { removelist.Add(ban.CIDR); } } // remove canditates from database foreach (string removed in removelist) { db.Query("DELETE FROM CIDRBans WHERE CIDR = @0", removed); } return(removelist); } catch (Exception ex) { TShock.Log.Error(ex.ToString()); } return(new List <string>()); }
/// <summary>Search CIDR bans with given IP</summary> /// <param name="IP">IP string for searching</param> /// <returns>First CIDRBan object found</returns> public CIDRBan GetCIDRBanByIP(string check) { try { // search for matching range in database List <CIDRBan> banlist = GetCIDRBanList(); foreach (CIDRBan ban in banlist) { if (CIDRBan.Check(check, ban.CIDR)) { return(ban); } } } catch (Exception ex) { TShock.Log.Error(ex.ToString()); } return(null); }
public void OnJoin(JoinEventArgs args) { if (args.Handled) { return; } if (!TShock.Config.EnableIPBans) { return; } // search a ban by player's IP TSPlayer player = TShock.Players[args.Who]; CIDRBan ban = cidrbans.GetCIDRBanByIP(player.IP); if (ban == null) { return; } // parse expiration date DateTime exp; if (!DateTime.TryParse(ban.Expiration, out exp)) { // no expiration date implies permaban player.Disconnect("You are banned forever: " + ban.Reason); } else { // remove a ban past the expiration date if (DateTime.UtcNow >= exp) { cidrbans.DelCIDRBanByRange(ban.CIDR); return; } // generate remaining ban time string for player TimeSpan ts = exp - DateTime.UtcNow; int months = ts.Days / 30; if (months > 0) { player.Disconnect(String.Format("You are banned for {0} month{1} and {2} day{3}: {4}", months, months == 1 ? "" : "s", ts.Days, ts.Days == 1 ? "" : "s", ban.Reason)); } else if (ts.Days > 0) { player.Disconnect(String.Format("You are banned for {0} day{1} and {2} hour{3}: {4}", ts.Days, ts.Days == 1 ? "" : "s", ts.Hours, ts.Hours == 1 ? "" : "s", ban.Reason)); } else if (ts.Hours > 0) { player.Disconnect(String.Format("You are banned for {0} hour{1} and {2} minute{3}: {4}", ts.Hours, ts.Hours == 1 ? "" : "s", ts.Minutes, ts.Minutes == 1 ? "" : "s", ban.Reason)); } else if (ts.Minutes > 0) { player.Disconnect(String.Format("You are banned for {0} minute{1} and {2} second{3}: {4}", ts.Minutes, ts.Minutes == 1 ? "" : "s", ts.Seconds, ts.Seconds == 1 ? "" : "s", ban.Reason)); } else { player.Disconnect(String.Format("You are banned for {0} second{1}: {2}", ts.Seconds, ts.Seconds == 1 ? "" : "s", ban.Reason)); } } args.Handled = true; }