-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyDBManager.php
More file actions
221 lines (181 loc) · 6.51 KB
/
KeyDBManager.php
File metadata and controls
221 lines (181 loc) · 6.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
declare(strict_types=1);
namespace Cleantalk\PHPAntiCrawler;
use RuntimeException;
final class KeyDBManager
{
public static function saveVisitor(RequestDto $request): bool
{
$redis = self::connectFromSettings();
$payload = json_encode([
'ip' => $request->ip,
'ua' => $request->uaName,
'created_at' => time(),
], JSON_UNESCAPED_SLASHES);
$result = $redis->set(
self::visitorKey($request->fingerprint),
$payload === false ? '{}' : $payload,
['nx', 'ex' => self::visitorTtl()]
);
return $result === true;
}
public static function updateLastSeen(RequestDto $request): void
{
$redis = self::connectFromSettings();
$key = self::visitorKey($request->fingerprint);
if ((int)$redis->exists($key) !== 1) {
return;
}
$redis->expire($key, self::visitorTtl());
}
public static function storeRequest(RequestDto $request, ResultDto $result): void
{
$payload = json_encode([
'ip' => $request->ip,
'fingerprint' => $request->fingerprint,
'blocked' => $result->goodRequest ? 0 : 1,
'timestamp_unixtime' => time(),
'ua_name' => $request->uaName,
'ua_id' => $request->uaId,
'url' => $request->url,
'request_status' => $result->status,
], JSON_UNESCAPED_SLASHES);
self::connectFromSettings()->rPush(self::pendingKey(), $payload === false ? '{}' : $payload);
}
public static function countPendingRequests(): int
{
$redis = self::connectFromSettings();
return (int)$redis->lLen(self::pendingKey()) + (int)$redis->lLen(self::processingKey());
}
public static function uploadRequestsToDB(string $apiKey): void
{
$data = self::getSyncPayloadRows();
if ($data === []) {
return;
}
try {
if (LogsSender::sendDataQuery($apiKey, $data) === false) {
throw new RuntimeException('failed to upload request logs');
}
} catch (\Throwable $e) {
self::handleSyncFailure();
throw $e;
}
self::markSynced();
}
/**
* @return array<int, array<int, mixed>>
*/
private static function getSyncPayloadRows(): array
{
$redis = self::connectFromSettings();
$pendingKey = self::pendingKey();
$processingKey = self::processingKey();
if ((int)$redis->lLen($processingKey) === 0 && (int)$redis->exists($pendingKey) === 1) {
$redis->rename($pendingKey, $processingKey);
}
$entries = $redis->lRange($processingKey, 0, -1);
if ($entries === [] || $entries === false) {
return [];
}
$aggregated = [];
foreach ($entries as $entry) {
$row = json_decode($entry, true);
if (is_array($row) === false) {
continue;
}
$fingerprint = (string)($row['fingerprint'] ?? '');
$status = (string)($row['request_status'] ?? '');
$groupKey = $fingerprint . "\x1f" . $status;
$timestamp = (int)($row['timestamp_unixtime'] ?? 0);
if (!isset($aggregated[$groupKey])) {
$aggregated[$groupKey] = [
'ip' => (string)($row['ip'] ?? ''),
'total_requests' => 0,
'total_good' => 0,
'last_request' => $timestamp,
'request_status' => $status,
'ua_name' => (string)($row['ua_name'] ?? ''),
'ua_id' => (int)($row['ua_id'] ?? 0),
'first_visited_url' => (string)($row['url'] ?? ''),
'last_visited_url' => (string)($row['url'] ?? ''),
];
}
$aggregated[$groupKey]['total_requests']++;
$aggregated[$groupKey]['total_good'] += ((int)($row['blocked'] ?? 0) === 0) ? 1 : 0;
if ($timestamp >= $aggregated[$groupKey]['last_request']) {
$aggregated[$groupKey]['last_request'] = $timestamp;
$aggregated[$groupKey]['last_visited_url'] = (string)($row['url'] ?? '');
}
}
$data = [];
foreach ($aggregated as $row) {
$data[] = [
$row['ip'],
$row['total_requests'],
$row['total_good'],
$row['last_request'],
$row['request_status'],
$row['ua_name'],
$row['ua_id'],
['fu' => $row['first_visited_url'], 'lu' => $row['last_visited_url']],
];
}
return $data;
}
public static function handleSyncFailure(): void
{
}
public static function markSynced(): void
{
self::connectFromSettings()->del(self::processingKey());
}
/**
* @return \Redis
*/
private static function connectFromSettings()
{
if (class_exists(\Redis::class) === false) {
throw new RuntimeException(
'keydb request storage requires the php redis extension (`ext-redis`)'
);
}
$redis = new \Redis();
$connected = $redis->connect(
Settings::$keyDbHost,
Settings::$keyDbPort,
Settings::$keyDbTimeout
);
if ($connected === false) {
throw new RuntimeException('failed to connect to keydb');
}
if (Settings::$keyDbPassword !== '' && $redis->auth(Settings::$keyDbPassword) === false) {
throw new RuntimeException('keydb authentication failed');
}
if ($redis->select(Settings::$keyDbDatabase) === false) {
throw new RuntimeException('failed to select keydb database');
}
return $redis;
}
private static function pendingKey(): string
{
return self::prefix() . ':requests:pending';
}
private static function processingKey(): string
{
return self::prefix() . ':requests:processing';
}
private static function prefix(): string
{
$normalizedPrefix = trim(Settings::$keyDbPrefix);
return $normalizedPrefix === '' ? 'anticrawler' : $normalizedPrefix;
}
private static function visitorKey(string $fingerprint): string
{
return self::prefix() . ':visitors:' . $fingerprint;
}
private static function visitorTtl(): int
{
return max(1, (int)Settings::$visitorForgetAfter);
}
}