修复国家设置,允许不设置任何国家,不做判断

This commit is contained in:
root
2026-06-20 04:09:37 +08:00
parent 6b3b99b0f1
commit 24ff3f9657
11 changed files with 188 additions and 23 deletions
+56 -5
View File
@@ -6,18 +6,56 @@ require_once __DIR__ . '/CountryCodeNormalizer.php';
class CountryAllowlist
{
/**
* SHOW_SITE_COUNTRY 是否已配置(非空允许列表 = 开启国家条件)
*
* @param string|null $config 默认读取 SHOW_SITE_COUNTRY
*/
public static function isEnabled($config = null)
{
if ($config === null) {
$config = defined('SHOW_SITE_COUNTRY') ? SHOW_SITE_COUNTRY : '';
}
return self::parse($config) !== [];
}
/**
* 保存前规范化用户输入:兼容中文逗号/分号、顿号、全角空格及多余空白。
*
* @param string $config
*/
public static function normalizeInput($config)
{
$config = trim((string) $config);
if ($config === '') {
return '';
}
// 全角空格、不间断空格
$config = preg_replace('/[\x{3000}\x{00A0}]/u', ' ', $config);
// 常见误输入分隔符统一为英文逗号
$config = str_replace(['', ';', '', '、'], ',', $config);
// 去掉分隔符两侧空白,合并连续逗号
$config = preg_replace('/\s*,\s*/', ',', $config);
$config = preg_replace('/,+/', ',', $config);
return trim($config, " \t\n\r,");
}
/**
* @param string $config 逗号分隔国家码,如 US,GB,CA
* @return string[]
*/
public static function parse($config)
{
$config = trim((string) $config);
$config = self::normalizeInput($config);
if ($config === '') {
return [];
}
// 支持英文逗号、中文逗号、分号分隔
$parts = preg_split('/\s*[,;]\s*/u', $config);
$parts = explode(',', $config);
$out = [];
foreach ($parts as $part) {
$part = trim($part);
@@ -33,10 +71,23 @@ class CountryAllowlist
}
/**
* 保存配置前规范化国家列表字符串(dashboard 写入用
* 读取并规范化 SHOW_SITE_COUNTRY(大写 ISO2,逗号分隔
*
* @param string|null $config 默认 SHOW_SITE_COUNTRY
*/
public static function resolvedConfig($config = null)
{
if ($config === null) {
$config = defined('SHOW_SITE_COUNTRY') ? SHOW_SITE_COUNTRY : '';
}
return self::formatForConfig($config);
}
/**
* 保存配置前规范化国家列表字符串(dashboard 写入用,输出大写如 US,GB)
*
* @param string $config
* @return string 如 BR,CN
* @return string 如 US,GB
*/
public static function formatForConfig($config)
{
+10 -5
View File
@@ -2,6 +2,7 @@
require_once __DIR__ . '/../WhitelistGate.php';
require_once __DIR__ . '/../CloakJudgmentCache.php';
require_once __DIR__ . '/../RiskSecondaryGate.php';
require_once __DIR__ . '/../CountryAllowlist.php';
/**
* 流量判定主流水线
@@ -88,11 +89,15 @@ class CheckPipeline
return;
}
CloakPipelineTimer::stage('GeoIP国家判定');
include __DIR__ . '/stages/check_country_geoip.inc.php';
if (!empty($_SESSION['check_result'])) {
CloakPipelineTimer::finish();
return;
if (CountryAllowlist::isEnabled()) {
CloakPipelineTimer::stage('GeoIP国家判定');
include __DIR__ . '/stages/check_country_geoip.inc.php';
if (!empty($_SESSION['check_result'])) {
CloakPipelineTimer::finish();
return;
}
} elseif ($__cloak_debug_on) {
cloak_dbg_step(__LINE__, 'GeoIP国家判定', 'skip', '未开启国家条件');
}
CloakPipelineTimer::stage('广告来源限制');
@@ -1,9 +1,20 @@
<?php
require_once __DIR__ . '/../../CountryAllowlist.php';
require_once __DIR__ . '/../../ClientIpResolver.php';
require_once __DIR__ . '/../../GeoIpCountryResolver.php';
/**
* 仅用本地 MaxMind 解析国家并写入访客信息/Session(不做国家允许列表或其它判定)
* 未开启国家条件(SHOW_SITE_COUNTRY 为空)时不查询 MaxMind。
*
* 依赖全局:$v_info, $__cloak_debug_on
*/
if (!CountryAllowlist::isEnabled()) {
if (!empty($GLOBALS['__cloak_debug_on'])) {
cloak_dbg_step(__LINE__, 'MaxMind国家记录', 'skip', '未开启国家条件,跳过 MaxMind');
}
return;
}
$ip = ClientIpResolver::resolve();
if ($ip !== '' && $ip !== '0.0.0.0') {
$v_info->v_ip = $ip;
@@ -1,9 +1,11 @@
<?php
/**
* 屏蔽模式:GeoIP 国家判定(本地 MaxMind + 30 天 IP 缓存)
*
* 依赖全局:$v_info, $reason, $__cloak_debug_on
* 未开启国家条件(SHOW_SITE_COUNTRY 为空)时跳过。
*/
require_once __DIR__ . '/../../CountryAllowlist.php';
require_once __DIR__ . '/../../ClientIpResolver.php';
require_once __DIR__ . '/../../GeoIpCountryResolver.php';
if (!defined('SHOW_SITE_MODE_SWITCH') || SHOW_SITE_MODE_SWITCH !== 'ip_check') {
return;
}
@@ -14,6 +16,13 @@ if (!empty($_SESSION['check_result'])) {
return;
}
if (!CountryAllowlist::isEnabled()) {
if (!empty($GLOBALS['__cloak_debug_on'])) {
cloak_dbg_step(__LINE__, 'GeoIP国家判定', 'skip', '未开启国家条件,跳过本地 MaxMind 判定');
}
return;
}
$ip = ClientIpResolver::resolve();
if ($ip !== '' && $ip !== '0.0.0.0') {
$v_info->v_ip = $ip;
@@ -53,7 +62,7 @@ if (!CountryAllowlist::isAllowed($countryCode, $allowlist)) {
}
if (!empty($GLOBALS['__cloak_debug_on'])) {
cloak_dbg_step(__LINE__, 'GeoIP国家判定', 'pass', '国家允许列表为空', [
cloak_dbg_step(__LINE__, 'GeoIP国家判定', 'pass', '国家允许列表', [
'ip' => $ip,
'country' => $countryCode,
'allowlist' => $allowlist,
+1 -1
View File
@@ -230,7 +230,7 @@ class VisitorSimulationRunner
global $v_info, $reason, $config_name;
$config_name = $configName;
$v_info = new visitorInfo();
$v_info->get_visitor_Info(COSTM_IP_SCORE, CHECK_KEY, SHOW_SITE_COUNTRY);
$v_info->get_visitor_Info(COSTM_IP_SCORE, CHECK_KEY, CountryAllowlist::resolvedConfig());
$reason = '';
CheckPipeline::run();
+3 -3
View File
@@ -52,9 +52,9 @@ class CloakAdSourceGuard
];
private static $fullTemplates = [
self::PLATFORM_FB => 'utm_source=facebook&utm_medium=paid_social&utm_campaign={{campaign.name}}&utm_content={{ad.name}}&campaign_id={{campaign.id}}&adset_id={{adset.id}}&ad_id={{ad.id}}&site_source={{site_source_name}}&placement={{placement}}',
self::PLATFORM_GOOGLE => 'utm_source=google&utm_medium=cpc&utm_campaign={campaignid}&utm_content={adgroupid}&utm_term={keyword}&ad_id={creative}&network={network}&placement={placement}',
self::PLATFORM_TIKTOK => 'utm_source=tiktok&utm_medium=video_ad&utm_campaign=__CAMPAIGN_ID__&utm_content=__AID__&adgroup_id=__CID__&placement=__PLACEMENT__',
self::PLATFORM_FB => 'utm_source=facebook&site_source={{site_source_name}}&placement={{placement}}',
self::PLATFORM_GOOGLE => 'utm_source=google&utm_term={keyword}&network={network}&placement={placement}',
self::PLATFORM_TIKTOK => 'utm_source=tiktok&placement=__PLACEMENT__',
];
private static $multiPlatformParams = [