Files
BanGUI/backend/app/services/geo_service.py
Lukas 654dbdb000 T-04: Encapsulate geo_service module-level mutable state in GeoCache class
Create GeoCache class with all mutable state as instance attributes:
- _cache, _neg_cache, _dirty, _geoip_reader, _geoip_initialized, _cache_lock
- All public methods: lookup(), lookup_batch(), lookup_cached_only(), flush_dirty(), load_from_db(), clear(), etc.

Initialization & Dependency Injection:
- Instantiate GeoCache in startup.py and store on app.state.geo_cache
- Add get_geo_cache() dependency function in dependencies.py
- Inject into routes and tasks via FastAPI's dependency system

Backward Compatibility:
- Maintain module-level functions in geo_service.py as deprecated wrappers
- All old callers continue to work through _default_geo_cache instance
- Remove test-escape-hatch functions (clear_cache, clear_neg_cache moved to methods)

Background Tasks:
- Update geo_cache_flush.py and geo_re_resolve.py to receive GeoCache instance
- Tasks now operate on injected instance rather than module globals

Tests:
- Refactor test_geo_service.py with geo_cache fixture providing fresh instances
- Update patch paths to target GeoCache methods correctly
- Fix internal state assertions to access instance attributes

Documentation:
- Update Architekture.md to document GeoCache as managed stateful service
- Describe cache lifecycle (load on startup, flush periodically, re-resolve stale)
- Note process-local limitations for multi-worker deployments

Fixes violation of Single Responsibility Principle: module no longer owns both
lookup logic and cache lifecycle management. Cache is now a first-class
injectable service with transparent lifecycle.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-04-23 16:18:09 +02:00

235 lines
7.3 KiB
Python

"""Geo service — backward compatibility wrappers.
DEPRECATED: This module is kept for backward compatibility only. New code should
use :class:`GeoCache` directly from ``app.services.geo_cache``. The underlying
implementation has been refactored to eliminate module-level mutable state.
The :class:`GeoCache` instance should be injected into services and routers
via dependency injection, and stored on ``app.state.geo_cache``.
See :class:`app.services.geo_cache.GeoCache` for the implementation.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
from app.services.geo_cache import GeoCache
from app.models.geo import GeoInfo
if TYPE_CHECKING:
import aiohttp
import aiosqlite
__all__ = [
"GeoCache",
"clear_cache",
"clear_neg_cache",
"is_cached",
"lookup",
"lookup_batch",
"lookup_cached_only",
"cache_stats",
"count_unresolved",
"get_unresolved_ips",
"load_cache_from_db",
"flush_dirty",
"re_resolve_all",
"init_geoip",
]
#: Deprecated: Module-level cache instance for backward compatibility.
#: This exists only to provide backward-compatible module-level functions.
#: New code should inject GeoCache instances directly.
_default_geo_cache: GeoCache = GeoCache()
async def clear_cache() -> None:
"""(DEPRECATED) Flush both the positive and negative lookup caches.
Use :meth:`GeoCache.clear` instead. This function delegates to the
default module-level instance for backward compatibility only.
"""
await _default_geo_cache.clear()
async def clear_neg_cache() -> None:
"""(DEPRECATED) Flush only the negative (failed-lookups) cache.
Use :meth:`GeoCache.clear_neg_cache` instead. This function delegates to
the default module-level instance for backward compatibility only.
"""
await _default_geo_cache.clear_neg_cache()
def is_cached(ip: str) -> bool:
"""(DEPRECATED) Return ``True`` if *ip* has a positive cache entry.
Use :meth:`GeoCache.is_cached` instead. This function delegates to the
default module-level instance for backward compatibility only.
Args:
ip: IPv4 or IPv6 address string.
Returns:
``True`` when *ip* is in the cache with a known country code.
"""
return _default_geo_cache.is_cached(ip)
async def cache_stats(db: aiosqlite.Connection) -> dict[str, int]:
"""(DEPRECATED) Return diagnostic counters for the geo cache subsystem.
Use :meth:`GeoCache.cache_stats` instead. This function delegates to the
default module-level instance for backward compatibility only.
Args:
db: Open BanGUI application database connection.
Returns:
Dict with keys ``cache_size``, ``unresolved``, ``neg_cache_size``,
and ``dirty_size``.
"""
return await _default_geo_cache.cache_stats(db)
async def count_unresolved(db: aiosqlite.Connection) -> int:
"""(DEPRECATED) Return the number of unresolved entries in the geo cache.
Use :meth:`GeoCache.count_unresolved` instead.
"""
return await _default_geo_cache.count_unresolved(db)
async def get_unresolved_ips(db: aiosqlite.Connection) -> list[str]:
"""(DEPRECATED) Return IPs with NULL country_code in the persistent cache.
Use :meth:`GeoCache.get_unresolved_ips` instead.
Args:
db: Open BanGUI application database connection.
Returns:
List of IP addresses that are candidates for re-resolution.
"""
return await _default_geo_cache.get_unresolved_ips(db)
def init_geoip(mmdb_path: str | None) -> None:
"""(DEPRECATED) Initialise the MaxMind GeoLite2-Country database reader.
Use :meth:`GeoCache.init_geoip` instead. This function delegates to the
default module-level instance for backward compatibility only.
Args:
mmdb_path: Absolute path to a ``GeoLite2-Country.mmdb`` file.
"""
_default_geo_cache.init_geoip(mmdb_path)
async def load_cache_from_db(db: aiosqlite.Connection) -> None:
"""(DEPRECATED) Pre-populate the in-memory cache from ``geo_cache`` table.
Use :meth:`GeoCache.load_cache_from_db` instead. This function delegates
to the default module-level instance for backward compatibility only.
Args:
db: Open :class:`aiosqlite.Connection` to the BanGUI application
database (not the fail2ban database).
"""
await _default_geo_cache.load_cache_from_db(db)
async def lookup(
ip: str,
http_session: aiohttp.ClientSession,
db: aiosqlite.Connection | None = None,
) -> GeoInfo | None:
"""(DEPRECATED) Resolve an IP address to country, ASN, organisation metadata.
Use :meth:`GeoCache.lookup` instead. This function delegates to the
default module-level instance for backward compatibility only.
Args:
ip: IPv4 or IPv6 address string.
http_session: Shared :class:`aiohttp.ClientSession`.
db: Optional BanGUI application database for persistence.
Returns:
A :class:`GeoInfo` instance, or ``None`` on lookup failure.
"""
return await _default_geo_cache.lookup(ip, http_session, db=db)
def lookup_cached_only(
ips: list[str],
) -> tuple[dict[str, GeoInfo], list[str]]:
"""(DEPRECATED) Return cached geo data without making external API calls.
Use :meth:`GeoCache.lookup_cached_only` instead. This function delegates
to the default module-level instance for backward compatibility only.
Args:
ips: IP address strings to look up.
Returns:
A ``(geo_map, uncached)`` tuple.
"""
return _default_geo_cache.lookup_cached_only(ips)
async def lookup_batch(
ips: list[str],
http_session: aiohttp.ClientSession,
db: aiosqlite.Connection | None = None,
) -> dict[str, GeoInfo]:
"""(DEPRECATED) Resolve multiple IPs in bulk using ip-api.com batch endpoint.
Use :meth:`GeoCache.lookup_batch` instead. This function delegates to the
default module-level instance for backward compatibility only.
Args:
ips: List of IP address strings to resolve.
http_session: Shared :class:`aiohttp.ClientSession`.
db: Optional BanGUI application database for persistent cache writes.
Returns:
Dict mapping ``ip → GeoInfo`` for every input IP.
"""
return await _default_geo_cache.lookup_batch(ips, http_session, db=db)
async def flush_dirty(db: aiosqlite.Connection) -> int:
"""(DEPRECATED) Persist all new in-memory geo entries to the database.
Use :meth:`GeoCache.flush_dirty` instead. This function delegates to the
default module-level instance for backward compatibility only.
Args:
db: Open :class:`aiosqlite.Connection` to the BanGUI application
database.
Returns:
The number of rows successfully upserted.
"""
return await _default_geo_cache.flush_dirty(db)
async def re_resolve_all(
db: aiosqlite.Connection,
http_session: aiohttp.ClientSession,
) -> dict[str, int]:
"""(DEPRECATED) Retry geo resolution for all unresolved cache entries.
Use :meth:`GeoCache.re_resolve_all` instead. This function delegates to
the default module-level instance for backward compatibility only.
Args:
db: BanGUI application database connection.
http_session: Shared aiohttp client session.
Returns:
A dict with ``resolved`` and ``total`` counts.
"""
return await _default_geo_cache.re_resolve_all(db, http_session)