2019-05-09 23:35:04 -04:00
|
|
|
#!/usr/bin/env python3
|
2020-01-15 21:57:56 -05:00
|
|
|
# usage: generate-ip-mappings.py
|
|
|
|
# 2 files ipv4.csv and ipv6.csv will be generated
|
2019-05-09 23:35:04 -04:00
|
|
|
# in sqlite3 terminal:
|
|
|
|
#
|
|
|
|
# .mode csv
|
2020-01-15 21:57:56 -05:00
|
|
|
# .import `full path to ipv4.csv` ip_mapping
|
|
|
|
# .import `full path to ipv6.csv` ipv6_mapping
|
2019-05-09 23:35:04 -04:00
|
|
|
#
|
|
|
|
|
|
|
|
# For query by ip:
|
|
|
|
# SELECT * FROM ip_mapping WHERE `ip_start` <= ip-in-decimal AND `ip_end` >= ip-in-decimal ORDER BY `ip_start` DESC LIMIT 1;
|
2020-01-15 21:57:56 -05:00
|
|
|
# SELECT * FROM ipv6_mapping WHERE `ip_start` <= upperIPv6("ipv6_addr") AND `ip_end` >= upperIPv6("ipv6_addr") ORDER BY `ip_start` DESC LIMIT 1;
|
2019-05-09 23:35:04 -04:00
|
|
|
import socket
|
|
|
|
import struct
|
|
|
|
import csv
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
# import zipfile
|
|
|
|
# import urllib.request
|
|
|
|
|
2020-01-15 21:57:56 -05:00
|
|
|
def ip2int(addr):
|
|
|
|
return struct.unpack("!I", socket.inet_aton(addr))[0]
|
2019-05-09 23:35:04 -04:00
|
|
|
|
2020-01-15 21:57:56 -05:00
|
|
|
# Keep only the upper 64bit, as we only need that for geolocation
|
|
|
|
def ipv62int64(addr):
|
|
|
|
hi, lo = struct.unpack('!QQ', socket.inet_pton(socket.AF_INET6, addr))
|
|
|
|
return hi
|
2019-05-09 23:35:04 -04:00
|
|
|
|
2020-01-15 21:57:56 -05:00
|
|
|
CSV_WEB_LINK = 'https://download.db-ip.com/free/dbip-city-lite-2020-01.csv.gz'
|
|
|
|
CSV_FILE = 'dbip-city-lite-2020-01.csv'
|
2019-05-09 23:35:04 -04:00
|
|
|
|
|
|
|
if not os.path.exists(CSV_FILE):
|
|
|
|
print("File = {} does not exist. Download it from = {} ".format(CSV_FILE, CSV_WEB_LINK))
|
|
|
|
sys.exit(1)
|
|
|
|
|
2020-01-15 21:57:56 -05:00
|
|
|
# Format: 1.0.0.0,1.0.0.255,OC,AU,Queensland,"South Brisbane",-27.4748,153.017
|
|
|
|
with open(CSV_FILE, 'r') as csvfile, open('ipv4.csv', 'w') as ipv4, open('ipv6.csv', 'w') as ipv6:
|
2019-05-09 23:35:04 -04:00
|
|
|
iplist = csv.reader(csvfile, delimiter=',', quotechar='"')
|
|
|
|
for row in iplist:
|
2020-01-15 21:57:56 -05:00
|
|
|
# Skip reserved range
|
|
|
|
if row[3] == "ZZ":
|
|
|
|
continue
|
|
|
|
# Skip empty latitude and longitude
|
|
|
|
if row[6] is "" or row[7] is "":
|
2019-05-09 23:35:04 -04:00
|
|
|
continue
|
|
|
|
|
2020-01-15 21:57:56 -05:00
|
|
|
if row[0].find(':') == -1:
|
|
|
|
ipv4_line = True
|
|
|
|
else:
|
|
|
|
ipv4_line = False
|
2019-05-09 23:35:04 -04:00
|
|
|
|
2020-01-15 21:57:56 -05:00
|
|
|
if ipv4_line:
|
|
|
|
ip_start = ip2int(row[0])
|
|
|
|
ip_end = ip2int(row[1])
|
|
|
|
else:
|
|
|
|
ip_start = ipv62int64(row[0])
|
|
|
|
ip_end = ipv62int64(row[1])
|
2019-05-09 23:35:04 -04:00
|
|
|
|
2020-01-15 21:57:56 -05:00
|
|
|
# Some IPv6 entries are duplicated after removing the lower 64bit
|
|
|
|
if ip_start == ip_end:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Sqlite doesn't support unsigned int 64
|
|
|
|
_int64_max = pow(2, 63) - 1
|
|
|
|
if ip_start > _int64_max or ip_end > _int64_max:
|
|
|
|
continue
|
|
|
|
latitude = float(row[6])
|
|
|
|
longitude = float(row[7])
|
|
|
|
country = row[3]
|
|
|
|
if ipv4_line:
|
|
|
|
print('%d,%d,%f,%f,%s' % (ip_start, ip_end, latitude, longitude, country), file = ipv4)
|
|
|
|
else:
|
|
|
|
print('%d,%d,%f,%f,%s' % (ip_start, ip_end, latitude, longitude, country), file = ipv6)
|