Download src/hooks/useBehaviorAnalysis.ts from felix-fx/fx-1: direct link, hf CLI and curl.
- Browser
- Download file 6.77 kB
-
https://huggingface.co/felix-fx/fx-1/resolve/main/src/hooks/useBehaviorAnalysis.ts
- Command line
-
hf download hf://felix-fx/fx-1/src/hooks/useBehaviorAnalysis.ts
-
curl -L -o useBehaviorAnalysis.ts https://huggingface.co/felix-fx/fx-1/resolve/main/src/hooks/useBehaviorAnalysis.ts
6.77 kB
| import { useRef, useCallback, useEffect } from 'react'; | |
| interface BehaviorMetrics { | |
| mouseMovements: number; | |
| mouseSpeed: number[]; | |
| clickPositions: { x: number; y: number; t: number }[]; | |
| keystrokeTiming: number[]; | |
| touchEvents: number; | |
| scrollEvents: number; | |
| formStartTime: number; | |
| formInteractions: number; | |
| cursorPath: { x: number; y: number }[]; | |
| } | |
| /** | |
| * Hook لتحليل سلوك المستخدم وتحديد ما إذا كان بشرياً أم بوت | |
| * يتتبع: حركة الماوس، سرعة الكتابة، أنماط اللمس، وقت ملء النموذج | |
| */ | |
| export function useBehaviorAnalysis() { | |
| const metricsRef = useRef<BehaviorMetrics>({ | |
| mouseMovements: 0, | |
| mouseSpeed: [], | |
| clickPositions: [], | |
| keystrokeTiming: [], | |
| touchEvents: 0, | |
| scrollEvents: 0, | |
| formStartTime: Date.now(), | |
| formInteractions: 0, | |
| cursorPath: [], | |
| }); | |
| const lastMousePos = useRef<{ x: number; y: number; t: number } | null>(null); | |
| const lastKeystrokeTime = useRef<number>(0); | |
| const handleMouseMove = useCallback((e: MouseEvent) => { | |
| const m = metricsRef.current; | |
| m.mouseMovements++; | |
| m.formInteractions++; | |
| const now = performance.now(); | |
| if (lastMousePos.current) { | |
| const dx = e.clientX - lastMousePos.current.x; | |
| const dy = e.clientY - lastMousePos.current.y; | |
| const dt = now - lastMousePos.current.t; | |
| if (dt > 0) { | |
| const speed = Math.sqrt(dx * dx + dy * dy) / dt; | |
| m.mouseSpeed.push(speed); | |
| // Keep only last 50 speeds | |
| if (m.mouseSpeed.length > 50) m.mouseSpeed.shift(); | |
| } | |
| } | |
| lastMousePos.current = { x: e.clientX, y: e.clientY, t: now }; | |
| // Track cursor path (sample every 5th movement) | |
| if (m.mouseMovements % 5 === 0) { | |
| m.cursorPath.push({ x: e.clientX, y: e.clientY }); | |
| if (m.cursorPath.length > 30) m.cursorPath.shift(); | |
| } | |
| }, []); | |
| const handleClick = useCallback((e: MouseEvent) => { | |
| metricsRef.current.clickPositions.push({ | |
| x: e.clientX, | |
| y: e.clientY, | |
| t: performance.now(), | |
| }); | |
| metricsRef.current.formInteractions++; | |
| }, []); | |
| const handleKeyDown = useCallback(() => { | |
| const now = performance.now(); | |
| if (lastKeystrokeTime.current > 0) { | |
| const delta = now - lastKeystrokeTime.current; | |
| metricsRef.current.keystrokeTiming.push(delta); | |
| if (metricsRef.current.keystrokeTiming.length > 50) { | |
| metricsRef.current.keystrokeTiming.shift(); | |
| } | |
| } | |
| lastKeystrokeTime.current = now; | |
| metricsRef.current.formInteractions++; | |
| }, []); | |
| const handleTouch = useCallback(() => { | |
| metricsRef.current.touchEvents++; | |
| metricsRef.current.formInteractions++; | |
| }, []); | |
| const handleScroll = useCallback(() => { | |
| metricsRef.current.scrollEvents++; | |
| }, []); | |
| useEffect(() => { | |
| document.addEventListener('mousemove', handleMouseMove, { passive: true }); | |
| document.addEventListener('click', handleClick, { passive: true }); | |
| document.addEventListener('keydown', handleKeyDown, { passive: true }); | |
| document.addEventListener('touchstart', handleTouch, { passive: true }); | |
| document.addEventListener('scroll', handleScroll, { passive: true }); | |
| return () => { | |
| document.removeEventListener('mousemove', handleMouseMove); | |
| document.removeEventListener('click', handleClick); | |
| document.removeEventListener('keydown', handleKeyDown); | |
| document.removeEventListener('touchstart', handleTouch); | |
| document.removeEventListener('scroll', handleScroll); | |
| }; | |
| }, [handleMouseMove, handleClick, handleKeyDown, handleTouch, handleScroll]); | |
| /** | |
| * يحسب \"درجة بشرية\" من 0-100 | |
| * أقل من 30 = احتمال عالي أنه بوت | |
| */ | |
| const getHumanScore = useCallback((): number => { | |
| const m = metricsRef.current; | |
| let score = 0; | |
| // 1. وقت ملء النموذج (3-300 ثانية طبيعي) | |
| const timeSec = (Date.now() - m.formStartTime) / 1000; | |
| if (timeSec < 2) score += 0; // سريع جداً = بوت | |
| else if (timeSec < 5) score += 5; | |
| else if (timeSec < 300) score += 15; | |
| else score += 10; // طويل جداً | |
| // 2. حركة الماوس (البشر يحركون الماوس) | |
| if (m.mouseMovements > 5) score += 10; | |
| if (m.mouseMovements > 20) score += 5; | |
| if (m.mouseMovements > 50) score += 5; | |
| // 3. تنوع سرعة الماوس (البشر لديهم سرعات متفاوتة) | |
| if (m.mouseSpeed.length > 3) { | |
| const avg = m.mouseSpeed.reduce((a, b) => a + b, 0) / m.mouseSpeed.length; | |
| const variance = m.mouseSpeed.reduce((sum, s) => sum + (s - avg) ** 2, 0) / m.mouseSpeed.length; | |
| const stdDev = Math.sqrt(variance); | |
| if (stdDev > 0.05) score += 10; // تفاوت طبيعي | |
| if (stdDev > 0.2) score += 5; | |
| } | |
| // 4. أنماط الكتابة (البشر لديهم إيقاع كتابة متفاوت) | |
| if (m.keystrokeTiming.length > 3) { | |
| score += 10; | |
| const avgTiming = m.keystrokeTiming.reduce((a, b) => a + b, 0) / m.keystrokeTiming.length; | |
| // بشر: 50-300ms بين الضغطات، بوت: < 20ms أو ثابت تماماً | |
| if (avgTiming > 30 && avgTiming < 500) score += 10; | |
| // التباين في سرعة الكتابة | |
| const timingVariance = m.keystrokeTiming.reduce( | |
| (sum, t) => sum + (t - avgTiming) ** 2, 0 | |
| ) / m.keystrokeTiming.length; | |
| if (Math.sqrt(timingVariance) > 20) score += 5; | |
| } | |
| // 5. تفاعلات اللمس (للموبايل) | |
| if (m.touchEvents > 0) score += 10; | |
| // 6. مسار الماوس (البشر لا يتحركون بخطوط مستقيمة) | |
| if (m.cursorPath.length > 5) { | |
| let dirChanges = 0; | |
| for (let i = 2; i < m.cursorPath.length; i++) { | |
| const dx1 = m.cursorPath[i - 1].x - m.cursorPath[i - 2].x; | |
| const dy1 = m.cursorPath[i - 1].y - m.cursorPath[i - 2].y; | |
| const dx2 = m.cursorPath[i].x - m.cursorPath[i - 1].x; | |
| const dy2 = m.cursorPath[i].y - m.cursorPath[i - 1].y; | |
| if (Math.sign(dx1) !== Math.sign(dx2) || Math.sign(dy1) !== Math.sign(dy2)) { | |
| dirChanges++; | |
| } | |
| } | |
| if (dirChanges > 2) score += 10; | |
| } | |
| // 7. تفاعلات عامة | |
| if (m.formInteractions > 10) score += 5; | |
| return Math.min(100, score); | |
| }, []); | |
| const resetMetrics = useCallback(() => { | |
| metricsRef.current = { | |
| mouseMovements: 0, | |
| mouseSpeed: [], | |
| clickPositions: [], | |
| keystrokeTiming: [], | |
| touchEvents: 0, | |
| scrollEvents: 0, | |
| formStartTime: Date.now(), | |
| formInteractions: 0, | |
| cursorPath: [], | |
| }; | |
| lastMousePos.current = null; | |
| lastKeystrokeTime.current = 0; | |
| }, []); | |
| return { getHumanScore, resetMetrics }; | |
| } | |