-
Notifications
You must be signed in to change notification settings - Fork 64
feat(services): add log and login/session services #982
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
karlitschek
wants to merge
4
commits into
master
Choose a base branch
from
split/05-services-logs-sessions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5fbd0c4
feat(services): add log and login/session services
karlitschek b48f36c
feat(settings): wire log and login/session services into admin settings
ChristophWurst 2a370ea
fixup! feat(services): add log and login/session services
ChristophWurst 4e880d0
fixup! feat(services): add log and login/session services
ChristophWurst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| <?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\IDBConnection; | ||
|
|
||
| class ActiveConnections { | ||
| public function __construct( | ||
| private IDBConnection $db, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * Approximate active sessions/connections by counting auth tokens | ||
| * with recent activity. Each token corresponds to one client (browser | ||
| * tab, mobile app, desktop client, etc.). | ||
| * | ||
| * @return array{ | ||
| * last5min: int, | ||
| * last1h: int, | ||
| * totalTokens: int, | ||
| * byType: array<string, int> | ||
| * } | ||
| */ | ||
| public function getActiveConnections(): array { | ||
| try { | ||
| return [ | ||
| 'last5min' => $this->countSince(time() - 300), | ||
| 'last1h' => $this->countSince(time() - 3600), | ||
| 'totalTokens' => $this->countTotal(), | ||
| 'byType' => $this->byType(), | ||
| ]; | ||
| } catch (\Throwable) { | ||
| return ['last5min' => 0, 'last1h' => 0, 'totalTokens' => 0, 'byType' => []]; | ||
| } | ||
| } | ||
|
|
||
| private function countSince(int $ts): int { | ||
| $qb = $this->db->getQueryBuilder(); | ||
| $qb->select($qb->func()->count('id')) | ||
| ->from('authtoken') | ||
| ->where($qb->expr()->gte('last_activity', $qb->createNamedParameter($ts))); | ||
| $result = $qb->executeQuery(); | ||
| $count = (int)$result->fetchOne(); | ||
| $result->closeCursor(); | ||
| return $count; | ||
| } | ||
|
|
||
| private function countTotal(): int { | ||
| $qb = $this->db->getQueryBuilder(); | ||
| $qb->select($qb->func()->count('id'))->from('authtoken'); | ||
| $result = $qb->executeQuery(); | ||
| $count = (int)$result->fetchOne(); | ||
| $result->closeCursor(); | ||
| return $count; | ||
| } | ||
|
|
||
| /** | ||
| * @return array<string, int> | ||
| */ | ||
| private function byType(): array { | ||
| $qb = $this->db->getQueryBuilder(); | ||
| $qb->select('type') | ||
| ->selectAlias($qb->func()->count('id'), 'count') | ||
| ->from('authtoken') | ||
| ->where($qb->expr()->gte('last_activity', $qb->createNamedParameter(time() - 3600))) | ||
| ->groupBy('type'); | ||
| $result = $qb->executeQuery(); | ||
| $out = ['session' => 0, 'permanent' => 0]; | ||
| while (($row = $result->fetch()) !== false) { | ||
| $type = (int)($row['type'] ?? 0) === 0 ? 'session' : 'permanent'; | ||
| $out[$type] = (int)($row['count'] ?? 0); | ||
| } | ||
| $result->closeCursor(); | ||
| return $out; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| <?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\App\IAppManager; | ||
| use OCP\IDBConnection; | ||
|
|
||
| class ActivityRate { | ||
| public function __construct( | ||
| private IAppManager $appManager, | ||
| private IDBConnection $db, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * Counts user-facing activity events from the activity app over | ||
| * recent time windows. Useful as a "is anything happening on the | ||
| * server right now" signal. | ||
| * | ||
| * @return array{ | ||
| * installed: bool, | ||
| * last1h: int, | ||
| * last24h: int, | ||
| * last7d: int, | ||
| * topActions: list<array{action: string, count: int}> | ||
| * } | ||
| */ | ||
| public function getActivityRate(): array { | ||
| if (!$this->appManager->isInstalled('activity')) { | ||
| return ['installed' => false, 'last1h' => 0, 'last24h' => 0, 'last7d' => 0, 'topActions' => []]; | ||
| } | ||
|
|
||
| try { | ||
| return [ | ||
| 'installed' => true, | ||
| 'last1h' => $this->countSince(time() - 3600), | ||
| 'last24h' => $this->countSince(time() - 86400), | ||
| 'last7d' => $this->countSince(time() - 7 * 86400), | ||
| 'topActions' => $this->topActions(), | ||
| ]; | ||
| } catch (\Throwable) { | ||
| return ['installed' => true, 'last1h' => 0, 'last24h' => 0, 'last7d' => 0, 'topActions' => []]; | ||
| } | ||
| } | ||
|
|
||
| private function countSince(int $ts): int { | ||
| $qb = $this->db->getQueryBuilder(); | ||
| $qb->select($qb->func()->count('activity_id')) | ||
| ->from('activity') | ||
| ->where($qb->expr()->gte('timestamp', $qb->createNamedParameter($ts))); | ||
| $result = $qb->executeQuery(); | ||
| $count = (int)$result->fetchOne(); | ||
| $result->closeCursor(); | ||
| return $count; | ||
| } | ||
|
|
||
| /** | ||
| * @return list<array{action: string, count: int}> | ||
| */ | ||
| private function topActions(int $limit = 5): array { | ||
| $qb = $this->db->getQueryBuilder(); | ||
| $qb->select('subjectparams', 'type') | ||
| ->selectAlias($qb->func()->count('activity_id'), 'count') | ||
| ->from('activity') | ||
| ->where($qb->expr()->gte('timestamp', $qb->createNamedParameter(time() - 86400))) | ||
| ->groupBy('type', 'subjectparams') | ||
|
Comment on lines
+69
to
+73
|
||
| ->orderBy('count', 'DESC') | ||
| ->setMaxResults($limit); | ||
| $result = $qb->executeQuery(); | ||
| $out = []; | ||
| while (($row = $result->fetch()) !== false) { | ||
| $out[] = [ | ||
| 'action' => (string)($row['type'] ?? 'unknown'), | ||
| 'count' => (int)($row['count'] ?? 0), | ||
| ]; | ||
| } | ||
| $result->closeCursor(); | ||
| return $out; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| <?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\Log\IFileBased; | ||
| use OCP\Log\ILogFactory; | ||
|
|
||
| class LogTailReader { | ||
| public function __construct( | ||
| private IConfig $config, | ||
| private ILogFactory $logFactory, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * @return array{ | ||
| * entries: list<array{time: string, level: int, app: string, message: string}>, | ||
| * available: bool, | ||
| * reason?: string | ||
| * } | ||
| */ | ||
| public function recentErrors(int $limit = 8, int $minLevel = 2): array { | ||
| $logType = $this->config->getSystemValue('log_type', 'file'); | ||
|
ChristophWurst marked this conversation as resolved.
|
||
| if ($logType !== 'file') { | ||
| return ['entries' => [], 'available' => false, 'reason' => 'log_type_not_file']; | ||
| } | ||
|
|
||
| $log = $this->logFactory->get('file'); | ||
| if (!($log instanceof IFileBased)) { | ||
| return ['entries' => [], 'available' => false, 'reason' => 'log_not_readable']; | ||
| } | ||
|
|
||
| $raw = $log->getEntries($limit * 10); | ||
| $collected = []; | ||
| foreach ($raw as $entry) { | ||
| if (count($collected) >= $limit) { | ||
| break; | ||
| } | ||
| $level = (int)($entry['level'] ?? 0); | ||
| if ($level < $minLevel) { | ||
| continue; | ||
| } | ||
| $collected[] = [ | ||
| 'time' => (string)($entry['time'] ?? ''), | ||
| 'level' => $level, | ||
| 'app' => (string)($entry['app'] ?? ''), | ||
| 'message' => $this->snippet((string)($entry['message'] ?? '')), | ||
| ]; | ||
| } | ||
|
|
||
| return ['entries' => $collected, 'available' => true]; | ||
| } | ||
|
|
||
| private function snippet(string $msg, int $max = 200): string { | ||
| $msg = trim($msg); | ||
| if (mb_strlen($msg) > $max) { | ||
| return mb_substr($msg, 0, $max - 1) . '…'; | ||
| } | ||
| return $msg; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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))); | ||
| } | ||
|
Comment on lines
+73
to
+78
|
||
| $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') | ||
|
ChristophWurst marked this conversation as resolved.
|
||
| ->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; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.