From cbcb03af5b0b11764b0757fd23083525f2a9e3d1 Mon Sep 17 00:00:00 2001 From: make Date: Mon, 18 Aug 2025 21:15:04 +0800 Subject: [PATCH] =?UTF-8?q?1.SiteContext.php=20=E5=A2=9E=E5=8A=A0=20checkC?= =?UTF-8?q?nzzCookie=20=E6=A3=80=E6=B5=8B=20=E6=98=AF=E5=90=A6=E6=9C=89cnz?= =?UTF-8?q?z=E7=9A=84cooke=EF=BC=8C=E6=B2=A1=E6=9C=89=E5=88=99=E6=98=AF=20?= =?UTF-8?q?=E7=88=AC=E8=99=AB=E8=A1=8C=E4=B8=BA=EF=BC=8C=E9=9C=80=E8=A6=81?= =?UTF-8?q?=E6=8B=A6=E6=88=AA=20=20=20=20=202.404=E9=A1=B5=E9=9D=A2?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BB=9F=E8=AE=A1cnzz=E7=BB=9F=E8=AE=A1?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=20=20=20=20=203.=E5=85=81=E8=AE=B8=E7=94=A8?= =?UTF-8?q?=E6=88=B73=E6=AC=A1=E6=97=A0cooke=20=E8=AE=BF=E9=97=AE=EF=BC=8C?= =?UTF-8?q?=203=E6=AC=A1=E5=90=8E=E6=97=A0cooke=20=E5=88=99=E8=BF=94?= =?UTF-8?q?=E5=9B=9E404?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/app/home/view/public/error.html | 12 +++++ code/app/services/SiteContext.php | 65 +++++++++++++++++++++++++--- code/public/404.html | 10 +++++ doc/update-history.md | 4 ++ 4 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 doc/update-history.md 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