11package cert
22
33import (
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+
2159func 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