payload.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package cert
  2. import (
  3. "log"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "time"
  8. "github.com/0xJacky/Nginx-UI/internal/helper"
  9. "github.com/0xJacky/Nginx-UI/internal/nginx"
  10. "github.com/0xJacky/Nginx-UI/model"
  11. "github.com/0xJacky/Nginx-UI/query"
  12. "github.com/go-acme/lego/v4/certcrypto"
  13. "github.com/pkg/errors"
  14. "github.com/uozi-tech/cosy/logger"
  15. )
  16. type ConfigPayload struct {
  17. CertID uint64 `json:"cert_id"`
  18. ServerName []string `json:"server_name"`
  19. ChallengeMethod string `json:"challenge_method"`
  20. DNSCredentialID uint64 `json:"dns_credential_id"`
  21. ACMEUserID uint64 `json:"acme_user_id"`
  22. KeyType certcrypto.KeyType `json:"key_type"`
  23. Resource *model.CertificateResource `json:"resource,omitempty"`
  24. MustStaple bool `json:"must_staple"`
  25. LegoDisableCNAMESupport bool `json:"lego_disable_cname_support"`
  26. NotBefore time.Time `json:"-"`
  27. CertificateDir string `json:"-"`
  28. SSLCertificatePath string `json:"-"`
  29. SSLCertificateKeyPath string `json:"-"`
  30. RevokeOld bool `json:"revoke_old"`
  31. }
  32. func (c *ConfigPayload) GetACMEUser() (user *model.AcmeUser, err error) {
  33. u := query.AcmeUser
  34. // if acme_user_id == 0, use default user
  35. if c.ACMEUserID == 0 {
  36. return GetDefaultACMEUser()
  37. }
  38. // use the acme_user_id to get the acme user
  39. user, err = u.Where(u.ID.Eq(c.ACMEUserID)).First()
  40. // if acme_user not exist, use default user
  41. if err != nil {
  42. logger.Error(err)
  43. return GetDefaultACMEUser()
  44. }
  45. return
  46. }
  47. func (c *ConfigPayload) GetKeyType() certcrypto.KeyType {
  48. return helper.GetKeyType(c.KeyType)
  49. }
  50. func (c *ConfigPayload) mkCertificateDir() (err error) {
  51. dir := c.getCertificateDirPath()
  52. if !helper.FileExists(dir) {
  53. err = os.MkdirAll(dir, 0755)
  54. if err == nil {
  55. return nil
  56. }
  57. } else {
  58. return nil
  59. }
  60. // For windows, replace * with # (issue #403)
  61. c.CertificateDir = strings.ReplaceAll(c.CertificateDir, "*", "#")
  62. if _, err = os.Stat(c.CertificateDir); os.IsNotExist(err) {
  63. err = os.MkdirAll(c.CertificateDir, 0755)
  64. if err == nil {
  65. return nil
  66. }
  67. }
  68. return
  69. }
  70. func (c *ConfigPayload) WriteFile(l *log.Logger, errChan chan error) {
  71. err := c.mkCertificateDir()
  72. if err != nil {
  73. errChan <- errors.Wrap(err, "make certificate dir error")
  74. return
  75. }
  76. // Each certificate comes back with the cert bytes, the bytes of the client's
  77. // private key, and a certificate URL. SAVE THESE TO DISK.
  78. l.Println("[INFO] [Nginx UI] Writing certificate to disk")
  79. err = os.WriteFile(c.GetCertificatePath(),
  80. c.Resource.Certificate, 0644)
  81. if err != nil {
  82. errChan <- errors.Wrap(err, "write fullchain.cer error")
  83. return
  84. }
  85. l.Println("[INFO] [Nginx UI] Writing certificate private key to disk")
  86. err = os.WriteFile(c.GetCertificateKeyPath(),
  87. c.Resource.PrivateKey, 0644)
  88. if err != nil {
  89. errChan <- errors.Wrap(err, "write private.key error")
  90. return
  91. }
  92. // update database
  93. if c.CertID <= 0 {
  94. return
  95. }
  96. db := model.UseDB()
  97. db.Where("id = ?", c.CertID).Updates(&model.Cert{
  98. SSLCertificatePath: c.GetCertificatePath(),
  99. SSLCertificateKeyPath: c.GetCertificateKeyPath(),
  100. Resource: c.Resource,
  101. })
  102. }
  103. func (c *ConfigPayload) getCertificateDirPath() string {
  104. if c.CertificateDir != "" {
  105. return c.CertificateDir
  106. }
  107. c.CertificateDir = nginx.GetConfPath("ssl", strings.Join(c.ServerName, "_")+"_"+string(c.GetKeyType()))
  108. return c.CertificateDir
  109. }
  110. func (c *ConfigPayload) GetCertificatePath() string {
  111. if c.SSLCertificatePath != "" {
  112. return c.SSLCertificatePath
  113. }
  114. c.SSLCertificatePath = filepath.Join(c.getCertificateDirPath(), "fullchain.cer")
  115. return c.SSLCertificatePath
  116. }
  117. func (c *ConfigPayload) GetCertificateKeyPath() string {
  118. if c.SSLCertificateKeyPath != "" {
  119. return c.SSLCertificateKeyPath
  120. }
  121. c.SSLCertificateKeyPath = filepath.Join(c.getCertificateDirPath(), "private.key")
  122. return c.SSLCertificateKeyPath
  123. }