using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Collections;
using System.Text.RegularExpressions;
using System.Xml;
using Core;
namespace GreenWebService
{
#region MyRegion
/// <summary>
/// 此类为单例模式,实现ip认证,调用方法如果下
/// AuthIP.getInstance(AppDomain.CurrentDomain.BaseDirectory + "App_Data/ipconfig.xml").AuIp("192.168.0.1")
/// 其中AppDomain.CurrentDomain.BaseDirectory + "App_Data/ipconfig.xml"为配置文件xml的路径
/// 192.168.0.1为需要认证的IP地址
/// </summary>
public class AuthIP
{
public const String REGEX_IP = @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$"; //验证ip地址正则表达式
//验证ip段地址正则表达式
public const String REGEX_AREA_IP = @"((25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)\.){3}(25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)-((25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)\.){3}(25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)";
private ArrayList auths = new ArrayList();
private static AuthIP authInstance;
private AuthIP() { }
public static AuthIP getInstance(String filepath)
{
if (null == authInstance)
{
authInstance = new AuthIP();
authInstance.ParseXml(filepath);
}
return authInstance;
}
public bool AuthIp(String ip)
{
if(null == ip || !IsIp(ip))
{
throw new Exception("参数是空的或者参数不是ip");
}
foreach (IAuthIP iau in auths)
{
if (iau.Auth(ip))
{
return true;
}
}
return false;
}
/// <summary>
/// 读取配置文件里的配置信息
/// </summary>
/// <param name="filepath"></param>
private void ParseXml(String filepath)
{
XmlNodeList nlist = XmlHelper.GetAreaString(filepath);
if (nlist.Count < 1) return ;
IAuthIP auth = null;
foreach (XmlNode xn in nlist)
{
if (xn.LocalName == "ip" || xn.LocalName == "ips")
{
auth = new ArrayIAuthIP(xn.InnerText);
auths.Add(auth);
}
else if (xn.LocalName == "areaip")
{
auth = new AreaIAuthIP(xn.InnerText);
auths.Add(auth);
}
}
}
/// <summary>
/// 认证接口,如果要添加新的认证规则 则必须要实现此接口
/// </summary>
public interface IAuthIP {
bool Auth(String ip);
}
private class ArrayIAuthIP : IAuthIP {
private ArrayList authIps;
public ArrayIAuthIP (String tag)
{
authIps = new ArrayList();
Inflater(tag);
}
/// <summary>
/// 转换给定ip规则字符串,如192.168.0.1,192.168.0.1
/// </summary>
/// <param name="tag"></param>
public void Inflater(String tag)
{
if (null == tag || tag == "") return;
String[] strs = tag.Split(',');
foreach (String str in strs)
{
if (IsIp(str))
{
authIps.Add(str);
}
else
{
throw new Exception("请检查配置文件<ip>或者<ips>标签");
}
}
}
public bool Auth(String ip)
{
foreach (String authIp in authIps)
{
if (authIp == ip)
{
return true;
}
}
return false;
}
}
private class AreaIAuthIP : IAuthIP
{
private String areaStr;
public AreaIAuthIP(String tag)
{
Inflater(tag);
}
private void Inflater(string tag)
{
if (null == tag || tag == "") return;
if (IsAreaIp(tag))
{
areaStr = tag;
}
else
{
throw new Exception("请检查配置文件areaip标签");
}
}
public bool Auth(String authIp)
{
if(null == areaStr)
{
return false;
}
String[] areaIps = areaStr.Split('-');
String startIp = areaIps[0];
String endIp = areaIps[1];
String[] startIpArray = startIp.Split('.');
String[] endIpArray = endIp.Split('.');
String[] authIpArray = authIp.Split('.');
long startIpLong = 0L;
long endIpLong = 0L;
long authIpLong = 0L;
for (int i = 0; i < 4; i++ )
{
startIpLong = startIpLong << 8 | Convert.ToUInt32(startIpArray[i]);
endIpLong = endIpLong << 8 | Convert.ToUInt32(endIpArray[i]);
authIpLong = authIpLong << 8 | Convert.ToUInt32(authIpArray[i]);
}
if (startIpLong > endIpLong)
{
long temp = 0L;
temp = startIpLong;
startIpLong = endIpLong;
endIpLong = temp;
}
return startIpLong <= authIpLong && endIpLong >= authIpLong ;
}
}
/// <summary>
/// 检查给定的字符串是否是ip类型
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public static bool IsIp(string ip){
return Regex.IsMatch(ip, REGEX_IP);
}
/// <summary>
/// 检查给定的字符串是否是ip段类型
/// </summary>
/// <param name="areaIp"></param>
/// <returns></returns>
public static bool IsAreaIp(String areaIp)
{
return Regex.IsMatch(areaIp , REGEX_AREA_IP);
}
}
#endregion
}
配置文件
<?xml version="1.0" encoding="utf-8"?> <ipallows> <ip>192.168.0.1</ip> <ips>127.0.0.1,127.0.0.2</ips> <areaip>61.234.145.102-61.235.145.102</areaip> </ipallows>
本文介绍了一种基于单例模式的IP认证方法,通过配置文件解析实现IP地址的认证和访问控制。包括正则表达式验证IP地址、IP段地址,以及通过配置文件加载规则进行访问控制。
7114

被折叠的 条评论
为什么被折叠?



