IPScanner.cs

public class IPScanner
{ 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 IPScanner(string dataPath)
{
using (FileStream fs = new FileStream(dataPath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
data = new byte[fs.Length];
fs.Read(data, , data.Length);
}
byte[] buffer = new byte[];
Array.Copy(data, , buffer, , );
firstStartIpOffset = ((buffer[] + (buffer[] * 0x100)) + ((buffer[] * 0x100) * 0x100)) + (((buffer[] * 0x100) * 0x100) * 0x100);
lastStartIpOffset = ((buffer[] + (buffer[] * 0x100)) + ((buffer[] * 0x100) * 0x100)) + (((buffer[] * 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 == )
{
ip = ip + ".0";
}
string[] strArray = ip.Split(separator);
long num2 = ((long.Parse(strArray[]) * 0x100L) * 0x100L) * 0x100L;
long num3 = (long.Parse(strArray[]) * 0x100L) * 0x100L;
long num4 = long.Parse(strArray[]) * 0x100L;
long num5 = long.Parse(strArray[]);
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) >> ;
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)
{
ip = "300.300.300.300";
}
IPLocation ipLocation = new 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 = ;
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 = "未知的IP地址";
ipLocation.Local = "";
}
return ipLocation;
}
private long GetStartIp(long left, out long endIpOff)
{
long leftOffset = firstStartIpOffset + (left * 7L);
byte[] buffer = new byte[];
Array.Copy(data, leftOffset, buffer, , );
endIpOff = (Convert.ToInt64(buffer[].ToString()) + (Convert.ToInt64(buffer[].ToString()) * 0x100L)) + ((Convert.ToInt64(buffer[].ToString()) * 0x100L) * 0x100L);
return ((Convert.ToInt64(buffer[].ToString()) + (Convert.ToInt64(buffer[].ToString()) * 0x100L)) + ((Convert.ToInt64(buffer[].ToString()) * 0x100L) * 0x100L)) + (((Convert.ToInt64(buffer[].ToString()) * 0x100L) * 0x100L) * 0x100L);
}
private long GetEndIp(long endIpOff, out int countryFlag)
{
byte[] buffer = new byte[];
Array.Copy(data, endIpOff, buffer, , );
countryFlag = buffer[];
return ((Convert.ToInt64(buffer[].ToString()) + (Convert.ToInt64(buffer[].ToString()) * 0x100L)) + ((Convert.ToInt64(buffer[].ToString()) * 0x100L) * 0x100L)) + (((Convert.ToInt64(buffer[].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 :
case :
country = GetFlagStr(ref offset, ref countryFlag, ref endIpOff);
offset = endIpOff + 8L;
local = ( == 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 = ;
byte[] buffer = new byte[]; while (true)
{
//用于向前累加偏移量
long forwardOffset = offset;
flag = data[forwardOffset++];
//没有重定向
if (flag != && flag != )
{
break;
}
Array.Copy(data, forwardOffset, buffer, , );
forwardOffset += ;
if (flag == )
{
countryFlag = ;
endIpOff = offset - 4L;
}
offset = (Convert.ToInt64(buffer[].ToString()) + (Convert.ToInt64(buffer[].ToString()) * 0x100L)) + ((Convert.ToInt64(buffer[].ToString()) * 0x100L) * 0x100L);
}
if (offset < 12L)
{
return "";
}
return GetStr(ref offset);
}
private string GetStr(ref long offset)
{
byte lowByte = ;
byte highByte = ;
StringBuilder stringBuilder = new StringBuilder();
byte[] bytes = new byte[];
Encoding encoding = Encoding.GetEncoding("GB2312");
while (true)
{
lowByte = data[offset++];
if (lowByte == )
{
return stringBuilder.ToString();
}
if (lowByte > 0x7f)
{
highByte = data[offset++];
bytes[] = lowByte;
bytes[] = highByte;
if (highByte == )
{
return stringBuilder.ToString();
}
stringBuilder.Append(encoding.GetString(bytes));
}
else
{
stringBuilder.Append((char)lowByte);
}
}
}
}

映射类如下:

IPLocation.cs

 public class IPLocation
{
public string IP { get; set; }
public string Country { get; set; }
public string Local { get; set; }
}

调用方法:

IPScanner qqWry = new IPScanner(Server.MapPath("~/Common/QQWry.dat"));

IPLocation ip = qqWry.Query("真实ip地址");

C#实现根据IP 查找真实地址的更多相关文章

  1. PHP转换IP地址到真实地址的方法详解

    本篇文章是对PHP转换IP地址到真实地址的方法进行了详细的分析介绍,需要的朋友参考下   想要把IPv4地址转为真实的地址,肯定要参考IP数据库,商业的IP数据库存储在关系型数据库中,查询和使用都非常 ...

  2. 绕过CDN查找真实IP方法总结

    CDN的全称是Content Delivery Network,即内容分发网络.CDN是构建在现有网络基础之上的智能虚拟网络,依靠部署在各地的边缘服务器,通过中心平台的负载均衡.内容分发.调度等功能模 ...

  3. [转载]绕过CDN查找真实IP方法总结

    前言 类似备忘录形式记录一下,这里结合了几篇绕过CDN寻找真实IP的文章,总结一下绕过CDN查找真实的IP的方法 介绍 CDN的全称是Content Delivery Network,即内容分发网络. ...

  4. 绕过CDN查找真实IP的方法

    正常情况下,通过cmd命令可以快速找到域名对应IP,最常见的命令如ping.nslookup.但很多站点出于用户体验和安全的角度,使用CDN加速,将域名解析到CDN,这时候就需要绕过CDN来查找真实I ...

  5. 百度乐播音乐真实地址查找api接口

    1.百度乐播官网:http://lebo.baidu.com: 随便点击进去一个音乐界面,如:http://lebo.baidu.com/album/9036366 2.chrome浏览器右击'检查' ...

  6. CIDR详解和ip最长地址前缀匹配

    1.CIDR是什么 无类域间路由(CIDR)编址方案 摒弃传统的基于类的地址分配方式,允许使用任意长度的地址前缀,有效提高地址空间的利用率. 就是一个ip加一个网络掩码,不过这个掩码不是之前只有3个值 ...

  7. Python爬虫教程-11-proxy代理IP,隐藏地址(猫眼电影)

    Python爬虫教程-11-proxy代理IP,隐藏地址(猫眼电影) ProxyHandler处理(代理服务器),使用代理IP,是爬虫的常用手段,通常使用UserAgent 伪装浏览器爬取仍然可能被网 ...

  8. 谢欣伦 - OpenDev原创教程 - 本地IP查找类CxLocalHostIPAddrFind

    这是一个精练的本地IP查找类,类名.函数名和变量名均采用匈牙利命名法.小写的x代表我的姓氏首字母(谢欣伦),个人习惯而已,如有雷同,纯属巧合. CxLocalHostIPAddrFind的使用如下: ...

  9. PHP ip 查找 城市名

    PHP ip 查找 城市名 header("content-type:text/html;charset=utf-8");date_default_timezone_set(&qu ...

随机推荐

  1. [英语学习]国外的在线广播网站,类似喜马拉雅和荔枝FM

    今天在Seattle Art  Museum 网站上看东西,发现了这个好网站. 主要是外文资料,可以练听力,也可以找到<我爱吕西安>的英文版本. https://soundcloud.co ...

  2. [ZZ] Understanding 3D rendering step by step with 3DMark11 - BeHardware >> Graphics cards

    http://www.behardware.com/art/lire/845/ --> Understanding 3D rendering step by step with 3DMark11 ...

  3. ecshop session机制

    ecshop session机制 2014-06-12    1455     懒人程序   ecshop的cls_session.php分析,主要是讲述ecshop中的session机制.我们都知道 ...

  4. easy ui 问题

    easyui  的样式  和Bootstrap css   有冲突,不要一起使用 日期禁止输入 editable="false" ------------------------- ...

  5. 浏览器cookie数

    <?php for ($w=0; $w < 200 ; $w++) { setcookie('name'.$w,'value'.$w, time()+3600*10 ); } var_du ...

  6. 防止sql注入,过滤敏感关键字

    //sql过滤关键字 public static bool CheckKeyWord(string sWord) { //过滤关键字 string StrKeyWord = @"select ...

  7. Java高级之类结构的认识

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! Java体系包括,各种版本的虚拟机,可执行文件,各种第三方类库,Java API类库,Java语言 ...

  8. AOP 底层技术比较

    表 1. AOP 底层技术比较 AOP 底层技术 功能 性能 面向接口编程 编程难度 直接改写 class 文件 完全控制类 无明显性能代价 不要求 高,要求对 class 文件结构和 Java 字节 ...

  9. Wordpress更改后台地址

    wordpress默认的后台地址是 xx/wp-admin  或xx/wp-login.php ,谁都知道感觉很不安全, 方法一:使用插件 通过插件在地址上加上只有你知道的参数才能访问 1.后台搜索插 ...

  10. HttpPost发送Json

    1.public static JSONObject post(String url,JSONObject json){ 2. HttpClient client = new DefaultHttpC ...