diff --git a/internal/data/cert.go b/internal/data/cert.go index 972a6e53..861ee72c 100644 --- a/internal/data/cert.go +++ b/internal/data/cert.go @@ -108,10 +108,7 @@ func (r *certRepo) Create(req *request.CertCreate) (*biz.Cert, error) { func (r *certRepo) Update(req *request.CertUpdate) error { info, err := pkgcert.ParseCert(req.Cert) - if err != nil { - return err - } - if req.Type == "upload" { + if err == nil && req.Type == "upload" { req.Domains = info.DNSNames } @@ -147,11 +144,11 @@ func (r *certRepo) ObtainAuto(id uint) (*acme.Certificate, error) { client.UseDns(acme.DnsType(cert.DNS.Type), cert.DNS.Data) } else { if cert.Website == nil { - return nil, errors.New("该证书没有关联网站,无法自动签发") + return nil, errors.New("this certificate is not associated with a website and cannot be signed. You can try to sign it manually") } else { for _, domain := range cert.Domains { if strings.Contains(domain, "*") { - return nil, errors.New("通配符域名无法使用 HTTP 验证") + return nil, errors.New("wildcard domains cannot use HTTP verification") } } conf := fmt.Sprintf("%s/server/vhost/acme/%s.conf", app.Root, cert.Website.Name) @@ -185,7 +182,7 @@ func (r *certRepo) ObtainManual(id uint) (*acme.Certificate, error) { } if r.client == nil { - return nil, errors.New("请重新获取 DNS 解析记录") + return nil, errors.New("please retry the manual obtain operation") } ssl, err := r.client.ObtainCertificateManual() @@ -219,18 +216,18 @@ func (r *certRepo) Renew(id uint) (*acme.Certificate, error) { } if cert.CertURL == "" { - return nil, errors.New("该证书没有签发成功,无法续签") + return nil, errors.New("this certificate has not been signed successfully and cannot be renewed") } if cert.DNS != nil { client.UseDns(acme.DnsType(cert.DNS.Type), cert.DNS.Data) } else { if cert.Website == nil { - return nil, errors.New("该证书没有关联网站,无法续签,可以尝试手动签发") + return nil, errors.New("this certificate is not associated with a website and cannot be signed. You can try to sign it manually") } else { for _, domain := range cert.Domains { if strings.Contains(domain, "*") { - return nil, errors.New("通配符域名无法使用 HTTP 验证") + return nil, errors.New("wildcard domains cannot use HTTP verification") } } conf := fmt.Sprintf("%s/server/vhost/acme/%s.conf", app.Root, cert.Website.Name) @@ -290,7 +287,7 @@ func (r *certRepo) Deploy(ID, WebsiteID uint) error { } if cert.Cert == "" || cert.Key == "" { - return errors.New("该证书没有签发成功,无法部署") + return errors.New("this certificate has not been signed successfully and cannot be deployed") } website, err := NewWebsiteRepo().Get(WebsiteID) @@ -314,7 +311,7 @@ func (r *certRepo) Deploy(ID, WebsiteID uint) error { func (r *certRepo) getClient(cert *biz.Cert) (*acme.Client, error) { if cert.Account == nil { - return nil, errors.New("该证书没有关联账号,无法签发") + return nil, errors.New("this certificate is not associated with an ACME account and cannot be signed") } var ca string diff --git a/internal/http/request/cert.go b/internal/http/request/cert.go index c69e1a0e..231395c4 100644 --- a/internal/http/request/cert.go +++ b/internal/http/request/cert.go @@ -18,8 +18,8 @@ type CertUpdate struct { ID uint `form:"id" json:"id" validate:"required,exists=certs id"` Type string `form:"type" json:"type" validate:"required,oneof=upload P256 P384 2048 3072 4096"` Domains []string `form:"domains" json:"domains" validate:"min=1,dive,required"` - Cert string `form:"cert" json:"cert" validate:"required"` - Key string `form:"key" json:"key" validate:"required"` + Cert string `form:"cert" json:"cert"` + Key string `form:"key" json:"key"` AutoRenew bool `form:"auto_renew" json:"auto_renew"` AccountID uint `form:"account_id" json:"account_id"` DNSID uint `form:"dns_id" json:"dns_id"` diff --git a/internal/route/http.go b/internal/route/http.go index 161ee787..4d94c908 100644 --- a/internal/route/http.go +++ b/internal/route/http.go @@ -86,7 +86,8 @@ func Http(r chi.Router) { r.Put("/{id}", cert.Update) r.Get("/{id}", cert.Get) r.Delete("/{id}", cert.Delete) - r.Post("/{id}/obtain", cert.Obtain) + r.Post("/{id}/obtainAuto", cert.ObtainAuto) + r.Post("/{id}/obtainManual", cert.ObtainManual) r.Post("/{id}/renew", cert.Renew) r.Post("/{id}/manualDNS", cert.ManualDNS) r.Post("/{id}/deploy", cert.Deploy) diff --git a/internal/service/cert.go b/internal/service/cert.go index 05c82aca..379ed0fb 100644 --- a/internal/service/cert.go +++ b/internal/service/cert.go @@ -194,26 +194,29 @@ func (s *CertService) Delete(w http.ResponseWriter, r *http.Request) { Success(w, nil) } -func (s *CertService) Obtain(w http.ResponseWriter, r *http.Request) { +func (s *CertService) ObtainAuto(w http.ResponseWriter, r *http.Request) { req, err := Bind[request.ID](r) if err != nil { Error(w, http.StatusUnprocessableEntity, "%v", err) return } - cert, err := s.certRepo.Get(req.ID) - if err != nil { + if _, err = s.certRepo.ObtainAuto(req.ID); err != nil { Error(w, http.StatusInternalServerError, "%v", err) return } - if cert.DNS != nil || cert.Website != nil { - _, err = s.certRepo.ObtainAuto(req.ID) - } else { - _, err = s.certRepo.ObtainManual(req.ID) + Success(w, nil) +} + +func (s *CertService) ObtainManual(w http.ResponseWriter, r *http.Request) { + req, err := Bind[request.ID](r) + if err != nil { + Error(w, http.StatusUnprocessableEntity, "%v", err) + return } - if err != nil { + if _, err = s.certRepo.ObtainManual(req.ID); err != nil { Error(w, http.StatusInternalServerError, "%v", err) return } diff --git a/web/src/api/panel/cert/index.ts b/web/src/api/panel/cert/index.ts index ebe7906c..5481b27a 100644 --- a/web/src/api/panel/cert/index.ts +++ b/web/src/api/panel/cert/index.ts @@ -47,9 +47,12 @@ export default { request.put(`/cert/cert/${id}`, data), // 证书删除 certDelete: (id: number): Promise> => request.delete(`/cert/cert/${id}`), - // 签发 - obtain: (id: number): Promise> => - request.post(`/cert/cert/${id}/obtain`, { id }), + // 证书自动签发 + obtainAuto: (id: number): Promise> => + request.post(`/cert/cert/${id}/obtainAuto`, { id }), + // 证书手动签发 + obtainManual: (id: number): Promise> => + request.post(`/cert/cert/${id}/obtainManual`, { id }), // 续签 renew: (id: number): Promise> => request.post(`/cert/cert/${id}/renew`, { id }), diff --git a/web/src/views/cert/CertView.vue b/web/src/views/cert/CertView.vue index 9bc904bc..43180fec 100644 --- a/web/src/views/cert/CertView.vue +++ b/web/src/views/cert/CertView.vue @@ -1,10 +1,11 @@ + + + +