asp.net 纯真ip库取得所在地实现代码

有关纯真IP库的相关知识:查看( pdf格式)纯真IP库官方下载地址:http://www.cz88.net/
使用的类(已经经过调试.net 2.0,已对错误做了修改):


代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace QQWry.NET
{

public class IPLocation
{
private string _IP;
private string _Country;
private string _Local;
public string IP
{ get{return _IP;}
set{_IP = value;}
}
public string Country
{ get{return _Country;}
set{_Country = value;}
}
public string Local
{ get{return _Local;}
set{_Local = value;}
}
}
public class QQWryLocator
{
private byte[] data;
Regex regex = new Regex(@"(((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))\.){3}((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))");
long firstStartIpOffset;
long lastStartIpOffset;
long ipCount;
public long Count { get { return ipCount; } }
public QQWryLocator(string dataPath)
{
using (FileStream fs = new FileStream(dataPath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
data = new byte[fs.Length];
fs.Read(data, 0, data.Length);
}
byte[] buffer = new byte[8];
Array.Copy(data, 0, buffer, 0, 8);
firstStartIpOffset = ((buffer[0] + (buffer[1] * 0x100)) + ((buffer[2] * 0x100) * 0x100)) + (((buffer[3] * 0x100) * 0x100) * 0x100);
lastStartIpOffset = ((buffer[4] + (buffer[5] * 0x100)) + ((buffer[6] * 0x100) * 0x100)) + (((buffer[7] * 0x100) * 0x100) * 0x100);
ipCount = Convert.ToInt64((double)(((double)(lastStartIpOffset - firstStartIpOffset)) / 7.0));

if (ipCount <= 1L)
{
throw new ArgumentException("ip FileDataError");
}
}
private static long IpToInt(string ip)
{
char[] separator = new char[] { '.' };
if (ip.Split(separator).Length == 3)
{
ip = ip + ".0";
}
string[] strArray = ip.Split(separator);
long num2 = ((long.Parse(strArray[0]) * 0x100L) * 0x100L) * 0x100L;
long num3 = (long.Parse(strArray[1]) * 0x100L) * 0x100L;
long num4 = long.Parse(strArray[2]) * 0x100L;
long num5 = long.Parse(strArray[3]);
return (((num2 + num3) + num4) + num5);
}
private static string IntToIP(long ip_Int)
{
long num = (long)((ip_Int & 0xff000000L) >> 0x18);
if (num < 0L)
{
num += 0x100L;
}
long num2 = (ip_Int & 0xff0000L) >> 0x10;
if (num2 < 0L)
{
num2 += 0x100L;
}
long num3 = (ip_Int & 0xff00L) >> 8;
if (num3 < 0L)
{
num3 += 0x100L;
}
long num4 = ip_Int & 0xffL;
if (num4 < 0L)
{
num4 += 0x100L;
}
return (num.ToString() + "." + num2.ToString() + "." + num3.ToString() + "." + num4.ToString());
}
public IPLocation Query(string ip)
{
if (!regex.Match(ip).Success)
{
throw new ArgumentException("IP格式错误");
}
IPLocation ipLocation = new IPLocation();
ipLocation.IP = ip;
long intIP = IpToInt(ip);
if ((intIP >= IpToInt("127.0.0.1") && (intIP <= IpToInt("127.255.255.255"))))
{
ipLocation.Country = "本机内部环回地址";
ipLocation.Local = "";
}
else
{
if ((((intIP >= IpToInt("0.0.0.0")) && (intIP <= IpToInt("2.255.255.255"))) || ((intIP >= IpToInt("64.0.0.0")) && (intIP <= IpToInt("126.255.255.255")))) ||
((intIP >= IpToInt("58.0.0.0")) && (intIP <= IpToInt("60.255.255.255"))))
{
ipLocation.Country = "网络保留地址";
ipLocation.Local = "";
}
}
long right = ipCount;
long left = 0L;
long middle = 0L;
long startIp = 0L;
long endIpOff = 0L;
long endIp = 0L;
int countryFlag = 0;
while (left < (right - 1L))
{
middle = (right + left) / 2L;
startIp = GetStartIp(middle, out endIpOff);
if (intIP == startIp)
{
left = middle;
break;
}
if (intIP > startIp)
{
left = middle;
}
else
{
right = middle;
}
}
startIp = GetStartIp(left, out endIpOff);
endIp = GetEndIp(endIpOff, out countryFlag);
if ((startIp <= intIP) && (endIp >= intIP))
{
string local;
ipLocation.Country = GetCountry(endIpOff, countryFlag, out local);
ipLocation.Local = local;
}
else
{
ipLocation.Country = "未知";
ipLocation.Local = "";
}
return ipLocation;
}
private long GetStartIp(long left, out long endIpOff)
{
long leftOffset = firstStartIpOffset + (left * 7L);
byte[] buffer = new byte[7];
Array.Copy(data, leftOffset, buffer, 0, 7);
endIpOff = (Convert.ToInt64(buffer[4].ToString()) + (Convert.ToInt64(buffer[5].ToString()) * 0x100L)) + ((Convert.ToInt64(buffer[6].ToString()) * 0x100L) * 0x100L);
return ((Convert.ToInt64(buffer[0].ToString()) + (Convert.ToInt64(buffer[1].ToString()) * 0x100L)) + ((Convert.ToInt64(buffer[2].ToString()) * 0x100L) * 0x100L)) + (((Convert.ToInt64(buffer[3].ToString()) * 0x100L) * 0x100L) * 0x100L);
}
private long GetEndIp(long endIpOff, out int countryFlag)
{
byte[] buffer = new byte[5];
Array.Copy(data, endIpOff, buffer, 0, 5);
countryFlag = buffer[4];
return ((Convert.ToInt64(buffer[0].ToString()) + (Convert.ToInt64(buffer[1].ToString()) * 0x100L)) + ((Convert.ToInt64(buffer[2].ToString()) * 0x100L) * 0x100L)) + (((Convert.ToInt64(buffer[3].ToString()) * 0x100L) * 0x100L) * 0x100L);
}
/// <summary>
/// Gets the country.
/// </summary>
/// <param name="endIpOff">The end ip off.</param>
/// <param name="countryFlag">The country flag.</param>
/// <param name="local">The local.</param>
/// <returns>country</returns>
private string GetCountry(long endIpOff, int countryFlag, out string local)
{
string country = "";
long offset = endIpOff + 4L;
switch (countryFlag)
{
case 1:
case 2:
country = GetFlagStr(ref offset, ref countryFlag, ref endIpOff);
offset = endIpOff + 8L;
local = (1 == countryFlag) ? "" : GetFlagStr(ref offset, ref countryFlag, ref endIpOff);
break;
default:
country = GetFlagStr(ref offset, ref countryFlag, ref endIpOff);
local = GetFlagStr(ref offset, ref countryFlag, ref endIpOff);
break;
}
return country;
}
private string GetFlagStr(ref long offset, ref int countryFlag, ref long endIpOff)
{
int flag = 0;
byte[] buffer = new byte[3];

while (true)
{
//用于向前累加偏移量
long forwardOffset = offset;
flag = data[forwardOffset++];
//没有重定向
if (flag != 1 && flag != 2)
{
break;
}
Array.Copy(data, forwardOffset, buffer, 0, 3);
forwardOffset += 3;
if (flag == 2)
{
countryFlag = 2;
endIpOff = offset - 4L;
}
offset = (Convert.ToInt64(buffer[0].ToString()) + (Convert.ToInt64(buffer[1].ToString()) * 0x100L)) + ((Convert.ToInt64(buffer[2].ToString()) * 0x100L) * 0x100L);
}
if (offset < 12L)
{
return "";
}
return GetStr(ref offset);
}
private string GetStr(ref long offset)
{
byte lowByte = 0;
byte highByte = 0;
StringBuilder stringBuilder = new StringBuilder();
byte[] bytes = new byte[2];
Encoding encoding = Encoding.GetEncoding("GB2312");
while (true)
{
lowByte = data[offset++];
if (lowByte == 0)
{
return stringBuilder.ToString();
}
if (lowByte > 0x7f)
{
highByte = data[offset++];
bytes[0] = lowByte;
bytes[1] = highByte;
if (highByte == 0)
{
return stringBuilder.ToString();
}
stringBuilder.Append(encoding.GetString(bytes));
}
else
{
stringBuilder.Append((char)lowByte);
}
}
}
}
}

使用:


代码如下:

QQWry.NET.QQWryLocator qqWry = new QQWry.NET.QQWryLocator(Server.MapPath("QQWry.Dat"));
QQWry.NET.IPLocation ip = qqWry.Query("120.67.217.7");
Response.Write(ip.IP + "<br />");
Response.Write(ip.Country + "<br />");
Response.Write(ip.Local);

结果:

(0)

相关推荐

  • asp.net 纯真ip库取得所在地实现代码

    有关纯真IP库的相关知识:查看( pdf格式)纯真IP库官方下载地址:http://www.cz88.net/ 使用的类(已经经过调试.net 2.0,已对错误做了修改): 复制代码 代码如下: using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Text.RegularExpressions; namespace QQWry.NET { public c

  • asp.net DZ论坛中根据IP地址取得所在地的代码

    使用方法: IpSearch.GetAddressWithIP("202.96.128.167")CS类代码 复制代码 代码如下: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI

  • C#读取QQ纯真IP数据库QQWry.Dat的代码

    纯真版QQ IP数据库 复制代码 代码如下: using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Web; using System.Configuration; namespace BLL { public class IPLocationSearch { private static readonly QQWry qq = new QQWry(Conf

  • 珊瑚虫IP库浅析

    这不是什么新鲜事情了,很早之前就已经有人做出来了.就是使用PHP操作纯真IP库或珊瑚虫IP库,根据来访者的IP得到所在的物理位置. 我先帖出代码.然后再慢慢一步步浅析出来.希望对想了解这一块的朋友们有帮助. Only For PHP5的代码.会继续优化代码的. class IpLocation{    private $fp;    private $wrydat;    private $wrydat_version;    private $ipnumber;    private $fir

  • python访问纯真IP数据库的代码

    核心代码: #!/usr/bin/env python # -*- coding: utf-8 -*- from bisect import bisect _LIST1, _LIST2 = [], [] _INIT = False ip2int = lambda ip_str: reduce(lambda a, b: (a << 8) + b, [int(i) for i in ip_str.split('.')]) def _init(): global _LIST, _INIT if no

  • PHP获取IP地址所在地信息的实例(使用纯真IP数据库qqwry.dat)

    如下所示: <?php $data = '254.254.254.254'; echo ip2long($data); function getIP() { if(getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) $ip = getenv("HTTP_CLIENT_IP"); elseif(getenv

  • asp.net下将纯真IP数据导入数据库中的代码

    纯真IP数据包含381085条,可以通过下载的查询软件将数据解压为文本格式,并将其编码改为UTF8,否则在程序中读取中文会乱码! 下面为程序执行分析IP数据并插入到Sql Server的截图: 程序通过AJAX在客户端进行数据插入实时更新: 实现代码如下: 前端页面及javascript: 复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.or

  • Python实现一个转存纯真IP数据库的脚本分享

    前言 之前写过很多关于扫描脚本的文章,一直都没写自己的扫描IP段是哪里搞来的,也会有朋友经常来问一些扫描经验,说实话我觉得这个工具并没有实际的技术含量,但是能提高工作效率,就共享出来给大家耍耍- 谈到扫描经验,我个人通常都会针对不同的设备,不同的应用选择不同类型的段. 比如我现在扫描的目标是一款电信光猫,那自然是选择电信的IP段,光猫一般是家庭用户,我们筛选下家庭用户的活跃IP段,这样我们就有针对性了. 再比如我现在想扫一款企业路由设备,那么我就可以选择企业公司多的段. 纯真IP真心是个不错的工

  • 实现纯真IP txt转mdb数据库的方法

    纯真ip数据库转动网数据库ASP脚本 去纯真网站上下载ip数据库,同时随数据库附送的查询程序(Showip.exe)具有在线检测并升级IP数据库的功能,只要运行该程序,点击右上角的 "在线升级",就可以升级IP数据库到最新的版本,无需再到下载网站下载新版的IP数据库. 同时查询程序(Showip.exe)还提供了解压为.txt文件的功能:) 我们就借助这一功能实现转MDB格式的数据库. 第一步:打开查询程序(Showip.exe),点击解压 第二步:保存导出的数据文本为ipaddres

  • 淘宝IP地址库采集器c#代码

    采集器概貌,如下: 最近做一个项目,功能类似于CNZZ站长统计功能,要求显示Ip所在的省份市区/提供商等信息.网上的Ip纯真数据库,下载下来一看,发现没提供商内容,省市区都很少,居然有XXX网吧,哥瞬间倒了.没标准化.并且杂乱.还不连续的IP段.总体说来没达到要求. 在百度上找啊找,找到淘宝Ip地址库,官方介绍的相当诱人,准确率高,数据质量有保障,提供国家.省.市.县.运营商全方位信息,信息维度广,格式规范,但是限制每秒10次的访问(这个比较无语). 淘宝IP地址库,提供API    http:

随机推荐