123 Zeilen
4.2 KiB
Python
123 Zeilen
4.2 KiB
Python
"""
|
|
Generate Account Fingerprint Use Case - Generiert und verwaltet Fingerprints für Accounts
|
|
"""
|
|
|
|
import logging
|
|
import uuid
|
|
import json
|
|
from typing import Dict, Any, Optional
|
|
from datetime import datetime
|
|
|
|
from domain.entities.browser_fingerprint import BrowserFingerprint
|
|
from infrastructure.services.advanced_fingerprint_service import AdvancedFingerprintService
|
|
|
|
logger = logging.getLogger("generate_account_fingerprint_use_case")
|
|
|
|
|
|
class GenerateAccountFingerprintUseCase:
|
|
"""
|
|
Use Case für die Generierung und Zuweisung von Browser-Fingerprints zu Accounts.
|
|
Stellt sicher, dass jeder Account einen eindeutigen Fingerprint hat.
|
|
"""
|
|
|
|
def __init__(self, db_manager, fingerprint_service=None):
|
|
self.db_manager = db_manager
|
|
self.fingerprint_service = fingerprint_service or AdvancedFingerprintService()
|
|
|
|
def execute(self, account_id: int) -> Optional[str]:
|
|
"""
|
|
Generiert einen Fingerprint für einen Account oder gibt den existierenden zurück.
|
|
|
|
Args:
|
|
account_id: ID des Accounts
|
|
|
|
Returns:
|
|
Fingerprint ID oder None bei Fehler
|
|
"""
|
|
try:
|
|
# Prüfe ob Account bereits einen Fingerprint hat
|
|
conn = self.db_manager.get_connection()
|
|
cursor = conn.cursor()
|
|
|
|
cursor.execute(
|
|
"SELECT fingerprint_id FROM accounts WHERE id = ?",
|
|
(account_id,)
|
|
)
|
|
result = cursor.fetchone()
|
|
|
|
if result and result[0]:
|
|
logger.info(f"Account {account_id} hat bereits Fingerprint: {result[0]}")
|
|
return result[0]
|
|
|
|
# Generiere neuen Fingerprint über AdvancedFingerprintService
|
|
fingerprint = self.fingerprint_service.create_account_fingerprint(
|
|
account_id=str(account_id),
|
|
profile_type="desktop"
|
|
)
|
|
|
|
# Aktualisiere Account mit Fingerprint ID
|
|
cursor.execute(
|
|
"UPDATE accounts SET fingerprint_id = ? WHERE id = ?",
|
|
(fingerprint.fingerprint_id, account_id)
|
|
)
|
|
|
|
conn.commit()
|
|
|
|
logger.info(f"Neuer Fingerprint {fingerprint.fingerprint_id} für Account {account_id} generiert und verknüpft")
|
|
return fingerprint.fingerprint_id
|
|
|
|
except Exception as e:
|
|
logger.error(f"Fehler beim Generieren des Fingerprints für Account {account_id}: {e}")
|
|
return None
|
|
finally:
|
|
if conn:
|
|
conn.close()
|
|
|
|
def assign_fingerprints_to_all_accounts(self) -> Dict[str, Any]:
|
|
"""
|
|
Weist allen Accounts ohne Fingerprint einen neuen zu.
|
|
|
|
Returns:
|
|
Statistik über die Zuweisung
|
|
"""
|
|
stats = {
|
|
"total_accounts": 0,
|
|
"accounts_without_fingerprint": 0,
|
|
"fingerprints_assigned": 0,
|
|
"errors": 0
|
|
}
|
|
|
|
try:
|
|
# Hole alle Accounts ohne Fingerprint
|
|
conn = self.db_manager.get_connection()
|
|
cursor = conn.cursor()
|
|
|
|
cursor.execute("SELECT COUNT(*) FROM accounts")
|
|
stats["total_accounts"] = cursor.fetchone()[0]
|
|
|
|
cursor.execute(
|
|
"SELECT id, username, platform FROM accounts WHERE fingerprint_id IS NULL"
|
|
)
|
|
accounts = cursor.fetchall()
|
|
stats["accounts_without_fingerprint"] = len(accounts)
|
|
|
|
for account_id, username, platform in accounts:
|
|
logger.info(f"Generiere Fingerprint für Account {username} ({platform})")
|
|
|
|
fingerprint_id = self.execute(account_id)
|
|
if fingerprint_id:
|
|
stats["fingerprints_assigned"] += 1
|
|
else:
|
|
stats["errors"] += 1
|
|
|
|
conn.close()
|
|
|
|
logger.info(f"Fingerprint-Zuweisung abgeschlossen: {stats}")
|
|
return stats
|
|
|
|
except Exception as e:
|
|
logger.error(f"Fehler bei der Fingerprint-Zuweisung: {e}")
|
|
stats["errors"] += 1
|
|
return stats
|
|
|