BruteforceAttempts.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 OC\Core\Command\Security;
  8. use OC\Core\Command\Base;
  9. use OCP\Security\Bruteforce\IThrottler;
  10. use Symfony\Component\Console\Input\InputArgument;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. class BruteforceAttempts extends Base {
  14. public function __construct(
  15. protected IThrottler $throttler,
  16. ) {
  17. parent::__construct();
  18. }
  19. protected function configure(): void {
  20. parent::configure();
  21. $this
  22. ->setName('security:bruteforce:attempts')
  23. ->setDescription('Show bruteforce attempts status for a given IP address')
  24. ->addArgument(
  25. 'ipaddress',
  26. InputArgument::REQUIRED,
  27. 'IP address for which the attempts status is to be shown',
  28. )
  29. ->addArgument(
  30. 'action',
  31. InputArgument::OPTIONAL,
  32. 'Only count attempts for the given action',
  33. )
  34. ;
  35. }
  36. protected function execute(InputInterface $input, OutputInterface $output): int {
  37. $ip = $input->getArgument('ipaddress');
  38. if (!filter_var($ip, FILTER_VALIDATE_IP)) {
  39. $output->writeln('<error>"' . $ip . '" is not a valid IP address</error>');
  40. return 1;
  41. }
  42. $data = [
  43. 'bypass-listed' => $this->throttler->isBypassListed($ip),
  44. 'attempts' => $this->throttler->getAttempts(
  45. $ip,
  46. (string) $input->getArgument('action'),
  47. ),
  48. 'delay' => $this->throttler->getDelay(
  49. $ip,
  50. (string) $input->getArgument('action'),
  51. ),
  52. ];
  53. $this->writeArrayInOutputFormat($input, $output, $data);
  54. return 0;
  55. }
  56. }