auto_cert.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package cert
  2. import (
  3. "github.com/0xJacky/Nginx-UI/server/model"
  4. "log"
  5. "time"
  6. )
  7. func AutoCert() {
  8. defer func() {
  9. if err := recover(); err != nil {
  10. log.Println("[AutoCert] Recover", err)
  11. }
  12. }()
  13. log.Println("[AutoCert] Start")
  14. autoCertList := model.GetAutoCertList()
  15. for i := range autoCertList {
  16. domain := autoCertList[i].Domain
  17. certModel, err := model.FirstCert(domain)
  18. if err != nil {
  19. log.Println("[AutoCert] Error get certificate from database", err)
  20. continue
  21. }
  22. if certModel.SSLCertificatePath != "" {
  23. log.Println("[AutoCert] Error ssl_certificate_path is empty, " +
  24. "try to reopen auto-cert for this domain:" + domain)
  25. continue
  26. }
  27. cert, err := GetCertInfo(certModel.SSLCertificatePath)
  28. if err != nil {
  29. log.Println("GetCertInfo Err", err)
  30. // Get certificate info error, ignore this domain
  31. continue
  32. }
  33. // before 1 mo
  34. if time.Now().Before(cert.NotBefore.AddDate(0, 1, 0)) {
  35. continue
  36. }
  37. // after 1 mo, reissue certificate
  38. err = IssueCert(domain)
  39. if err != nil {
  40. log.Println(err)
  41. }
  42. }
  43. }