payload.go 4.1 KB

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