certificate.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package certificate
  2. import (
  3. "github.com/0xJacky/Nginx-UI/api"
  4. "github.com/0xJacky/Nginx-UI/api/cosy"
  5. "github.com/0xJacky/Nginx-UI/internal/cert"
  6. "github.com/0xJacky/Nginx-UI/model"
  7. "github.com/0xJacky/Nginx-UI/query"
  8. "github.com/gin-gonic/gin"
  9. "github.com/spf13/cast"
  10. "net/http"
  11. "os"
  12. )
  13. type APICertificate struct {
  14. *model.Cert
  15. SSLCertificate string `json:"ssl_certificate,omitempty"`
  16. SSLCertificateKey string `json:"ssl_certificate_key,omitempty"`
  17. CertificateInfo *cert.Info `json:"certificate_info,omitempty"`
  18. }
  19. func Transformer(certModel *model.Cert) (certificate *APICertificate) {
  20. var sslCertificationBytes, sslCertificationKeyBytes []byte
  21. var certificateInfo *cert.Info
  22. if certModel.SSLCertificatePath != "" {
  23. if _, err := os.Stat(certModel.SSLCertificatePath); err == nil {
  24. sslCertificationBytes, _ = os.ReadFile(certModel.SSLCertificatePath)
  25. }
  26. certificateInfo, _ = cert.GetCertInfo(certModel.SSLCertificatePath)
  27. }
  28. if certModel.SSLCertificateKeyPath != "" {
  29. if _, err := os.Stat(certModel.SSLCertificateKeyPath); err == nil {
  30. sslCertificationKeyBytes, _ = os.ReadFile(certModel.SSLCertificateKeyPath)
  31. }
  32. }
  33. return &APICertificate{
  34. Cert: certModel,
  35. SSLCertificate: string(sslCertificationBytes),
  36. SSLCertificateKey: string(sslCertificationKeyBytes),
  37. CertificateInfo: certificateInfo,
  38. }
  39. }
  40. func GetCertList(c *gin.Context) {
  41. cosy.Core[model.Cert](c).SetFussy("name", "domain").SetTransformer(func(m *model.Cert) any {
  42. info, _ := cert.GetCertInfo(m.SSLCertificatePath)
  43. return APICertificate{
  44. Cert: m,
  45. CertificateInfo: info,
  46. }
  47. }).PagingList()
  48. }
  49. func GetCert(c *gin.Context) {
  50. q := query.Cert
  51. certModel, err := q.FirstByID(cast.ToInt(c.Param("id")))
  52. if err != nil {
  53. api.ErrHandler(c, err)
  54. return
  55. }
  56. c.JSON(http.StatusOK, Transformer(certModel))
  57. }
  58. func AddCert(c *gin.Context) {
  59. var json struct {
  60. Name string `json:"name"`
  61. SSLCertificatePath string `json:"ssl_certificate_path" binding:"required"`
  62. SSLCertificateKeyPath string `json:"ssl_certificate_key_path" binding:"required"`
  63. SSLCertificate string `json:"ssl_certificate"`
  64. SSLCertificateKey string `json:"ssl_certificate_key"`
  65. ChallengeMethod string `json:"challenge_method"`
  66. DnsCredentialID int `json:"dns_credential_id"`
  67. }
  68. if !api.BindAndValid(c, &json) {
  69. return
  70. }
  71. certModel := &model.Cert{
  72. Name: json.Name,
  73. SSLCertificatePath: json.SSLCertificatePath,
  74. SSLCertificateKeyPath: json.SSLCertificateKeyPath,
  75. ChallengeMethod: json.ChallengeMethod,
  76. DnsCredentialID: json.DnsCredentialID,
  77. }
  78. err := certModel.Insert()
  79. if err != nil {
  80. api.ErrHandler(c, err)
  81. return
  82. }
  83. content := &cert.Content{
  84. SSLCertificatePath: json.SSLCertificatePath,
  85. SSLCertificateKeyPath: json.SSLCertificateKeyPath,
  86. SSLCertificate: json.SSLCertificate,
  87. SSLCertificateKey: json.SSLCertificateKey,
  88. }
  89. err = content.WriteFile()
  90. if err != nil {
  91. api.ErrHandler(c, err)
  92. return
  93. }
  94. c.JSON(http.StatusOK, Transformer(certModel))
  95. }
  96. func ModifyCert(c *gin.Context) {
  97. id := cast.ToInt(c.Param("id"))
  98. var json struct {
  99. Name string `json:"name"`
  100. SSLCertificatePath string `json:"ssl_certificate_path" binding:"required"`
  101. SSLCertificateKeyPath string `json:"ssl_certificate_key_path" binding:"required"`
  102. SSLCertificate string `json:"ssl_certificate"`
  103. SSLCertificateKey string `json:"ssl_certificate_key"`
  104. ChallengeMethod string `json:"challenge_method"`
  105. DnsCredentialID int `json:"dns_credential_id"`
  106. }
  107. if !api.BindAndValid(c, &json) {
  108. return
  109. }
  110. q := query.Cert
  111. certModel, err := q.FirstByID(id)
  112. if err != nil {
  113. api.ErrHandler(c, err)
  114. return
  115. }
  116. err = certModel.Updates(&model.Cert{
  117. Name: json.Name,
  118. SSLCertificatePath: json.SSLCertificatePath,
  119. SSLCertificateKeyPath: json.SSLCertificateKeyPath,
  120. ChallengeMethod: json.ChallengeMethod,
  121. DnsCredentialID: json.DnsCredentialID,
  122. })
  123. if err != nil {
  124. api.ErrHandler(c, err)
  125. return
  126. }
  127. content := &cert.Content{
  128. SSLCertificatePath: json.SSLCertificatePath,
  129. SSLCertificateKeyPath: json.SSLCertificateKeyPath,
  130. SSLCertificate: json.SSLCertificate,
  131. SSLCertificateKey: json.SSLCertificateKey,
  132. }
  133. err = content.WriteFile()
  134. if err != nil {
  135. api.ErrHandler(c, err)
  136. return
  137. }
  138. GetCert(c)
  139. }
  140. func RemoveCert(c *gin.Context) {
  141. cosy.Core[model.Cert](c).Destroy()
  142. }