OpenSource CLI-App to install and handle stuff related to Web-Server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Commands;
  3. use Illuminate\Console\Scheduling\Schedule;
  4. use LaravelZero\Framework\Commands\Command;
  5. use PhpSchool\CliMenu\CliMenu;
  6. use PhpSchool\CliMenu\MenuItem\CheckboxItem;
  7. use PhpSchool\CliMenu\Builder\SplitItemBuilder;
  8. use App\Facades\Menus\StylesFactory;
  9. /**
  10. * Manage Fail2ban Configuration
  11. *
  12. *
  13. * @author Björn Hase, Tentakelfabrik
  14. * @license http://opensource.org/licenses/MIT The MIT License
  15. * @link https://gitea.tentakelfabrik.de/Tentakelfabrik/mcp
  16. *
  17. */
  18. class Fail2banManageCommand extends Command
  19. {
  20. // destination to jail
  21. const DESTINATION_FAIL2BAN_JAIL_DIRECTORY = '/etc/fail2ban/jail.d';
  22. // source to jail
  23. const SOURCE_FAIL2BAN_JAIL_DIRECTORY = '/resources/fail2ban/jail.d';
  24. // ignore files
  25. const IGNORE_FILES = [
  26. '.', '..', 'defaults-debian.conf'
  27. ];
  28. // configuration
  29. private $configuration = [];
  30. // enabled
  31. private $enabled = [];
  32. /**
  33. * The signature of the command.
  34. *
  35. * @var string
  36. */
  37. protected $signature = 'fail2ban:manage';
  38. /**
  39. * The description of the command.
  40. *
  41. * @var string
  42. */
  43. protected $description = 'manage fail2ban configuration';
  44. /**
  45. *
  46. * @param [type] $file [description]
  47. * @return boolean [description]
  48. */
  49. private function name($file)
  50. {
  51. return str_replace('.conf', '', $file);
  52. }
  53. /**
  54. * Execute the console command.
  55. *
  56. * @return mixed
  57. */
  58. public function handle()
  59. {
  60. foreach(scandir(self::DESTINATION_FAIL2BAN_JAIL_DIRECTORY) as $file) {
  61. if (!in_array($file, self::IGNORE_FILES)) {
  62. $this->enabled[] = $this->name($file);
  63. }
  64. }
  65. foreach(scandir(base_path().self::SOURCE_FAIL2BAN_JAIL_DIRECTORY) as $file) {
  66. if (!in_array($file, self::IGNORE_FILES)) {
  67. $name = $this->name($file);
  68. $this->configuration[$name] = in_array($name, $this->enabled);
  69. }
  70. }
  71. // create menu
  72. $builder = $this->menu('Fail2ban');
  73. foreach($this->configuration as $name => $single) {
  74. // create checkbox
  75. $checkbox = new CheckboxItem($name, function(CliMenu $menu) use ($name) {
  76. if ($this->configuration[$name] === true) {
  77. $this->call('fail2ban:disable', [ 'configuration' => $name ]);
  78. $menu->redraw();
  79. $menu->confirm($name.' is disabled!')->display('OK!');
  80. } else {
  81. $this->call('fail2ban:enable', [ 'configuration' => $name ]);
  82. $menu->redraw();
  83. $menu->confirm($name.' is enabled!')->display('OK!');
  84. }
  85. // getting new value
  86. $this->configuration[$name] = $menu->getSelectedItem()->getChecked();
  87. $menu->redraw();
  88. });
  89. // set default value
  90. if ($this->configuration[$name]) {
  91. $checkbox->setChecked(true);
  92. }
  93. $builder->addMenuItem($checkbox);
  94. }
  95. // apperance
  96. $builder = StylesFactory::setMenuStyles($builder);
  97. $builder->addLineBreak('-');
  98. $mainmenu = $builder->build();
  99. $mainmenu->open();
  100. }
  101. }