示例#1
0
        /// <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);
        }
示例#2
0
        /// <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>());
        }
示例#3
0
        /// <summary>Fired when a player joins the server</summary>
        /// <param name="args">The JoinEventArgs object</param>
        public void OnJoin(JoinEventArgs args)
        {
            //If player was disconnected by another component, skip
            if (args.Handled)
            {
                return;
            }
            //If IP banning isn't used, skip
            if (!TShock.Config.EnableIPBans)
            {
                return;
            }
            //Creates TSPlayer player object for easy access
            TSPlayer player = TShock.Players[args.Who];
            //Searches a ban by player's IP
            CIDRBan ban = cidrbans.GetCIDRBanByIP(player.IP);

            //If no ban is found or ban has expired, skip
            if (ban == null)
            {
                return;
            }
            //DateTime exp object
            DateTime exp;

            //If player's ban has no expiration date, say ban forever
            if (!DateTime.TryParse(ban.Expiration, out exp))
            {
                player.Disconnect("You are banned forever: " + ban.Reason);
            }
            //If player's ban is temporary, check if it has expired or not
            else
            {
                //If player's ban has expired, remove the ban
                if (DateTime.UtcNow >= exp)
                {
                    cidrbans.DelCIDRBanByRange(ban.CIDR);
                    return;
                }
                //Finds time left from now to expiration date
                TimeSpan ts = exp - DateTime.UtcNow;
                //Makes 30 days count shows as one month
                int months = ts.Days / 30;
                //Converts timespan object into time string to inform player
                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));
                }
            }
            //Sets player's connection handled
            args.Handled = true;
        }