payload.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. NotBefore time.Time `json:"-"`
  25. CertificateDir string `json:"-"`
  26. SSLCertificatePath string `json:"-"`
  27. SSLCertificateKeyPath string `json:"-"`
  28. }
  29. func (c *ConfigPayload) GetACMEUser() (user *model.AcmeUser, err error) {
  30. u := query.AcmeUser
  31. // if acme_user_id == 0, use default user
  32. if c.ACMEUserID == 0 {
  33. return GetDefaultACMEUser()
  34. }
  35. // use the acme_user_id to get the acme user
  36. user, err = u.Where(u.ID.Eq(c.ACMEUserID)).First()
  37. // if acme_user not exist, use default user
  38. if err != nil {
  39. logger.Error(err)
  40. return GetDefaultACMEUser()
  41. }
  42. return
  43. }
  44. func (c *ConfigPayload) GetKeyType() certcrypto.KeyType {
  45. return helper.GetKeyType(c.KeyType)
  46. }
  47. func (c *ConfigPayload) mkCertificateDir() (err error) {
  48. dir := c.getCertificateDirPath()
  49. if _, err = os.Stat(dir); os.IsNotExist(err) {
  50. err = os.MkdirAll(dir, 0755)
  51. if err == nil {
  52. return nil
  53. }
  54. }
  55. // For windows, replace # with * (issue #403)
  56. c.CertificateDir = strings.ReplaceAll(c.CertificateDir, "#", "*")
  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. return
  64. }
  65. func (c *ConfigPayload) WriteFile(l *log.Logger, errChan chan error) {
  66. err := c.mkCertificateDir()
  67. if err != nil {
  68. errChan <- errors.Wrap(err, "make certificate dir error")
  69. return
  70. }
  71. // Each certificate comes back with the cert bytes, the bytes of the client's
  72. // private key, and a certificate URL. SAVE THESE TO DISK.
  73. l.Println("[INFO] [Nginx UI] Writing certificate to disk")
  74. err = os.WriteFile(c.GetCertificatePath(),
  75. c.Resource.Certificate, 0644)
  76. if err != nil {
  77. errChan <- errors.Wrap(err, "write fullchain.cer error")
  78. return
  79. }
  80. l.Println("[INFO] [Nginx UI] Writing certificate private key to disk")
  81. err = os.WriteFile(c.GetCertificateKeyPath(),
  82. c.Resource.PrivateKey, 0644)
  83. if err != nil {
  84. errChan <- errors.Wrap(err, "write private.key error")
  85. return
  86. }
  87. // update database
  88. if c.CertID <= 0 {
  89. return
  90. }
  91. db := model.UseDB()
  92. db.Where("id = ?", c.CertID).Updates(&model.Cert{
  93. SSLCertificatePath: c.GetCertificatePath(),
  94. SSLCertificateKeyPath: c.GetCertificateKeyPath(),
  95. })
  96. }
  97. func (c *ConfigPayload) getCertificateDirPath() string {
  98. if c.CertificateDir != "" {
  99. return c.CertificateDir
  100. }
  101. c.CertificateDir = nginx.GetConfPath("ssl", strings.Join(c.ServerName, "_")+"_"+string(c.GetKeyType()))
  102. return c.CertificateDir
  103. }
  104. func (c *ConfigPayload) GetCertificatePath() string {
  105. if c.SSLCertificatePath != "" {
  106. return c.SSLCertificatePath
  107. }
  108. c.SSLCertificatePath = filepath.Join(c.getCertificateDirPath(), "fullchain.cer")
  109. return c.SSLCertificatePath
  110. }
  111. func (c *ConfigPayload) GetCertificateKeyPath() string {
  112. if c.SSLCertificateKeyPath != "" {
  113. return c.SSLCertificateKeyPath
  114. }
  115. c.SSLCertificateKeyPath = filepath.Join(c.getCertificateDirPath(), "private.key")
  116. return c.SSLCertificateKeyPath
  117. }