From 021e151946fabe2d854ad91211b433739a433c1b Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Fri, 11 Jun 2021 17:30:27 -0400 Subject: [PATCH] Switch python-geoacumen to python-geoacumen-city (#1911) * Closes #1872 * IP Addresses in the Admin Panel will now show the city of the IP address as well as the country --- .../admin/templates/modals/users/addresses.html | 17 +++++++++++------ CTFd/utils/countries/geoip.py | 14 +++++++++++--- CTFd/utils/initialization/__init__.py | 3 ++- requirements.in | 2 +- requirements.txt | 4 ++-- 5 files changed, 27 insertions(+), 13 deletions(-) diff --git a/CTFd/themes/admin/templates/modals/users/addresses.html b/CTFd/themes/admin/templates/modals/users/addresses.html index 75af0a26..c0e60023 100644 --- a/CTFd/themes/admin/templates/modals/users/addresses.html +++ b/CTFd/themes/admin/templates/modals/users/addresses.html @@ -5,6 +5,7 @@ IP Address Last Seen + City Country @@ -15,20 +16,24 @@ - {% set country = lookup_ip_address(addr.ip) %} - {% if country %} + {% set city = lookup_ip_address_city(addr.ip) %} + {% if city %}{{ city }}{% endif %} + + {% set country = lookup_ip_address(addr.ip) %} + + {% if country %}   {{ lookup_country_code(country) }} + {% endif %} - {% else %} - - - {% endif %} {% endfor %} + + IP Geolocation by DB-IP + diff --git a/CTFd/utils/countries/geoip.py b/CTFd/utils/countries/geoip.py index 4935ff31..79d462f1 100644 --- a/CTFd/utils/countries/geoip.py +++ b/CTFd/utils/countries/geoip.py @@ -1,9 +1,9 @@ -import geoacumen +import geoacumen_city import maxminddb from flask import current_app IP_ADDR_LOOKUP = maxminddb.open_database( - current_app.config.get("GEOIP_DATABASE_PATH", geoacumen.db_path) + current_app.config.get("GEOIP_DATABASE_PATH", geoacumen_city.db_path) ) @@ -11,5 +11,13 @@ def lookup_ip_address(addr): try: response = IP_ADDR_LOOKUP.get(addr) return response["country"]["iso_code"] - except (KeyError, ValueError): + except (KeyError, ValueError, TypeError): + return None + + +def lookup_ip_address_city(addr): + try: + response = IP_ADDR_LOOKUP.get(addr) + return response["city"]["names"]["en"] + except (KeyError, ValueError, TypeError): return None diff --git a/CTFd/utils/initialization/__init__.py b/CTFd/utils/initialization/__init__.py index 20fbde00..3cc0351a 100644 --- a/CTFd/utils/initialization/__init__.py +++ b/CTFd/utils/initialization/__init__.py @@ -67,7 +67,7 @@ def init_template_globals(app): scores_visible, ) from CTFd.utils.countries import get_countries, lookup_country_code - from CTFd.utils.countries.geoip import lookup_ip_address + from CTFd.utils.countries.geoip import lookup_ip_address, lookup_ip_address_city app.jinja_env.globals.update(config=config) app.jinja_env.globals.update(get_pages=get_pages) @@ -89,6 +89,7 @@ def init_template_globals(app): app.jinja_env.globals.update(get_countries=get_countries) app.jinja_env.globals.update(lookup_country_code=lookup_country_code) app.jinja_env.globals.update(lookup_ip_address=lookup_ip_address) + app.jinja_env.globals.update(lookup_ip_address_city=lookup_ip_address_city) app.jinja_env.globals.update(accounts_visible=accounts_visible) app.jinja_env.globals.update(challenges_visible=challenges_visible) app.jinja_env.globals.update(registration_visible=registration_visible) diff --git a/requirements.in b/requirements.in index 45cb47a1..65b233d8 100644 --- a/requirements.in +++ b/requirements.in @@ -25,7 +25,7 @@ boto3==1.13.9 marshmallow==2.20.2 pydantic==1.6.2 WTForms==2.3.1 -python-geoacumen==0.0.1 +python-geoacumen-city maxminddb==1.5.4 tenacity==6.2.0 pybluemonday==0.0.6 diff --git a/requirements.txt b/requirements.txt index 18ba3bda..0bbe89c1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -37,7 +37,7 @@ mako==1.1.3 # via alembic markupsafe==1.1.1 # via jinja2, mako, wtforms marshmallow-sqlalchemy==0.17.0 # via -r requirements.in marshmallow==2.20.2 # via -r requirements.in, flask-marshmallow, marshmallow-sqlalchemy -maxminddb==1.5.4 # via -r requirements.in, python-geoacumen +maxminddb==1.5.4 # via -r requirements.in, python-geoacumen-city passlib==1.7.2 # via -r requirements.in pybluemonday==0.0.6 # via -r requirements.in pycparser==2.20 # via cffi @@ -47,7 +47,7 @@ pyrsistent==0.17.3 # via jsonschema python-dateutil==2.8.1 # via alembic, botocore python-dotenv==0.13.0 # via -r requirements.in python-editor==1.0.4 # via alembic -python-geoacumen==0.0.1 # via -r requirements.in +python-geoacumen-city==0.0.1 # via -r requirements.in pytz==2020.4 # via flask-restx redis==3.5.2 # via -r requirements.in requests==2.23.0 # via -r requirements.in