Exemplo n.º 1
0
        public void InitializeIpBlockList(string path)
        {
            Logger.Debug("Read IP block list from {0}", Path.GetFileName(path));
            var content = File.ReadAllText(path);

            _blockedIpAddresses = new List <BlockedIpAddresses>();
            foreach (var line in content.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries))
            {
                var trimmedLine = line?.Trim();

                if (string.IsNullOrWhiteSpace(trimmedLine) || trimmedLine.StartsWith("#"))
                {
                    continue;
                }

                BlockedIpAddresses blockedIpAddresses;
                if (BlockedIpAddresses.TryParseCIDRNotation(trimmedLine, out blockedIpAddresses) ||
                    BlockedIpAddresses.TryParseIpAddressRange(trimmedLine, out blockedIpAddresses))
                {
                    _blockedIpAddresses.Add(blockedIpAddresses);
                    continue;
                }

                Logger.Warn("The blocked IP address \"{0}\" could not be parsed", trimmedLine);
            }

            if (_blockedIpAddresses.Count == 1)
            {
                Logger.Info("Loaded {0} IP address block", _blockedIpAddresses.Count);
            }
            else if (_blockedIpAddresses.Count > 1)
            {
                Logger.Info("Loaded {0} IP address blocks", _blockedIpAddresses.Count);
            }
        }
Exemplo n.º 2
0
 private void AddInternal(string addressStr)
 {
     try
     {
         BlockedIpAddresses.Add(addressStr);
     }
     catch
     {
         // this will be thrown if there is already an element in there, so just ignore.
     }
 }
Exemplo n.º 3
0
 public bool IsBlockedAddress(string addressStr)
 {
     return(BlockedIpAddresses.Contains(addressStr));
 }