cert.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. domain := strings.Join(buffer.ServerName, "_")
  74. go handleIssueCertLogChan(ws, logChan)
  75. // block, unless errChan closed
  76. for err = range errChan {
  77. log.Println("Error cert.IssueCert", err)
  78. err = ws.WriteJSON(IssueCertResponse{
  79. Status: Error,
  80. Message: err.Error(),
  81. })
  82. if err != nil {
  83. log.Println("Error WriteJSON", err)
  84. return
  85. }
  86. return
  87. }
  88. close(logChan)
  89. sslCertificatePath := nginx.GetNginxConfPath("ssl/" + domain + "/fullchain.cer")
  90. sslCertificateKeyPath := nginx.GetNginxConfPath("ssl/" + domain + "/private.key")
  91. certModel, err := model.FirstOrCreateCert(domain)
  92. if err != nil {
  93. log.Println(err)
  94. }
  95. err = certModel.Updates(&model.Cert{
  96. SSLCertificatePath: sslCertificatePath,
  97. SSLCertificateKeyPath: sslCertificateKeyPath,
  98. })
  99. if err != nil {
  100. log.Println(err)
  101. }
  102. err = ws.WriteJSON(IssueCertResponse{
  103. Status: Success,
  104. Message: "Issued certificate successfully",
  105. SSLCertificate: sslCertificatePath,
  106. SSLCertificateKey: sslCertificateKeyPath,
  107. })
  108. if err != nil {
  109. log.Println(err)
  110. return
  111. }
  112. }
  113. func GetCertList(c *gin.Context) {
  114. certList := model.GetCertList(c.Query("name"), c.Query("domain"))
  115. c.JSON(http.StatusOK, gin.H{
  116. "data": certList,
  117. })
  118. }
  119. func getCert(c *gin.Context, certModel model.Cert) {
  120. type resp struct {
  121. model.Cert
  122. SSLCertification string `json:"ssl_certification"`
  123. SSLCertificationKey string `json:"ssl_certification_key"`
  124. CertificateInfo *CertificateInfo `json:"certificate_info,omitempty"`
  125. }
  126. var sslCertificationBytes, sslCertificationKeyBytes []byte
  127. var certificateInfo *CertificateInfo
  128. if certModel.SSLCertificatePath != "" {
  129. if _, err := os.Stat(certModel.SSLCertificatePath); err == nil {
  130. sslCertificationBytes, _ = os.ReadFile(certModel.SSLCertificatePath)
  131. }
  132. pubKey, err := cert.GetCertInfo(certModel.SSLCertificatePath)
  133. if err != nil {
  134. ErrHandler(c, err)
  135. return
  136. }
  137. certificateInfo = &CertificateInfo{
  138. SubjectName: pubKey.Subject.CommonName,
  139. IssuerName: pubKey.Issuer.CommonName,
  140. NotAfter: pubKey.NotAfter,
  141. NotBefore: pubKey.NotBefore,
  142. }
  143. }
  144. if certModel.SSLCertificateKeyPath != "" {
  145. if _, err := os.Stat(certModel.SSLCertificateKeyPath); err == nil {
  146. sslCertificationKeyBytes, _ = os.ReadFile(certModel.SSLCertificateKeyPath)
  147. }
  148. }
  149. c.JSON(http.StatusOK, resp{
  150. certModel,
  151. string(sslCertificationBytes),
  152. string(sslCertificationKeyBytes),
  153. certificateInfo,
  154. })
  155. }
  156. func GetCert(c *gin.Context) {
  157. certModel, err := model.FirstCertByID(cast.ToInt(c.Param("id")))
  158. if err != nil {
  159. ErrHandler(c, err)
  160. return
  161. }
  162. getCert(c, certModel)
  163. }
  164. func AddCert(c *gin.Context) {
  165. var json struct {
  166. Name string `json:"name"`
  167. Domain string `json:"domain" binding:"required"`
  168. SSLCertificatePath string `json:"ssl_certificate_path" binding:"required"`
  169. SSLCertificateKeyPath string `json:"ssl_certificate_key_path" binding:"required"`
  170. SSLCertification string `json:"ssl_certification"`
  171. SSLCertificationKey string `json:"ssl_certification_key"`
  172. }
  173. if !BindAndValid(c, &json) {
  174. return
  175. }
  176. certModel, err := model.FirstOrCreateCert(json.Domain)
  177. if err != nil {
  178. ErrHandler(c, err)
  179. return
  180. }
  181. err = certModel.Updates(&model.Cert{
  182. Name: json.Name,
  183. Domain: json.Domain,
  184. SSLCertificatePath: json.SSLCertificatePath,
  185. SSLCertificateKeyPath: json.SSLCertificateKeyPath,
  186. })
  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. Domain: json.Domain,
  238. SSLCertificatePath: json.SSLCertificatePath,
  239. SSLCertificateKeyPath: json.SSLCertificateKeyPath,
  240. })
  241. if err != nil {
  242. ErrHandler(c, err)
  243. return
  244. }
  245. err = os.MkdirAll(filepath.Dir(json.SSLCertificatePath), 0644)
  246. if err != nil {
  247. ErrHandler(c, err)
  248. return
  249. }
  250. err = os.MkdirAll(filepath.Dir(json.SSLCertificateKeyPath), 0644)
  251. if err != nil {
  252. ErrHandler(c, err)
  253. return
  254. }
  255. if json.SSLCertification != "" {
  256. err = os.WriteFile(json.SSLCertificatePath, []byte(json.SSLCertification), 0644)
  257. if err != nil {
  258. ErrHandler(c, err)
  259. return
  260. }
  261. }
  262. if json.SSLCertificationKey != "" {
  263. err = os.WriteFile(json.SSLCertificateKeyPath, []byte(json.SSLCertificationKey), 0644)
  264. if err != nil {
  265. ErrHandler(c, err)
  266. return
  267. }
  268. }
  269. GetCert(c)
  270. }
  271. func RemoveCert(c *gin.Context) {
  272. id := cast.ToInt(c.Param("id"))
  273. certModel, err := model.FirstCertByID(id)
  274. if err != nil {
  275. ErrHandler(c, err)
  276. return
  277. }
  278. err = certModel.Remove()
  279. if err != nil {
  280. ErrHandler(c, err)
  281. return
  282. }
  283. c.JSON(http.StatusNoContent, nil)
  284. }