From eee07d38201921bb45763228e69e49d7c9cff1f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=96=BB=E5=8B=87=E7=A5=A5?= <237899745@qq.com> Date: Mon, 17 Nov 2025 21:01:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=A3=80=E6=B5=8B=E9=80=BB?= =?UTF-8?q?=E8=BE=91=EF=BC=8C=E5=87=8F=E5=B0=91=E8=AF=AF=E5=88=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复了正常访问时也提示"检测到开发者工具"的问题。 核心改进: 1. 提高检测阈值,避免误判 - 窗口尺寸差异从160px提高到200px - 要求console和其他方法同时检测到才触发 2. 降低检测频率 - 从每1秒改为每2秒检测一次 - 减少性能消耗和误判概率 3. 严格判定条件 - console检测为主要依据 - debugger和窗口尺寸作为辅助确认 检测逻辑: - consoleOpen && (debuggerPause || windowSizeAbnormal) - 必须console明确检测到,且至少有一个辅助条件满足 说明: 前端防护无法完全阻止技术人员查看源代码(Network面板、 右键查看源代码等),但可以有效防止普通用户使用F12。 真正的安全依赖于后端验证和权限控制。 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- frontend/app.html | 51 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/frontend/app.html b/frontend/app.html index dcaafd9..fcbfaae 100644 --- a/frontend/app.html +++ b/frontend/app.html @@ -12,14 +12,15 @@ if (isDebugMode) return; // 调试模式下跳过检测 let devtoolsOpen = false; + let checkCount = 0; // 检测次数计数器 - // 方法1: Console对象toString检测(最有效) + // 方法1: Console对象toString检测(最可靠) const checkElement = /./; checkElement.toString = function() { devtoolsOpen = true; }; - // 方法2: debugger暂停检测 + // 方法2: debugger暂停检测(可能误判,需要多次确认) function detectDebugger() { const start = performance.now(); debugger; @@ -27,12 +28,34 @@ return (end - start) > 100; } - // 立即检测 - console.log('%c', checkElement); - console.clear(); + // 方法3: 窗口尺寸检测(辅助判断) + function detectWindowSize() { + const widthDiff = window.outerWidth - window.innerWidth; + const heightDiff = window.outerHeight - window.innerHeight; + // 提高阈值,避免误判 + return widthDiff > 200 || heightDiff > 200; + } - if (devtoolsOpen || detectDebugger()) { - // 立即阻止页面渲染 + // 综合检测(需要多个条件同时满足才判定) + function checkDevTools() { + devtoolsOpen = false; + console.log('%c', checkElement); + console.clear(); + + const consoleOpen = devtoolsOpen; + const debuggerPause = detectDebugger(); + const windowSizeAbnormal = detectWindowSize(); + + // 严格判定:console检测为主,其他为辅助 + // 只有console明确检测到才触发警告 + return consoleOpen && (debuggerPause || windowSizeAbnormal); + } + + // 立即检测(页面加载时) + const initialCheck = checkDevTools(); + + if (initialCheck) { + // 检测到开发者工具,显示警告 document.write(''); document.write('
'); document.write('
'); @@ -43,8 +66,6 @@ document.write('

如需使用开发者工具,请联系管理员开启调试模式

'); document.write('
'); document.close(); - - // 阻止后续所有脚本执行 throw new Error('DevTools detected'); } @@ -60,16 +81,16 @@ } }); - // 持续监控 + // 持续监控(每2秒检测一次,避免频繁检测) setInterval(function() { - devtoolsOpen = false; - console.log('%c', checkElement); - console.clear(); + checkCount++; - if (devtoolsOpen || detectDebugger()) { + // 每次检测都要确认 + if (checkDevTools()) { + // 检测到开发者工具打开,刷新页面 location.reload(); } - }, 1000); + }, 2000); // 禁用console(非localhost) if (window.location.hostname !== 'localhost' && window.location.hostname !== '127.0.0.1') {