Skip to content

Commit e260860

Browse files
committed
feat: store error log of obtaining cert
1 parent 175d19a commit e260860

File tree

3 files changed

+70
-10
lines changed

3 files changed

+70
-10
lines changed

server/api/domain.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,11 +377,14 @@ func AddDomainToAutoCert(c *gin.Context) {
377377

378378
func RemoveDomainFromAutoCert(c *gin.Context) {
379379
name := c.Param("name")
380-
certModel := model.Cert{
381-
Filename: name,
380+
certModel, err := model.FirstCert(name)
381+
382+
if err != nil {
383+
ErrHandler(c, err)
384+
return
382385
}
383386

384-
err := certModel.Updates(&model.Cert{
387+
err = certModel.Updates(&model.Cert{
385388
AutoCert: model.AutoCertDisabled,
386389
})
387390

server/model/cert.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type Cert struct {
2121
SSLCertificatePath string `json:"ssl_certificate_path"`
2222
SSLCertificateKeyPath string `json:"ssl_certificate_key_path"`
2323
AutoCert int `json:"auto_cert"`
24+
Log string `json:"log"`
2425
}
2526

2627
func FirstCert(confName string) (c Cert, err error) {
@@ -40,8 +41,8 @@ func (c *Cert) Insert() error {
4041
return db.Create(c).Error
4142
}
4243

43-
func GetAutoCertList() (c []Cert) {
44-
var t []Cert
44+
func GetAutoCertList() (c []*Cert) {
45+
var t []*Cert
4546
db.Where("auto_cert", AutoCertEnabled).Find(&t)
4647

4748
// check if this domain is enabled
@@ -84,7 +85,7 @@ func FirstCertByID(id int) (c Cert, err error) {
8485
}
8586

8687
func (c *Cert) Updates(n *Cert) error {
87-
return db.Model(&Cert{}).Where("filename", c.Filename).Updates(n).Error
88+
return db.Model(&Cert{}).Where("id", c.ID).Updates(n).Error
8889
}
8990

9091
func (c *Cert) Remove() error {

server/pkg/cert/auto_cert.go

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package cert
22

33
import (
4+
"fmt"
45
"github.com/0xJacky/Nginx-UI/server/model"
6+
"github.com/pkg/errors"
57
"log"
68
"time"
79
)
@@ -18,6 +20,42 @@ func handleIssueCertLogChan(logChan chan string) {
1820
}
1921
}
2022

23+
type AutoCertErrorLog struct {
24+
buffer []string
25+
cert *model.Cert
26+
}
27+
28+
func (t *AutoCertErrorLog) SetCertModel(cert *model.Cert) {
29+
t.cert = cert
30+
}
31+
32+
func (t *AutoCertErrorLog) Push(text string, err error) {
33+
t.buffer = append(t.buffer, text+" "+err.Error())
34+
log.Println("[AutoCert Error]", text, err)
35+
}
36+
37+
func (t *AutoCertErrorLog) Exit(text string, err error) {
38+
t.buffer = append(t.buffer, text+" "+err.Error())
39+
log.Println("[AutoCert Error]", text, err)
40+
41+
if t.cert == nil {
42+
return
43+
}
44+
45+
_ = t.cert.Updates(&model.Cert{
46+
Log: t.ToString(),
47+
})
48+
}
49+
50+
func (t *AutoCertErrorLog) ToString() (content string) {
51+
52+
for _, v := range t.buffer {
53+
content += fmt.Sprintf("[AutoCert Error] %s\n", v)
54+
}
55+
56+
return
57+
}
58+
2159
func AutoObtain() {
2260
defer func() {
2361
if err := recover(); err != nil {
@@ -29,15 +67,29 @@ func AutoObtain() {
2967
for _, certModel := range autoCertList {
3068
confName := certModel.Filename
3169

70+
errLog := &AutoCertErrorLog{}
71+
errLog.SetCertModel(certModel)
72+
73+
if len(certModel.Filename) == 0 {
74+
errLog.Exit("", errors.New("filename is empty"))
75+
continue
76+
}
77+
78+
if len(certModel.Domains) == 0 {
79+
errLog.Exit(confName, errors.New("domains list is empty, "+
80+
"try to reopen auto-cert for this config:"+confName))
81+
continue
82+
}
83+
3284
if certModel.SSLCertificatePath == "" {
33-
log.Println("[AutoCert] Error ssl_certificate_path is empty, " +
34-
"try to reopen auto-cert for this config:" + confName)
85+
errLog.Exit(confName, errors.New("ssl_certificate_path is empty, "+
86+
"try to reopen auto-cert for this config:"+confName))
3587
continue
3688
}
3789

3890
cert, err := GetCertInfo(certModel.SSLCertificatePath)
3991
if err != nil {
40-
log.Println("GetCertInfo Err", err)
92+
errLog.Push("get cert info", err)
4193
// Get certificate info error, ignore this domain
4294
continue
4395
}
@@ -57,8 +109,12 @@ func AutoObtain() {
57109

58110
// block, unless errChan closed
59111
for err = range errChan {
60-
log.Println("Error cert.IssueCert", err)
112+
errLog.Push("issue cert", err)
61113
}
114+
// store error log to db
115+
_ = certModel.Updates(&model.Cert{
116+
Log: errLog.ToString(),
117+
})
62118

63119
close(logChan)
64120
}

0 commit comments

Comments
 (0)