From 70ec103e701a17793a396979fd0e170cfd831fc6 Mon Sep 17 00:00:00 2001 From: thomasciesla Date: Tue, 30 Dec 2025 16:07:25 +0100 Subject: [PATCH] jtl-wafi-agent.py aktualisiert --- jtl-wafi-agent.py | 72 +++++++++++++++++++++++++++++++---------------- 1 file changed, 48 insertions(+), 24 deletions(-) diff --git a/jtl-wafi-agent.py b/jtl-wafi-agent.py index 19f636a..14667db 100644 --- a/jtl-wafi-agent.py +++ b/jtl-wafi-agent.py @@ -2811,40 +2811,64 @@ def download_country_ranges_async(): thread.start() -def ensure_country_cache_symlink(ratelimit_path: str) -> bool: +def copy_country_cache_to_shop(ratelimit_path: str) -> int: """ - Erstellt einen Symlink vom Shop-spezifischen Pfad zum globalen Country-Cache. + Kopiert Country-Cache vom globalen Verzeichnis in den Shop. + (Symlinks funktionieren nicht wegen PHP open_basedir Restriction) Args: ratelimit_path: Pfad zum jtl-wafi_ratelimit Verzeichnis des Shops Returns: - True wenn erfolgreich, False sonst + Anzahl der kopierten Dateien """ - symlink_path = os.path.join(ratelimit_path, 'country_cache') + shop_cache_dir = os.path.join(ratelimit_path, 'country_cache') try: - # Falls bereits existiert - if os.path.islink(symlink_path): - # Prüfe ob Symlink korrekt ist - if os.readlink(symlink_path) == GLOBAL_COUNTRY_CACHE_DIR: - return True - # Falsches Ziel - entfernen und neu erstellen - os.remove(symlink_path) - elif os.path.isdir(symlink_path): - # Altes Verzeichnis existiert - entfernen (war vorher Kopie) - shutil.rmtree(symlink_path) - elif os.path.exists(symlink_path): - os.remove(symlink_path) + # Shop-Cache Verzeichnis erstellen + os.makedirs(shop_cache_dir, exist_ok=True) - # Symlink erstellen - os.symlink(GLOBAL_COUNTRY_CACHE_DIR, symlink_path) - logger.debug(f"Country-Cache Symlink erstellt: {symlink_path} -> {GLOBAL_COUNTRY_CACHE_DIR}") - return True + # Falls Symlink existiert (von alter Version), entfernen + if os.path.islink(shop_cache_dir): + os.remove(shop_cache_dir) + os.makedirs(shop_cache_dir, exist_ok=True) + + # Prüfe ob globaler Cache existiert + if not os.path.isdir(GLOBAL_COUNTRY_CACHE_DIR): + logger.debug(f"Globaler Country-Cache nicht vorhanden: {GLOBAL_COUNTRY_CACHE_DIR}") + return 0 + + copied = 0 + for filename in os.listdir(GLOBAL_COUNTRY_CACHE_DIR): + if filename.endswith('.ranges'): + src = os.path.join(GLOBAL_COUNTRY_CACHE_DIR, filename) + dst = os.path.join(shop_cache_dir, filename) + + # Nur kopieren wenn Quelle neuer ist oder Ziel nicht existiert + if not os.path.isfile(dst) or os.path.getmtime(src) > os.path.getmtime(dst): + shutil.copy2(src, dst) + copied += 1 + + if copied > 0: + logger.debug(f"Country-Cache kopiert: {copied} Dateien nach {shop_cache_dir}") + + return copied except Exception as e: - logger.error(f"Fehler beim Erstellen des Country-Cache Symlinks: {e}") - return False + logger.error(f"Fehler beim Kopieren des Country-Cache: {e}") + return 0 + + +def copy_country_cache_to_shop_async(ratelimit_path: str): + """Kopiert Country-Cache im Hintergrund.""" + def _copy(): + # Erst sicherstellen, dass globaler Cache existiert + if not os.path.isdir(GLOBAL_COUNTRY_CACHE_DIR) or not os.listdir(GLOBAL_COUNTRY_CACHE_DIR): + download_country_ranges(force=False) + copy_country_cache_to_shop(ratelimit_path) + + thread = threading.Thread(target=_copy, daemon=True) + thread.start() # ============================================================================= @@ -3009,8 +3033,8 @@ def activate_blocking(shop: str, silent: bool = True, monitor_only=monitor_only ) - # Step 5: Country-Cache Symlink erstellen (zeigt auf globalen Cache) - ensure_country_cache_symlink(ratelimit_path) + # Step 5: Country-Cache vom globalen Cache kopieren (wegen PHP open_basedir) + copy_country_cache_to_shop_async(ratelimit_path) if not silent: logger.info(f"Blocking aktiviert für {shop}")