使用QQWry.Dat根据IP查询归属地

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Diagnostics;

namespace QQIPSeekLib
{
    public class RedirectMode
    {
        public static readonly int Mode_1 = 1;
        public static readonly int Mode_2 = 2;
    }

    public class IPFormat
    {
        public static readonly int HeaderLength = 8;
        public static readonly int IndexRecLength = 7;
        public static readonly int IndexOffset = 3;
        public static readonly int RecOffsetLength = 3;

        public static readonly string UnknownCountry = "未知的国家";
        public static readonly string UnknownZone = "未知的地区";

        public static uint ToUint(byte[] val)
        {
            if (val.Length > 4) throw new ArgumentException();
            if (val.Length < 4)
            {
                byte[] copyBytes = new byte[4];
                Array.Copy(val, 0, copyBytes, 0, val.Length);
                return BitConverter.ToUInt32(copyBytes, 0);
            }
            else
            {
                return BitConverter.ToUInt32(val, 0);
            }
        }
    }

    public class IPLocation
    {
        private IPAddress m_ip;
        private string m_country;
        private string m_loc;

        public IPLocation(IPAddress ip, string country, string loc)
        {
            m_ip = ip;
            m_country = country;
            m_loc = loc;
        }

        public IPAddress IP
        {
            get { return m_ip; }
        }

        public string Country
        {
            get { return m_country; }
        }

        public string Zone
        {
            get { return m_loc; }
        }
    }

    /// <summary>
    /// This class used to control ip seek
    /// </summary>
    public class IPSeeker
    {
        private string m_libPath;
        private uint m_indexStart;
        private uint m_indexEnd;
        public IPSeeker(string libPath)
        {
            m_libPath = libPath;
            //Locate the index block
            using (FileStream fs = new FileStream(m_libPath, FileMode.Open, FileAccess.Read))
            {

                BinaryReader reader = new BinaryReader(fs);
                Byte[] header = reader.ReadBytes(IPFormat.HeaderLength);
                m_indexStart = BitConverter.ToUInt32(header, 0);
                m_indexEnd = BitConverter.ToUInt32(header, 4);

            }
        }

        /// <summary>
        /// 输入IP地址,获取IP所在的地区信息
        /// </summary>
        /// <param name="ip">待查询的IP地址</param>
        /// <returns></returns>
        public IPLocation GetLocation(IPAddress ip)
        {
            using (FileStream fs = new FileStream(m_libPath, FileMode.Open, FileAccess.Read))
            {
                BinaryReader reader = new BinaryReader(fs);
                //Because it is network order(BigEndian), so we need to transform it into LittleEndian
                Byte[] givenIpBytes = BitConverter.GetBytes(IPAddress.NetworkToHostOrder(BitConverter.ToInt32(ip.GetAddressBytes(), 0)));
                uint offset = FindStartPos(fs, reader, m_indexStart, m_indexEnd, givenIpBytes);
                return GetIPInfo(fs, reader, offset, ip, givenIpBytes);
            }
        }

        #region private method

        private uint FindStartPos(FileStream fs, BinaryReader reader, uint m_indexStart, uint m_indexEnd, byte[] givenIp)
        {
            uint givenVal = BitConverter.ToUInt32(givenIp, 0);
            fs.Position = m_indexStart;

            while (fs.Position <= m_indexEnd)
            {
                Byte[] bytes = reader.ReadBytes(IPFormat.IndexRecLength);
                uint curVal = BitConverter.ToUInt32(bytes, 0);
                if (curVal > givenVal)
                {
                    fs.Position = fs.Position - 2 * IPFormat.IndexRecLength;
                    bytes = reader.ReadBytes(IPFormat.IndexRecLength);
                    byte[] offsetByte = new byte[4];
                    Array.Copy(bytes, 4, offsetByte, 0, 3);
                    return BitConverter.ToUInt32(offsetByte, 0);
                }
            }
            return 0;
        }

        private IPLocation GetIPInfo(FileStream fs, BinaryReader reader, long offset, IPAddress ipToLoc, Byte[] ipBytes)
        {
            fs.Position = offset;
            //To confirm that the given ip is within the range of record IP range
            byte[] endIP = reader.ReadBytes(4);

            uint endIpVal = BitConverter.ToUInt32(endIP, 0);
            uint ipVal = BitConverter.ToUInt32(ipBytes, 0);
            if (endIpVal < ipVal) return null;

            string country;
            string zone;
            //Read the Redirection pattern byte
            Byte pattern = reader.ReadByte();

            if (pattern == RedirectMode.Mode_1)
            {
                Byte[] countryOffsetBytes = reader.ReadBytes(IPFormat.RecOffsetLength);
                uint countryOffset = IPFormat.ToUint(countryOffsetBytes);

                if (countryOffset == 0) return GetUnknownLocation(ipToLoc);

                fs.Position = countryOffset;
                if (fs.ReadByte() == RedirectMode.Mode_2)
                {
                    return ReadMode2Record(fs, reader, ipToLoc);
                }
                else
                {
                    fs.Position--;
                    country = ReadString(reader);
                    zone = ReadZone(fs, reader, Convert.ToUInt32(fs.Position));
                }
            }
            else if (pattern == RedirectMode.Mode_2)
            {
                return ReadMode2Record(fs, reader, ipToLoc);
            }
            else
            {
                fs.Position--;
                country = ReadString(reader);
                zone = ReadZone(fs, reader, Convert.ToUInt32(fs.Position));
            }
            return new IPLocation(ipToLoc, country, zone);

        }

        //When it is in Mode 2
        private IPLocation ReadMode2Record(FileStream fs, BinaryReader reader, IPAddress ip)
        {
            uint countryOffset = IPFormat.ToUint(reader.ReadBytes(IPFormat.RecOffsetLength));
            uint curOffset = Convert.ToUInt32(fs.Position);
            if (countryOffset == 0) return GetUnknownLocation(ip);
            fs.Position = countryOffset;
            string country = ReadString(reader);
            string zone = ReadZone(fs, reader, curOffset);
            return new IPLocation(ip, country, zone);
        }

        //return a Unknown Location
        private IPLocation GetUnknownLocation(IPAddress ip)
        {
            string country = IPFormat.UnknownCountry;
            string zone = IPFormat.UnknownZone;
            return new IPLocation(ip, country, zone);
        }

        //Retrieve the zone info
        private string ReadZone(FileStream fs, BinaryReader reader, uint offset)
        {
            fs.Position = offset;
            byte b = reader.ReadByte();
            if (b == RedirectMode.Mode_1 || b == RedirectMode.Mode_2)
            {
                uint zoneOffset = IPFormat.ToUint(reader.ReadBytes(3));
                if (zoneOffset == 0) return IPFormat.UnknownZone;
                return ReadZone(fs, reader, zoneOffset);
            }
            else
            {
                fs.Position--;
                return ReadString(reader);
            }
        }

        private string ReadString(BinaryReader reader)
        {
            List<byte> stringLst = new List<byte>();
            byte byteRead = 0;
            while ((byteRead = reader.ReadByte()) != 0)
            {
                stringLst.Add(byteRead);
            }
            return Encoding.GetEncoding("gb2312").GetString(stringLst.ToArray());
        }

        #endregion
    }
}

 

 

 

string strQQWry = System.Web.HttpContext.Current.Server.MapPath("QQWry.Dat").ToString();

QQIPSeekLib.IPSeeker seeker = new QQIPSeekLib.IPSeeker(strQQWry);

System.Net.IPAddress ipaddr = System.Net.IPAddress.Parse(dr["hostip"].ToString());

QQIPSeekLib.IPLocation loc = seeker.GetLocation(ipaddr);

loc.country;

loc.zone;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
lib-qqwry是一个高效查询纯真 IP 库(qqwry.dat)的模块。它是用 NodeJS 解析纯真 IP 库(QQwry.dat) ,支持 IP查询。为更好的字符转化效率,未使用iconv模块,直接使用gbk编码表文件。经过不断优化,相同环境下,单次查询速度从最初的0.6毫秒提升到现在的0.004毫秒。实现的功能1.通过IP地址或有效的IP数值,搜索IP地址对应的地理位置信息。2.搜索一个IP段的地理位置信息。3.IP地址与数值的互转。npm安装npm install lib-qqwry调用方法var qqwry = require('lib-qqwry').info(); //调用并初始化,普通机器初始需要70毫秒左右; var ipL = qqwry.searchIP("202.103.102.10"); //查询IP信息 var ipLA = qqwry.searchIPScope("0.0.0.0","1.0.0.0");  //查询IP段信息API标明的"静态方法"可以值接使用,无需初始化.初使化操作会将GBK编码表,IP库加载到内存中,以提高后续的查询效率,大概占用12M左右的内存.info(dataPath) IP库初始化dataPath : IP库路径,可选; //默认路径为data文件夹中(__dirname "/data/qqwry.dat");callback : 回调函数 //可在此时调用查询函数infoAsync(dataPath,callback) IP库初始化的异步方法info()的异步方法; 初始化需要70毫秒,以及占用9MB左右的内存,项目资源紧张可以异步初始化。//你可以这样 qqwry.info(); var ipL = qqwry.searchIP("202.103.102.10"); //也可以这样初始化,推荐; qqwry.infoAsync(function(){     var ipL = qqwry.searchIP("202.103.102.10"); });unInfo() 释放初始化后占用的资源info()的逆方法searchIP(IP) 单个IP查询IP : IP地址/IP数值 反回一个JSON对像;> qqwry.searchIP("255.255.255.255"); { ip: '255.255.255.255',   Country: '纯真网络',   Area: '2013年6月10日IP数据' }searchIPScope(beginIP,endIP) IP查询beginIP : 启始IPendIP : 结束IP反回一个JSON对像数组;> qqwry.searchIPScope("0.0.0.0","1.0.0.0"); [ { begIP: '0.0.0.0',     endIP: '0.255.255.255',     Country: 'IANA保留地址',     Area: ' CZ88.NET' },   { begIP: '1.0.0.0',     endIP: '1.0.0.255',     Country: '澳大利亚',     Area: ' CZ88.NET' } ]searchIPScopeAsync(beginIP,endIP,callback) IP查询的异步方法searchIPScope() 的异步方法,查询结果会以第一个参数的形式传给回调函数;DBUG(Bool) 调试模式开关,默认未启用DUBG模式会在控制台输出查询的关键信息,方便定位错误;var qqwry = require('lib-qqwry').DBUG().info(); //开启调试模式并初始化 qqrry.DBUG(false); //关闭调试模式;ipToInt(IP) IP地址转数值(静态方法)> qqwry.ipToInt("255.255.255.255") 4294967295intToIP(INT) 数值转IP地址(静态方法)> qqwry.intToIP(4294967295) '255.255.255.255'ipEndianChange(INT) 字节序转换(静态方法)按32位转换参数的字节序一些云平台的环境变量中IP信息可能是Little-Endian形式的数值;比如百度node.js环境中的 process.env.BAE_ENV_COOKIE_IP , 这时候就有用了;> qqwry.ipEndianChange(0x010000FF) 4278190081 //0xFF000001文档说明./data/qqwry.dat  默认IP库,可用最新I
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值