一个验证IP范围内权限的C#方法,支持 ? * -

来源:互联网 发布:淘宝迅雷会员1分钱 编辑:程序博客网 时间:2024/06/14 15:43
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3.     Response.Write(IsAllowIP("192.168.1.231-236:yes;192.168.1.238:no""192.168.1.235"));
  4.     Response.Write(IsAllowIP("192.168-169.1.23*:no""192.168.1.235"));
  5.     Response.Write(IsAllowIP("1*.168.1.23?:yes""192.168.1.235"));
  6.     Response.Write(IsAllowIP("1?2.16*.1.231-236:no""192.168.1.235"));
  7. }
  8. #region 验证在IP范围内是否允许
  9. /// <summary>
  10. /// 判断指定的IP是否在指定的 规则下允许的(三个特殊符号 -?*)
  11. /// rule[192.*.1.236-239:yes;192.*.1.226:no;218.85.*.*:no]最后一个不要加";"分号
  12. /// 前面的规则优先级高
  13. /// 注意,规则中的 * - ? 不能同时存在于同一个段内 如: 192.168.*?.123 会出错
  14. /// *号在同一段内只能有一个, 如 192.16*.1.*, 192.1**.1.1 是错误的,可以用 ?号代替
  15. /// </summary>
  16. /// <param name="rule">(192.*.1.236-239:yes;192.*.1.226:no;218.85.*.*:no) 最后一个规则不要再多加";"分号</param>
  17. /// <param name="ip">192.168.1.237(不正确的IP会出错)</param>
  18. /// <returns></returns>
  19. public static bool IsAllowIP(string rule, string ip)
  20. {
  21.     //IP正则表达式
  22.     string ipRegexString = @"^((2[0-4]/d|25[0-5]|[01]?/d/d?)/.){3}(2[0-4]/d|25[0-5]|[01]?/d/d?)$";
  23.     //如果IP地址是错的,禁止
  24.     if (!Regex.IsMatch(ip, ipRegexString))
  25.     {
  26.         throw new Exception("参数ip错误:错误的IP地址" + ip);
  27.     }
  28.     else
  29.     {
  30.         //分离规则
  31.         string[] ruleArray = rule.Split(new char[] { ';' });
  32.         string[] ipdata = ip.Split(new char[] { '.' });
  33.         bool retValue = false;//默认返回值
  34.         //遍历规则并验证
  35.         foreach (string s in ruleArray)
  36.         {
  37.             bool IsFind = false;
  38.             string[] data = s.Split(new char[] { ':' });
  39.             //如果没有用:分开
  40.             if (data.Length != 2) { throw new Exception("请用:分开 如:192.168.1.1:yes"); }
  41.             string ruleIp = data[0];//得到 192.168.20-60.*:yes 中的 [192.168.20-60.*]部分
  42.             retValue = data[1].ToString().ToLower() == "yes" ? true : false;
  43.             string[] ruleIpArray = ruleIp.Split(new char[] { '.' });
  44.             if (ruleIpArray.Length != 4) { throw new Exception("IP部分错误!"); }
  45.             #region
  46.             for (int i = 0; i < 4; i++)
  47.             {
  48.                 bool AA = ruleIpArray[i].Contains("*");
  49.                 bool BB = ruleIpArray[i].Contains("-");
  50.                 bool CC = ruleIpArray[i].Contains("?");
  51.                 if ((AA && BB) || (AA && CC) || (BB && CC) || (AA && BB && CC))
  52.                 {
  53.                     throw new Exception("这样的格式是错误的,192.168.15-20*,*与-不能包含在同一个部分! ");
  54.                 }
  55.                 else if (!AA && !BB && !CC) //没有包含 * 与 - 及 ?
  56.                 {
  57.                     if (!Regex.IsMatch(ruleIpArray[i], @"^2[0-4]/d|25[0-5]|[01]?/d/d?$"))
  58.                     {
  59.                         throw new Exception("IP段错误应该在1~255之间:" + ruleIpArray[i]);
  60.                     }
  61.                     else
  62.                     {
  63.                         #region 这里判断 111111111111
  64.                         if (ruleIpArray[i] == ipdata[i])
  65.                         {
  66.                             IsFind = true;
  67.                         }
  68.                         else
  69.                         {
  70.                             IsFind = false;
  71.                             break;
  72.                         }
  73.                         #endregion
  74.                     }
  75.                 }
  76.                 else if (AA && !BB && !CC) //包含 [*] 的
  77.                 {
  78.                     if (ruleIpArray[i] != "*")
  79.                     {
  80.                         if (ruleIpArray[i].StartsWith("*") || !ruleIpArray[i].EndsWith("*") || ruleIpArray[i].Contains("**"))
  81.                         {
  82.                             throw new Exception("IP中的*部分:不能以*开头,不能有两个**,只能以*结尾");
  83.                         }
  84.                     }
  85.                     else
  86.                     {
  87.                         #region 这里判断22222222222222
  88.                         if (ipdata[i].StartsWith(ruleIpArray[i].Replace("*""")))
  89.                         {
  90.                             IsFind = true;
  91.                         }
  92.                         else
  93.                         {
  94.                             IsFind = false;
  95.                             break;
  96.                         }
  97.                         #endregion
  98.                     }
  99.                 }
  100.                 else if (BB && !AA && !CC) //包含 [-] 的
  101.                 {
  102.                     string[] temp = ruleIpArray[i].Split(new char[] { '-' });
  103.                     if (temp.Length != 2)
  104.                     {
  105.                         throw new Exception("IP段错误, 如:23-50,在1~255之间");
  106.                     }
  107.                     else
  108.                     {
  109.                         if (Convert.ToInt32(temp[0]) < 1 || Convert.ToInt32(temp[1]) > 255)
  110.                         {
  111.                             throw new Exception("IP段错误, 如:23-50,在1~255之间");
  112.                         }
  113.                         else
  114.                         {
  115.                             #region 这里判断33333333333333333
  116.                             string[] Num = ruleIpArray[i].Split(new char[] { '-' });
  117.                             int p = int.Parse(ipdata[i]);
  118.                             if (p >= int.Parse(Num[0]) && p <= int.Parse(Num[1]))
  119.                             {
  120.                                 IsFind = true;
  121.                             }
  122.                             else
  123.                             {
  124.                                 IsFind = false;
  125.                                 break;
  126.                             }
  127.                             #endregion
  128.                         }
  129.                     }
  130.                 }
  131.                 else if (CC && !AA & !BB) //包含 [?] 的
  132.                 {
  133.                     //去掉问号后 
  134.                     string temp = ruleIpArray[i].Replace("?""");
  135.                     Regex re = new Regex(@"^/d/d?$");
  136.                     if (!re.IsMatch(temp) || temp.Length > 2)
  137.                     {
  138.                         throw new Exception("IP段错误:" + ruleIpArray[i]);
  139.                     }
  140.                     else
  141.                     {
  142.                         #region 这里判断4444444444444
  143.                         if (ruleIpArray[i].Length != ipdata[i].Length)
  144.                         {
  145.                             IsFind = false;
  146.                             break;
  147.                         }
  148.                         else
  149.                         {
  150.                             string tempRegstring = "^" + ruleIpArray[i].Replace("?", @"/d") + "$";
  151.                             Regex tempRe = new Regex(tempRegstring);
  152.                             if (tempRe.IsMatch(ipdata[i]))
  153.                             {
  154.                                 IsFind = true;
  155.                             }
  156.                             else
  157.                             {
  158.                                 IsFind = false;
  159.                                 break;
  160.                             }
  161.                         }
  162.                         #endregion
  163.                     }
  164.                 }
  165.                 else
  166.                 {
  167.                     IsFind = false;
  168.                     break;
  169.                 }
  170.             }
  171.             #endregion
  172.             if (IsFind)
  173.             {
  174.                 return retValue;//IP规则中 :后面的 yes/no 对应的 true false
  175.             }
  176.         }
  177.         return false;
  178.     }
  179. }
  180. #endregion
原创粉丝点击