protected void Page_Load(object sender, EventArgs e)
{
Response.Write(IsAllowIP("192.168.1.231-236:yes;192.168.1.238:no", "192.168.1.235"));
Response.Write(IsAllowIP("192.168-169.1.23*:no", "192.168.1.235"));
Response.Write(IsAllowIP("1*.168.1.23?:yes", "192.168.1.235"));
Response.Write(IsAllowIP("1?2.16*.1.231-236:no", "192.168.1.235"));
}
#region 验证在IP范围内是否允许
/// <summary>
/// 判断指定的IP是否在指定的 规则下允许的(三个特殊符号 -?*)
/// rule[192.*.1.236-239:yes;192.*.1.226:no;218.85.*.*:no]最后一个不要加";"分号
/// 前面的规则优先级高
/// 注意,规则中的 * - ? 不能同时存在于同一个段内 如: 192.168.*?.123 会出错
/// *号在同一段内只能有一个, 如 192.16*.1.*, 192.1**.1.1 是错误的,可以用 ?号代替
/// </summary>
/// <param name="rule">(192.*.1.236-239:yes;192.*.1.226:no;218.85.*.*:no) 最后一个规则不要再多加";"分号</param>
/// <param name="ip">192.168.1.237(不正确的IP会出错)</param>
/// <returns></returns>
public static bool IsAllowIP(string rule, string ip)
{
//IP正则表达式
string ipRegexString = @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$";
//如果IP地址是错的,禁止
if (!Regex.IsMatch(ip, ipRegexString))
{
throw new Exception("参数ip错误:错误的IP地址" + ip);
}
else
{
//分离规则
string[] ruleArray = rule.Split(new char[] { ';' });
string[] ipdata = ip.Split(new char[] { '.' });
bool retValue = false;//默认返回值
//遍历规则并验证
foreach (string s in ruleArray)
{
bool IsFind = false;
string[] data = s.Split(new char[] { ':' });
//如果没有用:分开
if (data.Length != 2) { throw new Exception("请用:分开 如:192.168.1.1:yes"); }
string ruleIp = data[0];//得到 192.168.20-60.*:yes 中的 [192.168.20-60.*]部分
retValue = data[1].ToString().ToLower() == "yes" ? true : false;
string[] ruleIpArray = ruleIp.Split(new char[] { '.' });
if (ruleIpArray.Length != 4) { throw new Exception("IP部分错误!"); }
#region
for (int i = 0; i < 4; i++)
{
bool AA = ruleIpArray[i].Contains("*");
bool BB = ruleIpArray[i].Contains("-");
bool CC = ruleIpArray[i].Contains("?");
if ((AA && BB) || (AA && CC) || (BB && CC) || (AA && BB && CC))
{
throw new Exception("这样的格式是错误的,192.168.15-20*,*与-不能包含在同一个部分! ");
}
else if (!AA && !BB && !CC) //没有包含 * 与 - 及 ?
{
if (!Regex.IsMatch(ruleIpArray[i], @"^2[0-4]\d|25[0-5]|[01]?\d\d?$"))
{
throw new Exception("IP段错误应该在1~255之间:" + ruleIpArray[i]);
}
else
{
#region 这里判断 111111111111
if (ruleIpArray[i] == ipdata[i])
{
IsFind = true;
}
else
{
IsFind = false;
break;
}
#endregion
}
}
else if (AA && !BB && !CC) //包含 [*] 的
{
if (ruleIpArray[i] != "*")
{
if (ruleIpArray[i].StartsWith("*") || !ruleIpArray[i].EndsWith("*") || ruleIpArray[i].Contains("**"))
{
throw new Exception("IP中的*部分:不能以*开头,不能有两个**,只能以*结尾");
}
}
else
{
#region 这里判断22222222222222
if (ipdata[i].StartsWith(ruleIpArray[i].Replace("*", "")))
{
IsFind = true;
}
else
{
IsFind = false;
break;
}
#endregion
}
}
else if (BB && !AA && !CC) //包含 [-] 的
{
string[] temp = ruleIpArray[i].Split(new char[] { '-' });
if (temp.Length != 2)
{
throw new Exception("IP段错误, 如:23-50,在1~255之间");
}
else
{
if (Convert.ToInt32(temp[0]) < 1 || Convert.ToInt32(temp[1]) > 255)
{
throw new Exception("IP段错误, 如:23-50,在1~255之间");
}
else
{
#region 这里判断33333333333333333
string[] Num = ruleIpArray[i].Split(new char[] { '-' });
int p = int.Parse(ipdata[i]);
if (p >= int.Parse(Num[0]) && p <= int.Parse(Num[1]))
{
IsFind = true;
}
else
{
IsFind = false;
break;
}
#endregion
}
}
}
else if (CC && !AA & !BB) //包含 [?] 的
{
//去掉问号后
string temp = ruleIpArray[i].Replace("?", "");
Regex re = new Regex(@"^\d\d?$");
if (!re.IsMatch(temp) || temp.Length > 2)
{
throw new Exception("IP段错误:" + ruleIpArray[i]);
}
else
{
#region 这里判断4444444444444
if (ruleIpArray[i].Length != ipdata[i].Length)
{
IsFind = false;
break;
}
else
{
string tempRegstring = "^" + ruleIpArray[i].Replace("?", @"\d") + "$";
Regex tempRe = new Regex(tempRegstring);
if (tempRe.IsMatch(ipdata[i]))
{
IsFind = true;
}
else
{
IsFind = false;
break;
}
}
#endregion
}
}
else
{
IsFind = false;
break;
}
}
#endregion
if (IsFind)
{
return retValue;//IP规则中 :后面的 yes/no 对应的 true false
}
}
return false;
}
}
#endregion
{
Response.Write(IsAllowIP("192.168.1.231-236:yes;192.168.1.238:no", "192.168.1.235"));
Response.Write(IsAllowIP("192.168-169.1.23*:no", "192.168.1.235"));
Response.Write(IsAllowIP("1*.168.1.23?:yes", "192.168.1.235"));
Response.Write(IsAllowIP("1?2.16*.1.231-236:no", "192.168.1.235"));
}
#region 验证在IP范围内是否允许
/// <summary>
/// 判断指定的IP是否在指定的 规则下允许的(三个特殊符号 -?*)
/// rule[192.*.1.236-239:yes;192.*.1.226:no;218.85.*.*:no]最后一个不要加";"分号
/// 前面的规则优先级高
/// 注意,规则中的 * - ? 不能同时存在于同一个段内 如: 192.168.*?.123 会出错
/// *号在同一段内只能有一个, 如 192.16*.1.*, 192.1**.1.1 是错误的,可以用 ?号代替
/// </summary>
/// <param name="rule">(192.*.1.236-239:yes;192.*.1.226:no;218.85.*.*:no) 最后一个规则不要再多加";"分号</param>
/// <param name="ip">192.168.1.237(不正确的IP会出错)</param>
/// <returns></returns>
public static bool IsAllowIP(string rule, string ip)
{
//IP正则表达式
string ipRegexString = @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$";
//如果IP地址是错的,禁止
if (!Regex.IsMatch(ip, ipRegexString))
{
throw new Exception("参数ip错误:错误的IP地址" + ip);
}
else
{
//分离规则
string[] ruleArray = rule.Split(new char[] { ';' });
string[] ipdata = ip.Split(new char[] { '.' });
bool retValue = false;//默认返回值
//遍历规则并验证
foreach (string s in ruleArray)
{
bool IsFind = false;
string[] data = s.Split(new char[] { ':' });
//如果没有用:分开
if (data.Length != 2) { throw new Exception("请用:分开 如:192.168.1.1:yes"); }
string ruleIp = data[0];//得到 192.168.20-60.*:yes 中的 [192.168.20-60.*]部分
retValue = data[1].ToString().ToLower() == "yes" ? true : false;
string[] ruleIpArray = ruleIp.Split(new char[] { '.' });
if (ruleIpArray.Length != 4) { throw new Exception("IP部分错误!"); }
#region
for (int i = 0; i < 4; i++)
{
bool AA = ruleIpArray[i].Contains("*");
bool BB = ruleIpArray[i].Contains("-");
bool CC = ruleIpArray[i].Contains("?");
if ((AA && BB) || (AA && CC) || (BB && CC) || (AA && BB && CC))
{
throw new Exception("这样的格式是错误的,192.168.15-20*,*与-不能包含在同一个部分! ");
}
else if (!AA && !BB && !CC) //没有包含 * 与 - 及 ?
{
if (!Regex.IsMatch(ruleIpArray[i], @"^2[0-4]\d|25[0-5]|[01]?\d\d?$"))
{
throw new Exception("IP段错误应该在1~255之间:" + ruleIpArray[i]);
}
else
{
#region 这里判断 111111111111
if (ruleIpArray[i] == ipdata[i])
{
IsFind = true;
}
else
{
IsFind = false;
break;
}
#endregion
}
}
else if (AA && !BB && !CC) //包含 [*] 的
{
if (ruleIpArray[i] != "*")
{
if (ruleIpArray[i].StartsWith("*") || !ruleIpArray[i].EndsWith("*") || ruleIpArray[i].Contains("**"))
{
throw new Exception("IP中的*部分:不能以*开头,不能有两个**,只能以*结尾");
}
}
else
{
#region 这里判断22222222222222
if (ipdata[i].StartsWith(ruleIpArray[i].Replace("*", "")))
{
IsFind = true;
}
else
{
IsFind = false;
break;
}
#endregion
}
}
else if (BB && !AA && !CC) //包含 [-] 的
{
string[] temp = ruleIpArray[i].Split(new char[] { '-' });
if (temp.Length != 2)
{
throw new Exception("IP段错误, 如:23-50,在1~255之间");
}
else
{
if (Convert.ToInt32(temp[0]) < 1 || Convert.ToInt32(temp[1]) > 255)
{
throw new Exception("IP段错误, 如:23-50,在1~255之间");
}
else
{
#region 这里判断33333333333333333
string[] Num = ruleIpArray[i].Split(new char[] { '-' });
int p = int.Parse(ipdata[i]);
if (p >= int.Parse(Num[0]) && p <= int.Parse(Num[1]))
{
IsFind = true;
}
else
{
IsFind = false;
break;
}
#endregion
}
}
}
else if (CC && !AA & !BB) //包含 [?] 的
{
//去掉问号后
string temp = ruleIpArray[i].Replace("?", "");
Regex re = new Regex(@"^\d\d?$");
if (!re.IsMatch(temp) || temp.Length > 2)
{
throw new Exception("IP段错误:" + ruleIpArray[i]);
}
else
{
#region 这里判断4444444444444
if (ruleIpArray[i].Length != ipdata[i].Length)
{
IsFind = false;
break;
}
else
{
string tempRegstring = "^" + ruleIpArray[i].Replace("?", @"\d") + "$";
Regex tempRe = new Regex(tempRegstring);
if (tempRe.IsMatch(ipdata[i]))
{
IsFind = true;
}
else
{
IsFind = false;
break;
}
}
#endregion
}
}
else
{
IsFind = false;
break;
}
}
#endregion
if (IsFind)
{
return retValue;//IP规则中 :后面的 yes/no 对应的 true false
}
}
return false;
}
}
#endregion