Skip to content

Instantly share code, notes, and snippets.

@eliphaslevy
Last active October 27, 2021 21:01
Show Gist options
  • Save eliphaslevy/6083d20dd5487fb740761e148f2da1ea to your computer and use it in GitHub Desktop.
Save eliphaslevy/6083d20dd5487fb740761e148f2da1ea to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
#coding:utf8
# vim:et:ts=4:sw=4:sts=4:ai:si:foldmethod=indent
# shamelessly copied from perl 20_convert_geolite2
# https://github.com/sander1/GeoLite2xtables
# (no perl modules in my server)
import os, sys, ipaddress
if not len(sys.argv) in (3,4) or \
not os.path.isfile(sys.argv[1]) or \
not os.path.isfile(sys.argv[2]):
print(f"""Usage:
{sys.argv[0]} countryFile.txt GeoLite2-Country-Blocks-IPvX.csv [--quiet]
destination file is STDOUT, any other messages go to STDERR.
""")
sys.exit(1)
countryFile = sys.argv[1]
geoliteCsv = sys.argv[2]
quiet = len(sys.argv) == 4 and sys.argv[3] == "--quiet"
countryInfo = {}
countryInfo['6255146'] = {'code': 'AF', 'name': 'Africa'}
countryInfo['6255147'] = {'code': 'AS', 'name': 'Asia'}
countryInfo['6255148'] = {'code': 'EU', 'name': 'Europe'}
countryInfo['6255149'] = {'code': 'NA', 'name': 'North America'}
countryInfo['6255150'] = {'code': 'SA', 'name': 'South America'}
countryInfo['6255151'] = {'code': 'OC', 'name': 'Oceania'}
countryInfo['6255152'] = {'code': 'AN', 'name': 'Antarctica'}
with open(countryFile) as country:
for line in country:
if line[0] == "#": continue # skip header/comments
fields = line.split('\t')
countryCode, countryName, countryId = fields[0], fields[4], fields[16]
countryInfo[countryId] = {'code': countryCode, 'name': countryName}
with open(geoliteCsv) as geoliteCsvFile:
if not quiet:
print("Generating...", flush=True, file=sys.stderr)
lineCounter = 0
for line in geoliteCsvFile:
if not line[0].isdigit(): continue # skip header
if not quiet: lineCounter+=1
fields = line.strip().split(',')
network, geoname_id, registered_country_geoname_id = fields[:3]
is_anonymous_proxy, is_satellite_provider = fields[4:6]
ip = ipaddress.ip_network(network)
start_ip, end_ip = ip.network_address, ip.broadcast_address
start_int, end_int = int(start_ip), int(end_ip)
if is_anonymous_proxy == "1":
code, name = "A1", "Anonymous Proxy"
elif is_satellite_provider == "1":
code, name = "A2", "Satellite Provider"
elif geoname_id in countryInfo:
code = countryInfo[geoname_id]['code']
name = countryInfo[geoname_id]['name']
elif registered_country_geoname_id in countryInfo:
code = countryInfo[registered_country_geoname_id]['code']
name = countryInfo[registered_country_geoname_id]['name']
else:
print("\nUnknown Geoname ID, panicking. This is a bug.", file=sys.stderr)
print(f"ID: {geoname_id}", file=sys.stderr)
print(f"ID Registered: {registered_country_geoname_id}", file=sys.stderr)
sys.exit(1)
# Legacy GeoIP listing format:
# "1.0.0.0","1.0.0.255","16777216","16777471","AU","Australia"
print(f'"{start_ip}","{end_ip}","{start_int}","{end_int}","{code}","{name}"')
if not quiet and lineCounter % 3000 == 0:
print(f"\x1b[1K\r{lineCounter}...", end="", flush=True, file=sys.stderr)
if not quiet:
print(f"\x1b[1K\r{lineCounter}\nFinished", flush=True, file=sys.stderr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment