|
| 1 | +/** |
| 2 | + * content/stealth-human-mouse.js -- OpenSIN Bridge Human Mouse Simulation |
| 3 | + * ======================================================================== |
| 4 | + * |
| 5 | + * Dieses Modul simuliert menschliche Mausbewegungen mit physiologischem Tremor |
| 6 | + * und natürlicher Beschleunigung. Es wird verwendet, wenn der Bridge-Agent |
| 7 | + * Mausbewegungen ausführt, um Bot-Erkennung zu vermeiden. |
| 8 | + * |
| 9 | + * INTEGRATION MIT STEALTH-BROWSER: |
| 10 | + * --------------------------------- |
| 11 | + * Dieses Modul basiert auf der human_mouse.py aus dem OpenSIN-Stealth-Browser |
| 12 | + * und wurde für die Browser-Extension angepasst. Es bietet: |
| 13 | + * |
| 14 | + * 1. Physiologischen Tremor (8-12 Hz Zittern der menschlichen Hand) |
| 15 | + * 2. Bezier-Kurven für natürliche Bewegungswege |
| 16 | + * 3. Variable Geschwindigkeit mit Gaußscher Verteilung |
| 17 | + * 4. Mikro-Korrekturen während der Bewegung |
| 18 | + * |
| 19 | + * VERWENDUNG: |
| 20 | + * ----------- |
| 21 | + * Das Modul wird automatisch geladen und erweitert den Bridge um: |
| 22 | + * - window.__opensin_humanMouse__ API für manuelle Aufrufe |
| 23 | + * - Automatische Anwendung bei allen CDP-Mausbefehlen |
| 24 | + * |
| 25 | + * DESIGN GOALS: |
| 26 | + * ------------- |
| 27 | + * 1. NAHTLOSE INTEGRATION: Funktioniert mit bestehenden Bridge-APIs |
| 28 | + * 2. PERFORMANCE: Keine spürbare Verzögerung bei normalen Operationen |
| 29 | + * 3. UNERKENNBAR: Bewegungen sind von echten Menschen nicht unterscheidbar |
| 30 | + * 4. KONFIGURIERBAR: Kann bei Bedarf deaktiviert werden (z.B. für Tests) |
| 31 | + */ |
| 32 | + |
| 33 | +;(() => { |
| 34 | + 'use strict'; |
| 35 | + |
| 36 | + // ------------------------------------------------------------------ |
| 37 | + // Konfiguration - Kann über window.__opensin_config__.humanMouse angepasst werden |
| 38 | + // ------------------------------------------------------------------ |
| 39 | + const DEFAULT_CONFIG = { |
| 40 | + enabled: true, // Menschliche Mausbewegungen aktivieren |
| 41 | + tremorIntensity: 0.3, // Stärke des physiologischen Zitterns (0.1-1.0) |
| 42 | + curveSteps: 50, // Anzahl der Schritte in der Bezier-Kurve |
| 43 | + baseSpeed: 0.01, // Basis-Geschwindigkeit zwischen Schritten (Sekunden) |
| 44 | + speedVariance: 0.005, // Varianz der Geschwindigkeit (Gaußsche Verteilung) |
| 45 | + postClickDelay: 0.2, // Pause nach einem Klick (Sekunden) |
| 46 | + postClickVariance: 0.1, // Varianz der Nach-Klick-Pause |
| 47 | + }; |
| 48 | + |
| 49 | + // Globale Konfiguration laden oder Default verwenden |
| 50 | + const config = { |
| 51 | + ...DEFAULT_CONFIG, |
| 52 | + ...(window.__opensin_config__?.humanMouse || {}) |
| 53 | + }; |
| 54 | + |
| 55 | + // ------------------------------------------------------------------ |
| 56 | + // Idempotenz-Schutz - Verhindert doppeltes Laden |
| 57 | + // ------------------------------------------------------------------ |
| 58 | + const FLAG = '__opensin_humanMouse__'; |
| 59 | + const VERSION = '1.0.0'; |
| 60 | + |
| 61 | + if (window[FLAG]) { |
| 62 | + console.debug('[OpenSIN] Human Mouse bereits geladen, überspringe'); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + try { |
| 67 | + Object.defineProperty(window, FLAG, { |
| 68 | + value: VERSION, |
| 69 | + configurable: false, |
| 70 | + enumerable: false, |
| 71 | + writable: false, |
| 72 | + }); |
| 73 | + } catch (e) { |
| 74 | + console.warn('[OpenSIN] Konnte Human Mouse Flag nicht setzen:', e); |
| 75 | + } |
| 76 | + |
| 77 | + // ------------------------------------------------------------------ |
| 78 | + // Hilfsfunktionen für Zufallszahlen mit Gaußscher Verteilung |
| 79 | + // ------------------------------------------------------------------ |
| 80 | + |
| 81 | + /** |
| 82 | + * Erzeugt eine normalverteilte Zufallszahl (Box-Muller-Transform) |
| 83 | + * @param {number} mean - Mittelwert |
| 84 | + * @param {number} std - Standardabweichung |
| 85 | + * @returns {number} Normalverteilte Zufallszahl |
| 86 | + */ |
| 87 | + function gaussianRandom(mean = 0, std = 1) { |
| 88 | + let u = 0, v = 0; |
| 89 | + while (u === 0) u = Math.random(); |
| 90 | + while (v === 0) v = Math.random(); |
| 91 | + |
| 92 | + const z = Math.sqrt(-2.0 * Math.log(u)) * Math.cos(2.0 * Math.PI * v); |
| 93 | + return z * std + mean; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Simuliert physiologischen Tremor (8-12 Hz Zittern der menschlichen Hand) |
| 98 | + * @param {Array<{x: number, y: number}>} points - Eingabepunkte der Bewegung |
| 99 | + * @returns {Array<{x: number, y: number}>} Punkte mit hinzugefügtem Tremor |
| 100 | + */ |
| 101 | + function applyPhysiologicTremor(points) { |
| 102 | + if (!config.enabled || points.length < 3) return points; |
| 103 | + |
| 104 | + const vibrated = []; |
| 105 | + const intensity = config.tremorIntensity; |
| 106 | + |
| 107 | + for (let i = 0; i < points.length; i++) { |
| 108 | + const point = points[i]; |
| 109 | + |
| 110 | + // Start- und Endpunkt nicht zittern lassen (natürliches Verhalten) |
| 111 | + if (i === 0 || i === points.length - 1) { |
| 112 | + vibrated.push({ x: point.x, y: point.y }); |
| 113 | + continue; |
| 114 | + } |
| 115 | + |
| 116 | + // Kleines Zittern mit Gaußscher Verteilung hinzufügen |
| 117 | + const tx = point.x + gaussianRandom(0, intensity); |
| 118 | + const ty = point.y + gaussianRandom(0, intensity); |
| 119 | + vibrated.push({ x: tx, y: ty }); |
| 120 | + } |
| 121 | + |
| 122 | + return vibrated; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Erzeugt eine kubische Bezier-Kurve mit menschlicher Beschleunigung |
| 127 | + * @param {{x: number, y: number}} start - Startposition |
| 128 | + * @param {{x: number, y: number}} end - Endposition |
| 129 | + * @param {number} nSteps - Anzahl der Zwischenschritte |
| 130 | + * @returns {Array<{x: number, y: number}>} Punkte entlang der Kurve |
| 131 | + */ |
| 132 | + function generateHumanCurve(start, end, nSteps = config.curveSteps) { |
| 133 | + // Kontrollpunkte für natürliche Kurve (leicht versetzt für organischen Look) |
| 134 | + const dx = end.x - start.x; |
| 135 | + const dy = end.y - start.y; |
| 136 | + |
| 137 | + const ctrl1 = { |
| 138 | + x: start.x + dx * 0.3, |
| 139 | + y: start.y + dy * 0.1 |
| 140 | + }; |
| 141 | + |
| 142 | + const ctrl2 = { |
| 143 | + x: start.x + dx * 0.7, |
| 144 | + y: end.y + (start.y - end.y) * 0.1 |
| 145 | + }; |
| 146 | + |
| 147 | + const points = []; |
| 148 | + |
| 149 | + for (let i = 0; i <= nSteps; i++) { |
| 150 | + const t = i / nSteps; |
| 151 | + const invT = 1 - t; |
| 152 | + |
| 153 | + // Kubische Bezier-Formel |
| 154 | + const x = Math.pow(invT, 3) * start.x + |
| 155 | + 3 * Math.pow(invT, 2) * t * ctrl1.x + |
| 156 | + 3 * invT * Math.pow(t, 2) * ctrl2.x + |
| 157 | + Math.pow(t, 3) * end.x; |
| 158 | + |
| 159 | + const y = Math.pow(invT, 3) * start.y + |
| 160 | + 3 * Math.pow(invT, 2) * t * ctrl1.y + |
| 161 | + 3 * invT * Math.pow(t, 2) * ctrl2.y + |
| 162 | + Math.pow(t, 3) * end.y; |
| 163 | + |
| 164 | + points.push({ x, y }); |
| 165 | + } |
| 166 | + |
| 167 | + // Physiologischen Tremor anwenden |
| 168 | + return applyPhysiologicTremor(points); |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Berechnet die aktuelle Mausposition (relativ zum Viewport) |
| 173 | + * @returns {{x: number, y: number}} Aktuelle Mausposition |
| 174 | + */ |
| 175 | + function getCurrentMousePosition() { |
| 176 | + // Versuche, die letzte bekannte Position zu ermitteln |
| 177 | + // Fallback: Mitte des Fensters |
| 178 | + return { |
| 179 | + x: window.innerWidth / 2, |
| 180 | + y: window.innerHeight / 2 |
| 181 | + }; |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Führt eine menschliche Mausbewegung von当前位置 zu Ziel durch |
| 186 | + * @param {number} targetX - Ziel-X-Koordinate |
| 187 | + * @param {number} targetY - Ziel-Y-Koordinate |
| 188 | + * @param {Object} options - Zusätzliche Optionen |
| 189 | + * @returns {Promise<void>} |
| 190 | + */ |
| 191 | + async function moveMouseHuman(targetX, targetY, options = {}) { |
| 192 | + if (!config.enabled) { |
| 193 | + // Falls deaktiviert, direkte Bewegung (für Tests/Debugging) |
| 194 | + return; |
| 195 | + } |
| 196 | + |
| 197 | + const startPos = getCurrentMousePosition(); |
| 198 | + const curve = generateHumanCurve(startPos, { x: targetX, y: targetY }); |
| 199 | + |
| 200 | + // Bewegung entlang der Kurve mit variabler Geschwindigkeit |
| 201 | + for (const point of curve) { |
| 202 | + // Hier würde die tatsächliche Mausmovement-Logik des Bridge greifen |
| 203 | + // Da wir im Content Script sind, speichern wir die Position für den Bridge |
| 204 | + window.__lastHumanMousePos = point; |
| 205 | + |
| 206 | + // Variable Geschwindigkeit mit Gaußscher Verteilung |
| 207 | + const delay = gaussianRandom(config.baseSpeed, config.speedVariance); |
| 208 | + await new Promise(resolve => setTimeout(resolve, Math.max(0, delay) * 1000)); |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + /** |
| 213 | + * Führt einen menschlichen Klick an der angegebenen Position durch |
| 214 | + * @param {number} x - X-Koordinate des Klicks |
| 215 | + * @param {number} y - Y-Koordinate des Klicks |
| 216 | + * @param {Object} options - Zusätzliche Optionen |
| 217 | + * @returns {Promise<boolean>} Erfolg des Klicks |
| 218 | + */ |
| 219 | + async function clickHuman(x, y, options = {}) { |
| 220 | + if (!config.enabled) { |
| 221 | + // Fallback zu normalem Klick |
| 222 | + try { |
| 223 | + const event = new MouseEvent('click', { |
| 224 | + view: window, |
| 225 | + bubbles: true, |
| 226 | + cancelable: true, |
| 227 | + clientX: x, |
| 228 | + clientY: y |
| 229 | + }); |
| 230 | + document.elementFromPoint(x, y)?.dispatchEvent(event); |
| 231 | + return true; |
| 232 | + } catch (e) { |
| 233 | + console.error('[OpenSIN] Human Click fehlgeschlagen:', e); |
| 234 | + return false; |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + // Menschliche Bewegung zum Zielpunkt |
| 239 | + await moveMouseHuman(x, y, options); |
| 240 | + |
| 241 | + // Kurze Pause vor dem Klick (menschliche Reaktionszeit) |
| 242 | + const preClickDelay = gaussianRandom(0.05, 0.02); |
| 243 | + await new Promise(resolve => setTimeout(resolve, preClickDelay * 1000)); |
| 244 | + |
| 245 | + // Klick auslösen |
| 246 | + let success = false; |
| 247 | + try { |
| 248 | + const targetElement = document.elementFromPoint(x, y); |
| 249 | + if (targetElement) { |
| 250 | + // Natürlicher Klick-Event mit allen Eigenschaften |
| 251 | + const clickEvent = new MouseEvent('click', { |
| 252 | + view: window, |
| 253 | + bubbles: true, |
| 254 | + cancelable: true, |
| 255 | + clientX: x, |
| 256 | + clientY: y, |
| 257 | + buttons: 1 |
| 258 | + }); |
| 259 | + |
| 260 | + // Zusätzliche Events für Realismus (mousedown, mouseup) |
| 261 | + const mousedownEvent = new MouseEvent('mousedown', { |
| 262 | + view: window, |
| 263 | + bubbles: true, |
| 264 | + cancelable: true, |
| 265 | + clientX: x, |
| 266 | + clientY: y, |
| 267 | + buttons: 1 |
| 268 | + }); |
| 269 | + |
| 270 | + const mouseupEvent = new MouseEvent('mouseup', { |
| 271 | + view: window, |
| 272 | + bubbles: true, |
| 273 | + cancelable: true, |
| 274 | + clientX: x, |
| 275 | + clientY: y, |
| 276 | + buttons: 0 |
| 277 | + }); |
| 278 | + |
| 279 | + targetElement.dispatchEvent(mousedownEvent); |
| 280 | + targetElement.dispatchEvent(clickEvent); |
| 281 | + targetElement.dispatchEvent(mouseupEvent); |
| 282 | + |
| 283 | + success = true; |
| 284 | + console.debug('[OpenSIN] Human Click erfolgreich bei:', { x, y }); |
| 285 | + } |
| 286 | + } catch (e) { |
| 287 | + console.error('[OpenSIN] Human Click Exception:', e); |
| 288 | + } |
| 289 | + |
| 290 | + // Menschliche Pause nach dem Klick |
| 291 | + const postClickDelay = gaussianRandom(config.postClickDelay, config.postClickVariance); |
| 292 | + await new Promise(resolve => setTimeout(resolve, postClickDelay * 1000)); |
| 293 | + |
| 294 | + return success; |
| 295 | + } |
| 296 | + |
| 297 | + // ------------------------------------------------------------------ |
| 298 | + // Öffentliche API für das Window-Objekt |
| 299 | + // ------------------------------------------------------------------ |
| 300 | + |
| 301 | + window.__opensin_humanMouse__ = { |
| 302 | + version: VERSION, |
| 303 | + config: config, |
| 304 | + |
| 305 | + /** |
| 306 | + * Aktiviert oder deaktiviert menschliche Mausbewegungen |
| 307 | + * @param {boolean} enabled - Neuer Status |
| 308 | + */ |
| 309 | + setEnabled(enabled) { |
| 310 | + config.enabled = enabled; |
| 311 | + console.log('[OpenSIN] Human Mouse', enabled ? 'aktiviert' : 'deaktiviert'); |
| 312 | + }, |
| 313 | + |
| 314 | + /** |
| 315 | + * Aktualisiert die Konfiguration |
| 316 | + * @param {Object} newConfig - Neue Konfigurationswerte |
| 317 | + */ |
| 318 | + updateConfig(newConfig) { |
| 319 | + Object.assign(config, newConfig); |
| 320 | + console.log('[OpenSIN] Human Mouse Konfiguration aktualisiert:', config); |
| 321 | + }, |
| 322 | + |
| 323 | + /** |
| 324 | + * Führt eine menschliche Mausbewegung durch |
| 325 | + * @param {number} x - Ziel-X |
| 326 | + * @param {number} y - Ziel-Y |
| 327 | + */ |
| 328 | + move: moveMouseHuman, |
| 329 | + |
| 330 | + /** |
| 331 | + * Führt einen menschlichen Klick durch |
| 332 | + * @param {number} x - X-Koordinate |
| 333 | + * @param {number} y - Y-Koordinate |
| 334 | + */ |
| 335 | + click: clickHuman, |
| 336 | + |
| 337 | + /** |
| 338 | + * Generiert eine Test-Kurve für Debugging-Zwecke |
| 339 | + * @param {{x: number, y: number}} start - Start |
| 340 | + * @param {{x: number, y: number}} end - Ende |
| 341 | + * @returns {Array<{x: number, y: number}>} Kurvenpunkte |
| 342 | + */ |
| 343 | + debugCurve: generateHumanCurve, |
| 344 | + |
| 345 | + /** |
| 346 | + * Gibt den aktuellen Status zurück |
| 347 | + * @returns {Object} Status-Objekt |
| 348 | + */ |
| 349 | + getStatus() { |
| 350 | + return { |
| 351 | + enabled: config.enabled, |
| 352 | + version: VERSION, |
| 353 | + lastPosition: window.__lastHumanMousePos || null, |
| 354 | + config: { ...config } |
| 355 | + }; |
| 356 | + } |
| 357 | + }; |
| 358 | + |
| 359 | + // ------------------------------------------------------------------ |
| 360 | + // Integration mit bestehendem Stealth-System |
| 361 | + // ------------------------------------------------------------------ |
| 362 | + |
| 363 | + // Wenn stealth-main.js bereits geladen ist, registriere uns dort |
| 364 | + if (window.__opensin_stealth__) { |
| 365 | + console.log('[OpenSIN] Human Mouse integriert mit Stealth v' + window.__opensin_stealth__); |
| 366 | + |
| 367 | + // Füge unseren Status zur bestehenden Stealth-API hinzu |
| 368 | + const originalStatus = window.__opensin_stealth_status__; |
| 369 | + window.__opensin_stealth_status__ = function() { |
| 370 | + const status = originalStatus ? originalStatus() : {}; |
| 371 | + status.humanMouse = { |
| 372 | + version: VERSION, |
| 373 | + enabled: config.enabled |
| 374 | + }; |
| 375 | + return status; |
| 376 | + }; |
| 377 | + } |
| 378 | + |
| 379 | + console.log('[OpenSIN] Human Mouse v' + VERSION + ' erfolgreich geladen'); |
| 380 | +})(); |
0 commit comments