cert.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. "os"
  12. "path/filepath"
  13. "strings"
  14. )
  15. const (
  16. Success = "success"
  17. Info = "info"
  18. Error = "error"
  19. )
  20. type IssueCertResponse struct {
  21. Status string `json:"status"`
  22. Message string `json:"message"`
  23. SSLCertificate string `json:"ssl_certificate,omitempty"`
  24. SSLCertificateKey string `json:"ssl_certificate_key,omitempty"`
  25. }
  26. func handleIssueCertLogChan(conn *websocket.Conn, logChan chan string) {
  27. defer func() {
  28. if err := recover(); err != nil {
  29. log.Println("api.handleIssueCertLogChan recover", err)
  30. }
  31. }()
  32. for logString := range logChan {
  33. err := conn.WriteJSON(IssueCertResponse{
  34. Status: Info,
  35. Message: logString,
  36. })
  37. if err != nil {
  38. log.Println("Error handleIssueCertLogChan", err)
  39. return
  40. }
  41. }
  42. }
  43. func IssueCert(c *gin.Context) {
  44. var upGrader = websocket.Upgrader{
  45. CheckOrigin: func(r *http.Request) bool {
  46. return true
  47. },
  48. }
  49. // upgrade http to websocket
  50. ws, err := upGrader.Upgrade(c.Writer, c.Request, nil)
  51. if err != nil {
  52. log.Println(err)
  53. return
  54. }
  55. defer func(ws *websocket.Conn) {
  56. err := ws.Close()
  57. if err != nil {
  58. log.Println("defer websocket close err", err)
  59. }
  60. }(ws)
  61. // read
  62. var buffer struct {
  63. ServerName []string `json:"server_name"`
  64. }
  65. err = ws.ReadJSON(&buffer)
  66. if err != nil {
  67. log.Println(err)
  68. return
  69. }
  70. logChan := make(chan string, 1)
  71. errChan := make(chan error, 1)
  72. go cert.IssueCert(buffer.ServerName, logChan, errChan)
  73. go handleIssueCertLogChan(ws, logChan)
  74. // block, unless errChan closed
  75. for err = range errChan {
  76. log.Println("Error cert.IssueCert", err)
  77. err = ws.WriteJSON(IssueCertResponse{
  78. Status: Error,
  79. Message: err.Error(),
  80. })
  81. if err != nil {
  82. log.Println("Error WriteJSON", err)
  83. return
  84. }
  85. return
  86. }
  87. close(logChan)
  88. certDirName := strings.Join(buffer.ServerName, "_")
  89. sslCertificatePath := nginx.GetConfPath("ssl", certDirName, "fullchain.cer")
  90. sslCertificateKeyPath := nginx.GetConfPath("ssl", certDirName, "private.key")
  91. certModel, err := model.FirstOrCreateCert(c.Param("name"))
  92. if err != nil {
  93. log.Println(err)
  94. }
  95. err = certModel.Updates(&model.Cert{
  96. Domains: buffer.ServerName,
  97. SSLCertificatePath: sslCertificatePath,
  98. SSLCertificateKeyPath: sslCertificateKeyPath,
  99. })
  100. if err != nil {
  101. log.Println(err)
  102. err = ws.WriteJSON(IssueCertResponse{
  103. Status: Error,
  104. Message: err.Error(),
  105. })
  106. return
  107. }
  108. err = ws.WriteJSON(IssueCertResponse{
  109. Status: Success,
  110. Message: "Issued certificate successfully",
  111. SSLCertificate: sslCertificatePath,
  112. SSLCertificateKey: sslCertificateKeyPath,
  113. })
  114. if err != nil {
  115. log.Println(err)
  116. return
  117. }
  118. }
  119. func GetCertList(c *gin.Context) {
  120. certList := model.GetCertList(c.Query("name"), c.Query("domain"))
  121. c.JSON(http.StatusOK, gin.H{
  122. "data": certList,
  123. })
  124. }
  125. func getCert(c *gin.Context, certModel *model.Cert) {
  126. type resp struct {
  127. *model.Cert
  128. SSLCertification string `json:"ssl_certification"`
  129. SSLCertificationKey string `json:"ssl_certification_key"`
  130. CertificateInfo *CertificateInfo `json:"certificate_info,omitempty"`
  131. }
  132. var sslCertificationBytes, sslCertificationKeyBytes []byte
  133. var certificateInfo *CertificateInfo
  134. if certModel.SSLCertificatePath != "" {
  135. if _, err := os.Stat(certModel.SSLCertificatePath); err == nil {
  136. sslCertificationBytes, _ = os.ReadFile(certModel.SSLCertificatePath)
  137. }
  138. pubKey, err := cert.GetCertInfo(certModel.SSLCertificatePath)
  139. if err != nil {
  140. ErrHandler(c, err)
  141. return
  142. }
  143. certificateInfo = &CertificateInfo{
  144. SubjectName: pubKey.Subject.CommonName,
  145. IssuerName: pubKey.Issuer.CommonName,
  146. NotAfter: pubKey.NotAfter,
  147. NotBefore: pubKey.NotBefore,
  148. }
  149. }
  150. if certModel.SSLCertificateKeyPath != "" {
  151. if _, err := os.Stat(certModel.SSLCertificateKeyPath); err == nil {
  152. sslCertificationKeyBytes, _ = os.ReadFile(certModel.SSLCertificateKeyPath)
  153. }
  154. }
  155. c.JSON(http.StatusOK, resp{
  156. certModel,
  157. string(sslCertificationBytes),
  158. string(sslCertificationKeyBytes),
  159. certificateInfo,
  160. })
  161. }
  162. func GetCert(c *gin.Context) {
  163. certModel, err := model.FirstCertByID(cast.ToInt(c.Param("id")))
  164. if err != nil {
  165. ErrHandler(c, err)
  166. return
  167. }
  168. getCert(c, &certModel)
  169. }
  170. func AddCert(c *gin.Context) {
  171. var json struct {
  172. Name string `json:"name"`
  173. SSLCertificatePath string `json:"ssl_certificate_path" binding:"required"`
  174. SSLCertificateKeyPath string `json:"ssl_certificate_key_path" binding:"required"`
  175. SSLCertification string `json:"ssl_certification"`
  176. SSLCertificationKey string `json:"ssl_certification_key"`
  177. }
  178. if !BindAndValid(c, &json) {
  179. return
  180. }
  181. certModel := &model.Cert{
  182. Name: json.Name,
  183. SSLCertificatePath: json.SSLCertificatePath,
  184. SSLCertificateKeyPath: json.SSLCertificateKeyPath,
  185. }
  186. err := certModel.Insert()
  187. if err != nil {
  188. ErrHandler(c, err)
  189. return
  190. }
  191. err = os.MkdirAll(filepath.Dir(json.SSLCertificatePath), 0644)
  192. if err != nil {
  193. ErrHandler(c, err)
  194. return
  195. }
  196. err = os.MkdirAll(filepath.Dir(json.SSLCertificateKeyPath), 0644)
  197. if err != nil {
  198. ErrHandler(c, err)
  199. return
  200. }
  201. if json.SSLCertification != "" {
  202. err = os.WriteFile(json.SSLCertificatePath, []byte(json.SSLCertification), 0644)
  203. if err != nil {
  204. ErrHandler(c, err)
  205. return
  206. }
  207. }
  208. if json.SSLCertificationKey != "" {
  209. err = os.WriteFile(json.SSLCertificateKeyPath, []byte(json.SSLCertificationKey), 0644)
  210. if err != nil {
  211. ErrHandler(c, err)
  212. return
  213. }
  214. }
  215. getCert(c, certModel)
  216. }
  217. func ModifyCert(c *gin.Context) {
  218. id := cast.ToInt(c.Param("id"))
  219. certModel, err := model.FirstCertByID(id)
  220. var json struct {
  221. Name string `json:"name"`
  222. Domain string `json:"domain" binding:"required"`
  223. SSLCertificatePath string `json:"ssl_certificate_path" binding:"required"`
  224. SSLCertificateKeyPath string `json:"ssl_certificate_key_path" binding:"required"`
  225. SSLCertification string `json:"ssl_certification"`
  226. SSLCertificationKey string `json:"ssl_certification_key"`
  227. }
  228. if !BindAndValid(c, &json) {
  229. return
  230. }
  231. if err != nil {
  232. ErrHandler(c, err)
  233. return
  234. }
  235. err = certModel.Updates(&model.Cert{
  236. Name: json.Name,
  237. SSLCertificatePath: json.SSLCertificatePath,
  238. SSLCertificateKeyPath: json.SSLCertificateKeyPath,
  239. })
  240. if err != nil {
  241. ErrHandler(c, err)
  242. return
  243. }
  244. err = os.MkdirAll(filepath.Dir(json.SSLCertificatePath), 0644)
  245. if err != nil {
  246. ErrHandler(c, err)
  247. return
  248. }
  249. err = os.MkdirAll(filepath.Dir(json.SSLCertificateKeyPath), 0644)
  250. if err != nil {
  251. ErrHandler(c, err)
  252. return
  253. }
  254. if json.SSLCertification != "" {
  255. err = os.WriteFile(json.SSLCertificatePath, []byte(json.SSLCertification), 0644)
  256. if err != nil {
  257. ErrHandler(c, err)
  258. return
  259. }
  260. }
  261. if json.SSLCertificationKey != "" {
  262. err = os.WriteFile(json.SSLCertificateKeyPath, []byte(json.SSLCertificationKey), 0644)
  263. if err != nil {
  264. ErrHandler(c, err)
  265. return
  266. }
  267. }
  268. GetCert(c)
  269. }
  270. func RemoveCert(c *gin.Context) {
  271. id := cast.ToInt(c.Param("id"))
  272. certModel, err := model.FirstCertByID(id)
  273. if err != nil {
  274. ErrHandler(c, err)
  275. return
  276. }
  277. err = certModel.Remove()
  278. if err != nil {
  279. ErrHandler(c, err)
  280. return
  281. }
  282. c.JSON(http.StatusNoContent, nil)
  283. }