Exemple #1
0
        /// <summary>
        /// Checks if an IP from the IpAddressCollection or the IpNetworkCollection is banned
        /// </summary>
        /// <param name="ipAddress">IP address</param>
        /// <returns>False or true</returns>
        public static bool IsIpAddressBanned(BannedIpAddress ipAddress)
        {
            // Check if the IP is valid
            if (!IsValidIp(ipAddress.Address.Trim()))
            {
                throw new NopException("The following isn't a valid IP address: " + ipAddress.Address);
            }

            // Check if the IP is in the banned IP addresses
            BannedIpAddressCollection ipAddressCollection = GetBannedIpAddressAll();

            //if (ipAddressCollection.Contains(ipAddress))
            foreach (BannedIpAddress ip in ipAddressCollection)
            {
                if (IsEqual(ipAddress.Address, ip.Address))
                {
                    return(true);
                }
            }

            // Check if the IP is in the banned IP networks
            BannedIpNetworkCollection ipNetworkCollection = GetBannedIpNetworkAll();

            foreach (BannedIpNetwork ipNetwork in ipNetworkCollection)
            {
                // Get the first and last IPs in the network
                string[] rangeItem = ipNetwork.ToString().Split("-".ToCharArray());

                // Get the exceptions as a list
                List <string> exceptionItem = new List <string>();
                exceptionItem.AddRange(ipNetwork.IpException.ToString().Split(";".ToCharArray()));
                // Check if the IP is an exception
                if (exceptionItem.Contains(ipAddress.Address.ToString()))
                {
                    return(false);
                }

                // Check if the 1st IP is valid
                if (!IsValidIp(rangeItem[0].Trim()))
                {
                    throw new NopException("The following isn't a valid IP address: " + rangeItem[0]);
                }

                // Check if the 2nd IP is valid
                if (!IsValidIp(rangeItem[1].Trim()))
                {
                    throw new NopException("The following isn't a valid IP address: " + rangeItem[1]);
                }

                //Check if the IP is in the given range
                if (IsGreaterOrEqual(ipAddress.Address.ToString(), rangeItem[0].Trim()) &&
                    IsLessOrEqual(ipAddress.Address.ToString(), rangeItem[1].Trim()))
                {
                    return(true);
                }
            }
            // Return false otherwise
            return(false);
        }
        private void BindGrid()
        {
            BannedIpAddressCollection ipAddressCollection = IpBlacklistManager.GetBannedIpAddressAll();

            gvBannedIpAddress.DataSource = ipAddressCollection;
            gvBannedIpAddress.DataBind();

            BannedIpNetworkCollection ipNetworkCollection = IpBlacklistManager.GetBannedIpNetworkAll();

            gvBannedIpNetwork.DataSource = ipNetworkCollection;
            gvBannedIpNetwork.DataBind();
        }
Exemple #3
0
        private static BannedIpAddressCollection DBMapping(DBBannedIpAddressCollection dbCollection)
        {
            if (dbCollection == null)
            {
                return(null);
            }

            var ipCollection = new BannedIpAddressCollection();

            foreach (var dbItem in dbCollection)
            {
                var item = DBMapping(dbItem);
                ipCollection.Add(item);
            }
            return(ipCollection);
        }
Exemple #4
0
        /// <summary>
        /// Gets all IP addresses
        /// </summary>
        /// <returns>An IP address collection</returns>
        public static BannedIpAddressCollection GetBannedIpAddressAll()
        {
            string key  = BLACKLIST_ALLIP_KEY;
            object obj2 = NopCache.Get(key);

            if (CacheEnabled && (obj2 != null))
            {
                return((BannedIpAddressCollection)obj2);
            }

            DBBannedIpAddressCollection dbCollection = DBProviderManager <DBBlacklistProvider> .Provider.GetIpAddressAll();

            BannedIpAddressCollection collection = DBMapping(dbCollection);

            if (IpBlacklistManager.CacheEnabled)
            {
                NopCache.Max(key, collection);
            }
            return(collection);
        }