日志升级与缓存修复最终版
This commit is contained in:
Regular → Executable
+47
-43
@@ -6,7 +6,11 @@ require_once dirname(__DIR__) . '/DbHelper.php';
|
||||
require_once __DIR__ . '/FpUrlHelper.php';
|
||||
require_once __DIR__ . '/VisitorApiAudit.php';
|
||||
require_once __DIR__ . '/RiskSecondaryGate.php';
|
||||
require_once __DIR__ . '/CloakJudgmentCache.php';
|
||||
require_once __DIR__ . '/RiskSecondaryCache.php';
|
||||
require_once __DIR__ . '/VisitorVisitContext.php';
|
||||
require_once __DIR__ . '/ReasonHelper.php';
|
||||
require_once __DIR__ . '/VisitorRepository.php';
|
||||
require_once __DIR__ . '/VisitorLogSchema.php';
|
||||
require_once __DIR__ . '/ClientIpResolver.php';
|
||||
|
||||
@@ -46,6 +50,9 @@ class FCheckHandler
|
||||
}
|
||||
$config_name = ConfigLoader::loadByHost($fp_host);
|
||||
$log_id = (int) ($fingerprint_info['log_id'] ?? 0);
|
||||
if ($log_id <= 0) {
|
||||
$log_id = VisitorVisitContext::currentLogId();
|
||||
}
|
||||
|
||||
if (!RiskSecondaryGate::isEnabled()) {
|
||||
$_SESSION['visit_to_3'] = 3;
|
||||
@@ -62,9 +69,9 @@ class FCheckHandler
|
||||
if ($log_id > 0) {
|
||||
self::updateLog($log_id, [
|
||||
'result' => 'true',
|
||||
'reason' => '二次风控已关闭',
|
||||
'reason' => '',
|
||||
'fp_url' => $response['link'],
|
||||
], $fingerprint_info, null, null);
|
||||
], $fingerprint_info, null, null, false);
|
||||
}
|
||||
|
||||
return array_merge(['ok' => true, 'status' => 200, 'fingerprint_info' => $fingerprint_info], $response);
|
||||
@@ -163,7 +170,7 @@ class FCheckHandler
|
||||
self::persistCache($update_log, $clientIp);
|
||||
|
||||
if ($log_id > 0) {
|
||||
self::updateLog($log_id, $update_log, $fingerprint_info, $jsonData, $return);
|
||||
self::updateLog($log_id, $update_log, $fingerprint_info, $jsonData, $return, true);
|
||||
}
|
||||
|
||||
return array_merge([
|
||||
@@ -186,7 +193,7 @@ class FCheckHandler
|
||||
|
||||
$update_log = [
|
||||
'result' => $response['result'] ? 'true' : 'false',
|
||||
'reason' => $response['reason'],
|
||||
'reason' => cloak_cache_hit_log_reason($update_log['result']),
|
||||
'fp_url' => trim((string) ($response['link'] ?? '')),
|
||||
];
|
||||
if ($update_log['fp_url'] === '') {
|
||||
@@ -196,14 +203,7 @@ class FCheckHandler
|
||||
}
|
||||
|
||||
if ($log_id > 0) {
|
||||
$cacheMarker = [
|
||||
'cached' => true,
|
||||
'result' => $update_log['result'],
|
||||
'reason' => $cached['reason'] ?? '',
|
||||
'api_reason' => $cached['reason'] ?? '',
|
||||
'cached_at' => $cached['cached_at'] ?? null,
|
||||
];
|
||||
self::updateLog($log_id, $update_log, $fingerprint_info, null, $cacheMarker);
|
||||
self::updateLog($log_id, $update_log, $fingerprint_info, null, null, false);
|
||||
}
|
||||
|
||||
return array_merge([
|
||||
@@ -222,7 +222,10 @@ class FCheckHandler
|
||||
{
|
||||
$update_log = [];
|
||||
$update_log['result'] = !empty($response['result']) ? 'true' : 'false';
|
||||
$update_log['reason'] = $response['reason'] ?? '';
|
||||
$update_log['reason'] = cloak_normalize_log_reason(
|
||||
$update_log['result'],
|
||||
$response['reason'] ?? ''
|
||||
);
|
||||
$update_log['fp_url'] = trim((string) ($response['link'] ?? ''));
|
||||
if ($update_log['fp_url'] === '') {
|
||||
$update_log['fp_url'] = $update_log['result'] === 'true'
|
||||
@@ -239,11 +242,12 @@ class FCheckHandler
|
||||
*/
|
||||
private static function persistCache(array $update_log, $clientIp)
|
||||
{
|
||||
RiskSecondaryCache::store([
|
||||
'ip' => $clientIp,
|
||||
'result' => $update_log['result'],
|
||||
'reason' => $update_log['reason'],
|
||||
'fp_url' => $update_log['fp_url'],
|
||||
CloakJudgmentCache::storeFromByApiRisk([
|
||||
'ip' => $clientIp,
|
||||
'country' => (string) ($_SESSION['gcu_country'] ?? ''),
|
||||
'result' => $update_log['result'],
|
||||
'reason' => $update_log['reason'],
|
||||
'fp_url' => $update_log['fp_url'],
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -253,36 +257,36 @@ class FCheckHandler
|
||||
* @param array $fingerprint_info
|
||||
* @param array|null $apiRequest
|
||||
* @param array|null $apiResponse
|
||||
* @param bool $includeFpAndApi 缓存路径不写 fp_hdata / api_risk_*
|
||||
*/
|
||||
public static function updateLog($log_id, array $update_log, array $fingerprint_info, $apiRequest, $apiResponse)
|
||||
public static function updateLog($log_id, array $update_log, array $fingerprint_info, $apiRequest, $apiResponse, $includeFpAndApi = true)
|
||||
{
|
||||
$conn = @new mysqli('localhost', DB_USERNAME, DB_PASSWORD, DB_NAME);
|
||||
if (!$conn || $conn->connect_error) {
|
||||
return false;
|
||||
}
|
||||
VisitorLogSchema::ensureColumns($conn);
|
||||
$payload = [
|
||||
'result' => (string) ($update_log['result'] ?? ''),
|
||||
'reason' => cloak_normalize_log_reason(
|
||||
$update_log['result'] ?? '',
|
||||
$update_log['reason'] ?? ''
|
||||
),
|
||||
'fp_url' => (string) ($update_log['fp_url'] ?? ''),
|
||||
];
|
||||
|
||||
$resultEsc = cloak_db_escape($conn, $update_log['result'] ?? '');
|
||||
$reasonEsc = cloak_db_escape($conn, strip_tags(cloak_db_string($update_log['reason'] ?? '')));
|
||||
$fpUrlEsc = cloak_db_escape($conn, strip_tags(cloak_db_string($update_log['fp_url'] ?? '')));
|
||||
$fpHdataJson = json_encode($fingerprint_info, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
if ($fpHdataJson === false) {
|
||||
$fpHdataJson = '';
|
||||
}
|
||||
$fpHdataEsc = cloak_db_escape($conn, $fpHdataJson);
|
||||
$apiRiskReqEsc = cloak_db_escape($conn, $apiRequest !== null ? VisitorApiAudit::encode($apiRequest) : '');
|
||||
$apiRiskRespEsc = cloak_db_escape($conn, $apiResponse !== null ? VisitorApiAudit::encode($apiResponse) : '');
|
||||
$sql = "UPDATE `visitor_logs` SET `result`='{$resultEsc}',`reason`='{$reasonEsc}',`fp_url`='{$fpUrlEsc}',`fp_hdata`='{$fpHdataEsc}',`api_risk_request`='{$apiRiskReqEsc}',`api_risk_response`='{$apiRiskRespEsc}' WHERE `id`='" . (int) $log_id . "'";
|
||||
|
||||
$ok = $conn->query($sql) === true;
|
||||
if (!$ok) {
|
||||
$handle = @fopen(dirname(__DIR__, 2) . '/err.txt', 'a+');
|
||||
if ($handle) {
|
||||
fwrite($handle, date('Y-m-d H:i:s') . ' | FCheckHandler update: ' . $conn->error . "\n");
|
||||
fclose($handle);
|
||||
if ($includeFpAndApi) {
|
||||
$fpHdataJson = json_encode($fingerprint_info, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
if ($fpHdataJson === false) {
|
||||
$fpHdataJson = '';
|
||||
}
|
||||
$payload['fp_hdata'] = $fpHdataJson;
|
||||
$payload['api_risk_request'] = $apiRequest !== null ? VisitorApiAudit::encode($apiRequest) : '';
|
||||
$payload['api_risk_response'] = $apiResponse !== null ? VisitorApiAudit::encode($apiResponse) : '';
|
||||
}
|
||||
$conn->close();
|
||||
|
||||
$hadForceSync = !empty($GLOBALS['__cloak_force_sync_log']);
|
||||
$GLOBALS['__cloak_force_sync_log'] = true;
|
||||
$ok = cloak_update_log_db((int) $log_id, $payload, ['skip_enrich' => true]) !== false;
|
||||
if (!$hadForceSync) {
|
||||
unset($GLOBALS['__cloak_force_sync_log']);
|
||||
}
|
||||
|
||||
return $ok;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user