Ejemplo n.º 1
0
        private static void AppendRange(StringBuilder b, PortRange range)
        {
            string rangeString = range.ToString();

            if (rangeString != null)
            {
                b.Append(range);
                b.Append(',');
            }
        }
Ejemplo n.º 2
0
        private void ParseFirewallBlockRules()
        {
            string firewallBlockRuleString = null;

            GetConfig <string>("FirewallRules", ref firewallBlockRuleString);
            firewallBlockRuleString = (firewallBlockRuleString ?? string.Empty).Trim();
            if (firewallBlockRuleString.Length == 0)
            {
                return;
            }
            IEnumerable <string> firewallBlockRuleList = firewallBlockRuleString.Trim().Split('\n').Select(s => s.Trim()).Where(s => s.Length != 0);

            foreach (string firewallBlockRule in firewallBlockRuleList)
            {
                string[] pieces = firewallBlockRule.Split(';');
                if (pieces.Length == 5)
                {
                    IPBanFirewallRule firewallBlockRuleObj = new IPBanFirewallRule
                    {
                        Block           = (pieces[1].Equals("block", StringComparison.OrdinalIgnoreCase)),
                        IPAddressRanges = pieces[2].Split(',').Select(p => IPAddressRange.Parse(p)).ToList(),
                        Name            = "EXTRA_" + pieces[0].Trim(),
                        AllowPortRanges = pieces[3].Split(',').Select(p => PortRange.Parse(p)).Where(p => p.MinPort >= 0).ToList(),
                        PlatformRegex   = new Regex(pieces[4].Replace('*', '.'), RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)
                    };
                    if (firewallBlockRuleObj.PlatformRegex.IsMatch(OSUtility.Name))
                    {
                        extraRules.Add(firewallBlockRuleObj);
                    }
                }
                else
                {
                    Logger.Warn("Firewall block rule entry should have 3 comma separated pieces: name;ips;ports. Invalid entry: {0}", firewallBlockRule);
                }
            }
        }
Ejemplo n.º 3
0
 public MemoryFirewallRuleRanges(List <IPAddressRange> ipRanges, List <PortRange> allowedPorts, bool block)
 {
     allowedPorts = (allowedPorts ?? new List <PortRange>(0));
     foreach (IPAddressRange range in ipRanges)
     {
         // optimized storage, no pointers or other overhead
         if (range.Begin.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
         {
             uint begin = range.Begin.ToUInt32();
             uint end   = range.End.ToUInt32();
             Debug.Assert(end >= begin);
             ipv4.Add(new IPV4Range {
                 Begin = begin, End = end
             });
         }
         else
         {
             UInt128 begin = range.Begin.ToUInt128();
             UInt128 end   = range.End.ToUInt128();
             Debug.Assert(end.CompareTo(begin) >= 0);
             ipv6.Add(new IPV6Range {
                 Begin = begin, End = end
             });
         }
     }
     ipv4.TrimExcess();
     ipv6.TrimExcess();
     if (block)
     {
         string portString = IPBanFirewallUtility.GetPortRangeStringBlockExcept(allowedPorts);
         this.portRanges = (string.IsNullOrWhiteSpace(portString) ? new List <PortRange>(0) : portString.Split(',').Select(s => PortRange.Parse(s)).ToList());
     }
     else
     {
         this.portRanges = allowedPorts;
     }
 }