'use client' import React, { useState, useEffect } from 'react' import { useRouter, useParams } from 'next/navigation' import { Shield, Monitor, Copy, AlertTriangle, CheckCircle, ArrowRight, Eye, Lock, Maximize } from 'lucide-react' export default function SecurityCheckPage() { const router = useRouter() const params = useParams() const examCode = params.examCode as string const [securityChecks, setSecurityChecks] = useState({ fullScreen: false, noVirtualBox: false, copyPasteBlocked: false, focusDetection: false }) const [isFullScreen, setIsFullScreen] = useState(false) const [securityPassed, setSecurityPassed] = useState(false) const [agreementAccepted, setAgreementAccepted] = useState(false) const [focusLostCount, setFocusLostCount] = useState(0) const [warningMessage, setWarningMessage] = useState('') useEffect(() => { // Check for virtual machine detection detectVirtualMachine() // Block copy/paste blockCopyPaste() // Setup fullscreen detection setupFullScreenDetection() // Setup focus detection setupFocusDetection() // Block right-click blockRightClick() // Block developer tools blockDevTools() return () => { // Cleanup event listeners document.removeEventListener('contextmenu', handleRightClick) document.removeEventListener('keydown', handleKeyDown) window.removeEventListener('blur', handleWindowBlur) window.removeEventListener('focus', handleWindowFocus) } }, []) // ✅ VIRTUAL MACHINE DETECTION const detectVirtualMachine = () => { try { const canvas = document.createElement('canvas') const gl = canvas.getContext('webgl') || canvas.getContext('experimental-webgl') if (gl) { const debugInfo = gl.getExtension('WEBGL_debug_renderer_info') if (debugInfo) { const renderer = gl.getParameter(debugInfo.UNMASKED_RENDERER_WEBGL).toLowerCase() const vendor = gl.getParameter(debugInfo.UNMASKED_VENDOR_WEBGL).toLowerCase() const vmIndicators = [ 'virtualbox', 'vmware', 'parallels', 'qemu', 'virtual', 'vm', 'hyper-v', 'kvm' ] const isVM = vmIndicators.some(indicator => renderer.includes(indicator) || vendor.includes(indicator) ) if (isVM) { setWarningMessage('❌ Virtual machines are not allowed for this exam') return } } } // Additional VM detection checks if ( navigator.hardwareConcurrency < 2 || screen.width < 1024 || screen.height < 768 || navigator.deviceMemory && navigator.deviceMemory < 2 ) { setWarningMessage('⚠️ Your system may not meet the minimum requirements') } setSecurityChecks(prev => ({ ...prev, noVirtualBox: true })) } catch (error) { console.error('VM detection failed:', error) setSecurityChecks(prev => ({ ...prev, noVirtualBox: true })) } } // ✅ BLOCK COPY/PASTE const blockCopyPaste = () => { const preventCopyPaste = (e: Event) => { e.preventDefault() setWarningMessage('⚠️ Copy/Paste is disabled during the exam') setTimeout(() => setWarningMessage(''), 3000) } document.addEventListener('copy', preventCopyPaste) document.addEventListener('paste', preventCopyPaste) document.addEventListener('cut', preventCopyPaste) document.addEventListener('drag', preventCopyPaste) document.addEventListener('drop', preventCopyPaste) document.addEventListener('selectstart', preventCopyPaste) // Disable text selection document.body.style.userSelect = 'none' document.body.style.webkitUserSelect = 'none' setSecurityChecks(prev => ({ ...prev, copyPasteBlocked: true })) } // ✅ FULLSCREEN DETECTION const setupFullScreenDetection = () => { const checkFullScreen = () => { const isFS = !!( document.fullscreenElement || (document as any).webkitFullscreenElement || (document as any).mozFullScreenElement || (document as any).msFullscreenElement ) setIsFullScreen(isFS) setSecurityChecks(prev => ({ ...prev, fullScreen: isFS })) if (!isFS && securityPassed) { setWarningMessage('⚠️ You must stay in fullscreen mode during the exam') setTimeout(() => { router.push('/coding') }, 3000) } } document.addEventListener('fullscreenchange', checkFullScreen) document.addEventListener('webkitfullscreenchange', checkFullScreen) document.addEventListener('mozfullscreenchange', checkFullScreen) document.addEventListener('MSFullscreenChange', checkFullScreen) } // ✅ FOCUS DETECTION const setupFocusDetection = () => { let focusLost = false const handleWindowBlur = () => { if (securityPassed) { focusLost = true setFocusLostCount(prev => prev + 1) setWarningMessage('⚠️ You switched tabs/windows. This is being monitored.') if (focusLostCount >= 2) { alert('Multiple focus violations detected. Exam will be terminated.') router.push('/coding') } } } const handleWindowFocus = () => { if (focusLost) { focusLost = false setTimeout(() => setWarningMessage(''), 3000) } } window.addEventListener('blur', handleWindowBlur) window.addEventListener('focus', handleWindowFocus) setSecurityChecks(prev => ({ ...prev, focusDetection: true })) } // ✅ BLOCK RIGHT-CLICK const blockRightClick = () => { const handleRightClick = (e: MouseEvent) => { e.preventDefault() setWarningMessage('⚠️ Right-click is disabled during the exam') setTimeout(() => setWarningMessage(''), 2000) } document.addEventListener('contextmenu', handleRightClick) } // ✅ BLOCK DEVELOPER TOOLS const blockDevTools = () => { const handleKeyDown = (e: KeyboardEvent) => { // Block F12, Ctrl+Shift+I, Ctrl+Shift+C, Ctrl+U if ( e.key === 'F12' || (e.ctrlKey && e.shiftKey && (e.key === 'I' || e.key === 'C')) || (e.ctrlKey && e.key === 'U') || (e.ctrlKey && e.shiftKey && e.key === 'J') ) { e.preventDefault() setWarningMessage('⚠️ Developer tools are not allowed during the exam') setTimeout(() => setWarningMessage(''), 3000) } } document.addEventListener('keydown', handleKeyDown) // Detect if dev tools are open let devtools = { open: false, orientation: null } const threshold = 160 setInterval(() => { if ( window.outerHeight - window.innerHeight > threshold || window.outerWidth - window.innerWidth > threshold ) { if (!devtools.open) { devtools.open = true setWarningMessage('⚠️ Developer tools detected. Please close them.') } } else { devtools.open = false } }, 500) } // ✅ ENTER FULLSCREEN const enterFullScreen = async () => { try { const element = document.documentElement if (element.requestFullscreen) { await element.requestFullscreen() } else if ((element as any).webkitRequestFullscreen) { await (element as any).webkitRequestFullscreen() } else if ((element as any).mozRequestFullScreen) { await (element as any).mozRequestFullScreen() } else if ((element as any).msRequestFullscreen) { await (element as any).msRequestFullscreen() } } catch (error) { setWarningMessage('❌ Failed to enter fullscreen. Please try again.') } } // ✅ CHECK ALL SECURITY MEASURES useEffect(() => { const allChecksPassed = Object.values(securityChecks).every(check => check === true) setSecurityPassed(allChecksPassed && agreementAccepted) }, [securityChecks, agreementAccepted]) // ✅ PROCEED TO EXAM const proceedToExam = () => { if (securityPassed && isFullScreen) { // Store security session sessionStorage.setItem('exam_security_passed', 'true') sessionStorage.setItem('exam_start_time', new Date().toISOString()) router.push(`/coding/exam/${examCode}`) } else { setWarningMessage('❌ Please complete all security requirements') } } return (
Exam Code: {examCode}
{warningMessage}
Required for exam security
Checking for virtual machines
Disabled for exam integrity
Tab switching will be tracked
By proceeding with this exam, I agree to:
Violations: {focusLostCount}/2 (3rd violation = automatic termination)