1.SiteContext.php 增加 checkCnzzCookie 检测 是否有cnzz的cooke,没有则是 爬虫行为,需要拦截

2.404页面增加统计cnzz统计代码
    3.允许用户3次无cooke 访问, 3次后无cooke 则返回404
This commit is contained in:
make
2025-08-18 21:15:04 +08:00
parent f795e105b6
commit cbcb03af5b
4 changed files with 84 additions and 7 deletions

View File

@@ -97,6 +97,18 @@
<p>倒计时:<span id="countdown" class="countdown">5</span></p>
<p>或者 <a href="/">点击这里返回首页</a></p>
</div>
{site:cfg code="PUBLIC_TONGJI_CODE" encode="false"/}
<script>
var _czc = _czc || [];
(function () {
var um = document.createElement("script");
um.src = "https://s9.cnzz.com/z.js?id=1281427615&async=1";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(um, s);
})();
</script>
</body>
</html>

View File

@@ -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');
}
}
}
/**

View File

@@ -84,6 +84,16 @@
<p>倒计时:<span id="countdown" class="countdown">5</span></p>
<p>或者 <a href="/">点击这里返回首页</a></p>
</div>
<!--404统计 -->
<script>
var _czc = _czc || [];
(function () {
var um = document.createElement("script");
um.src = "https://s9.cnzz.com/z.js?id=1281427615&async=1";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(um, s);
})();
</script>
</body>
</html>