public void TestParseFormatExceptions() { Assert.Throws <FormatException>(() => PortRange.Parse("0.0.0.0/24;x/2;;")); Assert.Throws <FormatException>(() => PortRange.Parse("abf,d")); Assert.Throws <FormatException>(() => PortRange.Parse("0-1-2")); Assert.Throws <FormatException>(() => PortRange.Parse("0,1-2,f,")); }
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(IPBanOS.Name)) { extraRules.Add(firewallBlockRuleObj); } } else { IPBanLog.Warn("Firewall block rule entry should have 3 comma separated pieces: name;ips;ports. Invalid entry: {0}", firewallBlockRule); } } }
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; } }