日志升级与缓存修复最终版

This commit is contained in:
root
2026-06-16 04:58:56 +08:00
parent bcb9f293a6
commit 6b3b99b0f1
61 changed files with 2405 additions and 333 deletions
Regular → Executable
+53 -26
View File
@@ -1,7 +1,7 @@
#!/usr/bin/env php
<?php
/**
* 二次风控 Session 缓存回归(CLI 输出 JSON
* CloakJudgmentCache / RiskSecondaryCache 回归(CLI JSON
*/
if (PHP_SAPI !== 'cli') {
exit(1);
@@ -9,41 +9,69 @@ if (PHP_SAPI !== 'cli') {
$root = dirname(__DIR__);
require_once $root . '/cong.php';
require_once $root . '/lib/CloakHttpHelpers.php';
require_once $root . '/lib/Cloak/ClientIpResolver.php';
require_once $root . '/lib/Cloak/RiskSecondaryGate.php';
require_once $root . '/lib/Cloak/CloakJudgmentCache.php';
require_once $root . '/lib/Cloak/RiskSecondaryCache.php';
if (!defined('CLOAK_RISK_NUMBER')) {
define('CLOAK_RISK_NUMBER', 51);
}
if (!defined('COSTM_IP_SCORE')) {
define('COSTM_IP_SCORE', 'test_campaign');
}
$_SESSION = [];
$tests = [];
$tests = [];
RiskSecondaryCache::store([
'ip' => '203.0.113.10',
'result' => 'true',
'reason' => 'ok',
'fp_url' => 'https://fp.example/a',
CloakJudgmentCache::storeFromByApiRisk([
'ip' => '203.0.113.10',
'country' => 'US',
'result' => 'true',
'reason' => '',
'fp_url' => 'https://fp.example/a',
]);
$hit = RiskSecondaryCache::get('203.0.113.10');
$tests['same_ip_hit'] = is_array($hit) && ($hit['result'] ?? '') === 'true';
$hit = CloakJudgmentCache::getFinal('203.0.113.10');
$tests['same_ip_final_hit'] = is_array($hit) && ($hit['result'] ?? '') === 'true' && ($hit['stage'] ?? '') === CloakJudgmentCache::STAGE_FINAL;
RiskSecondaryCache::applyToSession($hit);
$tests['apply_session'] = ($_SESSION['visit_to_3'] ?? '') == 3 && ($_SESSION['check_result'] ?? '') === 'true';
if (is_array($hit)) {
CloakJudgmentCache::applyToSession($hit);
}
$tests['apply_session_final'] = ($_SESSION['visit_to_3'] ?? '') == 3 && ($_SESSION['check_result'] ?? '') === 'true';
$miss = RiskSecondaryCache::get('198.51.100.1');
$tests['ip_change_miss'] = $miss === null && empty($_SESSION[RiskSecondaryCache::SESSION_KEY]);
$tests['ip_change_resets_visit_to_3'] = ($_SESSION['visit_to_3'] ?? '') === '1';
$hitNewIp = CloakJudgmentCache::get('198.51.100.5');
$tests['ip_change_still_valid'] = is_array($hitNewIp) && ($hitNewIp['result'] ?? '') === 'true';
$tests['ip_change_updates_ip_field'] = ($hitNewIp['ip'] ?? '') === '198.51.100.5';
$tests['ip_change_keeps_session_key'] = !empty($_SESSION[CloakJudgmentCache::SESSION_KEY]);
RiskSecondaryCache::store([
'ip' => '203.0.113.10',
'result' => 'false',
'reason' => 'deny',
'fp_url' => 'https://zp.example/',
$partialOnly = CloakJudgmentCache::getFinal('198.51.100.5');
$tests['risk_cache_get_skips_partial'] = true;
CloakJudgmentCache::clear();
CloakJudgmentCache::storeFromByApi([
'ip' => '203.0.113.10',
'country' => 'US',
'result' => 'true',
'reason' => 'api ok',
]);
$api = RiskSecondaryCache::toApiResponse(RiskSecondaryCache::get('203.0.113.10'));
$tests['to_api_response'] = $api['result'] === false && $api['reason'] === RiskSecondaryCache::hitReason();
$tests['byapi_stage_is_partial'] = CloakJudgmentCache::isPartial();
$tests['risk_cache_get_skips_partial'] = RiskSecondaryCache::get('203.0.113.10') === null;
RiskSecondaryCache::clear();
$tests['clear'] = RiskSecondaryCache::get('203.0.113.10') === null;
CloakJudgmentCache::storeFromByApiRisk([
'ip' => '203.0.113.10',
'country' => 'US',
'result' => 'false',
'reason' => 'deny',
'fp_url' => 'https://zp.example/',
]);
$apiHit = CloakJudgmentCache::getFinal('203.0.113.10');
$api = is_array($apiHit) ? CloakJudgmentCache::toApiResponse($apiHit) : ['result' => true, 'reason' => ''];
$tests['to_api_response'] = $api['result'] === false && $api['reason'] === CloakJudgmentCache::hitReason();
$_SESSION[CloakJudgmentCache::SESSION_KEY]['risk_number'] = -1;
$tests['risk_number_change_invalidates'] = CloakJudgmentCache::getFinal('203.0.113.10') === null;
CloakJudgmentCache::clear();
$tests['clear'] = !CloakJudgmentCache::hasSession();
$originOk = false;
$_SERVER['HTTP_HOST'] = 'cloak.example.com';
@@ -59,6 +87,5 @@ $badOrigin = !(bool) $method->invoke(null, 'https://evil.other.com');
$tests['cors_reject_foreign'] = $badOrigin;
$ok = !in_array(false, $tests, true);
echo json_encode(['ok' => $ok, 'tests' => $tests], JSON_UNESCAPED_UNICODE) . "\n";
exit($ok ? 0 : 1);