1.SiteContext.php 增加 checkCnzzCookie 检测 是否有cnzz的cooke,没有则是 爬虫行为,需要拦截
2.404页面增加统计cnzz统计代码
3.允许用户3次无cooke 访问, 3次后无cooke 则返回404
This commit is contained in:
@@ -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');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user