payload.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package cert
  2. import (
  3. "os"
  4. "path/filepath"
  5. "strings"
  6. "time"
  7. "github.com/0xJacky/Nginx-UI/internal/helper"
  8. "github.com/0xJacky/Nginx-UI/internal/nginx"
  9. "github.com/0xJacky/Nginx-UI/internal/translation"
  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/uozi-tech/cosy"
  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 *Logger) error {
  71. err := c.mkCertificateDir()
  72. if err != nil {
  73. return cosy.WrapErrorWithParams(ErrMakeCertificateDir, err.Error())
  74. }
  75. // Each certificate comes back with the cert bytes, the bytes of the client's
  76. // private key, and a certificate URL. SAVE THESE TO DISK.
  77. l.Info(translation.C("[Nginx UI] Writing certificate to disk"))
  78. err = os.WriteFile(c.GetCertificatePath(),
  79. c.Resource.Certificate, 0644)
  80. if err != nil {
  81. return cosy.WrapErrorWithParams(ErrWriteFullchainCer, err.Error())
  82. }
  83. l.Info(translation.C("[Nginx UI] Writing certificate private key to disk"))
  84. err = os.WriteFile(c.GetCertificateKeyPath(),
  85. c.Resource.PrivateKey, 0644)
  86. if err != nil {
  87. return cosy.WrapErrorWithParams(ErrWritePrivateKey, err.Error())
  88. }
  89. // update database
  90. if c.CertID <= 0 {
  91. return nil
  92. }
  93. db := model.UseDB()
  94. db.Where("id = ?", c.CertID).Updates(&model.Cert{
  95. SSLCertificatePath: c.GetCertificatePath(),
  96. SSLCertificateKeyPath: c.GetCertificateKeyPath(),
  97. Resource: c.Resource,
  98. })
  99. return nil
  100. }
  101. func (c *ConfigPayload) getCertificateDirPath() string {
  102. if c.CertificateDir != "" {
  103. return c.CertificateDir
  104. }
  105. c.CertificateDir = nginx.GetConfPath("ssl", strings.Join(c.ServerName, "_")+"_"+string(c.GetKeyType()))
  106. return c.CertificateDir
  107. }
  108. func (c *ConfigPayload) GetCertificatePath() string {
  109. if c.SSLCertificatePath != "" {
  110. return c.SSLCertificatePath
  111. }
  112. c.SSLCertificatePath = filepath.Join(c.getCertificateDirPath(), "fullchain.cer")
  113. return c.SSLCertificatePath
  114. }
  115. func (c *ConfigPayload) GetCertificateKeyPath() string {
  116. if c.SSLCertificateKeyPath != "" {
  117. return c.SSLCertificateKeyPath
  118. }
  119. c.SSLCertificateKeyPath = filepath.Join(c.getCertificateDirPath(), "private.key")
  120. return c.SSLCertificateKeyPath
  121. }