payload.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package cert
  2. import (
  3. "github.com/0xJacky/Nginx-UI/internal/helper"
  4. "github.com/0xJacky/Nginx-UI/internal/logger"
  5. "github.com/0xJacky/Nginx-UI/internal/nginx"
  6. "github.com/0xJacky/Nginx-UI/model"
  7. "github.com/0xJacky/Nginx-UI/query"
  8. "github.com/go-acme/lego/v4/certcrypto"
  9. "github.com/pkg/errors"
  10. "log"
  11. "os"
  12. "path/filepath"
  13. "strings"
  14. "time"
  15. )
  16. type ConfigPayload struct {
  17. CertID int `json:"cert_id"`
  18. ServerName []string `json:"server_name"`
  19. ChallengeMethod string `json:"challenge_method"`
  20. DNSCredentialID int `json:"dns_credential_id"`
  21. ACMEUserID int `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. }
  57. if _, err = os.Stat(c.CertificateDir); os.IsNotExist(err) {
  58. err = os.MkdirAll(c.CertificateDir, 0755)
  59. if err == nil {
  60. return nil
  61. }
  62. }
  63. // For windows, replace * with # (issue #403)
  64. c.CertificateDir = strings.ReplaceAll(c.CertificateDir, "*", "#")
  65. if _, err = os.Stat(c.CertificateDir); os.IsNotExist(err) {
  66. err = os.MkdirAll(c.CertificateDir, 0755)
  67. if err == nil {
  68. return nil
  69. }
  70. }
  71. return
  72. }
  73. func (c *ConfigPayload) WriteFile(l *log.Logger, errChan chan error) {
  74. err := c.mkCertificateDir()
  75. if err != nil {
  76. errChan <- errors.Wrap(err, "make certificate dir error")
  77. return
  78. }
  79. // Each certificate comes back with the cert bytes, the bytes of the client's
  80. // private key, and a certificate URL. SAVE THESE TO DISK.
  81. l.Println("[INFO] [Nginx UI] Writing certificate to disk")
  82. err = os.WriteFile(c.GetCertificatePath(),
  83. c.Resource.Certificate, 0644)
  84. if err != nil {
  85. errChan <- errors.Wrap(err, "write fullchain.cer error")
  86. return
  87. }
  88. l.Println("[INFO] [Nginx UI] Writing certificate private key to disk")
  89. err = os.WriteFile(c.GetCertificateKeyPath(),
  90. c.Resource.PrivateKey, 0644)
  91. if err != nil {
  92. errChan <- errors.Wrap(err, "write private.key error")
  93. return
  94. }
  95. // update database
  96. if c.CertID <= 0 {
  97. return
  98. }
  99. db := model.UseDB()
  100. db.Where("id = ?", c.CertID).Updates(&model.Cert{
  101. SSLCertificatePath: c.GetCertificatePath(),
  102. SSLCertificateKeyPath: c.GetCertificateKeyPath(),
  103. })
  104. }
  105. func (c *ConfigPayload) getCertificateDirPath() string {
  106. if c.CertificateDir != "" {
  107. return c.CertificateDir
  108. }
  109. c.CertificateDir = nginx.GetConfPath("ssl", strings.Join(c.ServerName, "_")+"_"+string(c.GetKeyType()))
  110. return c.CertificateDir
  111. }
  112. func (c *ConfigPayload) GetCertificatePath() string {
  113. if c.SSLCertificatePath != "" {
  114. return c.SSLCertificatePath
  115. }
  116. c.SSLCertificatePath = filepath.Join(c.getCertificateDirPath(), "fullchain.cer")
  117. return c.SSLCertificatePath
  118. }
  119. func (c *ConfigPayload) GetCertificateKeyPath() string {
  120. if c.SSLCertificateKeyPath != "" {
  121. return c.SSLCertificateKeyPath
  122. }
  123. c.SSLCertificateKeyPath = filepath.Join(c.getCertificateDirPath(), "private.key")
  124. return c.SSLCertificateKeyPath
  125. }