C#实现根据IP 查找真实地址

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地址");
上一篇:UVa 1590 IP网络(简单位运算)


下一篇:几种获取IP 根据IP获取地址的方法 JS,第三方 新浪 网易 腾讯