From 59c216c18486514f0d123103defcc8bbb9c54bad Mon Sep 17 00:00:00 2001 From: zhengkunwang <31820853+zhengkunwang223@users.noreply.github.com> Date: Tue, 28 Nov 2023 15:42:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=89=8B=E5=8A=A8=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E8=AF=81=E4=B9=A6=E5=A2=9E=E5=8A=A0=E6=9B=B4=E6=96=B0=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=20(#3087)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refs https://github.com/1Panel-dev/1Panel/issues/2329 --- backend/app/dto/request/website_ssl.go | 9 +++- backend/app/model/website_ssl.go | 1 + backend/app/service/website_ssl.go | 45 +++++++++++-------- backend/init/migration/migrations/v_1_9.go | 2 +- frontend/src/api/interface/website.ts | 1 + .../src/views/website/ssl/detail/index.vue | 4 +- frontend/src/views/website/ssl/index.vue | 16 ++++++- .../src/views/website/ssl/upload/index.vue | 17 ++++--- 8 files changed, 62 insertions(+), 33 deletions(-) diff --git a/backend/app/dto/request/website_ssl.go b/backend/app/dto/request/website_ssl.go index 3a36d1326..d9f9fdbf1 100644 --- a/backend/app/dto/request/website_ssl.go +++ b/backend/app/dto/request/website_ssl.go @@ -63,8 +63,12 @@ type WebsiteBatchDelReq struct { } type WebsiteSSLUpdate struct { - ID uint `json:"id" validate:"required"` - AutoRenew bool `json:"autoRenew"` + ID uint `json:"id" validate:"required"` + AutoRenew bool `json:"autoRenew"` + Description string `json:"description"` + PrivateKey string `json:"privateKey"` + Certificate string `json:"certificate"` + Type string `json:"type" validate:"required,oneof=autoRenew description certificate privateKey"` } type WebsiteSSLUpload struct { @@ -73,6 +77,7 @@ type WebsiteSSLUpload struct { PrivateKeyPath string `json:"privateKeyPath"` CertificatePath string `json:"certificatePath"` Type string `json:"type" validate:"required,oneof=paste local"` + SSLID uint `json:"sslID"` } type WebsiteCASearch struct { diff --git a/backend/app/model/website_ssl.go b/backend/app/model/website_ssl.go index 3e5ce0954..aef16ca0f 100644 --- a/backend/app/model/website_ssl.go +++ b/backend/app/model/website_ssl.go @@ -28,6 +28,7 @@ type WebsiteSSL struct { KeyType string `gorm:"not null;default:2048" json:"keyType"` PushDir bool `gorm:"not null;default:0" json:"pushDir"` Dir string `json:"dir"` + Description string `json:"description"` AcmeAccount WebsiteAcmeAccount `json:"acmeAccount" gorm:"-:migration"` DnsAccount WebsiteDnsAccount `json:"dnsAccount" gorm:"-:migration"` diff --git a/backend/app/service/website_ssl.go b/backend/app/service/website_ssl.go index 837b08dc9..414f0aa73 100644 --- a/backend/app/service/website_ssl.go +++ b/backend/app/service/website_ssl.go @@ -345,10 +345,16 @@ func (w WebsiteSSLService) Update(update request.WebsiteSSLUpdate) error { } func (w WebsiteSSLService) Upload(req request.WebsiteSSLUpload) error { - newSSL := &model.WebsiteSSL{ + websiteSSL := &model.WebsiteSSL{ Provider: constant.Manual, } - + var err error + if req.SSLID > 0 { + websiteSSL, err = websiteSSLRepo.GetFirst(commonRepo.WithByID(req.SSLID)) + if err != nil { + return err + } + } if req.Type == "local" { fileOp := files.NewFileOp() if !fileOp.Stat(req.PrivateKeyPath) { @@ -360,24 +366,24 @@ func (w WebsiteSSLService) Upload(req request.WebsiteSSLUpload) error { if content, err := fileOp.GetContent(req.PrivateKeyPath); err != nil { return err } else { - newSSL.PrivateKey = string(content) + websiteSSL.PrivateKey = string(content) } if content, err := fileOp.GetContent(req.CertificatePath); err != nil { return err } else { - newSSL.Pem = string(content) + websiteSSL.Pem = string(content) } } else { - newSSL.PrivateKey = req.PrivateKey - newSSL.Pem = req.Certificate + websiteSSL.PrivateKey = req.PrivateKey + websiteSSL.Pem = req.Certificate } - privateKeyCertBlock, _ := pem.Decode([]byte(newSSL.PrivateKey)) + privateKeyCertBlock, _ := pem.Decode([]byte(websiteSSL.PrivateKey)) if privateKeyCertBlock == nil { return buserr.New("ErrSSLKeyFormat") } - certBlock, _ := pem.Decode([]byte(newSSL.Pem)) + certBlock, _ := pem.Decode([]byte(websiteSSL.Pem)) if certBlock == nil { return buserr.New("ErrSSLCertificateFormat") } @@ -385,23 +391,23 @@ func (w WebsiteSSLService) Upload(req request.WebsiteSSLUpload) error { if err != nil { return err } - newSSL.ExpireDate = cert.NotAfter - newSSL.StartDate = cert.NotBefore - newSSL.Type = cert.Issuer.CommonName + websiteSSL.ExpireDate = cert.NotAfter + websiteSSL.StartDate = cert.NotBefore + websiteSSL.Type = cert.Issuer.CommonName if len(cert.Issuer.Organization) > 0 { - newSSL.Organization = cert.Issuer.Organization[0] + websiteSSL.Organization = cert.Issuer.Organization[0] } else { - newSSL.Organization = cert.Issuer.CommonName + websiteSSL.Organization = cert.Issuer.CommonName } var domains []string if len(cert.DNSNames) > 0 { - newSSL.PrimaryDomain = cert.DNSNames[0] + websiteSSL.PrimaryDomain = cert.DNSNames[0] domains = cert.DNSNames[1:] } if len(cert.IPAddresses) > 0 { - if newSSL.PrimaryDomain == "" { - newSSL.PrimaryDomain = cert.IPAddresses[0].String() + if websiteSSL.PrimaryDomain == "" { + websiteSSL.PrimaryDomain = cert.IPAddresses[0].String() for _, ip := range cert.IPAddresses[1:] { domains = append(domains, ip.String()) } @@ -411,9 +417,12 @@ func (w WebsiteSSLService) Upload(req request.WebsiteSSLUpload) error { } } } - newSSL.Domains = strings.Join(domains, ",") + websiteSSL.Domains = strings.Join(domains, ",") - return websiteSSLRepo.Create(context.Background(), newSSL) + if websiteSSL.ID > 0 { + return websiteSSLRepo.Save(websiteSSL) + } + return websiteSSLRepo.Create(context.Background(), websiteSSL) } func (w WebsiteSSLService) SyncForRestart() error { diff --git a/backend/init/migration/migrations/v_1_9.go b/backend/init/migration/migrations/v_1_9.go index a065c31cf..0d4a544f6 100644 --- a/backend/init/migration/migrations/v_1_9.go +++ b/backend/init/migration/migrations/v_1_9.go @@ -27,7 +27,7 @@ var AddWebsiteCA = &gormigrate.Migration{ } var UpdateWebsiteSSL = &gormigrate.Migration{ - ID: "20231127-update-website-ssl", + ID: "20231128-update-website-ssl", Migrate: func(tx *gorm.DB) error { if err := tx.AutoMigrate(&model.WebsiteSSL{}); err != nil { return err diff --git a/frontend/src/api/interface/website.ts b/frontend/src/api/interface/website.ts index 82f8a0c52..0529a4733 100644 --- a/frontend/src/api/interface/website.ts +++ b/frontend/src/api/interface/website.ts @@ -451,6 +451,7 @@ export namespace Website { privateKeyPath: string; certificatePath: string; type: string; + sslID: number; } export interface SSLObtain { diff --git a/frontend/src/views/website/ssl/detail/index.vue b/frontend/src/views/website/ssl/detail/index.vue index 821e0a996..080104754 100644 --- a/frontend/src/views/website/ssl/detail/index.vue +++ b/frontend/src/views/website/ssl/detail/index.vue @@ -53,7 +53,7 @@