diff --git a/.gitignore b/.gitignore index b4e5e80..2ead5c3 100644 --- a/.gitignore +++ b/.gitignore @@ -140,3 +140,4 @@ dmypy.json # Cython debug symbols cython_debug/ +poetry.lock diff --git a/etc/server.conf.sample b/etc/server.conf.sample index fe69826..ac51ed2 100644 --- a/etc/server.conf.sample +++ b/etc/server.conf.sample @@ -1,5 +1,6 @@ [global] mmdb_file = db/GeoOpen-Country.mmdb,db/GeoOpen-Country-ASN.mmdb +#mmdb_file = db/ipinfo_country_asn.mmdb,db/ipinfo_country.mmdb country_file = db/country.json lookup_pubsub = no port = 8000 diff --git a/mmdb_server/mmdb_server.py b/mmdb_server/mmdb_server.py index 63c61cd..c7c2c42 100644 --- a/mmdb_server/mmdb_server.py +++ b/mmdb_server/mmdb_server.py @@ -15,7 +15,7 @@ import falcon import maxminddb -version = "0.5" +version = "0.6" config = configparser.ConfigParser() config.read('etc/server.conf') mmdb_file = config['global'].get('mmdb_file') @@ -30,6 +30,7 @@ if pubsub: import redis + rdb = redis.Redis(host='127.0.0.1') mmdbs = [] @@ -70,9 +71,30 @@ def countryLookup(country: str) -> dict: return {} +def _process_lookup(ip): + """Helper function to process IP lookup and format response.""" + ret = [] + for mmdb in mmdbs: + georesult = mmdb['reader'].get(ip) or {} # Ensure dictionary, prevent NoneType errors + m = mmdb.copy() + del m['reader'] + georesult['meta'] = m + georesult['ip'] = ip + + # Determine country code from old or new format + country_code = None + if isinstance(georesult.get('country'), dict): + country_code = georesult['country'].get('iso_code') + elif isinstance(georesult.get('country'), str): + country_code = georesult['country'] + + georesult['country_info'] = countryLookup(country_code) if country_code else {} + ret.append(georesult) + return ret + + class GeoLookup: def on_get(self, req, resp, value): - ret = [] ua = req.get_header('User-Agent') ips = req.access_route if not validIPAddress(value): @@ -80,39 +102,13 @@ def on_get(self, req, resp, value): resp.media = "IPv4 or IPv6 address is in an incorrect format. Dotted decimal for IPv4 or textual representation for IPv6 are required." return pubLookup(value=f'{value} via {ips} using {ua}') - for mmdb in mmdbs: - m = {} - georesult = mmdb['reader'].get(value) - m = mmdb.copy() - del m['reader'] - georesult['meta'] = m - georesult['ip'] = value - if georesult['country']['iso_code'] != 'None': - georesult['country_info'] = countryLookup(country=georesult['country']['iso_code']) - else: - georesult['country_info'] = {} - ret.append(georesult) - resp.media = ret - return + resp.media = _process_lookup(value) class MyGeoLookup: def on_get(self, req, resp): - ret = [] ips = req.access_route - for mmdb in mmdbs: - georesult = mmdb['reader'].get(ips[0]) - m = mmdb.copy() - del m['reader'] - georesult['meta'] = m - georesult['ip'] = ips[0] - if georesult['country']['iso_code'] != 'None': - georesult['country_info'] = countryLookup(country=georesult['country']['iso_code']) - else: - georesult['country_info'] = {} - ret.append(georesult) - resp.media = ret - return + resp.media = _process_lookup(ips[0]) app = falcon.App() @@ -120,6 +116,7 @@ def on_get(self, req, resp): app.add_route('/geolookup/{value}', GeoLookup()) app.add_route('/', MyGeoLookup()) + def main(): with make_server('', port, app) as httpd: print(f'Serving on port {port}...') @@ -127,5 +124,4 @@ def main(): if __name__ == '__main__': - main() - + main() \ No newline at end of file diff --git a/poetry.lock b/poetry.lock deleted file mode 100644 index 3cc80f5..0000000 --- a/poetry.lock +++ /dev/null @@ -1,57 +0,0 @@ -# This file is automatically @generated by Poetry 1.6.1 and should not be changed by hand. - -[[package]] -name = "falcon" -version = "3.1.1" -description = "The ultra-reliable, fast ASGI+WSGI framework for building data plane APIs at scale." -optional = false -python-versions = ">=3.5" -files = [ - {file = "falcon-3.1.1-cp310-cp310-macosx_10_15_x86_64.whl", hash = "sha256:10ff3080aebe84fb45955cb02375ce13b6a3556c73edad282325eb67aeb42a46"}, - {file = "falcon-3.1.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:ca798f3240283a89881209dfa8eb20e2eaf8d01c50b33be5f70865c0902577ec"}, - {file = "falcon-3.1.1-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:394e16249d9b61dcdbb6653311c4a208f9fc68b696d0123d29f781fbd338cfd4"}, - {file = "falcon-3.1.1-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:6245344fab1a7faeb9267c75b8f4fd6c4bda35e1a2fe8f547b832b547c7f2128"}, - {file = "falcon-3.1.1-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:8fc0ef213d6e66bb997d172ceaa04f6daa309cac47e2fcd4320234806c806467"}, - {file = "falcon-3.1.1-cp310-cp310-win_amd64.whl", hash = "sha256:016fe952a526045292fb591f4c724d5fdf4127e88d0369e2dc147925dc51835c"}, - {file = "falcon-3.1.1-cp311-cp311-macosx_10_9_universal2.whl", hash = "sha256:00e6c6b3ec846193cfd30be26b10dbb7cc31ee3442f80f1d5ffd14c410619156"}, - {file = "falcon-3.1.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a7e6e1e6af16d1055454eaed5ceaceabca97656b28a8a924b426fbf0e26ec0f0"}, - {file = "falcon-3.1.1-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:d53dabcf8212c38137e40a61795e312224dc7a437b03d7fb0a1b0dc3ed8d4b5b"}, - {file = "falcon-3.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:762854cc9f13082166c166c93fd6f2178ba1787170bacee9a4b37fab412f602e"}, - {file = "falcon-3.1.1-cp311-cp311-win_amd64.whl", hash = "sha256:686a0167af40565a2057f3902a9fb8f15a423ad17a80c9caee932b668478c9ad"}, - {file = "falcon-3.1.1-cp36-cp36m-macosx_10_14_x86_64.whl", hash = "sha256:b8302953d72405750450d4f8b7651dc6c5a5199dbb104b598036818f917b1d8c"}, - {file = "falcon-3.1.1-cp36-cp36m-win_amd64.whl", hash = "sha256:f187040b6632ed434c3f6bcedb98fb6559973123d1799e77718502d2b693701e"}, - {file = "falcon-3.1.1-cp37-cp37m-macosx_10_15_x86_64.whl", hash = "sha256:1b8dfce6c379ba14d962abf479137258c694017752bc5b585ab366e2e8106a3e"}, - {file = "falcon-3.1.1-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:1d9c3dc6c5a8a2f2c3f1fd433a6b4e4bcef22c52166b91e2d6d985fbcadcc62b"}, - {file = "falcon-3.1.1-cp37-cp37m-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:2abecd50121ed969fa34d5c035a340ee4b21afc84dcd354acd548ab2edcc67b2"}, - {file = "falcon-3.1.1-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:f6e3c42f3c20af33c040affe0a3e8cd358153304b48eb441adfd261c3bfd51d3"}, - {file = "falcon-3.1.1-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:b7aab2dd6683437d8739a0cc9d6ab6542f48e05445a0138b356f63983a7c98fe"}, - {file = "falcon-3.1.1-cp37-cp37m-win_amd64.whl", hash = "sha256:6fbc130a12e35ca76d782201af7a558ac57d4e5e66ba3a8017f5a3baaed64f8b"}, - {file = "falcon-3.1.1-cp38-cp38-macosx_10_15_x86_64.whl", hash = "sha256:550566250ac2bc0418075f2ad177b7e01adef1815459c2d962e579dff07162fb"}, - {file = "falcon-3.1.1-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:1cf50b9a2dcf9c8f6ae8de94e2e6ac082449380784fb9d1a1fc80fade052aead"}, - {file = "falcon-3.1.1-cp38-cp38-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:8a5fa02feaf67a2bd0407201dfec92edb0eee59803c3e1e717cfa5a2232ffc77"}, - {file = "falcon-3.1.1-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:ff2eaf9807ea357ced1cc60e1d2871f55aa6ea29162386efb95fb4e5a730e6de"}, - {file = "falcon-3.1.1-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:f049eeeeea08e0a5fbb87d1fe131f85c7a0310c3a0a4226146463709fbfe12eb"}, - {file = "falcon-3.1.1-cp38-cp38-win_amd64.whl", hash = "sha256:66d937b7b796b68640d63e006e475d9268f68dfb3f1468415259507db72ee065"}, - {file = "falcon-3.1.1-cp39-cp39-macosx_10_15_x86_64.whl", hash = "sha256:13121ab6a86597ec849e397272662f5cafcbe534e12c01e2913035fe4120dcd1"}, - {file = "falcon-3.1.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:5af63f2d7f509353552b2436501449065f30f27542d1e58c864656bd3a7a9ef1"}, - {file = "falcon-3.1.1-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:fd1eaf1a5d9d936f29f9aca3f268cf375621d1ffcbf27a6e14c187b489bf5f26"}, - {file = "falcon-3.1.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:bec014dc19a38d5a525ab948a8eccc885f28d2611bdf3f73842fadc44b185702"}, - {file = "falcon-3.1.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:271fa0c4b0634e4e238dc7c2fcd57be5f9dd0f200553e46677ff704f6a8090e6"}, - {file = "falcon-3.1.1-cp39-cp39-win_amd64.whl", hash = "sha256:7a7ecb8eafada89389c19eda44811e14786599c1d86c6cffa58c65150b24bc43"}, - {file = "falcon-3.1.1.tar.gz", hash = "sha256:5dd393dbf01cbaf99493893de4832121bd495dc49a46c571915b79c59aad7ef4"}, -] - -[[package]] -name = "maxminddb" -version = "2.4.0" -description = "Reader for the MaxMind DB format" -optional = false -python-versions = ">=3.7" -files = [ - {file = "maxminddb-2.4.0.tar.gz", hash = "sha256:81e54e53408bd502650e5969ccba16780af659ec1db1c44b2c997e4330a5ed96"}, -] - -[metadata] -lock-version = "2.0" -python-versions = "^3.10" -content-hash = "fad5c71f6eca9d52d2b78f9e5d3b58632a52a41500ef9ae21babc3cb3cbe69ca" diff --git a/pyproject.toml b/pyproject.toml index 09ceaee..1390cce 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "mmdb-server" -version = "0.5.0" +version = "0.6.0" description = "" authors = ["Alexandre Dulaunoy"] readme = "README.md"