示例#1
0
        public override Task <bool> BlockIPAddresses(string ruleNamePrefix, IEnumerable <string> ipAddresses, IEnumerable <PortRange> allowedPorts = null, CancellationToken cancelToken = default)
        {
            IEnumerable <string> ipv4 = ipAddresses.Where(i => IPAddress.TryParse(i, out IPAddress obj) && obj.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
            IEnumerable <string> ipv6 = ipAddresses.Where(i => IPAddress.TryParse(i, out IPAddress obj) && obj.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6);
            bool result = base.BlockIPAddresses(ruleNamePrefix, ipv4, allowedPorts, cancelToken).Sync();

            if (result)
            {
                result = firewall6.BlockIPAddresses(ruleNamePrefix, ipv6, allowedPorts, cancelToken).Sync();
            }
            return(Task.FromResult(result));
        }
示例#2
0
        public async Task <bool> BlockIPAddresses(string ruleNamePrefix, IEnumerable <string> ipAddresses, CancellationToken cancelToken = default)
        {
            bool result = await firewall6.BlockIPAddresses(ruleNamePrefix, ipAddresses, cancelToken);

            if (!result)
            {
                return(false);
            }

            try
            {
                string ruleName = (string.IsNullOrWhiteSpace(ruleNamePrefix) ? BlockRuleName : RulePrefix + ruleNamePrefix);
                bannedIPAddresses = UpdateRule(ruleName, "DROP", ipAddresses, bannedIPAddresses, "ip", blockRuleMaxCount, false, null, cancelToken, out result);
                return(result);
            }
            catch (Exception ex)
            {
                IPBanLog.Error(ex);
                return(false);
            }
        }