Files
AccountForger-neuerUpload/browser/stealth_config.py
Claude Project Manager 04585e95b6 Initial commit
2025-08-01 23:50:28 +02:00

216 Zeilen
7.9 KiB
Python

"""
Stealth-Konfiguration für Playwright - Anti-Bot-Erkennung
"""
import json
import logging
import os
import random
import platform
from pathlib import Path
from typing import Dict, Any, List
# Konfiguriere Logger
logger = logging.getLogger("stealth_config")
class StealthConfig:
"""
Konfiguriert Anti-Bot-Erkennungs-Einstellungen für Playwright.
Generiert und verwaltet verschiedene Fingerprint-Einstellungen.
"""
# Standardwerte für User-Agents
CHROME_DESKTOP_AGENTS = [
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36"
]
MOBILE_AGENTS = [
"Mozilla/5.0 (iPhone; CPU iPhone OS 16_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Mobile/15E148 Safari/604.1",
"Mozilla/5.0 (Linux; Android 13; SM-G991B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Mobile Safari/537.36",
"Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/135.0.0.0 Mobile/15E148 Safari/604.1"
]
# Plattformen
PLATFORMS = {
"windows": "Win32",
"macos": "MacIntel",
"linux": "Linux x86_64",
"android": "Linux armv8l",
"ios": "iPhone"
}
# Browser-Sprachen
LANGUAGES = [
"de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7",
"de-DE,de;q=0.9,en;q=0.8",
"de;q=0.9,en-US;q=0.8,en;q=0.7",
"en-US,en;q=0.9,de;q=0.8"
]
# Zeitzone für Deutschland
TIMEZONE_ID = "Europe/Berlin"
def __init__(self, config_dir: str = None):
"""
Initialisiert die Stealth-Konfiguration.
Args:
config_dir: Verzeichnis für Konfigurationsdateien
"""
self.config_dir = config_dir or os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "config")
os.makedirs(self.config_dir, exist_ok=True)
self.config_path = os.path.join(self.config_dir, "stealth_config.json")
# Lade benutzerdefinierte User-Agents, falls vorhanden
self.user_agents = self._load_user_agents()
# Lade gespeicherte Konfiguration oder erstelle eine neue
self.config = self._load_or_create_config()
def _load_user_agents(self) -> Dict[str, List[str]]:
"""Lädt benutzerdefinierte User-Agents aus der Konfigurationsdatei."""
user_agents_path = os.path.join(self.config_dir, "user_agents.json")
if os.path.exists(user_agents_path):
try:
with open(user_agents_path, 'r', encoding='utf-8') as f:
agents = json.load(f)
if isinstance(agents, dict) and "desktop" in agents and "mobile" in agents:
return agents
except Exception as e:
logger.warning(f"Fehler beim Laden von user_agents.json: {e}")
# Standardwerte zurückgeben
return {
"desktop": self.CHROME_DESKTOP_AGENTS,
"mobile": self.MOBILE_AGENTS
}
def _load_or_create_config(self) -> Dict[str, Any]:
"""Lädt die Konfiguration oder erstellt eine neue, falls keine existiert."""
if os.path.exists(self.config_path):
try:
with open(self.config_path, 'r', encoding='utf-8') as f:
config = json.load(f)
logger.info("Stealth-Konfiguration geladen")
return config
except Exception as e:
logger.warning(f"Konnte Stealth-Konfiguration nicht laden: {e}")
# Erstelle eine neue Konfiguration
config = self.generate_config()
self.save_config(config)
return config
def generate_config(self, device_type: str = "desktop") -> Dict[str, Any]:
"""
Generiert eine neue Stealth-Konfiguration.
Args:
device_type: "desktop" oder "mobile"
Returns:
Dict[str, Any]: Die generierte Konfiguration
"""
# Wähle Plattform und entsprechenden User-Agent
if device_type == "mobile":
platform_name = random.choice(["android", "ios"])
user_agent = random.choice(self.user_agents["mobile"])
else:
# Wähle eine Plattform, die zum System passt
system = platform.system().lower()
if system == "darwin":
platform_name = "macos"
elif system == "windows":
platform_name = "windows"
else:
platform_name = "linux"
user_agent = random.choice(self.user_agents["desktop"])
platform_value = self.PLATFORMS.get(platform_name, "Win32")
# Wähle weitere Konfigurationen
config = {
"user_agent": user_agent,
"platform": platform_value,
"vendor": "Google Inc." if "Chrome" in user_agent else "Apple Computer, Inc.",
"accept_language": random.choice(self.LANGUAGES),
"timezone_id": self.TIMEZONE_ID,
"device_scale_factor": random.choice([1.0, 1.25, 1.5, 2.0]) if random.random() < 0.3 else 1.0,
"color_depth": random.choice([24, 30, 48]),
"hardware_concurrency": random.choice([2, 4, 8, 12, 16]),
"device_memory": random.choice([2, 4, 8, 16]),
"webdriver": False,
"fingerprint_noise": True,
"device_type": device_type
}
return config
def save_config(self, config: Dict[str, Any]) -> None:
"""
Speichert die Konfiguration in einer Datei.
Args:
config: Die zu speichernde Konfiguration
"""
try:
with open(self.config_path, 'w', encoding='utf-8') as f:
json.dump(config, f, indent=2)
logger.info(f"Stealth-Konfiguration gespeichert in: {self.config_path}")
except Exception as e:
logger.error(f"Fehler beim Speichern der Stealth-Konfiguration: {e}")
def get_config(self) -> Dict[str, Any]:
"""Gibt die aktuelle Konfiguration zurück."""
return self.config
def rotate_config(self, device_type: str = None) -> Dict[str, Any]:
"""
Generiert eine neue Konfiguration und speichert sie.
Args:
device_type: "desktop" oder "mobile", oder None für bestehenden Typ
Returns:
Dict[str, Any]: Die neue Konfiguration
"""
if device_type is None:
device_type = self.config.get("device_type", "desktop")
self.config = self.generate_config(device_type)
self.save_config(self.config)
return self.config
def get_user_agent(self) -> str:
"""Gibt den aktuellen User-Agent aus der Konfiguration zurück."""
return self.config.get("user_agent", self.CHROME_DESKTOP_AGENTS[0])
# Beispielnutzung, wenn direkt ausgeführt
if __name__ == "__main__":
# Konfiguriere Logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
# Beispiel für Konfigurationserstellung
stealth = StealthConfig()
print("Aktuelle Konfiguration:")
print(json.dumps(stealth.get_config(), indent=2))
print("\nNeue Desktop-Konfiguration:")
desktop_config = stealth.rotate_config("desktop")
print(json.dumps(desktop_config, indent=2))
print("\nNeue Mobile-Konfiguration:")
mobile_config = stealth.rotate_config("mobile")
print(json.dumps(mobile_config, indent=2))