ASP 纯真IP数据库QQWry.dat读取操作类

2014 年 10 月 14 日4830

通过该操作类可轻易的将IP转换成城市地区信息。

ASP code
<%

Class TQQWry

Country, LocalStr, Buf, OffSet

Private StartIP, EndIP, CountryFlag

Public QQWryFile

Public FirstStartIP, LastStartIP, RecordCount

Private Stream, EndIPOff

Class_Initialize()

Country = ""

LocalStr = ""

StartIP = 0

EndIP = 0

CountryFlag = 0

FirstStartIP = 0

LastStartIP = 0

EndIPOff = 0

QQWryFile = Server.MapPath("QQWry.Dat")

IPToInt(IP)

Dim IPArray, i

IPArray = Split(IP, ".", -1)

For i = 0 To 3

(IPArray(i)) Then IPArray(i) = 0

If CInt(IPArray(i)) < 0 Then IPArray(i) = Abs(CInt(IPArray(i)))

If CInt(IPArray(i)) > 255 Then IPArray(i) = 255

Next

IPToInt = (CInt(IPArray(0)) * 256 * 256 * 256) + (CInt(IPArray(1)) * 256 * 256) + (CInt(IPArray(2)) * 256) + CInt(IPArray(3))

IntToIP(IntValue)

p4 = IntValue - Fix(IntValue / 256) * 256

IntValue = (IntValue - p4) / 256

p3 = IntValue - Fix(IntValue / 256) * 256

IntValue = (IntValue - p3) / 256

p2 = IntValue - Fix(IntValue / 256) * 256

IntValue = (IntValue - p2) / 256

p1 = IntValue

IntToIP = CStr(p1) & "." & CStr(p2) & "." & CStr(p3) & "." & CStr(p4)

GetStartIP(RecNo)

OffSet = FirstStartIP + RecNo * 7

Stream.Position = OffSet

Buf = Stream.Read(7)

EndIPOff = AscB(MidB(Buf, 5, 1)) + (AscB(MidB(Buf, 6, 1)) * 256) + (AscB(MidB(Buf, 7, 1)) * 256 * 256)

StartIP = AscB(MidB(Buf, 1, 1)) + (AscB(MidB(Buf, 2, 1)) * 256) + (AscB(MidB(Buf, 3, 1)) * 256 * 256) + (AscB(MidB(Buf, 4, 1)) * 256 * 256 * 256)

GetStartIP = StartIP

GetEndIP()

Stream.Position = EndIPOff

Buf = Stream.Read(5)

EndIP = AscB(MidB(Buf, 1, 1)) + (AscB(MidB(Buf, 2, 1)) * 256) + (AscB(MidB(Buf, 3, 1)) * 256 * 256) + (AscB(MidB(Buf, 4, 1)) * 256 * 256 * 256)

CountryFlag = AscB(MidB(Buf, 5, 1))

GetEndIP = EndIP

GetCountry(IP)

If (CountryFlag = 1 Or CountryFlag = 2) Then

Country = GetFlagStr(EndIPOff + 4)

If CountryFlag = 1 Then

LocalStr = GetFlagStr(Stream.Position)

' 以下用来获取数据库版本信息

If IP >= IPToInt("255.255.255.0") And IP <= IPToInt("255.255.255.255") Then

LocalStr = GetFlagStr(EndIPOff + 21)

Country = GetFlagStr(EndIPOff + 12)

LocalStr = GetFlagStr(EndIPOff + 8)

Country = GetFlagStr(EndIPOff + 4)

LocalStr = GetFlagStr(Stream.Position)

Country = Trim(Country)

LocalStr = Trim(LocalStr)

(LocalStr, GetFlagStr(OffSet)

Dim Flag

Flag = 0

Do While (True)

Stream.Position = OffSet

Flag = AscB(Stream.Read(1))

If (Flag = 1 Or Flag = 2) Then

Buf = Stream.Read(3)

If (Flag = 2) Then

CountryFlag = 2

EndIPOff = OffSet - 4

End If

OffSet = AscB(MidB(Buf, 1, 1)) + (AscB(MidB(Buf, 2, 1)) * 256) + (AscB(MidB(Buf, 3, 1)) * 256 * 256)

(OffSet < 12) Then

GetFlagStr = ""

Else

Stream.Position = OffSet

GetFlagStr = GetStr()

GetStr()

Dim c

GetStr = (True)

c = AscB(Stream.Read(1))

c > 127 GetStr = GetStr & Chr(AscW(ChrB(AscB(Stream.Read(1))) & ChrB(c)))

Else

GetStr = GetStr & Chr(c)

QQWry(DotIP)

Dim IP, nRet

Dim RangB, RangE, RecNo

IP = IPToInt(DotIP)

Set Stream = CreateObject("ADodb.Stream")

Stream.Mode = 3

Stream.Type = 1

Stream.Open

Stream.LoadFromFile QQWryFile

Stream.Position = 0

Buf = Stream.Read(8)

FirstStartIP = AscB(MidB(Buf, 1, 1)) + (AscB(MidB(Buf, 2, 1)) * 256) + (AscB(MidB(Buf, 3, 1)) * 256 * 256) + (AscB(MidB(Buf, 4, 1)) * 256 * 256 * 256)

LastStartIP = AscB(MidB(Buf, 5, 1)) + (AscB(MidB(Buf, 6, 1)) * 256) + (AscB(MidB(Buf, 7, 1)) * 256 * 256) + (AscB(MidB(Buf, 8, 1)) * 256 * 256 * 256)

RecordCount = Int((LastStartIP - FirstStartIP) / 7)

' 在数据库中找不到任何IP地址

If (RecordCount <= 1) Then

Country = "未知"

QQWry = 2

RangB = 0

RangE = RecordCount

Do While (RangB < (RangE - 1))

RecNo = Int((RangB + RangE) / 2)

Call GetStartIP(RecNo)

If (IP = StartIP) Then

RangB = RecNo

(IP > StartIP) Then

RangB = RecNo

Else

RangE = RecNo

GetStartIP(RangB)

Call GetEndIP

If (StartIP <= IP) And (EndIP >= IP) Then

' 没有找到

nRet = 0

Else

' 正常

nRet = 3

GetCountry(IP)

QQWry = nRet

Class_Terminate()

Stream..Clear

%>

调用示例:

ASP code
<%

  IP = request("IP")

  If IP = "" then

  IP = Request.ServerVariables("REMOTE_ADDR")

  %>

  你的IP是:<%=IP%> <br> 来自 : <%=Disp_IPAddressData(IP,2)%><%=Disp_IPAddressData(IP ,3)%>

  <%else%>

  你查询的IP是:<%=IP%> <br> 来自 : <%=Disp_IPAddressData(IP,2)%><%=Disp_IPAddressData(IP ,3)%>

  <%End if%>

  <form name="form1" method="post" action="">

  <input name="IP" type="text" id="IP">

  <input type="submit" name="Submit" value="查询">

  </form> <%

  ' ============================================

  ' 返回IP信息 Disp_IPAddressData(IP,0)

  ' ============================================

  Function Look_Ip(IP)

  Dim Wry, IPType, QQWryVersion, IpCounter

  ' 设置类对象

  Set Wry = New TQQWry

  ' 开始搜索,并返回搜索结果

  ' 您可以根据 QQWry(IP) 返回值来判断该IP地址在数据库中是否存在,如果不存在可以执行其他的一些操作

  ' 比如您自建一个数据库作为追捕等,这里我就不详细说明了

  IPType = Wry.QQWry(IP)

  ' Country:国家地区字段

  ' LocalStr:省市及其他信息字段

  Look_Ip =Wry.Country & "" & Wry.LocalStr

  '''''Look_Ip = Wry.Country & ""

  End Function

  ' ============================================

  ' 返回IP信息 JS调用

  ' ============================================

  Function Disp_IPAddressData(IP, sType)

  Dim Wry, IPType

  Set Wry = New TQQWry

  IPType = Wry.QQWry(IP)

  Select Case sType

  Case 1 Disp_IPAddressData = IP

  Case 2 Disp_IPAddressData = Wry.Country

  Case 3 Disp_IPAddressData = Wry.LocalStr

  'Case Else Disp_IPAddressData = Wry.Country & "" & Wry.LocalStr

  Case Else Disp_IPAddressData = Wry.Country

  End Select

  End Function

  ' ============================================

  ' 返回QQWry信息

  ' ============================================

  Function WryInfo()

  Dim Wry, IPType, QQWry_tem(0), QQWry_tem1(1)

  ' 设置类对象

  Set Wry = New TQQWry

  IPType = Wry.QQWry("255.255.255.254")

  ' 读取数据库版本信息

  QQWry_tem(0) = Wry.Country & " " & Wry.LocalStr

  ' 读取数据库IP地址数目

  QQWry_tem1(1) = Wry.RecordCount + 1

  WryInfo = QQWry_tem(0)& " " & QQWry_tem1(1)

  End Function

%>

0 0