/// <summary>Search CIDR bans with given IP</summary> /// <param name="IP">IP string for searching</param> /// <returns>CIDRBan object</returns> public CIDRBan GetCIDRBanByIP(string check) { List <CIDRBan> banlist = new List <CIDRBan>(); try { using (var reader = db.QueryReader("SELECT * FROM CIDRBans")) { while (reader.Read()) { banlist.Add(new CIDRBan(reader.Get <string>("CIDR"), reader.Get <string>("Reason"), reader.Get <string>("BanningUser"), reader.Get <string>("Date"), reader.Get <string>("Expiration"))); } //Check if the IP matches any of range in CIDR Ban List foreach (CIDRBan ban in banlist) { if (CIDRBan.Check(check, ban.CIDR)) { return(ban); } //If not, continue to next CIDR ban in range } } } catch (Exception ex) { TShock.Log.Error(ex.ToString()); } return(null); }
/// <summary>Delete all CIDR ranges matched up with an IP</summary> /// <param name="ip">Specified IP</param> /// <returns>List of CIDR range strings</returns> public List <string> DelCIDRBanByIP(string ip) { List <CIDRBan> banlist = new List <CIDRBan>(); List <string> removelist = new List <string>(); try { using (var reader = db.QueryReader("SELECT * FROM CIDRBans")) { while (reader.Read()) { banlist.Add(new CIDRBan(reader.Get <string>("CIDR"), reader.Get <string>("Reason"), reader.Get <string>("BanningUser"), reader.Get <string>("Date"), reader.Get <string>("Expiration"))); } //Check if the IP matches any of range in CIDR Ban List foreach (CIDRBan ban in banlist) { if (CIDRBan.Check(ip, ban.CIDR)) { removelist.Add(ban.CIDR); } //If true, add it to list for removal preparation //Loop until every CIDR range in list is checked } } //Start removing everything in prepared list foreach (string removed in removelist) { db.Query("DELETE FROM CIDRBans WHERE CIDR = @0", removed); } //Inform players return(removelist); } catch (Exception ex) { TShock.Log.Error(ex.ToString()); } return(new List <string>()); }