This commit is contained in:
Bozhidar 2024-08-05 16:49:51 +03:00
parent 42f850fef0
commit d11a162f97
3 changed files with 101 additions and 125 deletions

View file

@ -0,0 +1,86 @@
<?php
namespace Modules\LetsEncrypt\Jobs;
use App\Models\DomainSslCertificate;
use App\Settings;
class LetsEncryptSecureDomain
{
public $domainId;
public function __construct($domainId)
{
$this->domainId = $domainId;
}
public function handle(): void
{
$findDomain = \App\Models\Domain::where('id', $this->domainId)->first();
if (! $findDomain) {
throw new \Exception('Domain not found');
}
$generalSettings = Settings::general();
$sslCertificateFilePath = '/etc/letsencrypt/live/'.$findDomain->domain.'/cert.pem';
$sslCertificateKeyFilePath = '/etc/letsencrypt/live/'.$findDomain->domain.'/privkey.pem';
$sslCertificateChainFilePath = '/etc/letsencrypt/live/'.$findDomain->domain.'/fullchain.pem';
$certbotHttpSecureCommand = view('letsencrypt::actions.certbot-http-secure-command', [
'domain' => $findDomain->domain,
'domainRoot' => $findDomain->domain_root,
'domainPublic' => $findDomain->domain_public,
'email' => $generalSettings['master_email'],
'country' => $generalSettings['master_country'],
'locality' => $generalSettings['master_locality'],
'organization' => $generalSettings['organization_name'],
])->render();
$exec = shell_exec($certbotHttpSecureCommand);
$validateCertificates = [];
if (! file_exists($sslCertificateFilePath)
|| ! file_exists($sslCertificateKeyFilePath)
|| ! file_exists($sslCertificateChainFilePath)) {
// Cant get all certificates
throw new \Exception('Cant get all certificates');
}
$sslCertificateFileContent = file_get_contents($sslCertificateFilePath);
$sslCertificateKeyFileContent = file_get_contents($sslCertificateKeyFilePath);
$sslCertificateChainFileContent = file_get_contents($sslCertificateChainFilePath);
if (! empty($sslCertificateChainFileContent)) {
$validateCertificates['certificate'] = $sslCertificateFileContent;
}
if (! empty($sslCertificateKeyFileContent)) {
$validateCertificates['private_key'] = $sslCertificateKeyFileContent;
}
if (! empty($sslCertificateChainFileContent)) {
$validateCertificates['certificate_chain'] = $sslCertificateChainFileContent;
}
if (count($validateCertificates) !== 3) {
// Cant get all certificates
throw new \Exception('Cant get all certificates');
}
$websiteSslCertificate = new DomainSslCertificate();
$websiteSslCertificate->domain = $findDomain->domain;
$websiteSslCertificate->certificate = $validateCertificates['certificate'];
$websiteSslCertificate->private_key = $validateCertificates['private_key'];
$websiteSslCertificate->certificate_chain = $validateCertificates['certificate_chain'];
$websiteSslCertificate->customer_id = $findDomain->customer_id;
$websiteSslCertificate->is_active = 1;
$websiteSslCertificate->is_wildcard = 0;
$websiteSslCertificate->is_auto_renew = 1;
$websiteSslCertificate->provider = 'letsencrypt';
$websiteSslCertificate->save();
$findDomain->configureVirtualHost(true);
}
}

View file

@ -43,70 +43,12 @@ class DomainIsCreatedListener
return; return;
} }
$generalSettings = Settings::general(); try {
$secureDomain = new \Modules\LetsEncrypt\Jobs\LetsEncryptSecureDomain($findDomain->id);
$acmeConfigYaml = view('letsencrypt::actions.acme-config-yaml', [ $secureDomain->handle();
'domain' => $event->model->domain, } catch (\Exception $e) {
'domainRoot' => $event->model->domain_root,
'domainPublic' => $event->model->domain_public,
'email' => $generalSettings['master_email'],
'country' => $generalSettings['master_country'],
'locality' => $generalSettings['master_locality'],
'organization' => $generalSettings['organization_name'],
])->render();
file_put_contents($event->model->domain_root.'/acme-config.yaml', $acmeConfigYaml);
$amePHPPharFile = base_path().'/Modules/LetsEncrypt/Actions/acmephp.phar';
$phyrePHP = ApiClient::getPhyrePHP();
$command = $phyrePHP.' '.$amePHPPharFile.' run '.$event->model->domain_root.'/acme-config.yaml';
$execSSL = shell_exec($command);
$validateCertificates = [];
$sslCertificateFilePath = '/root/.acmephp/master/certs/'.$event->model->domain.'/public/cert.pem';
$sslCertificateKeyFilePath = '/root/.acmephp/master/certs/'.$event->model->domain.'/private/key.private.pem';
$sslCertificateChainFilePath = '/root/.acmephp/master/certs/'.$event->model->domain.'/public/fullchain.pem';
if (! file_exists($sslCertificateFilePath)
|| ! file_exists($sslCertificateKeyFilePath)
|| ! file_exists($sslCertificateChainFilePath)) {
// Cant get all certificates
return; return;
} }
$sslCertificateFileContent = file_get_contents($sslCertificateFilePath);
$sslCertificateKeyFileContent = file_get_contents($sslCertificateKeyFilePath);
$sslCertificateChainFileContent = file_get_contents($sslCertificateChainFilePath);
if (! empty($sslCertificateChainFileContent)) {
$validateCertificates['certificate'] = $sslCertificateFileContent;
}
if (! empty($sslCertificateKeyFileContent)) {
$validateCertificates['private_key'] = $sslCertificateKeyFileContent;
}
if (! empty($sslCertificateChainFileContent)) {
$validateCertificates['certificate_chain'] = $sslCertificateChainFileContent;
}
if (count($validateCertificates) !== 3) {
// Cant get all certificates
return;
}
$websiteSslCertificate = new DomainSslCertificate();
$websiteSslCertificate->domain = $event->model->domain;
$websiteSslCertificate->certificate = $validateCertificates['certificate'];
$websiteSslCertificate->private_key = $validateCertificates['private_key'];
$websiteSslCertificate->certificate_chain = $validateCertificates['certificate_chain'];
$websiteSslCertificate->customer_id = $event->model->customer_id;
$websiteSslCertificate->is_active = 1;
$websiteSslCertificate->is_wildcard = 0;
$websiteSslCertificate->is_auto_renew = 1;
$websiteSslCertificate->provider = 'letsencrypt';
$websiteSslCertificate->save();
$findDomain->configureVirtualHost();
} }
} }

View file

@ -28,7 +28,7 @@ Route::post('letsencrypt/secure', function () {
$email = request('email', null); $email = request('email', null);
$findDomain = \App\Models\Domain::where('domain', $domain)->first(); $findDomain = \App\Models\Domain::where('domain', $domain)->first();
if (! $findDomain) { if (!$findDomain) {
return response()->json(['error' => 'Domain not found'], 404); return response()->json(['error' => 'Domain not found'], 404);
} }
@ -38,73 +38,21 @@ Route::post('letsencrypt/secure', function () {
} }
$findHostingSubscription = \App\Models\HostingSubscription::where('id', $findDomain->hosting_subscription_id)->first(); $findHostingSubscription = \App\Models\HostingSubscription::where('id', $findDomain->hosting_subscription_id)->first();
if (! $findHostingSubscription) { if (!$findHostingSubscription) {
return response()->json(['error' => 'Domain not hosted'], 400); return response()->json(['error' => 'Domain not hosted'], 400);
} }
$generalSettings = Settings::general(); try {
$secureDomain = new \Modules\LetsEncrypt\Jobs\LetsEncryptSecureDomain($findDomain->id);
$secureDomain->handle();
$sslCertificateFilePath = '/etc/letsencrypt/live/'.$findDomain->domain.'/cert.pem'; ApacheBuild::dispatchSync();
$sslCertificateKeyFilePath = '/etc/letsencrypt/live/'.$findDomain->domain.'/privkey.pem';
$sslCertificateChainFilePath = '/etc/letsencrypt/live/'.$findDomain->domain.'/fullchain.pem';
$certbotHttpSecureCommand = view('letsencrypt::actions.certbot-http-secure-command', [ return [
'domain' => $findDomain->domain, 'success' => 'Domain secured successfully'
'domainRoot' => $findDomain->domain_root, ];
'domainPublic' => $findDomain->domain_public, } catch (\Exception $e) {
'email' => $generalSettings['master_email'], return response()->json(['error' => 'Can\'t secure domain'], 500);
'country' => $generalSettings['master_country'],
'locality' => $generalSettings['master_locality'],
'organization' => $generalSettings['organization_name'],
])->render();
$exec = shell_exec($certbotHttpSecureCommand);
$validateCertificates = [];
if (! file_exists($sslCertificateFilePath)
|| ! file_exists($sslCertificateKeyFilePath)
|| ! file_exists($sslCertificateChainFilePath)) {
// Cant get all certificates
return response()->json(['error' => 'Cant get all certificates'], 400);
} }
$sslCertificateFileContent = file_get_contents($sslCertificateFilePath);
$sslCertificateKeyFileContent = file_get_contents($sslCertificateKeyFilePath);
$sslCertificateChainFileContent = file_get_contents($sslCertificateChainFilePath);
if (! empty($sslCertificateChainFileContent)) {
$validateCertificates['certificate'] = $sslCertificateFileContent;
}
if (! empty($sslCertificateKeyFileContent)) {
$validateCertificates['private_key'] = $sslCertificateKeyFileContent;
}
if (! empty($sslCertificateChainFileContent)) {
$validateCertificates['certificate_chain'] = $sslCertificateChainFileContent;
}
if (count($validateCertificates) !== 3) {
// Cant get all certificates
return;
}
$websiteSslCertificate = new DomainSslCertificate();
$websiteSslCertificate->domain = $findDomain->domain;
$websiteSslCertificate->certificate = $validateCertificates['certificate'];
$websiteSslCertificate->private_key = $validateCertificates['private_key'];
$websiteSslCertificate->certificate_chain = $validateCertificates['certificate_chain'];
$websiteSslCertificate->customer_id = $findDomain->customer_id;
$websiteSslCertificate->is_active = 1;
$websiteSslCertificate->is_wildcard = 0;
$websiteSslCertificate->is_auto_renew = 1;
$websiteSslCertificate->provider = 'letsencrypt';
$websiteSslCertificate->save();
$findDomain->configureVirtualHost(true);
ApacheBuild::dispatchSync();
return [
'success' => 'Domain secured successfully'
];
}); });