Пример #1
0
        /// <summary>
        /// IP位置查询
        /// </summary>
        /// <param name="address">ipv4地址</param>
        /// <returns>
        /// IP位置信息
        /// </returns>
        public static IpLocation Find(string address)
        {
            if (IPAddress.TryParse(address, out IPAddress ip))
            {
                var octets = ip.GetAddressBytes();

                if (octets.Length == 4)
                {
                    return(Searcher.Value.Search(IpLocationHelper.IPv4ToInteger(ip.GetAddressBytes())));
                }
            }

            return(null);
        }
Пример #2
0
        /// <summary>
        /// IP查询
        /// </summary>
        /// <param name="address">IP地址</param>
        /// <returns>IP位置信息</returns>
        public IpLocation Search(uint address)
        {
            if (IpLocationHelper.IsReserved(address))
            {
                return(new IpLocation
                {
                    City = "内网IP",
                    Isp = "保留地址"
                });
            }

            //IP索引分区前缀
            var prefix = (byte)(address >> 24);

            FindPartition(prefix, out int start, out int end);

            //如果不在分区索引上则从整个IP数据储存进行搜索
            if (start < 0 || end < 0)
            {
                start = ip_start_position;
                end   = (int)DataSource.Length - 1;
            }

            string data;

            if (DataSource is MemoryDataSource)
            {
                data = FindDataByMemory(start, end, address);
            }
            else
            {
                data = FindData(start, end, address);
            }

            if (!string.IsNullOrWhiteSpace(data))
            {
                var seg = data.Split('|');

                return(new IpLocation
                {
                    Country = seg[0],
                    Province = seg[1],
                    City = seg[2],
                    Isp = seg[3]
                });
            }

            return(null);
        }