左耳出灵 2007-4-20 18:14
在自己的网页上实现IP地域查询(C#版本)
最近反编译了一个DLL文件
意外得到一个可以利用纯真IP库(QQWry.dat)实现IP地狱查询的类
一下是我的asp.net.0(C#)版本 主要方法为红色部分,这个类主要利用到了编码和索引 原理很简单
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.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace mymethod
{
public class IPLocation
{
// Fields
private static string _dataPath = "";
private static string _ip = "0.0.0.0";
private static string country;
private static int countryFlag = 0;
private static long endIp = 0;
private static long endIpOff = 0;
private static string errMsg = null;
private static long firstStartIp = 0;
private static long lastStartIp = 0;
private static string local;
private static FileStream objfs = null;
private static long startIp = 0;
// Methods
private static string GetCountry()
{
switch (countryFlag)
{
case 1:
case 2:
country = GetFlagStr(endIpOff + 4);
local = (1 == countryFlag) ? " " : GetFlagStr(endIpOff + 8);
break;
default:
country = GetFlagStr(endIpOff + 4);
local = GetFlagStr(objfs.Position);
break;
}
return " ";
}
private static long GetEndIp()
{
objfs.Position = endIpOff;
byte[] buffer = new byte[5];
objfs.Read(buffer, 0, 5);
endIp = ((Convert.ToInt64(buffer[0].ToString()) + (Convert.ToInt64(buffer[1].ToString()) * 0x100)) + ((Convert.ToInt64(buffer[2].ToString()) * 0x100) * 0x100)) + (((Convert.ToInt64(buffer[3].ToString()) * 0x100) * 0x100) * 0x100);
countryFlag = buffer[4];
return endIp;
}
private static string GetFlagStr(long offSet)
{
int num = 0;
byte[] buffer = new byte[3];
while (true)
{
objfs.Position = offSet;
num = objfs.ReadByte();
if ((num != 1) && (num != 2))
{
break;
}
objfs.Read(buffer, 0, 3);
if (num == 2)
{
countryFlag = 2;
endIpOff = offSet - 4;
}
offSet = (Convert.ToInt64(buffer[0].ToString()) + (Convert.ToInt64(buffer[1].ToString()) * 0x100)) + ((Convert.ToInt64(buffer[2].ToString()) * 0x100) * 0x100);
}
if (offSet < 12)
{
return " ";
}
objfs.Position = offSet;
return GetStr();
}
private static long GetStartIp(long recNO)
{
long num = firstStartIp + (recNO * 7);
objfs.Position = num;
byte[] buffer = new byte[7];
objfs.Read(buffer, 0, 7);
endIpOff = (Convert.ToInt64(buffer[4].ToString()) + (Convert.ToInt64(buffer[5].ToString()) * 0x100)) + ((Convert.ToInt64(buffer[6].ToString()) * 0x100) * 0x100);
startIp = ((Convert.ToInt64(buffer[0].ToString()) + (Convert.ToInt64(buffer[1].ToString()) * 0x100)) + ((Convert.ToInt64(buffer[2].ToString()) * 0x100) * 0x100)) + (((Convert.ToInt64(buffer[3].ToString()) * 0x100) * 0x100) * 0x100);
return startIp;
}
private static string GetStr()
{
byte num = 0;
byte num2 = 0;
string text = "";
byte[] bytes = new byte[2];
while (true)
{
num = (byte)objfs.ReadByte();
if (num == 0)
{
return text;
}
if (num > 0x7f)
{
num2 = (byte)objfs.ReadByte();
bytes[0] = num;
bytes[1] = num2;
Encoding encoding = Encoding.GetEncoding("GB2312");
if (num2 == 0)
{
return text;
}
text = text + encoding.GetString(bytes);
}
else
{
text = text + ((char)num);
}
}
}
private static string IntToIP(long ip_Int)
{
long num = (long)((ip_Int & 0xff000000) >> 0x18);
if (num < 0)
{
num += 0x100;
}
long num2 = (ip_Int & 0xff0000) >> 0x10;
if (num2 < 0)
{
num2 += 0x100;
}
long num3 = (ip_Int & 0xff00) >> 8;
if (num3 < 0)
{
num3 += 0x100;
}
long num4 = ip_Int & 0xff;
if (num4 < 0)
{
num4 += 0x100;
}
return (num.ToString() + "." + num2.ToString() + "." + num3.ToString() + "." + num4.ToString());
}
[color=Red] public static string IPLocate(string dataPath, string ip)[/color]
[color=Red] {[/color]
[color=Red] _dataPath = dataPath;[/color]
[color=Red] _ip = ip;[/color]
[color=Red] QQwry();[/color]
[color=Red] return (country + local).Replace("CZ88.NET", "");[/color]
[color=Red] }[/color]
private static long IpToInt(string ip)
{
char[] separator = new char[] { '.' };
if (ip.Split(separator).Length == 3)
{
ip = ip + ".0";
}
string[] textArray = ip.Split(separator);
long num2 = ((long.Parse(textArray[0]) * 0x100) * 0x100) * 0x100;
long num3 = (long.Parse(textArray[1]) * 0x100) * 0x100;
long num4 = long.Parse(textArray[2]) * 0x100;
long num5 = long.Parse(textArray[3]);
return (((num2 + num3) + num4) + num5);
}
private static int QQwry()
{
string pattern = @"(((\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]))";
Regex regex = new Regex(pattern);
if (!regex.Match(_ip).Success)
{
errMsg = "IP\u683c\u5f0f\u9519\u8bef";
return 4;
}
long num = IpToInt(_ip);
int num2 = 0;
if ((num >= IpToInt("127.0.0.0")) && (num <= IpToInt("127.255.255.255")))
{
country = "\u672c\u673a\u5185\u90e8\u73af\u56de\u5730\u5740";
local = "";
num2 = 1;
}
else if ((((num >= IpToInt("0.0.0.0")) && (num <= IpToInt("2.255.255.255"))) || ((num >= IpToInt("64.0.0.0")) && (num <= IpToInt("126.255.255.255")))) || ((num >= IpToInt("58.0.0.0")) && (num <= IpToInt("60.255.255.255"))))
{
country = "\u7f51\u7edc\u4fdd\u7559\u5730\u5740";
local = "";
num2 = 1;
}
try
{
objfs = new FileStream(_dataPath, FileMode.Open, FileAccess.Read);
}
catch
{
}
try
{
objfs.Position = 0;
byte[] buffer = new byte[8];
objfs.Read(buffer, 0, 8);
firstStartIp = ((buffer[0] + (buffer[1] * 0x100)) + ((buffer[2] * 0x100) * 0x100)) + (((buffer[3] * 0x100) * 0x100) * 0x100);
lastStartIp = ((buffer[4] + (buffer[5] * 0x100)) + ((buffer[6] * 0x100) * 0x100)) + (((buffer[7] * 0x100) * 0x100) * 0x100);
long num3 = Convert.ToInt64((double)(((double)(lastStartIp - firstStartIp)) / 7));
if (num3 <= 1)
{
country = "FileDataError";
objfs.Close();
return 2;
}
long num4 = num3;
long recNO = 0;
long num6 = 0;
while (recNO < (num4 - 1))
{
num6 = (num4 + recNO) / ((long)2);
GetStartIp(num6);
if (num == startIp)
{
recNO = num6;
break;
}
if (num > startIp)
{
recNO = num6;
}
else
{
num4 = num6;
}
}
GetStartIp(recNO);
GetEndIp();
if ((startIp <= num) && (endIp >= num))
{
GetCountry();
local = local.Replace("\uff08\u6211\u4eec\u4e00\u5b9a\u8981\u89e3\u653e\u53f0\u6e7e\uff01\uff01\uff01\uff09", "");
}
else
{
num2 = 3;
country = "\u672a\u77e5";
local = "";
}
objfs.Close();
return num2;
}
catch (Exception exception)
{
HttpContext.Current.Response.Write(exception.Message);
return 1;
}
}
// Properties
public static string Country
{
get
{
return country;
}
}
public static string DataPath
{
set
{
_dataPath = value;
}
}
public static string ErrMsg
{
get
{
return errMsg;
}
}
public static string IP
{
set
{
_ip = value;
}
}
public static FileStream IPFileStream
{
set
{
objfs = value;
}
}
public static string Local
{
get
{
return local;
}
}
}
}