<?php
|
|
|
|
namespace App\Commands;
|
|
|
|
use Illuminate\Console\Scheduling\Schedule;
|
|
use LaravelZero\Framework\Commands\Command;
|
|
|
|
use PhpSchool\CliMenu\CliMenu;
|
|
use PhpSchool\CliMenu\MenuItem\CheckboxItem;
|
|
use PhpSchool\CliMenu\Builder\SplitItemBuilder;
|
|
|
|
use App\Facades\Menus\StylesFactory;
|
|
|
|
/**
|
|
* Manage Fail2ban Configuration
|
|
*
|
|
*
|
|
* @author Björn Hase, Tentakelfabrik
|
|
* @license http://opensource.org/licenses/MIT The MIT License
|
|
* @link https://gitea.tentakelfabrik.de/Tentakelfabrik/mcp
|
|
*
|
|
*/
|
|
class Fail2banManageCommand extends Command
|
|
{
|
|
// destination to jail
|
|
const DESTINATION_FAIL2BAN_JAIL_DIRECTORY = '/etc/fail2ban/jail.d';
|
|
|
|
// source to jail
|
|
const SOURCE_FAIL2BAN_JAIL_DIRECTORY = '/resources/fail2ban/jail.d';
|
|
|
|
// ignore files
|
|
const IGNORE_FILES = [
|
|
'.', '..', 'defaults-debian.conf'
|
|
];
|
|
|
|
// configuration
|
|
private $configuration = [];
|
|
|
|
// enabled
|
|
private $enabled = [];
|
|
|
|
/**
|
|
* The signature of the command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'fail2ban:manage';
|
|
|
|
/**
|
|
* The description of the command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'manage fail2ban configuration';
|
|
|
|
/**
|
|
*
|
|
* @param [type] $file [description]
|
|
* @return boolean [description]
|
|
*/
|
|
private function name($file)
|
|
{
|
|
return str_replace('.conf', '', $file);
|
|
}
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function handle()
|
|
{
|
|
foreach(scandir(self::DESTINATION_FAIL2BAN_JAIL_DIRECTORY) as $file) {
|
|
if (!in_array($file, self::IGNORE_FILES)) {
|
|
$this->enabled[] = $this->name($file);
|
|
}
|
|
}
|
|
|
|
foreach(scandir(base_path().self::SOURCE_FAIL2BAN_JAIL_DIRECTORY) as $file) {
|
|
if (!in_array($file, self::IGNORE_FILES)) {
|
|
$name = $this->name($file);
|
|
$this->configuration[$name] = in_array($name, $this->enabled);
|
|
}
|
|
}
|
|
|
|
// create menu
|
|
$builder = $this->menu('Fail2ban');
|
|
|
|
foreach($this->configuration as $name => $single) {
|
|
|
|
// create checkbox
|
|
$checkbox = new CheckboxItem($name, function(CliMenu $menu) use ($name) {
|
|
if ($this->configuration[$name] === true) {
|
|
$this->call('fail2ban:disable', [ 'configuration' => $name ]);
|
|
$menu->redraw();
|
|
$menu->confirm($name.' is disabled!')->display('OK!');
|
|
} else {
|
|
$this->call('fail2ban:enable', [ 'configuration' => $name ]);
|
|
$menu->redraw();
|
|
$menu->confirm($name.' is enabled!')->display('OK!');
|
|
}
|
|
|
|
// getting new value
|
|
$this->configuration[$name] = $menu->getSelectedItem()->getChecked();
|
|
$menu->redraw();
|
|
});
|
|
|
|
// set default value
|
|
if ($this->configuration[$name]) {
|
|
$checkbox->setChecked(true);
|
|
}
|
|
|
|
$builder->addMenuItem($checkbox);
|
|
}
|
|
|
|
// apperance
|
|
$builder = StylesFactory::setMenuStyles($builder);
|
|
$builder->addLineBreak('-');
|
|
|
|
$mainmenu = $builder->build();
|
|
$mainmenu->open();
|
|
}
|
|
}
|