Beispiel #1
0
        /// <summary>
        /// Static constructor - migrate config file from DigitalRuby.IPBan.dll.config to ipban.config
        /// </summary>
        static IPBanConfig()
        {
            string oldConfigPath = null;
            string newConfigPath = null;

            try
            {
                // move DigitalRuby.IPBan.dll.config to ipban.config
                oldConfigPath = Path.Combine(AppContext.BaseDirectory, "DigitalRuby.IPBan.dll.config");
                newConfigPath = Path.Combine(AppContext.BaseDirectory, "ipban.config");
                if (File.Exists(oldConfigPath))
                {
                    ExtensionMethods.Retry(() =>
                    {
                        File.Copy(oldConfigPath, newConfigPath, true);
                        File.Delete(oldConfigPath);
                    });
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "Failed to copy old config file at {0} to new config file at {1}",
                             oldConfigPath, newConfigPath);
            }
        }
Beispiel #2
0
        private void PopulateList(HashSet <System.Net.IPAddress> set,
                                  HashSet <IPAddressRange> ranges,
                                  HashSet <string> others,
                                  ref Regex regex,
                                  string setValue,
                                  string regexValue)
        {
            setValue   = (setValue ?? string.Empty).Trim();
            regexValue = (regexValue ?? string.Empty).Replace("*", @"[0-9A-Fa-f:]+?").Trim();
            set.Clear();
            regex = null;

            if (!string.IsNullOrWhiteSpace(setValue))
            {
                foreach (string entry in setValue.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(e => e.Trim()))
                {
                    string entryWithoutComment = entry;
                    int    pos = entryWithoutComment.IndexOf('?');
                    if (pos >= 0)
                    {
                        entryWithoutComment = entryWithoutComment.Substring(0, pos);
                    }
                    entryWithoutComment = entryWithoutComment.Trim();
                    if (!ignoreListEntries.Contains(entryWithoutComment))
                    {
                        if (IPAddressRange.TryParse(entryWithoutComment, out IPAddressRange range))
                        {
                            if (range.Begin.Equals(range.End))
                            {
                                set.Add(range.Begin);
                            }
                            else
                            {
                                ranges.Add(range);
                            }
                        }
                        else if (Uri.CheckHostName(entryWithoutComment) != UriHostNameType.Unknown)
                        {
                            try
                            {
                                // add entries for each ip address that matches the dns entry
                                IPAddress[] addresses = null;
                                ExtensionMethods.Retry(() => addresses = dns.GetHostAddressesAsync(entryWithoutComment).Sync());
                                foreach (IPAddress adr in addresses)
                                {
                                    set.Add(adr);
                                }
                            }
                            catch (Exception ex)
                            {
                                Logger.Error(ex, "Unable to resolve dns for {0}", entryWithoutComment);

                                // eat exception, nothing we can do
                                others.Add(entryWithoutComment);
                            }
                        }
                        else
                        {
                            others.Add(entryWithoutComment);
                        }
                    }
                }
            }

            if (!string.IsNullOrWhiteSpace(regexValue))
            {
                regex = ParseRegex(regexValue);
            }
        }