certificate.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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/api/sites"
  6. "github.com/0xJacky/Nginx-UI/internal/cert"
  7. "github.com/0xJacky/Nginx-UI/model"
  8. "github.com/gin-gonic/gin"
  9. "github.com/spf13/cast"
  10. "net/http"
  11. "os"
  12. "path/filepath"
  13. )
  14. func GetCertList(c *gin.Context) {
  15. cosy.Core[model.Cert](c).SetFussy("name", "domain").PagingList()
  16. }
  17. func getCert(c *gin.Context, certModel *model.Cert) {
  18. type resp struct {
  19. *model.Cert
  20. SSLCertificate string `json:"ssl_certificate"`
  21. SSLCertificateKey string `json:"ssl_certificate_key"`
  22. CertificateInfo *sites.CertificateInfo `json:"certificate_info,omitempty"`
  23. }
  24. var sslCertificationBytes, sslCertificationKeyBytes []byte
  25. var certificateInfo *sites.CertificateInfo
  26. if certModel.SSLCertificatePath != "" {
  27. if _, err := os.Stat(certModel.SSLCertificatePath); err == nil {
  28. sslCertificationBytes, _ = os.ReadFile(certModel.SSLCertificatePath)
  29. }
  30. pubKey, err := cert.GetCertInfo(certModel.SSLCertificatePath)
  31. if err != nil {
  32. api.ErrHandler(c, err)
  33. return
  34. }
  35. certificateInfo = &sites.CertificateInfo{
  36. SubjectName: pubKey.Subject.CommonName,
  37. IssuerName: pubKey.Issuer.CommonName,
  38. NotAfter: pubKey.NotAfter,
  39. NotBefore: pubKey.NotBefore,
  40. }
  41. }
  42. if certModel.SSLCertificateKeyPath != "" {
  43. if _, err := os.Stat(certModel.SSLCertificateKeyPath); err == nil {
  44. sslCertificationKeyBytes, _ = os.ReadFile(certModel.SSLCertificateKeyPath)
  45. }
  46. }
  47. c.JSON(http.StatusOK, resp{
  48. certModel,
  49. string(sslCertificationBytes),
  50. string(sslCertificationKeyBytes),
  51. certificateInfo,
  52. })
  53. }
  54. func GetCert(c *gin.Context) {
  55. certModel, err := model.FirstCertByID(cast.ToInt(c.Param("id")))
  56. if err != nil {
  57. api.ErrHandler(c, err)
  58. return
  59. }
  60. getCert(c, &certModel)
  61. }
  62. func AddCert(c *gin.Context) {
  63. var json struct {
  64. Name string `json:"name"`
  65. SSLCertificatePath string `json:"ssl_certificate_path" binding:"required"`
  66. SSLCertificateKeyPath string `json:"ssl_certificate_key_path" binding:"required"`
  67. SSLCertification string `json:"ssl_certification"`
  68. SSLCertificationKey string `json:"ssl_certification_key"`
  69. }
  70. if !api.BindAndValid(c, &json) {
  71. return
  72. }
  73. certModel := &model.Cert{
  74. Name: json.Name,
  75. SSLCertificatePath: json.SSLCertificatePath,
  76. SSLCertificateKeyPath: json.SSLCertificateKeyPath,
  77. }
  78. err := certModel.Insert()
  79. if err != nil {
  80. api.ErrHandler(c, err)
  81. return
  82. }
  83. err = os.MkdirAll(filepath.Dir(json.SSLCertificatePath), 0644)
  84. if err != nil {
  85. api.ErrHandler(c, err)
  86. return
  87. }
  88. err = os.MkdirAll(filepath.Dir(json.SSLCertificateKeyPath), 0644)
  89. if err != nil {
  90. api.ErrHandler(c, err)
  91. return
  92. }
  93. if json.SSLCertification != "" {
  94. err = os.WriteFile(json.SSLCertificatePath, []byte(json.SSLCertification), 0644)
  95. if err != nil {
  96. api.ErrHandler(c, err)
  97. return
  98. }
  99. }
  100. if json.SSLCertificationKey != "" {
  101. err = os.WriteFile(json.SSLCertificateKeyPath, []byte(json.SSLCertificationKey), 0644)
  102. if err != nil {
  103. api.ErrHandler(c, err)
  104. return
  105. }
  106. }
  107. getCert(c, certModel)
  108. }
  109. func ModifyCert(c *gin.Context) {
  110. id := cast.ToInt(c.Param("id"))
  111. var json struct {
  112. Name string `json:"name"`
  113. SSLCertificatePath string `json:"ssl_certificate_path" binding:"required"`
  114. SSLCertificateKeyPath string `json:"ssl_certificate_key_path" binding:"required"`
  115. SSLCertificate string `json:"ssl_certificate"`
  116. SSLCertificateKey string `json:"ssl_certificate_key"`
  117. }
  118. if !api.BindAndValid(c, &json) {
  119. return
  120. }
  121. certModel, err := model.FirstCertByID(id)
  122. if err != nil {
  123. api.ErrHandler(c, err)
  124. return
  125. }
  126. err = certModel.Updates(&model.Cert{
  127. Name: json.Name,
  128. SSLCertificatePath: json.SSLCertificatePath,
  129. SSLCertificateKeyPath: json.SSLCertificateKeyPath,
  130. })
  131. if err != nil {
  132. api.ErrHandler(c, err)
  133. return
  134. }
  135. err = os.MkdirAll(filepath.Dir(json.SSLCertificatePath), 0644)
  136. if err != nil {
  137. api.ErrHandler(c, err)
  138. return
  139. }
  140. err = os.MkdirAll(filepath.Dir(json.SSLCertificateKeyPath), 0644)
  141. if err != nil {
  142. api.ErrHandler(c, err)
  143. return
  144. }
  145. if json.SSLCertificate != "" {
  146. err = os.WriteFile(json.SSLCertificatePath, []byte(json.SSLCertificate), 0644)
  147. if err != nil {
  148. api.ErrHandler(c, err)
  149. return
  150. }
  151. }
  152. if json.SSLCertificateKeyPath != "" {
  153. err = os.WriteFile(json.SSLCertificateKeyPath, []byte(json.SSLCertificateKey), 0644)
  154. if err != nil {
  155. api.ErrHandler(c, err)
  156. return
  157. }
  158. }
  159. GetCert(c)
  160. }
  161. func RemoveCert(c *gin.Context) {
  162. cosy.Core[model.Cert](c).Destroy()
  163. }