Bozhidar Slaveykov 1 年之前
父节点
当前提交
50b0ca33e1

+ 42 - 3
web/Modules/LetsEncrypt/Listeners/HostingAccountIsCreatedListener.php

@@ -5,6 +5,7 @@ namespace Modules\LetsEncrypt\Listeners;
 use App\ApiClient;
 use App\Events\HostingAccountIsCreated;
 use App\FileManagerApi;
+use App\Models\WebsiteSslCertificate;
 use App\Settings;
 use App\ShellApi;
 
@@ -41,11 +42,49 @@ class HostingAccountIsCreatedListener
         $amePHPPharFile = base_path() . '/Modules/LetsEncrypt/Actions/acmephp.phar';
 
         $phyrePHP = ApiClient::getPhyrePHP();
-        $command = $phyrePHP . ' ' . $amePHPPharFile . ' run ' . $event->model->domain_root . '/acme-config.yaml';
-        $execPhar = ShellApi::exec($command);
+        $phyrePHP = 'php';
 
+//        $command = $phyrePHP . ' ' . $amePHPPharFile . ' run ' . $event->model->domain_root . '/acme-config.yaml';
+//        $execPhar = ShellApi::exec($command);
 
-        dd($execPhar);
+
+        $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';
+
+        $sslCertificateFileContent = $fmApi->fileGetContents($sslCertificateFilePath);
+        $sslCertificateKeyFileContent = $fmApi->fileGetContents($sslCertificateKeyFilePath);
+        $sslCertificateChainFileContent = $fmApi->fileGetContents($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 WebsiteSslCertificate();
+        $websiteSslCertificate->domain = $event->model->domain;
+        $websiteSslCertificate->certificate = $validateCertificates['certificate'];
+        $websiteSslCertificate->private_key = $validateCertificates['private_key'];
+        $websiteSslCertificate->certificate_chain = $validateCertificates['certificate_chain'];
+        $websiteSslCertificate->user_id = $event->model->user_id;
+        $websiteSslCertificate->is_active = 1;
+        $websiteSslCertificate->is_wildcard = 0;
+        $websiteSslCertificate->is_auto_renew = 1;
+        $websiteSslCertificate->save();
+
+        dd($sslCertificateFileContent);
+
+        dd(1);
 
     }
 }

+ 5 - 0
web/app/FileManagerApi.php

@@ -9,6 +9,11 @@ class FileManagerApi
         ShellApi::exec('mkdir -p '.$path);
     }
 
+    public function fileGetContents($file)
+    {
+        return ShellApi::exec('cat ' . $file);
+    }
+
     public function filePutContents($file, $data)
     {
         $tempfileName = md5($file . time() . rand(111, 999));

+ 4 - 3
web/database/migrations/2024_04_01_073021_create_website_ssl_certificates_table.php

@@ -15,9 +15,6 @@ return new class extends Migration
             $table->id();
 
             $table->string('domain');
-            $table->string('certificate')->nullable();
-            $table->string('private_key')->nullable();
-            $table->string('certificate_chain')->nullable();
 
             $table->integer('user_id')->nullable();
             $table->integer('is_active')->nullable();
@@ -29,6 +26,10 @@ return new class extends Migration
             $table->timestamp('renewed_date')->nullable();
             $table->timestamp('renewed_until_date')->nullable();
 
+            $table->longText('certificate')->nullable();
+            $table->longText('private_key')->nullable();
+            $table->longText('certificate_chain')->nullable();
+
             $table->timestamps();
         });
     }