C#调用纯真IP离线地址库解析IP地址地理位置

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web;
using System.Configuration;

namespace IpAddressHelper
{
//以下是类文件
//根据LumaQQ改写而成.

/**/
///<summary>
/// 通过纯真IP离线数据包解析IP地址
///</summary>
public class IpAddress : IDisposable
{
    #region 变量
    //第一种模式
    ///<summary>
    ///第一种模式
    ///</summary>
    private const byte REDIRECT_MODE_1 = 0x01;

    //第二种模式
    ///<summary>
    ///第二种模式
    ///</summary>
    private const byte REDIRECT_MODE_2 = 0x02;

    //每条记录长度
    ///<summary>
    ///每条记录长度
    ///</summary>
    private const int IP_RECORD_LENGTH = 7;

    //数据库文件
    ///<summary>
    ///文件对象
    ///</summary>
    private FileStream ipFile;

    private const string unCountry = "未知国家";
    private const string unArea = "未知地区";

    //索引开始位置
    ///<summary>
    ///索引开始位置
    ///</summary>
    private long ipBegin;

    //索引结束位置
    ///<summary>
    ///索引结束位置
    ///</summary>
    private long ipEnd;

    //IP地址对象
    ///<summary>
    /// IP对象
    ///</summary>
    private IPLocation loc;

    //存储文本内容
    ///<summary>
    ///存储文本内容
    ///</summary>
    private byte[] buf;

    //存储3字节
    ///<summary>
    ///存储3字节
    ///</summary>
    private byte[] b3;

    //存储4字节
    ///<summary>
    ///存储4字节IP地址
    ///</summary>
    private byte[] b4;
    #endregion

    /// <summary>
    /// 注意 ~/res/IpAddress/qqwry.dat 路径可能会被 iis 报错,对路径的访问被拒绝。解决办法:给予“IIS_USRS”,赋予其“完全控制”权限。通过纯真IP离线数据包解析IP地址(要经常更新!)
    /// </summary>
    /// <param name="ipfile">qqwry.dat文件路径</param>
    public IpAddress(string ipfile)
    {

        buf = new byte[100];
        b3 = new byte[3];
        b4 = new byte[4];
        try
        {
            ipFile = new FileStream(ipfile, FileMode.Open);
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
        ipBegin = readLong4(0);
        ipEnd = readLong4(4);
        loc = new IPLocation();
    }

    ~IpAddress()
    {
        Dispose();
    }

    /*
    托管资源:
    Net平台中,CLR为程序员提供了一种很好的内存管理机制,使得程序员在编写代码时不要显式的去释放自己使用的内存资源(这些在先前C和C++中是需要程序员自己去显式的释放的)。
    这种管理机制称为GC(garbage collection)。GC的作用是很明显的,当系统内存资源匮乏时,它就会被激发,然后自动的去释放那些没有被使用的托管资源(也就是程序员没有显式释放的对象)。

    所以托管就是.net framework 负责帮你管理内存及资源释放,不需要自己控制,当然对象只针对托管资源(部分引用类型), 不回收非托管资源。 
    像数组,用户定义的类、接口、委托,object,字符串等引用类型,栈上保存着一个地址而已,当栈释放后, 即使对象已经没有用了,但堆上分配的内存还在,只能等GC收集时才能真正释放 ;
    但注意int,string,float,DateTime之类的值类型,GC会自动释放他们占用的内存,不需要GC来回收释放

    非托管资源: 
    对于非托管资源,GC只能跟踪非托管资源的生存期,而不知道如何去释放它。这样就会出现当资源用尽时就不能提供资源能够提供的服务,windows的运行速度就会变慢。
    比如当你链接了数据库,用完后你没有显式的释放数据库资源,如果还是不断的申请数据库资源,那么到一定时候程序就会抛出一个异常。
    所以,当我们在类中封装了对非托管资源的操作时,我们就需要显式,或者是隐式的释放这些资源。在.Net中释放非托管资源主要有2种方式,Dispose,Finalize,
    而Finalize和Dispose方法分别就是隐式和显式操作中分别使用到的方法。
    例如文件流,数据库的连接,系统的窗口句柄,打印机资源等等,当你读取文件之后,就需要对各种Stream进行Dispose等操作。比如 SqlDataReader 读取数据完毕之后,需要 reader.Dispose();等
    Finalize一般情况下用于基类不带close方法或者不带Dispose显式方法的类,也就是说,在Finalize过程中我们需要隐式的去实现非托管资源的释放,然后系统会在Finalize过程完成后,自己的去释放托管资源。
    在.NET中应该尽可能的少用析构函数释放资源,MSDN2上有这样一段话:实现 Finalize 方法或析构函数对性能可能会有负面影响,因此应避免不必要地使用它们。
    用 Finalize 方法回收对象使用的内存需要至少两次垃圾回收。所以有析构函数的对象,需要两次,第一次调用析构函数,第二次删除对象。
    而且在析构函数中包含大量的释放资源代码,会降低垃圾回收器的工作效率,影响性能。所以对于包含非托管资源的对象,最好及时的调用Dispose()方法来回收资源,而不是依赖垃圾回收器。
     */

    private bool IsDisposed = false;

    //这里的参数表示示是否需要释放那些实现IDisposable接口的托管对象
    public void Dispose()
    {
        if (IsDisposed)
            return; //如果已经被回收,就中断执行
                   
        buf = null;
        b3 = null;
        b4 = null;
        loc = null;
        if (ipFile != null)
        {
            ipFile.Close();
            ipFile.Dispose();
            ipFile = null;
        }
        
        IsDisposed = true;
    }


    //根据IP地址搜索
    ///<summary>
    ///搜索IP地址搜索
    ///</summary>
    ///<param name="ip"></param>
    ///<returns></returns>
    public IPLocation SearchIPLocation(string ip)
    {
        //将字符IP转换为字节
        string[] ipSp = ip.Split('.');
        if (ipSp.Length != 4)
        {
            throw new ArgumentOutOfRangeException("不是合法的IP地址!");
        }
        byte[] IP = new byte[4];
        for (int i = 0; i < IP.Length; i++)
        {
            IP[i] = (byte)(Int32.Parse(ipSp[i]) & 0xFF);
        }

        IPLocation local = null;
        long offset = locateIP(IP);

        if (offset != -1)
        {
            local = getIPLocation(offset);
        }

        if (local == null)
        {
            local = new IPLocation();
            local.area = unArea;
            local.country = unCountry;
        }
        return local;
    }

    //取得具体信息
    ///<summary>
    ///取得具体信息
    ///</summary>
    ///<param name="offset"></param>
    ///<returns></returns>
    private IPLocation getIPLocation(long offset)
    {
        ipFile.Position = offset + 4;
        //读取第一个字节判断是否是标志字节
        byte one = (byte)ipFile.ReadByte();
        if (one == REDIRECT_MODE_1)
        {
            //第一种模式
            //读取国家偏移
            long countryOffset = readLong3();
            //转至偏移处
            ipFile.Position = countryOffset;
            //再次检查标志字节
            byte b = (byte)ipFile.ReadByte();
            if (b == REDIRECT_MODE_2)
            {
                loc.country = readString(readLong3());
                ipFile.Position = countryOffset + 4;
            }
            else
                loc.country = readString(countryOffset);

            //读取地区标志
            loc.area = readArea(ipFile.Position);

        }
        else if (one == REDIRECT_MODE_2)
        {
            //第二种模式
            loc.country = readString(readLong3());
            loc.area = readArea(offset + 8);
        }
        else
        {
            //普通模式
            loc.country = readString(--ipFile.Position);
            loc.area = readString(ipFile.Position);
        }
        return loc;
    }

    //取得地区信息
    ///<summary>
    ///读取地区名称
    ///</summary>
    ///<param name="offset"></param>
    ///<returns></returns>
    private string readArea(long offset)
    {
        ipFile.Position = offset;
        byte one = (byte)ipFile.ReadByte();
        if (one == REDIRECT_MODE_1 || one == REDIRECT_MODE_2)
        {
            long areaOffset = readLong3(offset + 1);
            if (areaOffset == 0)
                return unArea;
            else
            {
                return readString(areaOffset);
            }
        }
        else
        {
            return readString(offset);
        }
    }

    //读取字符串
    ///<summary>
    ///读取字符串
    ///</summary>
    ///<param name="offset"></param>
    ///<returns></returns>
    private string readString(long offset)
    {
        ipFile.Position = offset;
        int i = 0;
        for (i = 0, buf[i] = (byte)ipFile.ReadByte(); buf[i] != (byte)(0); buf[++i] = (byte)ipFile.ReadByte()) ;

        if (i > 0)
            return Encoding.Default.GetString(buf, 0, i);
        else
            return "";
    }

    //查找IP地址所在的绝对偏移量
    ///<summary>
    ///查找IP地址所在的绝对偏移量
    ///</summary>
    ///<param name="ip"></param>
    ///<returns></returns>
    private long locateIP(byte[] ip)
    {
        long m = 0;
        int r;

        //比较第一个IP项
        readIP(ipBegin, b4);
        r = compareIP(ip, b4);
        if (r == 0)
            return ipBegin;
        else if (r < 0)
            return -1;
        //开始二分搜索
        for (long i = ipBegin, j = ipEnd; i < j;)
        {
            m = this.getMiddleOffset(i, j);
            readIP(m, b4);
            r = compareIP(ip, b4);
            if (r > 0)
                i = m;
            else if (r < 0)
            {
                if (m == j)
                {
                    j -= IP_RECORD_LENGTH;
                    m = j;
                }
                else
                {
                    j = m;
                }
            }
            else
                return readLong3(m + 4);
        }
        m = readLong3(m + 4);
        readIP(m, b4);
        r = compareIP(ip, b4);
        if (r <= 0)
            return m;
        else
            return -1;
    }

    //读出4字节的IP地址
    ///<summary>
    ///从当前位置读取四字节,此四字节是IP地址
    ///</summary>
    ///<param name="offset"></param>
    ///<param name="ip"></param>
    private void readIP(long offset, byte[] ip)
    {
        ipFile.Position = offset;
        ipFile.Read(ip, 0, ip.Length);
        byte tmp = ip[0];
        ip[0] = ip[3];
        ip[3] = tmp;
        tmp = ip[1];
        ip[1] = ip[2];
        ip[2] = tmp;
    }

    //比较IP地址是否相同
    ///<summary>
    ///比较IP地址是否相同
    ///</summary>
    ///<param name="ip"></param>
    ///<param name="beginIP"></param>
    ///<returns>0:相等,1:ip大于beginIP,-1:小于</returns>
    private int compareIP(byte[] ip, byte[] beginIP)
    {
        for (int i = 0; i < 4; i++)
        {
            int r = compareByte(ip[i], beginIP[i]);
            if (r != 0)
                return r;
        }
        return 0;
    }

    //比较两个字节是否相等
    ///<summary>
    ///比较两个字节是否相等
    ///</summary>
    ///<param name="bsrc"></param>
    ///<param name="bdst"></param>
    ///<returns></returns>
    private int compareByte(byte bsrc, byte bdst)
    {
        if ((bsrc & 0xFF) > (bdst & 0xFF))
            return 1;
        else if ((bsrc ^ bdst) == 0)
            return 0;
        else
            return -1;
    }

    //根据当前位置读取4字节
    ///<summary>
    ///从当前位置读取4字节,转换为长整型
    ///</summary>
    ///<param name="offset"></param>
    ///<returns></returns>
    private long readLong4(long offset)
    {
        long ret = 0;
        ipFile.Position = offset;
        //long value = (((long)hi) << 32) | ((uint)lo);   // correct  
        ret |= ((long)ipFile.ReadByte() & 0xFF);
        ret |= (((long)ipFile.ReadByte() << 8) & 0xFF00);
        ret |= (((long)ipFile.ReadByte() << 16) & 0xFF0000);
        ret |= (((long)ipFile.ReadByte() << 24) & 0xFF000000);
        return ret;
    }

    //根据当前位置,读取3字节
    ///<summary>
    ///根据当前位置,读取3字节
    ///</summary>
    ///<param name="offset"></param>
    ///<returns></returns>
    private long readLong3(long offset)
    {
        long ret = 0;
        ipFile.Position = offset;
        ret |= ((long)ipFile.ReadByte() & 0xFF);
        ret |= (((long)ipFile.ReadByte() << 8) & 0xFF00);
        ret |= (((long)ipFile.ReadByte() << 16) & 0xFF0000);
        return ret;
    }

    //从当前位置读取3字节
    ///<summary>
    ///从当前位置读取3字节
    ///</summary>
    ///<returns></returns>
    private long readLong3()
    {
        long ret = 0;
        ret |= ((long)ipFile.ReadByte() & 0xFF);
        ret |= (((long)ipFile.ReadByte() << 8) & 0xFF00);
        ret |= (((long)ipFile.ReadByte() << 16) & 0xFF0000);
        return ret;
    }

    //取得begin和end之间的偏移量
    ///<summary>
    ///取得begin和end中间的偏移
    ///</summary>
    ///<param name="begin"></param>
    ///<param name="end"></param>
    ///<returns></returns>
    private long getMiddleOffset(long begin, long end)
    {
        long records = (end - begin) / IP_RECORD_LENGTH;
        records >>= 1;
        if (records == 0)
            records = 1;
        return begin + records * IP_RECORD_LENGTH;
    }
} //class QQWry

public class IPLocation
{
    public String country;
    public String area;

    public IPLocation()
    {
        country = area = "";
    }

    public IPLocation getCopy()
    {
        IPLocation ret = new IPLocation();
        ret.country = country;
        ret.area = area;
        return ret;
    }
}

}

用例:
string ip = “8.8.8.8”;
IPLocation ipLocation = new IPLocation();
using (IpAddress qq = new IpAddress(Server.MapPath("~/res/IpAddress/qqwry.dat"))) //离线地址库的路径
{
    ipLocation = qq.SearchIPLocation(ip);
}
//如果不用using,那么用完一定要释放资源!
//qq.Dispose();
Label1.Text = $"您的IP地址:{ip} 地理位置:{ipLocation.country}_{ipLocation.area}";

最后打一波广告:
https://www.cz88.net/
纯真(CZ88.NET)自2005年起一直为广大社区用户提供社区版IP地址库,只要获得纯真的授权就能免费使用,并不断获取后续更新的版本。如果有需要免费版IP库的朋友可以前往纯真的官网进行申请。
纯真除了免费的社区版IP库外,还提供数据更加准确、服务更加周全的商业版IP地址查询数据。纯真围绕IP地址,基于 网络空间拓扑测绘 + 移动位置大数据 方案,对IP地址定位、IP网络风险、IP使用场景、IP网络类型、秒拨侦测、VPN侦测、代理侦测、爬虫侦测、真人度等均有近20年丰富的数据沉淀。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值