From fdf2a9d2473d526fe516f9a41c7d515b859642dd Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Mon, 3 Jul 2023 20:32:15 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=E7=BB=88=E7=AB=AF?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E9=94=99=E8=AF=AF=E6=97=B6=E5=89=8D=E7=AB=AF?= =?UTF-8?q?=E6=97=A0=E5=93=8D=E5=BA=94=E7=9A=84=E9=97=AE=E9=A2=98=20(#1518?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/v1/terminal.go | 94 +++++++++++++++------------------- 1 file changed, 42 insertions(+), 52 deletions(-) diff --git a/backend/app/api/v1/terminal.go b/backend/app/api/v1/terminal.go index 0cce319b2..24b7316fb 100644 --- a/backend/app/api/v1/terminal.go +++ b/backend/app/api/v1/terminal.go @@ -9,8 +9,6 @@ import ( "strings" "time" - "github.com/1Panel-dev/1Panel/backend/app/api/v1/helper" - "github.com/1Panel-dev/1Panel/backend/constant" "github.com/1Panel-dev/1Panel/backend/global" "github.com/1Panel-dev/1Panel/backend/utils/cmd" "github.com/1Panel-dev/1Panel/backend/utils/copier" @@ -22,24 +20,27 @@ import ( ) func (b *BaseApi) WsSsh(c *gin.Context) { - id, err := strconv.Atoi(c.Query("id")) + wsConn, err := upGrader.Upgrade(c.Writer, c.Request, nil) if err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + global.LOG.Errorf("gin context http handler failed, err: %v", err) + return + } + defer wsConn.Close() + + id, err := strconv.Atoi(c.Query("id")) + if wshandleError(wsConn, errors.WithMessage(err, "invalid param id in request")) { return } cols, err := strconv.Atoi(c.DefaultQuery("cols", "80")) - if err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if wshandleError(wsConn, errors.WithMessage(err, "invalid param cols in request")) { return } rows, err := strconv.Atoi(c.DefaultQuery("rows", "40")) - if err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if wshandleError(wsConn, errors.WithMessage(err, "invalid param rows in request")) { return } host, err := hostService.GetHostInfo(uint(id)) - if err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if wshandleError(wsConn, errors.WithMessage(err, "load host info by id failed")) { return } var connInfo ssh.ConnInfo @@ -49,13 +50,6 @@ func (b *BaseApi) WsSsh(c *gin.Context) { connInfo.PassPhrase = []byte(host.PassPhrase) } - wsConn, err := upGrader.Upgrade(c.Writer, c.Request, nil) - if err != nil { - global.LOG.Errorf("gin context http handler failed, err: %v", err) - return - } - defer wsConn.Close() - client, err := connInfo.NewClient() if wshandleError(wsConn, errors.WithMessage(err, "failed to set up the connection. Please check the host information")) { return @@ -85,28 +79,25 @@ func (b *BaseApi) WsSsh(c *gin.Context) { } func (b *BaseApi) RedisWsSsh(c *gin.Context) { - cols, err := strconv.Atoi(c.DefaultQuery("cols", "80")) - if err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) - return - } - rows, err := strconv.Atoi(c.DefaultQuery("rows", "40")) - if err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) - return - } - redisConf, err := redisService.LoadConf() - if err != nil { - global.LOG.Errorf("load redis container failed, err: %v", err) - helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) - return - } - wsConn, err := upGrader.Upgrade(c.Writer, c.Request, nil) if err != nil { global.LOG.Errorf("gin context http handler failed, err: %v", err) return } + + cols, err := strconv.Atoi(c.DefaultQuery("cols", "80")) + if wshandleError(wsConn, errors.WithMessage(err, "invalid param cols in request")) { + return + } + rows, err := strconv.Atoi(c.DefaultQuery("rows", "40")) + if wshandleError(wsConn, errors.WithMessage(err, "invalid param rows in request")) { + return + } + redisConf, err := redisService.LoadConf() + if wshandleError(wsConn, errors.WithMessage(err, "load redis container failed")) { + return + } + defer wsConn.Close() commands := "redis-cli" if len(redisConf.Requirepass) != 0 { @@ -138,24 +129,6 @@ func (b *BaseApi) RedisWsSsh(c *gin.Context) { } func (b *BaseApi) ContainerWsSsh(c *gin.Context) { - containerID := c.Query("containerid") - command := c.Query("command") - user := c.Query("user") - if len(command) == 0 || len(containerID) == 0 { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, errors.New("error param of command or containerID")) - return - } - cols, err := strconv.Atoi(c.DefaultQuery("cols", "80")) - if err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) - return - } - rows, err := strconv.Atoi(c.DefaultQuery("rows", "40")) - if err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) - return - } - wsConn, err := upGrader.Upgrade(c.Writer, c.Request, nil) if err != nil { global.LOG.Errorf("gin context http handler failed, err: %v", err) @@ -163,6 +136,23 @@ func (b *BaseApi) ContainerWsSsh(c *gin.Context) { } defer wsConn.Close() + containerID := c.Query("containerid") + command := c.Query("command") + user := c.Query("user") + if len(command) == 0 || len(containerID) == 0 { + if wshandleError(wsConn, errors.New("error param of command or containerID")) { + return + } + } + cols, err := strconv.Atoi(c.DefaultQuery("cols", "80")) + if wshandleError(wsConn, errors.WithMessage(err, "invalid param cols in request")) { + return + } + rows, err := strconv.Atoi(c.DefaultQuery("rows", "40")) + if wshandleError(wsConn, errors.WithMessage(err, "invalid param rows in request")) { + return + } + cmds := []string{"exec", containerID, command} if len(user) != 0 { cmds = []string{"exec", "-u", user, containerID, command}