Skip to content

Commit

Permalink
improve: improve ip v6 searching without network prefix by converting…
Browse files Browse the repository at this point in the history
… it into binary
  • Loading branch information
Ahmed Hekal committed Apr 16, 2024
1 parent 4d2c4f8 commit 9b0c746
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
14 changes: 10 additions & 4 deletions lib/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ def create_table(self):
(id INTEGER PRIMARY KEY,
first_ip TEXT,
last_ip TEXT,
first_ip_int INTEGER,
last_ip_int INTEGER,
first_ip_int TEXT,
last_ip_int TEXT,
ip_version INTEGER,
subnet INTEGER,
network_prefix TEXT,
netname TEXT,
country TEXT,
Expand All @@ -34,6 +35,7 @@ def insert_data(self, data):
for entry in data:
first_ip_int = entry.get('first_ip_int')
last_ip_int = entry.get('last_ip_int')
subnet = entry.get('subnet')

query = '''INSERT INTO ip_data
(first_ip, last_ip, ip_version, network_prefix,
Expand All @@ -44,10 +46,14 @@ def insert_data(self, data):

if first_ip_int is not None:
query += ', first_ip_int'
values.append(first_ip_int)
values.append(str(first_ip_int))
if last_ip_int is not None:
query += ', last_ip_int'
values.append(last_ip_int)
values.append(str(last_ip_int))

if subnet is not None:
query += ', subnet'
values.append(subnet)

query += ') VALUES (?' + ', ?' * (len(values) - 1) + ')'

Expand Down
11 changes: 7 additions & 4 deletions lib/ripe_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@ def __init__(self):

def get_ip_v6_first_and_last_ip(sub_net):
ip,subnet = sub_net.split("/",1)
first_ip = ipaddress.IPv6Address(ip).exploded
last_ipv6 = (ipaddress.IPv6Address(first_ip) + (2 ** (128 - int(subnet)) - 1)).exploded
return first_ip,last_ipv6,ip
first_ip = ipaddress.IPv6Address(ip)
last_ipv6 = (ipaddress.IPv6Address(first_ip) + (2 ** (128 - int(subnet)) - 1))
return first_ip.exploded,last_ipv6.exploded,ip,int(first_ip),int(last_ipv6),subnet


def format_block(block):
new_block = {}
if block.get("ipVersion",4) == 6:
first_ip,last_ip,prefex = RIPE_PARSER.get_ip_v6_first_and_last_ip(block["inetnum"])
first_ip,last_ip,prefex,first_ip_int,last_ip_int,subnet = RIPE_PARSER.get_ip_v6_first_and_last_ip(block["inetnum"])
new_block["first_ip"] = first_ip
new_block["last_ip"] = last_ip
new_block["network_prefix"] = prefex
new_block["first_ip_int"] = first_ip_int
new_block["last_ip_int"] = last_ip_int
new_block["subnet"] = subnet
else:
inetnum_splited = block["inetnum"].split(" - ")
new_block["first_ip"] = inetnum_splited[0]
Expand Down
3 changes: 1 addition & 2 deletions sqllite_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ def on_single_block_process(block):
total_blocks_processed += len(blocks)
blocks = []
print(f"Total blocks processed: {total_blocks_processed}")

RIPE_PARSER.parse_file(default_ripeV4_data,on_single_block_process)
RIPE_PARSER.parse_file(default_ripeV6_data,on_single_block_process)
RIPE_PARSER.parse_file(default_ripeV4_data,on_single_block_process)


print("Done")

0 comments on commit 9b0c746

Please sign in to comment.