From 231cab309d07e8aba6bd0c2a6b77f2e9f9ca82ce Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Fri, 24 Nov 2023 15:35:54 +0000 Subject: [PATCH] Log FQDN --- geocode/model.py | 1 + lookup.py | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/geocode/model.py b/geocode/model.py index e2c30c9..f4861b2 100644 --- a/geocode/model.py +++ b/geocode/model.py @@ -93,4 +93,5 @@ class LookupLog(Base): lat = Column(Float) lon = Column(Float) remote_addr = Column(String) + fqdn = Column(String) result = Column(postgresql.JSONB) diff --git a/lookup.py b/lookup.py index 64c1201..c79b53b 100755 --- a/lookup.py +++ b/lookup.py @@ -2,6 +2,7 @@ """Reverse geocode: convert lat/lon to Wikidata item & Wikimedia Commons category.""" import random +import socket import typing import sqlalchemy.exc @@ -18,6 +19,7 @@ app.config.from_object("config.default") database.init_app(app) Tags = typing.Mapping[str, str] +logging_enabled = True def get_random_lat_lon() -> tuple[float, float]: @@ -193,11 +195,17 @@ def index() -> str | Response: result = lat_lon_to_wikidata(lat, lon)["result"] result.pop("element", None) result.pop("geojson", None) - log = model.LookupLog( - lat=lat, lon=lon, remote_addr=request.remote_addr, result=result - ) - database.session.add(log) - database.session.commit() + if logging_enabled: + remote_addr = request.headers.get("X-Forwarded-For", request.remote_addr) + log = model.LookupLog( + lat=lat, + lon=lon, + remote_addr=remote_addr, + fqdn=socket.getfqdn(remote_addr), + result=result, + ) + database.session.add(log) + database.session.commit() return jsonify(result)