cloudflare / py-mmdb-encoder

Create mmdb files to encode prefix lists.
BSD 3-Clause "New" or "Revised" License
30 stars 11 forks source link

Adding ipv4 and ipv6 breaks lookup ability with maxmind reader #4

Open aaronpeterson opened 5 years ago

aaronpeterson commented 5 years ago

I can only seem to insert ipv4 into my files otherwise they become unreadable. If I write all ipv4 and even a single ipv6 the lookup using a maxmind reader fails (no errors, just empty results).

I believe the issue I am having is in how I am adding the ipv4 addresses.

import os
import mmdbencoder

OUTPUT_MMDB_FILENAME = 'output.mmdb'

if os.path.isfile(OUTPUT_MMDB_FILENAME):
    print("WARNING: Deleting Old MMDB File")
    os.remove(OUTPUT_MMDB_FILENAME)

mmdb_file = open(OUTPUT_MMDB_FILENAME, 'wb');

enc = mmdbencoder.Encoder(
    # Using 0th index
    6,
    32,
    'suspect',
    ['en'],  # Languages
    {'en': 'Description...'},  # Description
    compat=True)

# Remove the ipv6 and the ipv4 are readable, add them and only ipv6 are readable
ips = [
    '216.244.66.248',
    '3.15.9.167',
    '2a02:587:a12:f000:e9bf:9b70:4fd:c66',
    '2002:b9ea:da6b:0:0:0:b9ea:da6b',
    '192.0.91.147'
]

# just the same for all of these...
data = enc.insert_data({'region': 'United States', 'type': 'attack_source'})

for ip in ips:
    try:
        enc.insert_network(ip, data)
    except Exception as e:
        print("ERROR: Got error while adding row to the mmdb file. Details are below")
        print("-----: Error: {}".format(str(e)))

enc.write(mmdb_file)

print("Done")

There was some confusion in the Official Perl writer as well but I can't seem to figure out any ipv6 representation of an ipv4 that works such as:

https://github.com/maxmind/MaxMind-DB-Writer-perl/issues/44#issuecomment-137509409