mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-10 18:05:48 +02:00
enhance(cert): do not create cert in db if issue failed
This commit is contained in:
parent
09671501e0
commit
6c7edf5c25
3 changed files with 23 additions and 19 deletions
|
@ -1,13 +1,16 @@
|
|||
package certificate
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/0xJacky/Nginx-UI/internal/cert"
|
||||
"github.com/0xJacky/Nginx-UI/model"
|
||||
"github.com/0xJacky/Nginx-UI/query"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-acme/lego/v4/certcrypto"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/uozi-tech/cosy/logger"
|
||||
"net/http"
|
||||
"gorm.io/gen/field"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -46,6 +49,7 @@ func handleIssueCertLogChan(conn *websocket.Conn, log *cert.Logger, logChan chan
|
|||
}
|
||||
|
||||
func IssueCert(c *gin.Context) {
|
||||
name := c.Param("name")
|
||||
var upGrader = websocket.Upgrader{
|
||||
CheckOrigin: func(r *http.Request) bool {
|
||||
return true
|
||||
|
@ -72,16 +76,18 @@ func IssueCert(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
certModel, err := model.FirstOrCreateCert(c.Param("name"), payload.GetKeyType())
|
||||
certModel, err := model.FirstOrInit(name, payload.GetKeyType())
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
certInfo, _ := cert.GetCertInfo(certModel.SSLCertificatePath)
|
||||
if certInfo != nil {
|
||||
payload.Resource = certModel.Resource
|
||||
payload.NotBefore = certInfo.NotBefore
|
||||
if certModel.SSLCertificatePath != "" {
|
||||
certInfo, _ := cert.GetCertInfo(certModel.SSLCertificatePath)
|
||||
if certInfo != nil {
|
||||
payload.Resource = certModel.Resource
|
||||
payload.NotBefore = certInfo.NotBefore
|
||||
}
|
||||
}
|
||||
|
||||
logChan := make(chan string, 1)
|
||||
|
@ -109,22 +115,23 @@ func IssueCert(c *gin.Context) {
|
|||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
return
|
||||
return
|
||||
}
|
||||
|
||||
err = certModel.Updates(&model.Cert{
|
||||
cert := query.Cert
|
||||
|
||||
_, err = cert.Where(cert.Name.Eq(name), cert.Filename.Eq(name), cert.KeyType.Eq(string(payload.KeyType))).
|
||||
Assign(field.Attrs(&model.Cert{
|
||||
Domains: payload.ServerName,
|
||||
SSLCertificatePath: payload.GetCertificatePath(),
|
||||
SSLCertificateKeyPath: payload.GetCertificateKeyPath(),
|
||||
AutoCert: model.AutoCertEnabled,
|
||||
KeyType: payload.KeyType,
|
||||
ChallengeMethod: payload.ChallengeMethod,
|
||||
DnsCredentialID: payload.DNSCredentialID,
|
||||
Resource: payload.Resource,
|
||||
MustStaple: payload.MustStaple,
|
||||
LegoDisableCNAMESupport: payload.LegoDisableCNAMESupport,
|
||||
})
|
||||
|
||||
})).FirstOrCreate()
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
_ = ws.WriteJSON(IssueCertResponse{
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
// Auto-generated notification messages
|
||||
// Do not edit manually
|
||||
|
||||
const notifications: Record<string, string> = {
|
||||
"Sync_Certificate_Error_Error": "err.Error()",
|
||||
};
|
||||
|
||||
export default notifications;
|
|
@ -63,6 +63,11 @@ func FirstOrCreateCert(confName string, keyType certcrypto.KeyType) (c Cert, err
|
|||
return
|
||||
}
|
||||
|
||||
func FirstOrInit(confName string, keyType certcrypto.KeyType) (c Cert, err error) {
|
||||
err = db.FirstOrInit(&c, &Cert{Name: confName, Filename: confName, KeyType: keyType}).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Cert) Insert() error {
|
||||
return db.Create(c).Error
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue