cert.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package api
  2. import (
  3. "github.com/0xJacky/Nginx-UI/server/model"
  4. "github.com/0xJacky/Nginx-UI/server/pkg/cert"
  5. "github.com/0xJacky/Nginx-UI/server/pkg/nginx"
  6. "github.com/gin-gonic/gin"
  7. "github.com/gorilla/websocket"
  8. "github.com/spf13/cast"
  9. "log"
  10. "net/http"
  11. "strings"
  12. )
  13. const (
  14. Success = "success"
  15. Info = "info"
  16. Error = "error"
  17. )
  18. type IssueCertResponse struct {
  19. Status string `json:"status"`
  20. Message string `json:"message"`
  21. SSLCertificate string `json:"ssl_certificate,omitempty"`
  22. SSLCertificateKey string `json:"ssl_certificate_key,omitempty"`
  23. }
  24. func handleIssueCertLogChan(conn *websocket.Conn, logChan chan string) {
  25. defer func() {
  26. if err := recover(); err != nil {
  27. log.Println("api.handleIssueCertLogChan recover", err)
  28. }
  29. }()
  30. for logString := range logChan {
  31. err := conn.WriteJSON(IssueCertResponse{
  32. Status: Info,
  33. Message: logString,
  34. })
  35. if err != nil {
  36. log.Println("Error handleIssueCertLogChan", err)
  37. return
  38. }
  39. }
  40. }
  41. func IssueCert(c *gin.Context) {
  42. var upGrader = websocket.Upgrader{
  43. CheckOrigin: func(r *http.Request) bool {
  44. return true
  45. },
  46. }
  47. // upgrade http to websocket
  48. ws, err := upGrader.Upgrade(c.Writer, c.Request, nil)
  49. if err != nil {
  50. log.Println(err)
  51. return
  52. }
  53. defer func(ws *websocket.Conn) {
  54. err := ws.Close()
  55. if err != nil {
  56. log.Println("defer websocket close err", err)
  57. }
  58. }(ws)
  59. // read
  60. var buffer struct {
  61. ServerName []string `json:"server_name"`
  62. }
  63. err = ws.ReadJSON(&buffer)
  64. if err != nil {
  65. log.Println(err)
  66. return
  67. }
  68. logChan := make(chan string, 1)
  69. errChan := make(chan error, 1)
  70. go cert.IssueCert(buffer.ServerName, logChan, errChan)
  71. domain := strings.Join(buffer.ServerName, "_")
  72. go handleIssueCertLogChan(ws, logChan)
  73. // block, unless errChan closed
  74. for err = range errChan {
  75. log.Println("Error cert.IssueCert", err)
  76. err = ws.WriteJSON(IssueCertResponse{
  77. Status: Error,
  78. Message: err.Error(),
  79. })
  80. if err != nil {
  81. log.Println("Error WriteJSON", err)
  82. return
  83. }
  84. return
  85. }
  86. close(logChan)
  87. sslCertificatePath := nginx.GetNginxConfPath("ssl/" + domain + "/fullchain.cer")
  88. sslCertificateKeyPath := nginx.GetNginxConfPath("ssl/" + domain + "/private.key")
  89. certModel, err := model.FirstOrCreateCert(domain)
  90. if err != nil {
  91. log.Println(err)
  92. }
  93. err = certModel.Updates(&model.Cert{
  94. SSLCertificatePath: sslCertificatePath,
  95. SSLCertificateKeyPath: sslCertificateKeyPath,
  96. })
  97. if err != nil {
  98. log.Println(err)
  99. }
  100. err = ws.WriteJSON(IssueCertResponse{
  101. Status: Success,
  102. Message: "Issued certificate successfully",
  103. SSLCertificate: sslCertificatePath,
  104. SSLCertificateKey: sslCertificateKeyPath,
  105. })
  106. if err != nil {
  107. log.Println(err)
  108. return
  109. }
  110. }
  111. func GetCertList(c *gin.Context) {
  112. certList := model.GetCertList(c.Query("name"), c.Query("domain"))
  113. c.JSON(http.StatusOK, gin.H{
  114. "data": certList,
  115. })
  116. }
  117. func GetCert(c *gin.Context) {
  118. certModel, err := model.FirstCertByID(cast.ToInt(c.Param("id")))
  119. if err != nil {
  120. ErrHandler(c, err)
  121. return
  122. }
  123. c.JSON(http.StatusOK, certModel)
  124. }
  125. func AddCert(c *gin.Context) {
  126. var json struct {
  127. Name string `json:"name" binding:"required"`
  128. Domain string `json:"domain" binding:"required"`
  129. SSLCertificatePath string `json:"ssl_certificate_path" binding:"required"`
  130. SSLCertificateKeyPath string `json:"ssl_certificate_key_path" binding:"required"`
  131. }
  132. if !BindAndValid(c, &json) {
  133. return
  134. }
  135. certModel, err := model.FirstOrCreateCert(json.Domain)
  136. if err != nil {
  137. ErrHandler(c, err)
  138. return
  139. }
  140. err = certModel.Updates(&model.Cert{
  141. Name: json.Name,
  142. Domain: json.Domain,
  143. SSLCertificatePath: json.SSLCertificatePath,
  144. SSLCertificateKeyPath: json.SSLCertificateKeyPath,
  145. })
  146. if err != nil {
  147. ErrHandler(c, err)
  148. return
  149. }
  150. c.JSON(http.StatusOK, nil)
  151. }
  152. func ModifyCert(c *gin.Context) {
  153. id := cast.ToInt(c.Param("id"))
  154. certModel, err := model.FirstCertByID(id)
  155. var json struct {
  156. Name string `json:"name" binding:"required"`
  157. Domain string `json:"domain" binding:"required"`
  158. SSLCertificatePath string `json:"ssl_certificate_path" binding:"required"`
  159. SSLCertificateKeyPath string `json:"ssl_certificate_key_path" binding:"required"`
  160. }
  161. if !BindAndValid(c, &json) {
  162. return
  163. }
  164. if err != nil {
  165. ErrHandler(c, err)
  166. return
  167. }
  168. err = certModel.Updates(&model.Cert{
  169. Name: json.Name,
  170. Domain: json.Domain,
  171. SSLCertificatePath: json.SSLCertificatePath,
  172. SSLCertificateKeyPath: json.SSLCertificateKeyPath,
  173. })
  174. if err != nil {
  175. ErrHandler(c, err)
  176. return
  177. }
  178. c.JSON(http.StatusOK, certModel)
  179. }
  180. func RemoveCert(c *gin.Context) {
  181. id := cast.ToInt(c.Param("id"))
  182. certModel, err := model.FirstCertByID(id)
  183. if err != nil {
  184. ErrHandler(c, err)
  185. return
  186. }
  187. err = certModel.Remove()
  188. if err != nil {
  189. ErrHandler(c, err)
  190. return
  191. }
  192. c.JSON(http.StatusOK, nil)
  193. }