Exemplo n.º 1
0
        /// <summary>
        /// 初始化 <see cref="IPInfo"/> 实例
        /// </summary>
        /// <param name="rule">Block规则</param>
        public IPInfo(IPRule rule)
        {
            if (rule == null)
                throw new ArgumentNullException("rule");

            this.rule = rule;
        }
Exemplo n.º 2
0
        /// <summary>
        /// 解析规则字符串并返回 <see cref="IPRule"/> 实例
        /// </summary>
        /// <param name="value">规则字符串</param>
        /// <returns></returns>
        public static IPRule Parse(string value)
        {
            var vals = value.Split(',');
            if (vals.Length != 3)
                throw new ArgumentException("The value is not a valid format.", "value");

            var rule = new IPRule();
            if (string.IsNullOrWhiteSpace(vals[1])) //Mask默认为32,表示单个IP
            {
                rule.IP = uint.Parse(vals[0]);
                rule.MaskLevel = 32;
                rule.Rate = int.Parse(vals[2]);
            }
            else //表示IP段
            {
                int level = int.Parse(vals[1]);
                rule.IP = uint.Parse(vals[0]);
                rule.MaskLevel = level;
                rule.Rate = int.Parse(vals[2]);
            }

            if (rule.Rate == -1)
                rule.BlockType = BlockType.Block;
            else if (rule.Rate == 0)
                rule.BlockType = BlockType.Validate;
            else if (rule.Rate >= 1 && rule.Rate <= 9)
                rule.BlockType = BlockType.RateLimit;
            else
                rule.BlockType = BlockType.Unknown;

            return rule;
        }