Example #1
0
        public static bool FileContainsIP(string fileName, string ip)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException("fileName");
            }
            else if (string.IsNullOrEmpty(ip))
            {
                throw new ArgumentNullException("ip");
            }

            // TODOZ Handle IPv6
            string[] ConnectionOctets = ip.Split('.');
            if (ConnectionOctets.Length == 4)
            {
                string[] FileIPs = FileUtils.FileReadAllLines(fileName);
                foreach (string FileIP in FileIPs)
                {
                    if (FileIP.StartsWith(";"))
                    {
                        continue;
                    }

                    string[] FileOctets = FileIP.Split('.');
                    if (FileOctets.Length == 4)
                    {
                        bool Match = true;
                        for (int i = 0; i < 4; i++)
                        {
                            if ((FileOctets[i] == "*") || (FileOctets[i] == ConnectionOctets[i]))
                            {
                                // We still have a match
                                continue;
                            }
                            else
                            {
                                // No longer have a match
                                Match = false;
                                break;
                            }
                        }

                        // If we still have a match after the loop, it's a banned IP
                        if (Match)
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
Example #2
0
        private bool FileContainsIP(string filename, string ip)
        {
            // TODOZ Handle IPv6
            string[] ConnectionOctets = ip.Split('.');
            if (ConnectionOctets.Length == 4)
            {
                string[] FileIPs = FileUtils.FileReadAllLines(filename);
                foreach (string FileIP in FileIPs)
                {
                    if (FileIP.StartsWith(";"))
                    {
                        continue;
                    }

                    string[] FileOctets = FileIP.Split('.');
                    if (FileOctets.Length == 4)
                    {
                        bool Match = true;
                        for (int i = 0; i < 4; i++)
                        {
                            if ((FileOctets[i] == "*") || (FileOctets[i] == ConnectionOctets[i]))
                            {
                                // We still have a match
                                continue;
                            }
                            else
                            {
                                // No longer have a match
                                Match = false;
                                break;
                            }
                        }

                        // If we still have a match after the loop, it's a banned IP
                        if (Match)
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }