-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathLoginStats.php
More file actions
112 lines (101 loc) · 2.72 KB
/
LoginStats.php
File metadata and controls
112 lines (101 loc) · 2.72 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
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\ServerInfo;
use OCP\IConfig;
use OCP\IDBConnection;
class LoginStats {
public function __construct(
private IConfig $config,
private IDBConnection $db,
) {
}
/**
* @return array{
* bruteforceAttempts24h: int,
* bruteforceAttempts1h: int,
* bruteforceTotal: int,
* topIps: list<array{ip: string, count: int}>,
* available: bool,
* reason?: string
* }
*/
public function getStats(): array {
if ($this->usesRedisBruteforceBackend()) {
return [
'bruteforceAttempts24h' => 0,
'bruteforceAttempts1h' => 0,
'bruteforceTotal' => 0,
'topIps' => [],
'available' => false,
'reason' => 'redis_backend',
];
}
try {
$total = $this->countAttempts();
} catch (\Throwable) {
return [
'bruteforceAttempts24h' => 0,
'bruteforceAttempts1h' => 0,
'bruteforceTotal' => 0,
'topIps' => [],
'available' => false,
];
}
return [
'bruteforceAttempts24h' => $this->countAttempts(time() - 86400),
'bruteforceAttempts1h' => $this->countAttempts(time() - 3600),
'bruteforceTotal' => $total,
'topIps' => $this->topIps(),
'available' => true,
];
}
private function usesRedisBruteforceBackend(): bool {
if ($this->config->getSystemValueBool('auth.bruteforce.protection.force.database', false)) {
return false;
}
$distributed = ltrim($this->config->getSystemValueString('memcache.distributed', ''), '\\');
return $distributed === 'OC\Memcache\Redis';
}
private function countAttempts(?int $sinceTimestamp = null): int {
$qb = $this->db->getQueryBuilder();
$qb->select($qb->func()->count('id'))->from('bruteforce_attempts');
if ($sinceTimestamp !== null) {
$qb->where($qb->expr()->gte('occurred', $qb->createNamedParameter($sinceTimestamp)));
}
$result = $qb->executeQuery();
$count = (int)$result->fetchOne();
$result->closeCursor();
return $count;
}
/**
* @return list<array{ip: string, count: int}>
*/
private function topIps(int $limit = 5): array {
$qb = $this->db->getQueryBuilder();
$qb->select('ip')
->selectAlias($qb->func()->count('id'), 'count')
->from('bruteforce_attempts')
->where($qb->expr()->gte('occurred', $qb->createNamedParameter(time() - 86400)))
->groupBy('ip')
->orderBy('count', 'DESC')
->setMaxResults($limit);
try {
$result = $qb->executeQuery();
} catch (\Throwable) {
return [];
}
$out = [];
while (($row = $result->fetch()) !== false) {
$out[] = [
'ip' => (string)($row['ip'] ?? ''),
'count' => (int)($row['count'] ?? 0),
];
}
$result->closeCursor();
return $out;
}
}