From 47c651251faec9983cf3a032801c7be4107f4978 Mon Sep 17 00:00:00 2001 From: Bozhidar Date: Thu, 31 Oct 2024 12:23:31 +0200 Subject: [PATCH] Update WildcardDomain.php --- .../LetsEncrypt/Pages/WildcardDomain.php | 216 +++++++++++------- 1 file changed, 135 insertions(+), 81 deletions(-) diff --git a/web/Modules/LetsEncrypt/Filament/Clusters/LetsEncrypt/Pages/WildcardDomain.php b/web/Modules/LetsEncrypt/Filament/Clusters/LetsEncrypt/Pages/WildcardDomain.php index f639c43..5097fde 100644 --- a/web/Modules/LetsEncrypt/Filament/Clusters/LetsEncrypt/Pages/WildcardDomain.php +++ b/web/Modules/LetsEncrypt/Filament/Clusters/LetsEncrypt/Pages/WildcardDomain.php @@ -45,101 +45,155 @@ class WildcardDomain extends BaseSettings ]; } - public function installCertificates() + public function checkCertificateFilesExist($domain) { - $masterDomain = new MasterDomain(); - $masterDomain->domain = setting('general.wildcard_domain'); - $findWildcardSsl = DomainSslCertificate::where('domain', '*.'.$masterDomain->domain)->first(); - if ($findWildcardSsl) { - return [ - 'error' => 'Domain already secured' - ]; - } - - if (file_exists($this->installLogFilePath)) { - unlink($this->installLogFilePath); - } - - $acmeConfigYaml = view('letsencrypt::actions.acme-config-wildcard-yaml', [ - 'domain' => $masterDomain->domain, - 'domainRoot' => $masterDomain->domainRoot, - 'domainPublic' => $masterDomain->domainPublic, - 'email' => $masterDomain->email, - 'country' => $masterDomain->country, - 'locality' => $masterDomain->locality, - 'organization' => $masterDomain->organization - ])->render(); - - $acmeConfigYaml = preg_replace('~(*ANY)\A\s*\R|\s*(?!\r\n)\s$~mu', '', $acmeConfigYaml); - - file_put_contents($masterDomain->domainRoot.'/acme-wildcard-config.yaml', $acmeConfigYaml); - - $amePHPPharFile = base_path().'/Modules/LetsEncrypt/Actions/acmephp.phar'; - - if (!is_dir(dirname($this->installLogFilePath))) { - shell_exec('mkdir -p ' . dirname($this->installLogFilePath)); - } - - //$phyrePHP = ApiClient::getPhyrePHP(); - $phyrePHP = 'phyre-php'; - $command = $phyrePHP.' '.$amePHPPharFile.' run '.$masterDomain->domainRoot.'/acme-wildcard-config.yaml >> ' . $this->installLogFilePath . ' &'; - shell_exec($command); - - $validateCertificates = []; - $sslCertificateFilePath = '/root/.acmephp/master/certs/*.'.$masterDomain->domain.'/public/cert.pem'; - $sslCertificateKeyFilePath = '/root/.acmephp/master/certs/*.'.$masterDomain->domain.'/private/key.private.pem'; - $sslCertificateChainFilePath = '/root/.acmephp/master/certs/*.'.$masterDomain->domain.'/public/fullchain.pem'; - - if (! file_exists($sslCertificateFilePath) - || ! file_exists($sslCertificateKeyFilePath) - || ! file_exists($sslCertificateChainFilePath)) { - // Cant get all certificates - return [ - 'error' => 'Cant get all certificates.' - ]; - } + $sslCertificateFilePath = '/root/.acmephp/master/certs/*.'.$domain.'/public/cert.pem'; + $sslCertificateKeyFilePath = '/root/.acmephp/master/certs/*.'.$domain.'/private/key.private.pem'; + $sslCertificateChainFilePath = '/root/.acmephp/master/certs/*.'.$domain.'/public/fullchain.pem'; $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 + + if (file_exists($sslCertificateFilePath) + && file_exists($sslCertificateKeyFilePath) + && file_exists($sslCertificateChainFilePath)) { return [ - 'error' => 'Cant get all certificates.' + 'sslFiles' => [ + 'certificate' => $sslCertificateFilePath, + 'certificateContent' => $sslCertificateFileContent, + 'privateKey' => $sslCertificateKeyFilePath, + 'privateKeyContent' => $sslCertificateKeyFileContent, + 'certificateChain' => $sslCertificateChainFilePath, + 'certificateChainContent' => $sslCertificateChainFileContent + ] ]; } - $websiteSslCertificate = new DomainSslCertificate(); - $websiteSslCertificate->domain = '*.' . $masterDomain->domain; - $websiteSslCertificate->certificate = $validateCertificates['certificate']; - $websiteSslCertificate->private_key = $validateCertificates['private_key']; - $websiteSslCertificate->certificate_chain = $validateCertificates['certificate_chain']; - $websiteSslCertificate->customer_id = 0; - $websiteSslCertificate->is_active = 1; - $websiteSslCertificate->is_wildcard = 1; - $websiteSslCertificate->is_auto_renew = 1; - $websiteSslCertificate->provider = 'letsencrypt'; - $websiteSslCertificate->save(); + return false; - $mds = new MasterDomain(); - $mds->configureVirtualHost(); + } - ApacheBuild::dispatchSync(); + public function installCertificates() + { + $masterDomain = new MasterDomain(); + $masterDomain->domain = setting('general.wildcard_domain'); - return [ - 'success' => 'SSL certificate installed successfully.' - ]; + $checkCertificateFilesExist = $this->checkCertificateFilesExist($masterDomain->domain); + if (isset($checkCertificateFilesExist['sslFiles']['certificateContent'])) { + + $findWildcardSsl = DomainSslCertificate::where('domain', '*.'.$masterDomain->domain)->first(); + if (!$findWildcardSsl) { + $findWildcardSsl = new DomainSslCertificate(); + $findWildcardSsl->domain = '*.'.$masterDomain->domain; + $findWildcardSsl->customer_id = 0; + $findWildcardSsl->is_active = 1; + $findWildcardSsl->is_wildcard = 1; + $findWildcardSsl->is_auto_renew = 1; + $findWildcardSsl->provider = 'letsencrypt'; + } + + $findWildcardSsl->certificate = $checkCertificateFilesExist['sslFiles']['certificateContent']; + $findWildcardSsl->private_key = $checkCertificateFilesExist['sslFiles']['privateKeyContent']; + $findWildcardSsl->certificate_chain = $checkCertificateFilesExist['sslFiles']['certificateChainContent']; + $findWildcardSsl->save(); + + + $mds = new MasterDomain(); + $mds->configureVirtualHost(); + + ApacheBuild::dispatchSync(); + + return [ + 'error' => 'Domain SSL certificate updated.' + ]; + + } + + +// +// +// +// if (file_exists($this->installLogFilePath)) { +// unlink($this->installLogFilePath); +// } +// +// $acmeConfigYaml = view('letsencrypt::actions.acme-config-wildcard-yaml', [ +// 'domain' => $masterDomain->domain, +// 'domainRoot' => $masterDomain->domainRoot, +// 'domainPublic' => $masterDomain->domainPublic, +// 'email' => $masterDomain->email, +// 'country' => $masterDomain->country, +// 'locality' => $masterDomain->locality, +// 'organization' => $masterDomain->organization +// ])->render(); +// +// $acmeConfigYaml = preg_replace('~(*ANY)\A\s*\R|\s*(?!\r\n)\s$~mu', '', $acmeConfigYaml); +// +// file_put_contents($masterDomain->domainRoot.'/acme-wildcard-config.yaml', $acmeConfigYaml); +// +// $amePHPPharFile = base_path().'/Modules/LetsEncrypt/Actions/acmephp.phar'; +// +// if (!is_dir(dirname($this->installLogFilePath))) { +// shell_exec('mkdir -p ' . dirname($this->installLogFilePath)); +// } +// +// //$phyrePHP = ApiClient::getPhyrePHP(); +// $phyrePHP = 'phyre-php'; +// $command = $phyrePHP.' '.$amePHPPharFile.' run '.$masterDomain->domainRoot.'/acme-wildcard-config.yaml >> ' . $this->installLogFilePath . ' &'; +// shell_exec($command); +// +// $validateCertificates = []; +// +// if (! file_exists($sslCertificateFilePath) +// || ! file_exists($sslCertificateKeyFilePath) +// || ! file_exists($sslCertificateChainFilePath)) { +// // Cant get all certificates +// return [ +// 'error' => 'Cant get all certificates.' +// ]; +// } +// +// +// +// 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 [ +// 'error' => 'Cant get all certificates.' +// ]; +// } +// +// $websiteSslCertificate = new DomainSslCertificate(); +// $websiteSslCertificate->domain = '*.' . $masterDomain->domain; +// $websiteSslCertificate->certificate = $validateCertificates['certificate']; +// $websiteSslCertificate->private_key = $validateCertificates['private_key']; +// $websiteSslCertificate->certificate_chain = $validateCertificates['certificate_chain']; +// $websiteSslCertificate->customer_id = 0; +// $websiteSslCertificate->is_active = 1; +// $websiteSslCertificate->is_wildcard = 1; +// $websiteSslCertificate->is_auto_renew = 1; +// $websiteSslCertificate->provider = 'letsencrypt'; +// $websiteSslCertificate->save(); +// +// $mds = new MasterDomain(); +// $mds->configureVirtualHost(); +// +// ApacheBuild::dispatchSync(); +// +// return [ +// 'success' => 'SSL certificate installed successfully.' +// ]; } public function getInstallLog()