payload.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 _, err = os.Stat(dir); os.IsNotExist(err) {
  52. err = os.MkdirAll(dir, 0755)
  53. if err == nil {
  54. return nil
  55. }
  56. }
  57. // For windows, replace # with * (issue #403)
  58. c.CertificateDir = strings.ReplaceAll(c.CertificateDir, "#", "*")
  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. }
  65. return
  66. }
  67. func (c *ConfigPayload) WriteFile(l *log.Logger, errChan chan error) {
  68. err := c.mkCertificateDir()
  69. if err != nil {
  70. errChan <- errors.Wrap(err, "make certificate dir error")
  71. return
  72. }
  73. // Each certificate comes back with the cert bytes, the bytes of the client's
  74. // private key, and a certificate URL. SAVE THESE TO DISK.
  75. l.Println("[INFO] [Nginx UI] Writing certificate to disk")
  76. err = os.WriteFile(c.GetCertificatePath(),
  77. c.Resource.Certificate, 0644)
  78. if err != nil {
  79. errChan <- errors.Wrap(err, "write fullchain.cer error")
  80. return
  81. }
  82. l.Println("[INFO] [Nginx UI] Writing certificate private key to disk")
  83. err = os.WriteFile(c.GetCertificateKeyPath(),
  84. c.Resource.PrivateKey, 0644)
  85. if err != nil {
  86. errChan <- errors.Wrap(err, "write private.key error")
  87. return
  88. }
  89. // update database
  90. if c.CertID <= 0 {
  91. return
  92. }
  93. db := model.UseDB()
  94. db.Where("id = ?", c.CertID).Updates(&model.Cert{
  95. SSLCertificatePath: c.GetCertificatePath(),
  96. SSLCertificateKeyPath: c.GetCertificateKeyPath(),
  97. })
  98. }
  99. func (c *ConfigPayload) getCertificateDirPath() string {
  100. if c.CertificateDir != "" {
  101. return c.CertificateDir
  102. }
  103. c.CertificateDir = nginx.GetConfPath("ssl", strings.Join(c.ServerName, "_")+"_"+string(c.GetKeyType()))
  104. return c.CertificateDir
  105. }
  106. func (c *ConfigPayload) GetCertificatePath() string {
  107. if c.SSLCertificatePath != "" {
  108. return c.SSLCertificatePath
  109. }
  110. c.SSLCertificatePath = filepath.Join(c.getCertificateDirPath(), "fullchain.cer")
  111. return c.SSLCertificatePath
  112. }
  113. func (c *ConfigPayload) GetCertificateKeyPath() string {
  114. if c.SSLCertificateKeyPath != "" {
  115. return c.SSLCertificateKeyPath
  116. }
  117. c.SSLCertificateKeyPath = filepath.Join(c.getCertificateDirPath(), "private.key")
  118. return c.SSLCertificateKeyPath
  119. }