From b3e38d9bdfc762a5157015fe7757ca0272205396 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn?= Date: Thu, 6 Aug 2020 20:51:34 +0200 Subject: [PATCH] adding letsencrypt --- app/Commands/Certs.php | 126 ------------------ app/Commands/LetsEncryptAddCommand.php | 40 ++++++ ...mand.php => LetsEncryptInstallCommand.php} | 24 ++-- app/Commands/LetsEncryptRemoveCommand.php | 58 ++++++++ 4 files changed, 108 insertions(+), 140 deletions(-) delete mode 100644 app/Commands/Certs.php create mode 100644 app/Commands/LetsEncryptAddCommand.php rename app/Commands/{CertsInstallCommand.php => LetsEncryptInstallCommand.php} (70%) create mode 100644 app/Commands/LetsEncryptRemoveCommand.php diff --git a/app/Commands/Certs.php b/app/Commands/Certs.php deleted file mode 100644 index 916b7c5..0000000 --- a/app/Commands/Certs.php +++ /dev/null @@ -1,126 +0,0 @@ -menu('Nginx Let\'s Encrypt'); - - //$main - // ->addLineBreak('-') - // ->open(); - - - - /*** - - $itemCallable = function(CliMenu $menu) { - - // getting sites-available - $sitesAvailable = scandir('/etc/nginx/sites-available'); - - // getting sites-endabled - $sitesEnabled = scandir('/etc/nginx/sites-enabled'); - - foreach($sitesAvailable as $site) { - if ($site !== '.' && $site !== '..') { - - // getting title - $title = str_replace('.conf', '', $site); - - $checkbox = new CheckboxItem('enabled', function(CliMenu $menu) use ($site, $title) { - - // check status - if ($menu->getSelectedItem()->getChecked()) { - symlink('/etc/nginx/sites-available/'.$site, '/etc/nginx/sites-enabled/'.$site); - $status = 'enabled'; - } else { - unlink('/etc/nginx/sites-enabled/'.$site); - $status = 'disabled'; - } - - // starting - exec('service nginx restart'); - exec('service nginx status', $output); - - if (strpos(implode(' ', $output), 'active') !== false) { - $menu->confirm($title.' is '.$status.'!')->display('OK!'); - } else { - $menu->confirm('Error! Something not working!')->display('OK!'); - } - - }); - - // adding checkbox - if (in_array($site, $sitesEnabled)) { - $checkbox->setChecked(true); - } - - $menuMain - ->addSubmenu($title, function(CliMenuBuilder $builder) use ($site, $title, $checkbox) { - $builder->setTitle("Nginx > $title") - ->addItem('edit', function(CliMenu $menu) use ($site, $title) { - system('nano /etc/nginx/sites-available/'.$site.' > `tty`'); - }) - ->addItem('delete', function(CliMenu $menu) use ($site) { - if (file_exists('/etc/nginx/sites-enabled/'.$site)) { - $menu->confirm('Error! Please disable '.$title.' first!')->display('OK!'); - } else { - unlink('/etc/nginx/sites-available/'.$site); - $menu->confirm("$site is deleted!")->display('OK!'); - } - }) - ->addLineBreak('-') - ->addMenuItem($checkbox) - ->addLineBreak('-'); - }); - - } - } - };*/ - } -} diff --git a/app/Commands/LetsEncryptAddCommand.php b/app/Commands/LetsEncryptAddCommand.php new file mode 100644 index 0000000..1837423 --- /dev/null +++ b/app/Commands/LetsEncryptAddCommand.php @@ -0,0 +1,40 @@ +argument('domain'); + exec('certbot --nginx '.implode('-d ', $domains), $output); + } +} diff --git a/app/Commands/CertsInstallCommand.php b/app/Commands/LetsEncryptInstallCommand.php similarity index 70% rename from app/Commands/CertsInstallCommand.php rename to app/Commands/LetsEncryptInstallCommand.php index 42c3059..812d2ab 100644 --- a/app/Commands/CertsInstallCommand.php +++ b/app/Commands/LetsEncryptInstallCommand.php @@ -4,12 +4,12 @@ namespace App\Commands; use Illuminate\Console\Scheduling\Schedule; use LaravelZero\Framework\Commands\Command; -use Illuminate\Support\Facades\File; - -use App\Facades\Install; - -use Log; +/** + * + * + * + */ class LetsEncryptInstallCommand extends Command { /** @@ -24,7 +24,7 @@ class LetsEncryptInstallCommand extends Command * * @var string */ - protected $description = 'Install LetsEncrypt'; + protected $description = 'Install LetsEncrypt for nginx'; /** * Execute the console command. @@ -33,7 +33,7 @@ class LetsEncryptInstallCommand extends Command */ public function handle() { - $this->info('LetsEncrypt install...'); + $this->info('LetsEncrypt installing...'); exec('apt update 2>&1'); exec('apt-get install software-properties-common 2>&1'); @@ -43,13 +43,9 @@ class LetsEncryptInstallCommand extends Command exec('apt-get install certbot python3-certbot-nginx 2>&1'); if (Install::isReady('certbot')) { - - // get status of nginx - exec('nginx -v 2>&1', $output); - $status = "$output[0] installed"; - - $this->info($status); - Log::info($status); + $this->info("Success!"); + } else { + $this->error("Failed! Please check log-file!"); } } } diff --git a/app/Commands/LetsEncryptRemoveCommand.php b/app/Commands/LetsEncryptRemoveCommand.php new file mode 100644 index 0000000..5974212 --- /dev/null +++ b/app/Commands/LetsEncryptRemoveCommand.php @@ -0,0 +1,58 @@ +argument('domain'); + + foreach($domains as $domain) { + + $archive = self::CERT_DIR.'archive/'.$domain; + $live = self::CERT_DIR.'live/'.$domain; + $renewal = self::CERT_DIR.'renewal/'.$domain; + + rmdir($archiv); + rmdir($live); + rmdir($renewal); + + if (file_exists($archiv) && file_exists($live) && file_exists($renewal)) { + $this->info('Certifikate for '.$domain.' ...deleted!'); + } else { + $this->error('Certifikate for '.$domain.' ...not deleted!'); + } + } + } +}