From f57d9ee392cbb1c9db65bcae32faf64b646112bc Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Fri, 5 Jul 2024 10:46:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=97=A5=E5=BF=97=E6=9F=A5?= =?UTF-8?q?=E7=9C=8B=E5=8F=8A=E6=B7=BB=E5=8A=A0=E5=9B=9E=E8=B0=83=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=EF=BC=8C=E8=84=9A=E6=9C=AC=E5=86=85=E5=AE=B9=E5=BE=85?= =?UTF-8?q?=E6=8B=BC=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dao/cid.go | 16 +++++++++++-- handler/cid.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/dao/cid.go b/dao/cid.go index e8d4bca..3c4102d 100644 --- a/dao/cid.go +++ b/dao/cid.go @@ -83,8 +83,20 @@ func CreateRunLog(cid_id, auth_id int, log, err string) uint { return cidRunLog.ID } -func FindRunLogByAuthID(auth_id, cid_id int) []CIDRunLog { +func FindRunLogByAuthID(auth_id int) []CIDRunLog { var cidRunLogs []CIDRunLog - DB.Debug().Where("cid_id = ? and auth_id = ?", cid_id, auth_id).Find(&cidRunLogs) + DB.Debug().Where(" auth_id = ?", auth_id).Find(&cidRunLogs).Order("created_at desc") return cidRunLogs } + +func FindRunLogByID(auth_id, cid_id int) CIDRunLog { + var cidRunLog CIDRunLog + DB.Debug().Where("cid_id = ? and auth_id = ?", cid_id, auth_id).First(&cidRunLog) + return cidRunLog +} + +func FindCIDByIDAndToken(id int, token string) CID { + var cid CID + DB.Debug().Where("id = ? and token = ?", id, token).First(&cid) + return cid +} diff --git a/handler/cid.go b/handler/cid.go index 57ee73e..966fd69 100644 --- a/handler/cid.go +++ b/handler/cid.go @@ -5,6 +5,7 @@ import ( "github.com/gin-gonic/gin" "os" "os/exec" + "strconv" "videoplayer/dao" "videoplayer/proto" ) @@ -38,6 +39,9 @@ func SetUpCIDGroup(router *gin.Engine) { cidGroup.POST("/update", UpdateCID) cidGroup.POST("/list", GetCIDList) cidGroup.POST("/run", RunCID) + cidGroup.POST("/log", GetCIDLogList) //获取执行日志 + cidGroup.POST("/log/detail", GetCIDLog) //获取执行日志详情 + cidGroup.POST("/callback", CIDCallback) } func RunCID(c *gin.Context) { var req CIDRunReq @@ -137,3 +141,60 @@ func GetCIDList(c *gin.Context) { cids := dao.FindCIDByAuthID(authID) c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": cids}) } + +func GetCIDLog(c *gin.Context) { + var req CIDRunReq + if err := c.ShouldBind(&req); err == nil { + // 获取用户ID + id, _ := c.Get("id") + authID := int(id.(float64)) + cidLogs := dao.FindRunLogByID(req.ID, authID) + c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": cidLogs}) + } else { + c.JSON(200, gin.H{"error": err.Error(), "code": proto.ParameterError, "message": "failed"}) + } +} + +func GetCIDLogList(c *gin.Context) { + // 获取用户ID + id, _ := c.Get("id") + authID := int(id.(float64)) + cidLogs := dao.FindRunLogByAuthID(authID) + c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": cidLogs}) +} + +func CIDCallback(c *gin.Context) { + // 获取用户ID + token := c.Query("token") + cid_id := c.Query("id") + //将cid转换为int + cid, _ := strconv.Atoi(cid_id) + if token == "" || cid == 0 { + c.JSON(200, gin.H{"error": "parameter error", "code": proto.ParameterError, "message": "failed"}) + return + } + res := dao.FindCIDByIDAndToken(cid, token) + if res.ID != 0 { + scriptContent := `` + str, _ := generateRandomHexString(16) + scriptName := "" + str + `.sh` + // 写入脚本 + err2 := os.WriteFile(scriptName, []byte(scriptContent), 0755) + if err2 != nil { + c.JSON(200, gin.H{"error": err2.Error(), "code": proto.OperationFailed, "message": "failed"}) + return + } else { + //执行脚本 + cmd := exec.Command("/bin/bash", "-c", scriptName) + err3 := cmd.Run() + // 使用bytes.Buffer捕获输出 + var out bytes.Buffer + cmd.Stdout = &out + dao.CreateRunLog(cid, res.Auth_id, out.String(), err3.Error()) //添加执行日志 + c.JSON(200, gin.H{"code": proto.SuccessCode, "message": "success", "data": "success"}) + } + } else { + c.JSON(200, gin.H{"error": "CID not found by id and token", "code": proto.OperationFailed, "message": "failed"}) + return + } +}