certificate.go 4.3 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. if !cert.IsPublicKey(string(sslCertificationBytes)) {
  26. sslCertificationBytes = []byte{}
  27. }
  28. }
  29. certificateInfo, _ = cert.GetCertInfo(certModel.SSLCertificatePath)
  30. }
  31. if certModel.SSLCertificateKeyPath != "" {
  32. if _, err := os.Stat(certModel.SSLCertificateKeyPath); err == nil {
  33. sslCertificationKeyBytes, _ = os.ReadFile(certModel.SSLCertificateKeyPath)
  34. if !cert.IsPrivateKey(string(sslCertificationKeyBytes)) {
  35. sslCertificationKeyBytes = []byte{}
  36. }
  37. }
  38. }
  39. return &APICertificate{
  40. Cert: certModel,
  41. SSLCertificate: string(sslCertificationBytes),
  42. SSLCertificateKey: string(sslCertificationKeyBytes),
  43. CertificateInfo: certificateInfo,
  44. }
  45. }
  46. func GetCertList(c *gin.Context) {
  47. cosy.Core[model.Cert](c).SetFussy("name", "domain").SetTransformer(func(m *model.Cert) any {
  48. info, _ := cert.GetCertInfo(m.SSLCertificatePath)
  49. return APICertificate{
  50. Cert: m,
  51. CertificateInfo: info,
  52. }
  53. }).PagingList()
  54. }
  55. func GetCert(c *gin.Context) {
  56. q := query.Cert
  57. certModel, err := q.FirstByID(cast.ToInt(c.Param("id")))
  58. if err != nil {
  59. api.ErrHandler(c, err)
  60. return
  61. }
  62. c.JSON(http.StatusOK, Transformer(certModel))
  63. }
  64. type certJson struct {
  65. Name string `json:"name" binding:"required"`
  66. SSLCertificatePath string `json:"ssl_certificate_path" binding:"required,publickey_path"`
  67. SSLCertificateKeyPath string `json:"ssl_certificate_key_path" binding:"required,privatekey_path"`
  68. SSLCertificate string `json:"ssl_certificate" binding:"omitempty,publickey"`
  69. SSLCertificateKey string `json:"ssl_certificate_key" binding:"omitempty,privatekey"`
  70. ChallengeMethod string `json:"challenge_method"`
  71. DnsCredentialID int `json:"dns_credential_id"`
  72. }
  73. func AddCert(c *gin.Context) {
  74. var json certJson
  75. if !api.BindAndValid(c, &json) {
  76. return
  77. }
  78. certModel := &model.Cert{
  79. Name: json.Name,
  80. SSLCertificatePath: json.SSLCertificatePath,
  81. SSLCertificateKeyPath: json.SSLCertificateKeyPath,
  82. ChallengeMethod: json.ChallengeMethod,
  83. DnsCredentialID: json.DnsCredentialID,
  84. }
  85. err := certModel.Insert()
  86. if err != nil {
  87. api.ErrHandler(c, err)
  88. return
  89. }
  90. content := &cert.Content{
  91. SSLCertificatePath: json.SSLCertificatePath,
  92. SSLCertificateKeyPath: json.SSLCertificateKeyPath,
  93. SSLCertificate: json.SSLCertificate,
  94. SSLCertificateKey: json.SSLCertificateKey,
  95. }
  96. err = content.WriteFile()
  97. if err != nil {
  98. api.ErrHandler(c, err)
  99. return
  100. }
  101. c.JSON(http.StatusOK, Transformer(certModel))
  102. }
  103. func ModifyCert(c *gin.Context) {
  104. id := cast.ToInt(c.Param("id"))
  105. var json certJson
  106. if !api.BindAndValid(c, &json) {
  107. return
  108. }
  109. q := query.Cert
  110. certModel, err := q.FirstByID(id)
  111. if err != nil {
  112. api.ErrHandler(c, err)
  113. return
  114. }
  115. err = certModel.Updates(&model.Cert{
  116. Name: json.Name,
  117. SSLCertificatePath: json.SSLCertificatePath,
  118. SSLCertificateKeyPath: json.SSLCertificateKeyPath,
  119. ChallengeMethod: json.ChallengeMethod,
  120. DnsCredentialID: json.DnsCredentialID,
  121. })
  122. if err != nil {
  123. api.ErrHandler(c, err)
  124. return
  125. }
  126. content := &cert.Content{
  127. SSLCertificatePath: json.SSLCertificatePath,
  128. SSLCertificateKeyPath: json.SSLCertificateKeyPath,
  129. SSLCertificate: json.SSLCertificate,
  130. SSLCertificateKey: json.SSLCertificateKey,
  131. }
  132. err = content.WriteFile()
  133. if err != nil {
  134. api.ErrHandler(c, err)
  135. return
  136. }
  137. GetCert(c)
  138. }
  139. func RemoveCert(c *gin.Context) {
  140. cosy.Core[model.Cert](c).Destroy()
  141. }