Пример #1
0
        /// <inheritdoc />
        /// <summary>
        ///  获取指定IP所在地理位置
        /// </summary>
        /// <param name="strIp">要查询的IP地址</param>
        /// <returns></returns>
        public virtual IpLocation GetIpLocation(string strIp)
        {
            var loc = new IpLocation
            {
                Ip = strIp
            };

            if (!CheckIp(strIp))
            {
                return(loc);
            }

            var ip = IpToLong(strIp);

            if (ip == IpToLong("127.0.0.1"))
            {
                loc.Country = "本机内部环回地址";
                loc.Area    = string.Empty;
                return(loc);
            }

            if (!Init())
            {
                return(loc);
            }

            return(ReadLocation(loc, ip, _startPosition, _ipIndexCache, _qqwryDbBytes));
        }
Пример #2
0
        /// <inheritdoc />
        /// <summary>
        ///  获取指定IP所在地理位置
        /// </summary>
        /// <param name="strIp">要查询的IP地址</param>
        /// <param name="token"></param>
        /// <returns></returns>
        public virtual async Task <IpLocation> GetIpLocationAsync(string strIp, CancellationToken token = default(CancellationToken))
        {
            var loc = new IpLocation
            {
                Ip = strIp
            };

            if (!CheckIp(strIp))
            {
                return(loc);
            }
            long ip = IpToLong(strIp);

            if (ip == IpToLong("127.0.0.1"))
            {
                loc.Country = "本机内部环回地址";
                loc.Area    = string.Empty;
                return(loc);
            }
            if (!await InitAsync(false, token))
            {
                return(loc);
            }
            return(ReadLocation(loc, ip, _startPosition, _ipIndexCache, _qqwryDbBytes));
        }
Пример #3
0
        private static IpLocation ReadLocation(IpLocation loc, long ip, long startPosition, long[] ipIndex, byte[] qqwryDbBytes)
        {
            long offset = SearchIp(ip, ipIndex, 0, ipIndex.Length) * 7 + 4;

            //偏移
            var arrayOffset = startPosition + offset;

            //跳过结束IP
            arrayOffset = ReadLongX(qqwryDbBytes, arrayOffset, 3) + 4;
            //读取标志
            var flag = qqwryDbBytes[arrayOffset];

            arrayOffset += 1;
            //表示国家和地区被转向
            if (flag == 1)
            {
                arrayOffset = ReadLongX(qqwryDbBytes, arrayOffset, 3);
                //再读标志
                flag         = qqwryDbBytes[arrayOffset];
                arrayOffset += 1;
            }
            var countryOffset = arrayOffset;

            loc.Country = ReadString(qqwryDbBytes, flag, ref arrayOffset);

            if (flag == 2)
            {
                arrayOffset = countryOffset + 3;
            }

            flag         = qqwryDbBytes[arrayOffset];
            arrayOffset += 1;
            loc.Area     = ReadString(qqwryDbBytes, flag, ref arrayOffset);

            if (" CZ88.NET".Equals(loc.Area, StringComparison.CurrentCultureIgnoreCase))
            {
                loc.Area = string.Empty;
            }

            return(loc);
        }
Пример #4
0
        ///<summary>
        ///取得具体信息
        ///</summary>
        ///<param name="offset"></param>
        ///<returns></returns>
        private IpLocation getIPLocation(IpLocation loc, IpDbAccessor ipFile, long offset)
        {
            ipFile.Position = offset + 4;
            //读取第一个字节判断是否是标志字节
            byte one = (byte)ipFile.ReadByte();

            if (one == REDIRECT_MODE_1)
            {
                //第一种模式
                //读取国家偏移
                long countryOffset = ReadLongX(ipFile, 3);//readLong3();
                //转至偏移处
                ipFile.Position = countryOffset;
                //再次检查标志字节
                byte b = (byte)ipFile.ReadByte();
                if (b == REDIRECT_MODE_2)
                {
                    loc.Country     = readString(ipFile, ReadLongX(ipFile, 3));//readString(readLong3());
                    ipFile.Position = countryOffset + 4;
                }
                else
                {
                    loc.Country = readString(ipFile, countryOffset);
                }
                //读取地区标志
                loc.Area = readArea(ipFile, ipFile.Position);
            }
            else if (one == REDIRECT_MODE_2)
            {
                //第二种模式
                loc.Country = readString(ipFile, ReadLongX(ipFile, 3));//readString(readLong3());
                loc.Area    = readArea(ipFile, offset + 8);
            }
            else
            {
                //普通模式
                loc.Country = readString(ipFile, --ipFile.Position);
                loc.Area    = readString(ipFile, ipFile.Position);
            }
            return(loc);
        }
Пример #5
0
        /// <inheritdoc />
        /// <summary>
        ///  获取指定IP所在地理位置
        /// </summary>
        /// <param name="strIp">要查询的IP地址</param>
        /// <param name="token"></param>
        /// <returns></returns>
        public virtual async Task <IpLocation> GetIpLocationAsync(string strIp, CancellationToken token = default(CancellationToken))
        {
            var loc = new IpLocation
            {
                Ip = strIp
            };

            long ip = IpToLong(strIp);

            if (ip == IpToLong("127.0.0.1"))
            {
                loc.Country = "本机内部环回地址";
                loc.Area    = string.Empty;
                return(loc);
            }
            if (!await InitAsync(false, token))
            {
                return(loc);
            }
            return(ReadLocation(loc, strIp));
        }
Пример #6
0
        ///<summary>
        ///搜索IP地址搜索
        ///</summary>
        ///<param name="ip"></param>
        ///<returns></returns>
        public IpLocation ReadLocation(IpLocation loc, 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)(int.Parse(ipSp[i]) & 0xFF);
            }
            var  ipFile = new IpDbAccessor(_qqwryDbBytes);
            long offset = locateIP(ipFile, IP);

            if (offset != -1)
            {
                loc = getIPLocation(loc, ipFile, offset);
            }
            return(loc);
        }
Пример #7
0
        private static IpLocation ReadLocation(IpLocation loc, long ip, long startPosition, long[] ipIndex, byte[] qqwryDbBytes)
        {
            long offset = SearchIp(ip, ipIndex, 0, ipIndex.Length) * 7 + 4;

            using (var stream = new MemoryStream(qqwryDbBytes))
            {
                stream.Position = startPosition;
                //跳过起始IP
                stream.Position += offset;
                //跳过结束IP
                stream.Position = ReadLongX(stream, 3) + 4;

                //读取标志
                var flag = stream.ReadByte();
                //表示国家和地区被转向
                if (flag == 1)
                {
                    stream.Position = ReadLongX(stream, 3);
                    //再读标志
                    flag = stream.ReadByte();
                }
                var countryOffset = stream.Position;
                loc.Country = ReadString(stream, flag);

                if (flag == 2)
                {
                    stream.Position = countryOffset + 3;
                }
                flag     = stream.ReadByte();
                loc.Area = ReadString(stream, flag);
            }
            if (" CZ88.NET".Equals(loc.Area, StringComparison.CurrentCultureIgnoreCase))
            {
                loc.Area = string.Empty;
            }

            return(loc);
        }
Пример #8
0
        /// <inheritdoc />
        /// <summary>
        ///  获取指定IP所在地理位置
        /// </summary>
        /// <param name="strIp">要查询的IP地址</param>
        /// <returns></returns>
        public virtual IpLocation GetIpLocation(string strIp)
        {
            var loc = new IpLocation
            {
                Ip = strIp
            };

            var ip = IpToLong(strIp);

            if (ip == IpToLong("127.0.0.1"))
            {
                loc.Country = "本机内部环回地址";
                loc.Area    = string.Empty;
                return(loc);
            }

            if (!Init())
            {
                return(loc);
            }

            return(ReadLocation(loc, strIp));
        }