예제 #1
0
        /// <summary>
        /// 检测IP是否在某一个策略内
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="strStrategy"></param>
        /// <returns></returns>
        public StrategyState CheckIPInStragegy(string ip, string strStrategy)
        {
            Regex reg = new Regex(@"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", RegexOptions.Compiled | RegexOptions.Multiline);

            if (!reg.IsMatch(ip))
            {
                throw new Exception("IP格式不正确");
            }

            StrategyState result = StrategyState.DEFAULT;

            if (!String.IsNullOrEmpty(strStrategy))
            {
                StrategyInfo info = this[strStrategy];
                if (info != null && info.Enable)
                {
                    foreach (string rang in info.DenyIPRang)
                    {
                        if (CheckIPInRang(ip, rang))
                        {
                            return(StrategyState.DENY);
                        }
                    }

                    if (info.AllowIPRang.Count == 0)
                    {
                        result = StrategyState.ALLOW;
                    }
                    else
                    {
                        result = Check(delegate(){
                            foreach (string rang in info.AllowIPRang)
                            {
                                if (CheckIPInRang(ip, rang))
                                {
                                    return(true);
                                }
                            }
                            return(false);
                        }) ? StrategyState.ALLOW : StrategyState.DENY;
                    }
                }
            }
            return(result);
        }