优化检测逻辑,减少误判
修复了正常访问时也提示"检测到开发者工具"的问题。 核心改进: 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 <noreply@anthropic.com>
This commit is contained in:
@@ -12,14 +12,15 @@
|
|||||||
if (isDebugMode) return; // 调试模式下跳过检测
|
if (isDebugMode) return; // 调试模式下跳过检测
|
||||||
|
|
||||||
let devtoolsOpen = false;
|
let devtoolsOpen = false;
|
||||||
|
let checkCount = 0; // 检测次数计数器
|
||||||
|
|
||||||
// 方法1: Console对象toString检测(最有效)
|
// 方法1: Console对象toString检测(最可靠)
|
||||||
const checkElement = /./;
|
const checkElement = /./;
|
||||||
checkElement.toString = function() {
|
checkElement.toString = function() {
|
||||||
devtoolsOpen = true;
|
devtoolsOpen = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 方法2: debugger暂停检测
|
// 方法2: debugger暂停检测(可能误判,需要多次确认)
|
||||||
function detectDebugger() {
|
function detectDebugger() {
|
||||||
const start = performance.now();
|
const start = performance.now();
|
||||||
debugger;
|
debugger;
|
||||||
@@ -27,12 +28,34 @@
|
|||||||
return (end - start) > 100;
|
return (end - start) > 100;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 立即检测
|
// 方法3: 窗口尺寸检测(辅助判断)
|
||||||
console.log('%c', checkElement);
|
function detectWindowSize() {
|
||||||
console.clear();
|
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('<style>body{margin:0;overflow:hidden;}</style>');
|
document.write('<style>body{margin:0;overflow:hidden;}</style>');
|
||||||
document.write('<div style="position:fixed;top:0;left:0;width:100%;height:100%;background:linear-gradient(135deg,#667eea 0%,#764ba2 100%);display:flex;align-items:center;justify-content:center;font-family:Arial,sans-serif;z-index:999999;">');
|
document.write('<div style="position:fixed;top:0;left:0;width:100%;height:100%;background:linear-gradient(135deg,#667eea 0%,#764ba2 100%);display:flex;align-items:center;justify-content:center;font-family:Arial,sans-serif;z-index:999999;">');
|
||||||
document.write('<div style="background:white;padding:60px;border-radius:20px;text-align:center;max-width:500px;box-shadow:0 20px 60px rgba(0,0,0,0.3);">');
|
document.write('<div style="background:white;padding:60px;border-radius:20px;text-align:center;max-width:500px;box-shadow:0 20px 60px rgba(0,0,0,0.3);">');
|
||||||
@@ -43,8 +66,6 @@
|
|||||||
document.write('<p style="color:#999;font-size:14px;margin-top:20px;">如需使用开发者工具,请联系管理员开启调试模式</p>');
|
document.write('<p style="color:#999;font-size:14px;margin-top:20px;">如需使用开发者工具,请联系管理员开启调试模式</p>');
|
||||||
document.write('</div></div>');
|
document.write('</div></div>');
|
||||||
document.close();
|
document.close();
|
||||||
|
|
||||||
// 阻止后续所有脚本执行
|
|
||||||
throw new Error('DevTools detected');
|
throw new Error('DevTools detected');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,16 +81,16 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// 持续监控
|
// 持续监控(每2秒检测一次,避免频繁检测)
|
||||||
setInterval(function() {
|
setInterval(function() {
|
||||||
devtoolsOpen = false;
|
checkCount++;
|
||||||
console.log('%c', checkElement);
|
|
||||||
console.clear();
|
|
||||||
|
|
||||||
if (devtoolsOpen || detectDebugger()) {
|
// 每次检测都要确认
|
||||||
|
if (checkDevTools()) {
|
||||||
|
// 检测到开发者工具打开,刷新页面
|
||||||
location.reload();
|
location.reload();
|
||||||
}
|
}
|
||||||
}, 1000);
|
}, 2000);
|
||||||
|
|
||||||
// 禁用console(非localhost)
|
// 禁用console(非localhost)
|
||||||
if (window.location.hostname !== 'localhost' && window.location.hostname !== '127.0.0.1') {
|
if (window.location.hostname !== 'localhost' && window.location.hostname !== '127.0.0.1') {
|
||||||
|
|||||||
Reference in New Issue
Block a user