diff --git a/code/app/services/SiteContext.php b/code/app/services/SiteContext.php index 47b18ae..317ef42 100644 --- a/code/app/services/SiteContext.php +++ b/code/app/services/SiteContext.php @@ -112,7 +112,7 @@ class SiteContext public function initCfg() { $this->limitRequestRate(); - if(config('app.default_app_type') == 'novel'){ + if (config('app.default_app_type') == 'novel') { // 全局使用的URL模板 $strPcUrlTemp = $this->DomainModel->getFomartSubject('PC_PATH_URL', '', false); $strNanUrlTemp = $this->DomainModel->getFomartSubject('NAN_SHENG_URL', '', false); @@ -138,7 +138,7 @@ class SiteContext View::assign("strHistoryUrlTemp", $strHistoryUrlTemp); View::assign("strBookShelfUrlTemp", $strBookShelfUrlTemp); View::assign("strUserUrlTemp", $strUserUrlTemp); - }else{ + } else { // $strPcUrlTemp = $this->DomainModel->getFomartSubject('VIDEO_PC_PATH_URL', '', false); $strRankUrlTemp = $this->DomainModel->getFomartSubject('VIDEO_RANK_INDEX_URL', '', false); $strSearchListUrlTemp = $this->DomainModel->getFomartSubject('VIDEO_SEARCH_LIST_URL', '', false); @@ -148,7 +148,7 @@ class SiteContext View::assign("strSearchListUrlTemp", $strSearchListUrlTemp); View::assign("strHistoryUrlTemp", $strHistoryUrlTemp); } - + // 视频站点需要信息 // $strVideoCategoryIndexUrlTemp = $this->DomainModel->getFomartSubject('VIDEO_CATEGORY_INDEX_URL', '', false); @@ -158,7 +158,7 @@ class SiteContext // View::assign("strVideoSearchListUrlTemp", $strVideoSearchListUrlTemp); // View::assign("strVideoHistoryUrlTemp", $strVideoHistoryUrlTemp); - + View::assign('Request', $this->Request); View::assign('DomainModel', $this->DomainModel); @@ -188,7 +188,7 @@ class SiteContext public function converterTemplate(string $strCode, string | Null $strDiff = "0"): string { $strTitleTemplate = $this->DomainModel->getFomartSubject($strCode, $strDiff); - + return ConverterMovel::convert($strTitleTemplate); } @@ -231,20 +231,19 @@ class SiteContext { if ($strPlatform == 'h5') { if ($this->DomainModel->info_id > 0) { - if(config('app.default_app_type') == 'video'){ + if (config('app.default_app_type') == 'video') { $strView = 'video/getVideoInfo.html'; - }else{ + } else { $strView = 'novel/getNovelInfo.html'; } - } else { $strView = 'index/index.html'; } } else { if ($this->DomainModel->info_id > 0) { - if(config('app.default_app_type') == 'video'){ + if (config('app.default_app_type') == 'video') { $strView = 'pc/getVideoInfo.html'; - }else{ + } else { $strView = 'pc/getNovelInfo.html'; } } else { @@ -341,7 +340,7 @@ class SiteContext // 检查分类是否有效 if (!empty($strParentCategory) && $strParentCategory !== 'all' && array_key_exists($strParentCategory, StaticConfig::$arrVideoClass)) { $strVideoParentCategoryName = StaticConfig::$arrVideoClass[$strParentCategory]; - } + } // 设置分类名称 ConverterMovel::setVal([ 'strVideoParentCategoryName' => $strVideoParentCategoryName, @@ -350,7 +349,7 @@ class SiteContext // $strParentCategory = $this->Request->param('strParentCategory'); // if (array_key_exists($strParentCategory, StaticConfig::$arrVideoClass)) { - + // } // ConverterMovel::setVal([ // 'strVideoParentCategoryName' => empty($strParentCategory) || $strParentCategory == 'all' ? '' : StaticConfig::$arrVideoClass[$strParentCategory], @@ -408,7 +407,7 @@ class SiteContext 'intPage' => $intPage ?? 1, ]); } - + /** * 视图反向控制器(注释函数),在视图开头调用,用来初始化一些操作。这里尽量不要复杂逻辑。这是个样例 @@ -515,8 +514,8 @@ class SiteContext // 判断是否为爬虫 $arrVisitInfo['vil_is_spider'] = $this->isSpider($arrVisitInfo['vil_user_agent']); - - $strIp = $this->Request->ip(); + + $strIp = $this->getRealClientIp(); $strKeyLimit10 = 'access:' . $strIp; $strKeyLimit100 = 'access100:' . $strIp; $strKeyBlacklist = 'blacklist:ips'; // 黑名单缓存键 @@ -554,7 +553,7 @@ class SiteContext } $strAGEN = 'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; Amazonbot/0.1; +https://developer.amazon.com/support/amazonbot) Chrome/119.0.6045.214 Safari/537.36'; - if($arrVisitInfo['vil_user_agent'] == $strAGEN){ + if ($arrVisitInfo['vil_user_agent'] == $strAGEN) { throw new HttpException(404, 'Too Many Requests100'); //abort(403, 'Too Many Requests100'); } @@ -562,7 +561,6 @@ class SiteContext throw new HttpException(404, 'Too Many Requests100'); //abort(403, 'Too Many Requests100'); } - } /** @@ -599,4 +597,24 @@ class SiteContext return false; } + /** + * 获取真实ip + * + * @return void + */ + protected function getRealClientIp() + { + $headers = $this->Request->header(); + + if (isset($headers['x-forwarded-for'])) { + $ips = explode(',', $headers['x-forwarded-for']); + return trim($ips[0]); // 取第一个(用户 IP) + } + + if (isset($headers['x-real-ip'])) { + return $headers['x-real-ip']; + } + + return $this->Request->ip(); // 备用 + } }