BruteForceThrottler.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Settings\SetupChecks;
  8. use OCP\IL10N;
  9. use OCP\IRequest;
  10. use OCP\IURLGenerator;
  11. use OCP\Security\Bruteforce\IThrottler;
  12. use OCP\SetupCheck\ISetupCheck;
  13. use OCP\SetupCheck\SetupResult;
  14. class BruteForceThrottler implements ISetupCheck {
  15. public function __construct(
  16. private IL10N $l10n,
  17. private IURLGenerator $urlGenerator,
  18. private IRequest $request,
  19. private IThrottler $throttler,
  20. ) {
  21. }
  22. public function getCategory(): string {
  23. return 'system';
  24. }
  25. public function getName(): string {
  26. return $this->l10n->t('Brute-force Throttle');
  27. }
  28. public function run(): SetupResult {
  29. $address = $this->request->getRemoteAddress();
  30. if ($address === '') {
  31. if (\OC::$CLI) {
  32. /* We were called from CLI */
  33. return SetupResult::info($this->l10n->t('Your remote address could not be determined.'));
  34. } else {
  35. /* Should never happen */
  36. return SetupResult::error($this->l10n->t('Your remote address could not be determined.'));
  37. }
  38. } elseif ($this->throttler->showBruteforceWarning($address)) {
  39. return SetupResult::error(
  40. $this->l10n->t('Your remote address was identified as "%s" and is brute-force throttled at the moment slowing down the performance of various requests. If the remote address is not your address this can be an indication that a proxy is not configured correctly.', [$address]),
  41. $this->urlGenerator->linkToDocs('admin-reverse-proxy')
  42. );
  43. } else {
  44. return SetupResult::success(
  45. $this->l10n->t('Your remote address "%s" is not brute-force throttled.', [$address])
  46. );
  47. }
  48. }
  49. }