auto_cert.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package cert
  2. import (
  3. "github.com/0xJacky/Nginx-UI/internal/logger"
  4. "github.com/0xJacky/Nginx-UI/internal/notification"
  5. "github.com/0xJacky/Nginx-UI/model"
  6. "github.com/pkg/errors"
  7. "runtime"
  8. "strings"
  9. "time"
  10. )
  11. func AutoObtain() {
  12. defer func() {
  13. if err := recover(); err != nil {
  14. buf := make([]byte, 1024)
  15. runtime.Stack(buf, false)
  16. logger.Error("AutoCert Recover", err, string(buf))
  17. }
  18. }()
  19. logger.Info("AutoCert Worker Started")
  20. autoCertList := model.GetAutoCertList()
  21. for _, certModel := range autoCertList {
  22. certModel := certModel
  23. renew(certModel)
  24. }
  25. logger.Info("AutoCert Worker End")
  26. }
  27. func renew(certModel *model.Cert) {
  28. confName := certModel.Filename
  29. log := &Logger{}
  30. log.SetCertModel(certModel)
  31. defer log.Exit()
  32. if len(certModel.Filename) == 0 {
  33. log.Error(errors.New("filename is empty"))
  34. return
  35. }
  36. if len(certModel.Domains) == 0 {
  37. log.Error(errors.New("domains list is empty, " +
  38. "try to reopen auto-cert for this config:" + confName))
  39. notification.Error("Renew Certificate Error", confName)
  40. return
  41. }
  42. if certModel.SSLCertificatePath == "" {
  43. log.Error(errors.New("ssl certificate path is empty, " +
  44. "try to reopen auto-cert for this config:" + confName))
  45. notification.Error("Renew Certificate Error", confName)
  46. return
  47. }
  48. cert, err := GetCertInfo(certModel.SSLCertificatePath)
  49. if err != nil {
  50. // Get certificate info error, ignore this certificate
  51. log.Error(errors.Wrap(err, "get certificate info error"))
  52. notification.Error("Renew Certificate Error", strings.Join(certModel.Domains, ", "))
  53. return
  54. }
  55. if time.Now().Sub(cert.NotBefore).Hours()/24 < 7 {
  56. // not between 1 week, ignore this certificate
  57. return
  58. }
  59. // after 1 mo, reissue certificate
  60. logChan := make(chan string, 1)
  61. errChan := make(chan error, 1)
  62. // support SAN certification
  63. payload := &ConfigPayload{
  64. ServerName: certModel.Domains,
  65. ChallengeMethod: certModel.ChallengeMethod,
  66. DNSCredentialID: certModel.DnsCredentialID,
  67. }
  68. // errChan will be closed inside IssueCert
  69. go IssueCert(payload, logChan, errChan)
  70. go func() {
  71. for logString := range logChan {
  72. log.Info(strings.TrimSpace(logString))
  73. }
  74. }()
  75. // block, unless errChan closed
  76. for err := range errChan {
  77. log.Error(err)
  78. notification.Error("Renew Certificate Error", strings.Join(payload.ServerName, ", "))
  79. return
  80. }
  81. notification.Success("Renew Certificate Success", strings.Join(payload.ServerName, ", "))
  82. }