|
1 | 1 | package cert |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
5 | | - "github.com/0xJacky/Nginx-UI/server/model" |
6 | | - "github.com/pkg/errors" |
7 | | - "log" |
8 | | - "time" |
| 4 | + "fmt" |
| 5 | + "github.com/0xJacky/Nginx-UI/server/model" |
| 6 | + "github.com/pkg/errors" |
| 7 | + "log" |
| 8 | + "time" |
9 | 9 | ) |
10 | 10 |
|
11 | 11 | func handleIssueCertLogChan(logChan chan string) { |
12 | | - defer func() { |
13 | | - if err := recover(); err != nil { |
14 | | - log.Println("[Auto Cert] handleIssueCertLogChan", err) |
15 | | - } |
16 | | - }() |
17 | | - |
18 | | - for logString := range logChan { |
19 | | - log.Println("[Auto Cert] Info", logString) |
20 | | - } |
| 12 | + defer func() { |
| 13 | + if err := recover(); err != nil { |
| 14 | + log.Println("[Auto Cert] handleIssueCertLogChan", err) |
| 15 | + } |
| 16 | + }() |
| 17 | + |
| 18 | + for logString := range logChan { |
| 19 | + log.Println("[Auto Cert] Info", logString) |
| 20 | + } |
21 | 21 | } |
22 | 22 |
|
23 | 23 | type AutoCertErrorLog struct { |
24 | | - buffer []string |
25 | | - cert *model.Cert |
| 24 | + buffer []string |
| 25 | + cert *model.Cert |
26 | 26 | } |
27 | 27 |
|
28 | 28 | func (t *AutoCertErrorLog) SetCertModel(cert *model.Cert) { |
29 | | - t.cert = cert |
| 29 | + t.cert = cert |
30 | 30 | } |
31 | 31 |
|
32 | 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) |
| 33 | + t.buffer = append(t.buffer, text+" "+err.Error()) |
| 34 | + log.Println("[AutoCert Error]", text, err) |
35 | 35 | } |
36 | 36 |
|
37 | 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) |
| 38 | + t.buffer = append(t.buffer, text+" "+err.Error()) |
| 39 | + log.Println("[AutoCert Error]", text, err) |
40 | 40 |
|
41 | | - if t.cert == nil { |
42 | | - return |
43 | | - } |
| 41 | + if t.cert == nil { |
| 42 | + return |
| 43 | + } |
44 | 44 |
|
45 | | - _ = t.cert.Updates(&model.Cert{ |
46 | | - Log: t.ToString(), |
47 | | - }) |
| 45 | + _ = t.cert.Updates(&model.Cert{ |
| 46 | + Log: t.ToString(), |
| 47 | + }) |
48 | 48 | } |
49 | 49 |
|
50 | 50 | func (t *AutoCertErrorLog) ToString() (content string) { |
51 | 51 |
|
52 | | - for _, v := range t.buffer { |
53 | | - content += fmt.Sprintf("[AutoCert Error] %s\n", v) |
54 | | - } |
| 52 | + for _, v := range t.buffer { |
| 53 | + content += fmt.Sprintf("[AutoCert Error] %s\n", v) |
| 54 | + } |
55 | 55 |
|
56 | | - return |
| 56 | + return |
57 | 57 | } |
58 | 58 |
|
59 | 59 | func AutoObtain() { |
60 | | - defer func() { |
61 | | - if err := recover(); err != nil { |
62 | | - log.Println("[AutoCert] Recover", err) |
63 | | - } |
64 | | - }() |
65 | | - log.Println("[AutoCert] Start") |
66 | | - autoCertList := model.GetAutoCertList() |
67 | | - for _, certModel := range autoCertList { |
68 | | - confName := certModel.Filename |
69 | | - |
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 | | - |
84 | | - if certModel.SSLCertificatePath != "" { |
85 | | - cert, err := GetCertInfo(certModel.SSLCertificatePath) |
86 | | - if err != nil { |
87 | | - errLog.Push("get cert info", err) |
88 | | - // Get certificate info error, ignore this domain |
89 | | - continue |
90 | | - } |
91 | | - // every week |
92 | | - if time.Now().Sub(cert.NotBefore).Hours()/24 < 7 { |
93 | | - continue |
94 | | - } |
95 | | - } |
96 | | - // after 1 mo, reissue certificate |
97 | | - logChan := make(chan string, 1) |
98 | | - errChan := make(chan error, 1) |
99 | | - |
100 | | - // support SAN certification |
101 | | - payload := &ConfigPayload{ |
102 | | - ServerName: certModel.Domains, |
103 | | - ChallengeMethod: certModel.ChallengeMethod, |
104 | | - DNSCredentialID: certModel.DnsCredentialID, |
105 | | - } |
106 | | - go IssueCert(payload, logChan, errChan) |
107 | | - |
108 | | - go handleIssueCertLogChan(logChan) |
109 | | - |
110 | | - // block, unless errChan closed |
111 | | - for err := range errChan { |
112 | | - errLog.Push("issue cert", err) |
113 | | - } |
114 | | - |
115 | | - logStr := errLog.ToString() |
116 | | - if logStr != "" { |
117 | | - // store error log to db |
118 | | - _ = certModel.Updates(&model.Cert{ |
119 | | - Log: errLog.ToString(), |
120 | | - }) |
121 | | - } else { |
122 | | - certModel.ClearLog() |
123 | | - } |
124 | | - |
125 | | - close(logChan) |
126 | | - } |
127 | | - log.Println("[AutoCert] End") |
| 60 | + defer func() { |
| 61 | + if err := recover(); err != nil { |
| 62 | + log.Println("[AutoCert] Recover", err) |
| 63 | + } |
| 64 | + }() |
| 65 | + log.Println("[AutoCert] Start") |
| 66 | + autoCertList := model.GetAutoCertList() |
| 67 | + for _, certModel := range autoCertList { |
| 68 | + confName := certModel.Filename |
| 69 | + |
| 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 | + |
| 84 | + if certModel.SSLCertificatePath != "" { |
| 85 | + cert, err := GetCertInfo(certModel.SSLCertificatePath) |
| 86 | + if err != nil { |
| 87 | + errLog.Push("get cert info", err) |
| 88 | + // Get certificate info error, ignore this domain |
| 89 | + continue |
| 90 | + } |
| 91 | + // every week |
| 92 | + if time.Now().Sub(cert.NotBefore).Hours()/24 < 7 { |
| 93 | + continue |
| 94 | + } |
| 95 | + } |
| 96 | + // after 1 mo, reissue certificate |
| 97 | + logChan := make(chan string, 1) |
| 98 | + errChan := make(chan error, 1) |
| 99 | + |
| 100 | + // support SAN certification |
| 101 | + payload := &ConfigPayload{ |
| 102 | + ServerName: certModel.Domains, |
| 103 | + ChallengeMethod: certModel.ChallengeMethod, |
| 104 | + DNSCredentialID: certModel.DnsCredentialID, |
| 105 | + } |
| 106 | + go IssueCert(payload, logChan, errChan) |
| 107 | + |
| 108 | + go handleIssueCertLogChan(logChan) |
| 109 | + |
| 110 | + // block, unless errChan closed |
| 111 | + for err := range errChan { |
| 112 | + errLog.Push("issue cert", err) |
| 113 | + } |
| 114 | + |
| 115 | + logStr := errLog.ToString() |
| 116 | + if logStr != "" { |
| 117 | + // store error log to db |
| 118 | + _ = certModel.Updates(&model.Cert{ |
| 119 | + Log: errLog.ToString(), |
| 120 | + }) |
| 121 | + } else { |
| 122 | + certModel.ClearLog() |
| 123 | + } |
| 124 | + |
| 125 | + close(logChan) |
| 126 | + } |
| 127 | + log.Println("[AutoCert] End") |
128 | 128 | } |
0 commit comments