日志升级与缓存修复最终版
This commit is contained in:
+136
-34
@@ -6,6 +6,9 @@ require_once dirname(__DIR__) . '/DbHelper.php';
|
||||
require_once dirname(__DIR__) . '/CloakHttpHelpers.php';
|
||||
require_once __DIR__ . '/VisitorApiAudit.php';
|
||||
require_once __DIR__ . '/VisitorLogVtimes.php';
|
||||
require_once __DIR__ . '/VisitorVisitContext.php';
|
||||
require_once __DIR__ . '/RiskSecondarySession.php';
|
||||
require_once __DIR__ . '/VisitorLogWorker.php';
|
||||
|
||||
if (!function_exists('cloak_session_mark_real_visitor')) {
|
||||
/**
|
||||
@@ -27,6 +30,17 @@ if (!function_exists('cloak_session_mark_real_visitor')) {
|
||||
}
|
||||
|
||||
setCrossDomainCookie('gfuid', $gcuid, time() + 3600 * 24);
|
||||
|
||||
if (class_exists('CloakJudgmentCache', false)) {
|
||||
$ip = $_SERVER['REMOTE_ADDR'] ?? '';
|
||||
CloakJudgmentCache::storeFromByApiRisk([
|
||||
'ip' => $ip,
|
||||
'country' => $_SESSION['gcu_country'] ?? '',
|
||||
'result' => 'true',
|
||||
'reason' => '',
|
||||
'fp_url' => '',
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,11 +57,19 @@ if (!function_exists('cloak_session_clear_real_visitor')) {
|
||||
$_SESSION['gcu_country'],
|
||||
$_SESSION['gcu_Client'],
|
||||
$_SESSION['gcu_Browser'],
|
||||
$_SESSION['gcu_referer']
|
||||
$_SESSION['gcu_referer'],
|
||||
$_SESSION['cloak_whitelist_fast']
|
||||
);
|
||||
if (class_exists('RiskSecondaryCache', false)) {
|
||||
RiskSecondaryCache::clear();
|
||||
}
|
||||
if (class_exists('CloakJudgmentCache', false)) {
|
||||
CloakJudgmentCache::clear();
|
||||
}
|
||||
if (class_exists('RiskSecondarySession', false)) {
|
||||
RiskSecondarySession::clear();
|
||||
}
|
||||
VisitorVisitContext::clear();
|
||||
setCrossDomainCookie('gfuid', '', time() - 3600);
|
||||
}
|
||||
}
|
||||
@@ -163,49 +185,79 @@ if (!function_exists('cloak_write_log_db_sync')) {
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('cloak_write_log_db')) {
|
||||
function cloak_write_log_db($logs)
|
||||
if (!function_exists('cloak_update_log_db')) {
|
||||
/**
|
||||
* 同 visit 续步 UPDATE(不新增 vtimes)。
|
||||
*
|
||||
* @param int $logId
|
||||
* @param array $logs
|
||||
* @return int|false log id on success
|
||||
*/
|
||||
function cloak_update_log_db($logId, array $logs, array $options = [])
|
||||
{
|
||||
$logs = cloak_visitor_log_enrich($logs);
|
||||
$logId = (int) $logId;
|
||||
if ($logId <= 0) {
|
||||
return false;
|
||||
}
|
||||
if (empty($options['skip_enrich'])) {
|
||||
$logs = cloak_visitor_log_enrich($logs);
|
||||
}
|
||||
$logs = VisitorVisitContext::normalizeLogsForDb($logs);
|
||||
|
||||
if (!empty($GLOBALS['__cloak_force_sync_log'])) {
|
||||
if (($logs['result'] ?? '') === 'wait') {
|
||||
$logId = VisitorLogWorker::insertMinimal($logs);
|
||||
if ($logId !== null) {
|
||||
VisitorLogWorker::enrich($logId, $logs);
|
||||
}
|
||||
return $logId;
|
||||
$ok = false;
|
||||
$forceSync = !empty($GLOBALS['__cloak_force_sync_log'])
|
||||
|| !cloak_visitor_log_async_enabled()
|
||||
|| VisitorVisitContext::shouldFinalizeOnResult($logs['result'] ?? '');
|
||||
if ($forceSync) {
|
||||
$ok = VisitorLogWorker::updateFromLogs($logId, $logs);
|
||||
} else {
|
||||
if (VisitorLogQueue::push(['type' => 'update', 'log_id' => $logId, 'logs' => $logs])) {
|
||||
cloak_visitor_log_register_shutdown_drain();
|
||||
$ok = true;
|
||||
} else {
|
||||
$ok = VisitorLogWorker::updateFromLogs($logId, $logs);
|
||||
}
|
||||
return cloak_write_log_db_sync($logs);
|
||||
}
|
||||
|
||||
if (defined('WRITE_LOG') && WRITE_LOG == '1') {
|
||||
$my_url = $_SERVER['PHP_SELF'] ?? '';
|
||||
$my_file_name = substr($my_url, strrpos($my_url, '/') + 1);
|
||||
$config_name = str_replace('.php', '', $my_file_name);
|
||||
$fp = fopen(dirname(__DIR__, 2) . '/jump.txt', 'a+');
|
||||
fwrite($fp, date('Y-m-d H:i:s') . ' | 写入数据库 ' . ' | ' . ($_SESSION['check_result'] ?? '') . ' | ' . $config_name . ' | async=' . (cloak_visitor_log_async_enabled() ? '1' : '0') . "\n");
|
||||
fclose($fp);
|
||||
if ($ok && VisitorVisitContext::shouldFinalizeOnResult($logs['result'] ?? '')) {
|
||||
VisitorVisitContext::finalizeVisit($logs['result'] ?? '');
|
||||
}
|
||||
return $ok ? $logId : false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('cloak_write_log_db_wait_sync')) {
|
||||
/**
|
||||
* 二次风控 wait:同步完整 INSERT(需立即 log_id,且必须带上 byApi/页面/UA 等字段)。
|
||||
*
|
||||
* @param array $logs 已 enrich + normalize
|
||||
* @return int|null
|
||||
*/
|
||||
function cloak_write_log_db_wait_sync(array $logs)
|
||||
{
|
||||
$logId = VisitorLogWorker::insertFull($logs);
|
||||
if ($logId === null) {
|
||||
$logId = cloak_write_log_db_sync($logs);
|
||||
}
|
||||
return $logId;
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('cloak_write_log_db_insert')) {
|
||||
/**
|
||||
* @param array $logs
|
||||
* @return int|null
|
||||
*/
|
||||
function cloak_write_log_db_insert(array $logs)
|
||||
{
|
||||
$result = isset($logs['result']) ? (string) $logs['result'] : '';
|
||||
|
||||
// 二次风控 wait:同步最小 INSERT 获取 log_id,TEXT 字段异步 enrich
|
||||
if ($result === 'wait') {
|
||||
$logId = VisitorLogWorker::insertMinimal($logs);
|
||||
if ($logId === null) {
|
||||
return cloak_write_log_db_sync($logs);
|
||||
}
|
||||
if (cloak_visitor_log_async_enabled()) {
|
||||
VisitorLogQueue::push([
|
||||
'type' => 'enrich',
|
||||
'log_id' => $logId,
|
||||
'logs' => $logs,
|
||||
]);
|
||||
} else {
|
||||
VisitorLogWorker::enrich($logId, $logs);
|
||||
}
|
||||
return $logId;
|
||||
return cloak_write_log_db_wait_sync($logs);
|
||||
}
|
||||
|
||||
if (!empty($GLOBALS['__cloak_force_sync_log'])) {
|
||||
return cloak_write_log_db_sync($logs);
|
||||
}
|
||||
|
||||
if (!cloak_visitor_log_async_enabled()) {
|
||||
@@ -284,6 +336,56 @@ if (!function_exists('cloak_write_log_db')) {
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('cloak_write_log_db')) {
|
||||
function cloak_write_log_db($logs)
|
||||
{
|
||||
$logs = cloak_visitor_log_enrich($logs);
|
||||
$logs = VisitorVisitContext::normalizeLogsForDb($logs);
|
||||
|
||||
if (VisitorVisitContext::shouldPersistAsUpdate()) {
|
||||
return cloak_update_log_db(VisitorVisitContext::currentLogId(), $logs);
|
||||
}
|
||||
|
||||
VisitorVisitContext::prepareForNewInsert();
|
||||
|
||||
if (!empty($GLOBALS['__cloak_force_sync_log'])) {
|
||||
if (($logs['result'] ?? '') === 'wait') {
|
||||
$logId = cloak_write_log_db_wait_sync($logs);
|
||||
if ($logId !== null) {
|
||||
VisitorVisitContext::bindLogId($logId);
|
||||
}
|
||||
return $logId;
|
||||
}
|
||||
$logId = cloak_write_log_db_sync($logs);
|
||||
if ($logId !== null) {
|
||||
VisitorVisitContext::bindLogId($logId);
|
||||
if (VisitorVisitContext::shouldFinalizeOnResult($logs['result'] ?? '')) {
|
||||
VisitorVisitContext::finalizeVisit($logs['result'] ?? '');
|
||||
}
|
||||
}
|
||||
return $logId;
|
||||
}
|
||||
|
||||
if (defined('WRITE_LOG') && WRITE_LOG == '1') {
|
||||
$my_url = $_SERVER['PHP_SELF'] ?? '';
|
||||
$my_file_name = substr($my_url, strrpos($my_url, '/') + 1);
|
||||
$config_name = str_replace('.php', '', $my_file_name);
|
||||
$fp = fopen(dirname(__DIR__, 2) . '/jump.txt', 'a+');
|
||||
fwrite($fp, date('Y-m-d H:i:s') . ' | 写入数据库 ' . ' | ' . ($_SESSION['check_result'] ?? '') . ' | ' . $config_name . ' | async=' . (cloak_visitor_log_async_enabled() ? '1' : '0') . "\n");
|
||||
fclose($fp);
|
||||
}
|
||||
|
||||
$logId = cloak_write_log_db_insert($logs);
|
||||
if ($logId !== null) {
|
||||
VisitorVisitContext::bindLogId($logId);
|
||||
if (VisitorVisitContext::shouldFinalizeOnResult($logs['result'] ?? '')) {
|
||||
VisitorVisitContext::finalizeVisit($logs['result'] ?? '');
|
||||
}
|
||||
}
|
||||
return $logId;
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('write_log_db')) {
|
||||
function write_log_db($logs)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user