diff --git a/code/app/home/view/public/error.html b/code/app/home/view/public/error.html index ae1acb6..ccd2608 100644 --- a/code/app/home/view/public/error.html +++ b/code/app/home/view/public/error.html @@ -97,6 +97,18 @@

倒计时:5

或者 点击这里返回首页

+ + {site:cfg code="PUBLIC_TONGJI_CODE" encode="false"/} + + \ No newline at end of file diff --git a/code/app/services/SiteContext.php b/code/app/services/SiteContext.php index 8f106f6..5e8c9cd 100644 --- a/code/app/services/SiteContext.php +++ b/code/app/services/SiteContext.php @@ -538,7 +538,7 @@ class SiteContext } // 拉黑 - if ($intRequestTotalLimit100 > 200 && !$arrVisitInfo['vil_is_spider']) { + if ($intRequestTotalLimit100 > 100 && !$arrVisitInfo['vil_is_spider']) { // 将 IP 加入黑名单集合 Cache::store('redis')->handler()->sAdd($strKeyBlacklist, $strIp); // 设置黑名单过期时间(如:1 天后自动移除) @@ -549,12 +549,14 @@ class SiteContext /** * 不会拦掉真的 Googlebot(因为真的只会是 Chrome/41.0 固定 UA)。 - * 但是 如果你要更精准一点,可以只拦截 Googlebot + Chrome 但不是 Chrome/41 + * 但是 如果你要更精准一点,可以只拦截 Googlebot + Chrome 但不是 Chrome/41 */ - if (stripos($arrVisitInfo['vil_user_agent'], 'Googlebot/2.1') !== false - && stripos($arrVisitInfo['vil_user_agent'], 'Chrome/') !== false - && stripos($arrVisitInfo['vil_user_agent'], 'Chrome/41.0.2272.96') === false) { - throw new HttpException(403, 'HTTP/1.1 403 Forbidden'); + if ( + stripos($arrVisitInfo['vil_user_agent'], 'Googlebot/2.1') !== false + && stripos($arrVisitInfo['vil_user_agent'], 'Chrome/') !== false + && stripos($arrVisitInfo['vil_user_agent'], 'Chrome/41.0.2272.96') === false + ) { + throw new HttpException(404, '404 not found'); } // 定义需要屏蔽的 UA 关键字 / 正则 @@ -562,7 +564,6 @@ class SiteContext 'Amazonbot', // 屏蔽 Amazon 爬虫 'zgrab', // 安全扫描工具 'crawler', // 明示 crawler - 'Chrome\/119\.0\.6045\.214', // 特定版本号 UA ]; // 遍历检查 UA @@ -572,6 +573,56 @@ class SiteContext // 或者用 abort(403, 'Too Many Requests100'); } } + + // 检测cooke + $this->checkCnzzCookie(); + } + + /** + * 检测 是否有cnzz的cooke,没有则是 爬虫行为,需要拦截 + * + * @return void + */ + protected function checkCnzzCookie() + { + $strIp = $this->getRealClientIp(); + $strUa = $this->Request->server('HTTP_USER_AGENT'); + + // 如果是爬虫,直接放行 + if ($this->isSpider($strUa)) { + return true; + } + + // 真实谷歌爬虫 + if (stripos($strUa, 'Googlebot/2.1') !== false && stripos($strUa, 'Chrome/41.0.2272.96') !== false) { + return true; + } + + // 动态匹配 CNZZ/友盟 Cookie(只要 key 里有 CNZZDATA) + $boolCnzz = false; + foreach ($_COOKIE as $key => $val) { + if (stripos($key, 'CNZZDATA') === 0) { + $boolCnzz = true; + break; + } + } + $strCookieKey = "no_cnzz_cookie:" . $strIp; + + if (!$boolCnzz) { + // 自增统计 + $intCount = Cache::store('redis')->handler()->incr($strCookieKey); + + // 第一次访问时设置过期时间(例如 60 分钟) + if ($intCount === 1) { + Cache::store('redis')->handler()->expire($strCookieKey, 3600); + } + + if ($intCount > 3) { + + // 超过 3 次 → 拒绝 + throw new HttpException(404, '404 not found'); + } + } } /** diff --git a/code/public/404.html b/code/public/404.html index 85454d6..e01921c 100644 --- a/code/public/404.html +++ b/code/public/404.html @@ -84,6 +84,16 @@

倒计时:5

或者 点击这里返回首页

+ + \ No newline at end of file diff --git a/doc/update-history.md b/doc/update-history.md new file mode 100644 index 0000000..3832936 --- /dev/null +++ b/doc/update-history.md @@ -0,0 +1,4 @@ +2025-08-18: + 1.SiteContext.php 增加 checkCnzzCookie 检测 是否有cnzz的cooke,没有则是 爬虫行为,需要拦截 + 2.404页面增加统计cnzz统计代码 + 3.允许用户3次无cooke 访问, 3次后无cooke 则返回404 \ No newline at end of file