Refactor geo enrichment into jail_service and mark Task 14 done

This commit is contained in:
2026-04-17 16:36:22 +02:00
parent 487f252a4d
commit 900d111a5d
4 changed files with 56 additions and 7 deletions

View File

@@ -19,7 +19,7 @@ from typing import TYPE_CHECKING, TypedDict, cast
import structlog
from app.exceptions import JailNotFoundError, JailOperationError
from app.models.ban import ActiveBan, ActiveBanListResponse, JailBannedIpsResponse
from app.models.ban import ActiveBan, JailBannedIpsResponse
from app.models.config import BantimeEscalation
from app.models.geo import GeoDetail
from app.models.jail import (
@@ -29,6 +29,7 @@ from app.models.jail import (
JailStatus,
JailSummary,
)
from app.services import geo_service
from app.utils.config_file_utils import start_daemon, wait_for_fail2ban
from app.utils.fail2ban_client import (
Fail2BanClient,
@@ -179,6 +180,22 @@ def _ensure_list(value: object | None) -> list[str]:
return [str(value)]
async def _resolve_geo_info(
ip: str,
*,
http_session: aiohttp.ClientSession | None = None,
geo_enricher: GeoEnricher | None = None,
) -> GeoInfo | None:
"""Resolve geolocation using either a custom enricher or HTTP session."""
if geo_enricher is not None:
return await geo_enricher(ip)
if http_session is not None:
return await geo_service.lookup(ip, http_session)
return None
def _is_not_found_error(exc: Exception) -> bool:
"""Return ``True`` if *exc* indicates a jail does not exist.
@@ -1163,6 +1180,7 @@ async def lookup_ip(
socket_path: str,
ip: str,
geo_enricher: GeoEnricher | None = None,
http_session: aiohttp.ClientSession | None = None,
) -> IpLookupResult:
"""Return ban status and history for a single IP address.
@@ -1223,9 +1241,13 @@ async def lookup_ip(
pass
geo: GeoDetail | None = None
if geo_enricher is not None:
if geo_enricher is not None or http_session is not None:
with contextlib.suppress(Exception): # noqa: BLE001
raw_geo = await geo_enricher(ip)
raw_geo = await _resolve_geo_info(
ip,
http_session=http_session,
geo_enricher=geo_enricher,
)
if raw_geo is not None:
geo = GeoDetail(
country_code=raw_geo.country_code,