示例#1
0
        public Task <bool> BlockIPAddressesDelta(string ruleNamePrefix, IEnumerable <IPBanFirewallIPAddressDelta> ipAddresses, CancellationToken cancelToken = default)
        {
            List <IPBanFirewallIPAddressDelta> deltas = new List <IPBanFirewallIPAddressDelta>(ipAddresses.Where(i => IPAddress.Parse(i.IPAddress).AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork));
            List <IPBanFirewallIPAddressDelta> delta6 = new List <IPBanFirewallIPAddressDelta>(ipAddresses.Where(i => IPAddress.Parse(i.IPAddress).AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6));
            string ruleName = (string.IsNullOrWhiteSpace(ruleNamePrefix) ? BlockRuleName : RulePrefix + ruleNamePrefix);
            bool   changed  = false;

            foreach (IPBanFirewallIPAddressDelta delta in deltas)
            {
                if (IPAddress.TryParse(delta.IPAddress, out IPAddress ipObj))
                {
                    if (delta.Added)
                    {
                        changed = bannedIPAddresses.Add(ipObj.ToUInt32());
                    }
                    else
                    {
                        changed = bannedIPAddresses.Remove(ipObj.ToUInt32());
                    }
                }
            }
            if (changed)
            {
                bannedIPAddresses = UpdateRule(ruleName, "DROP", bannedIPAddresses.Select(b => b.ToIPAddress().ToString()), bannedIPAddresses, "ip", blockRuleMaxCount, false, null, cancelToken, out bool result);
                if (!result)
                {
                    return(Task.FromResult(false));
                }
            }
            return(firewall6.BlockIPAddressesDelta(ruleNamePrefix, delta6, cancelToken));
        }
示例#2
0
        public override Task <bool> BlockIPAddressesDelta(string ruleNamePrefix, IEnumerable <IPBanFirewallIPAddressDelta> ipAddresses, IEnumerable <PortRange> allowedPorts = null, CancellationToken cancelToken = default)
        {
            List <IPBanFirewallIPAddressDelta> deltas4 = new List <IPBanFirewallIPAddressDelta>(ipAddresses.Where(i => IPAddress.Parse(i.IPAddress).AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork));
            List <IPBanFirewallIPAddressDelta> deltas6 = new List <IPBanFirewallIPAddressDelta>(ipAddresses.Where(i => IPAddress.Parse(i.IPAddress).AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6));
            bool result = base.BlockIPAddressesDelta(ruleNamePrefix, deltas4, allowedPorts, cancelToken).Sync();

            if (result)
            {
                result = firewall6.BlockIPAddressesDelta(ruleNamePrefix, deltas6, allowedPorts, cancelToken).Sync();
            }
            return(Task.FromResult(result));
        }