Skip to content

Commit

Permalink
update code
Browse files Browse the repository at this point in the history
  • Loading branch information
ilsanbao committed Jan 9, 2015
1 parent 15ae5f7 commit 5fab66a
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 2 deletions.
107 changes: 107 additions & 0 deletions ipip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#!/usr/bin/env python
# coding: utf-8
# author: frk

import struct
from socket import inet_aton
import os

_unpack_V = lambda b: struct.unpack("<L", b)
_unpack_N = lambda b: struct.unpack(">L", b)
_unpack_C = lambda b: struct.unpack("B", b)

class IP:
offset = 0
index = 0
binary = ""

@staticmethod
def load(file):
try:
path = os.path.abspath(file)
with open(path, "rb") as f:
IP.binary = f.read()
IP.offset, = _unpack_N(IP.binary[:4])
IP.index = IP.binary[4:IP.offset]
except Exception as ex:
print "cannot open file %s" % file
print ex.message
exit(0)

@staticmethod
def find(ip):
index = IP.index
offset = IP.offset
binary = IP.binary
nip = inet_aton(ip)
ipdot = ip.split('.')
if int(ipdot[0]) < 0 or int(ipdot[0]) > 255 or len(ipdot) != 4:
return "N/A"

tmp_offset = int(ipdot[0]) * 4
start, = _unpack_V(index[tmp_offset:tmp_offset + 4])

index_offset = index_length = 0
max_comp_len = offset - 1028
start = start * 8 + 1024
while start < max_comp_len:
if index[start:start + 4] >= nip:
index_offset, = _unpack_V(index[start + 4:start + 7] + chr(0).encode('utf-8'))
index_length, = _unpack_C(index[start + 7])
break
start += 8

if index_offset == 0:
return "N/A"

res_offset = offset + index_offset - 1024
return binary[res_offset:res_offset + index_length].decode('utf-8')


class IPX:
binary = ""
index = 0
offset = 0

@staticmethod
def load(file):
try:
path = os.path.abspath(file)
with open(path, "rb") as f:
IPX.binary = f.read()
IPX.offset, = _unpack_N(IPX.binary[:4])
IPX.index = IPX.binary[4:IPX.offset]
except Exception as ex:
print "cannot open file %s" % file
print ex.message
exit(0)

@staticmethod
def find(ip):
index = IPX.index
offset = IPX.offset
binary = IPX.binary
nip = inet_aton(ip)
ipdot = ip.split('.')
if int(ipdot[0]) < 0 or int(ipdot[0]) > 255 or len(ipdot) != 4:
return "N/A"

tmp_offset = (int(ipdot[0]) * 256 + int(ipdot[1])) * 4
start, = _unpack_V(index[tmp_offset:tmp_offset + 4])

index_offset = index_length = -1
max_comp_len = offset - 262144 - 4
start = start * 9 + 262144

while start < max_comp_len:
if index[start:start + 4] >= nip:
index_offset, = _unpack_V(index[start + 4:start + 7] + chr(0).encode('utf-8'))
index_length, = _unpack_C(index[start + 8:start + 9])
break
start += 9

if index_offset == 0:
return "N/A"

res_offset = offset + index_offset - 262144
return binary[res_offset:res_offset + index_length].decode('utf-8')
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os

import IP
import IPX
from ipip import IP
from ipip import IPX

IP.load(os.path.abspath("mydata4vipday2.dat"))
print IP.find("118.28.8.8")
Expand Down

0 comments on commit 5fab66a

Please sign in to comment.