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
This commit is contained in:
Kevin Chung
2021-06-11 17:30:27 -04:00
committed by GitHub
parent ef6a5f8d64
commit 021e151946
5 changed files with 27 additions and 13 deletions

View File

@@ -5,6 +5,7 @@
<tr>
<td class="text-center"><b>IP Address</b></td>
<td class="text-center"><b>Last Seen</b></td>
<td class="text-center"><b>City</b></td>
<td class="text-center"><b>Country</b></td>
</tr>
</thead>
@@ -15,20 +16,24 @@
<td class="text-center solve-time">
<span data-time="{{ addr.date | isoformat }}"></span>
</td>
{% set country = lookup_ip_address(addr.ip) %}
{% if country %}
{% set city = lookup_ip_address_city(addr.ip) %}
<td class="text-center">
{% if city %}{{ city }}{% endif %}
</td>
{% set country = lookup_ip_address(addr.ip) %}
<td class="text-center">
{% if country %}
<i class="flag-{{ country.lower() }}"></i>
&nbsp;
{{ lookup_country_code(country) }}
{% endif %}
</td>
{% else %}
<td class="text-center">
</td>
{% endif %}
</tr>
{% endfor %}
</tbody>
</table>
<small class="float-right">
<a class="text-muted" href='https://db-ip.com'>IP Geolocation by DB-IP</a>
</small>
</div>
</div>

View File

@@ -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

View File

@@ -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)

View File

@@ -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

View File

@@ -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