2022-08-17 11:02:48 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2023-04-06 08:06:09 +00:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
2022-12-29 09:06:04 +00:00
|
|
|
"fmt"
|
2022-08-17 11:02:48 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2023-05-24 07:45:12 +00:00
|
|
|
"strings"
|
2022-08-17 11:02:48 +00:00
|
|
|
"time"
|
|
|
|
|
2022-10-17 08:32:31 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2023-03-13 03:14:00 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
2022-10-17 08:32:31 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/copier"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/ssh"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/terminal"
|
2022-08-17 11:02:48 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/gorilla/websocket"
|
2022-08-19 10:12:12 +00:00
|
|
|
"github.com/pkg/errors"
|
2022-08-17 11:02:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (b *BaseApi) WsSsh(c *gin.Context) {
|
2022-08-19 10:12:12 +00:00
|
|
|
id, err := strconv.Atoi(c.Query("id"))
|
2022-08-17 11:02:48 +00:00
|
|
|
if err != nil {
|
2022-08-19 10:12:12 +00:00
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
2022-08-17 11:02:48 +00:00
|
|
|
return
|
|
|
|
}
|
2022-08-19 10:12:12 +00:00
|
|
|
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)
|
2022-08-18 10:54:21 +00:00
|
|
|
return
|
|
|
|
}
|
2022-08-31 15:16:10 +00:00
|
|
|
host, err := hostService.GetHostInfo(uint(id))
|
2022-08-19 10:12:12 +00:00
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
2022-08-18 10:54:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
var connInfo ssh.ConnInfo
|
2023-03-13 16:15:51 +00:00
|
|
|
_ = copier.Copy(&connInfo, &host)
|
|
|
|
connInfo.PrivateKey = []byte(host.PrivateKey)
|
2023-04-10 13:50:24 +00:00
|
|
|
if len(host.PassPhrase) != 0 {
|
|
|
|
connInfo.PassPhrase = []byte(host.PassPhrase)
|
|
|
|
}
|
2022-08-18 10:54:21 +00:00
|
|
|
|
2022-08-19 10:12:12 +00:00
|
|
|
wsConn, err := upGrader.Upgrade(c.Writer, c.Request, nil)
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("gin context http handler failed, err: %v", err)
|
2022-08-17 11:02:48 +00:00
|
|
|
return
|
|
|
|
}
|
2022-08-19 10:12:12 +00:00
|
|
|
defer wsConn.Close()
|
2022-08-17 11:02:48 +00:00
|
|
|
|
2022-08-18 10:54:21 +00:00
|
|
|
client, err := connInfo.NewClient()
|
2022-09-01 02:25:38 +00:00
|
|
|
if wshandleError(wsConn, errors.WithMessage(err, "failed to set up the connection. Please check the host information")) {
|
2022-08-17 11:02:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer client.Close()
|
2022-08-18 10:54:21 +00:00
|
|
|
ssConn, err := connInfo.NewSshConn(cols, rows)
|
2022-08-17 11:02:48 +00:00
|
|
|
if wshandleError(wsConn, err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer ssConn.Close()
|
|
|
|
|
2022-08-18 10:54:21 +00:00
|
|
|
sws, err := terminal.NewLogicSshWsSession(cols, rows, true, connInfo.Client, wsConn)
|
2022-08-17 11:02:48 +00:00
|
|
|
if wshandleError(wsConn, err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer sws.Close()
|
|
|
|
|
|
|
|
quitChan := make(chan bool, 3)
|
|
|
|
sws.Start(quitChan)
|
|
|
|
go sws.Wait(quitChan)
|
|
|
|
|
|
|
|
<-quitChan
|
|
|
|
|
|
|
|
if wshandleError(wsConn, err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-29 09:06:04 +00:00
|
|
|
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
|
|
|
|
}
|
2023-02-20 07:39:55 +00:00
|
|
|
redisConf, err := redisService.LoadConf()
|
2022-12-29 09:06:04 +00:00
|
|
|
if err != nil {
|
2023-02-20 07:39:55 +00:00
|
|
|
global.LOG.Errorf("load redis container failed, err: %v", err)
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
2022-12-29 09:06:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-20 07:39:55 +00:00
|
|
|
wsConn, err := upGrader.Upgrade(c.Writer, c.Request, nil)
|
2022-12-29 09:06:04 +00:00
|
|
|
if err != nil {
|
2023-02-20 07:39:55 +00:00
|
|
|
global.LOG.Errorf("gin context http handler failed, err: %v", err)
|
2022-12-29 09:06:04 +00:00
|
|
|
return
|
|
|
|
}
|
2023-02-20 07:39:55 +00:00
|
|
|
defer wsConn.Close()
|
2023-04-25 06:32:16 +00:00
|
|
|
commands := "redis-cli"
|
2022-12-29 09:06:04 +00:00
|
|
|
if len(redisConf.Requirepass) != 0 {
|
2023-04-25 06:32:16 +00:00
|
|
|
commands = fmt.Sprintf("redis-cli -a %s --no-auth-warning", redisConf.Requirepass)
|
2022-12-29 09:06:04 +00:00
|
|
|
}
|
2023-05-24 07:45:12 +00:00
|
|
|
pidMap := loadMapFromDockerTop(redisConf.ContainerName)
|
2023-04-25 06:32:16 +00:00
|
|
|
slave, err := terminal.NewCommand(fmt.Sprintf("docker exec -it %s %s", redisConf.ContainerName, commands))
|
2023-02-24 10:49:34 +00:00
|
|
|
if wshandleError(wsConn, err) {
|
|
|
|
return
|
|
|
|
}
|
2023-05-24 07:45:12 +00:00
|
|
|
defer killBash(redisConf.ContainerName, commands, pidMap)
|
2023-02-24 10:49:34 +00:00
|
|
|
defer slave.Close()
|
|
|
|
|
|
|
|
tty, err := terminal.NewLocalWsSession(cols, rows, wsConn, slave)
|
|
|
|
if wshandleError(wsConn, err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
quitChan := make(chan bool, 3)
|
|
|
|
tty.Start(quitChan)
|
|
|
|
go slave.Wait(quitChan)
|
|
|
|
|
|
|
|
<-quitChan
|
|
|
|
|
|
|
|
global.LOG.Info("websocket finished")
|
|
|
|
if wshandleError(wsConn, err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer wsConn.Close()
|
2023-03-13 02:04:53 +00:00
|
|
|
|
2023-03-13 03:14:00 +00:00
|
|
|
cmds := fmt.Sprintf("docker exec %s %s", containerID, command)
|
|
|
|
if len(user) != 0 {
|
|
|
|
cmds = fmt.Sprintf("docker exec -u %s %s %s", user, containerID, command)
|
|
|
|
}
|
|
|
|
stdout, err := cmd.Exec(cmds)
|
|
|
|
if wshandleError(wsConn, errors.WithMessage(err, stdout)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-24 10:49:34 +00:00
|
|
|
commands := fmt.Sprintf("docker exec -it %s %s", containerID, command)
|
|
|
|
if len(user) != 0 {
|
|
|
|
commands = fmt.Sprintf("docker exec -it -u %s %s %s", user, containerID, command)
|
|
|
|
}
|
2023-05-24 07:45:12 +00:00
|
|
|
pidMap := loadMapFromDockerTop(containerID)
|
2023-02-24 10:49:34 +00:00
|
|
|
slave, err := terminal.NewCommand(commands)
|
2022-12-29 09:06:04 +00:00
|
|
|
if wshandleError(wsConn, err) {
|
|
|
|
return
|
|
|
|
}
|
2023-05-24 07:45:12 +00:00
|
|
|
defer killBash(containerID, command, pidMap)
|
2022-12-29 09:06:04 +00:00
|
|
|
defer slave.Close()
|
|
|
|
|
|
|
|
tty, err := terminal.NewLocalWsSession(cols, rows, wsConn, slave)
|
|
|
|
if wshandleError(wsConn, err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
quitChan := make(chan bool, 3)
|
|
|
|
tty.Start(quitChan)
|
|
|
|
go slave.Wait(quitChan)
|
|
|
|
|
|
|
|
<-quitChan
|
|
|
|
|
|
|
|
global.LOG.Info("websocket finished")
|
|
|
|
if wshandleError(wsConn, err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 11:02:48 +00:00
|
|
|
func wshandleError(ws *websocket.Conn, err error) bool {
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("handler ws faled:, err: %v", err)
|
|
|
|
dt := time.Now().Add(time.Second)
|
2023-03-13 03:14:00 +00:00
|
|
|
if ctlerr := ws.WriteControl(websocket.CloseMessage, []byte(err.Error()), dt); ctlerr != nil {
|
2023-04-06 08:06:09 +00:00
|
|
|
wsData, err := json.Marshal(terminal.WsMsg{
|
|
|
|
Type: terminal.WsMsgCmd,
|
|
|
|
Data: base64.StdEncoding.EncodeToString([]byte(err.Error())),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
_ = ws.WriteMessage(websocket.TextMessage, []byte("{\"type\":\"cmd\",\"data\":\"failed to encoding to json\"}"))
|
|
|
|
} else {
|
|
|
|
_ = ws.WriteMessage(websocket.TextMessage, wsData)
|
|
|
|
}
|
2022-08-17 11:02:48 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-05-24 07:45:12 +00:00
|
|
|
func loadMapFromDockerTop(containerID string) map[string]string {
|
|
|
|
pidMap := make(map[string]string)
|
|
|
|
sudo := cmd.SudoHandleCmd()
|
|
|
|
|
|
|
|
stdout, err := cmd.Execf("%s docker top %s -eo pid,command ", sudo, containerID)
|
|
|
|
if err != nil {
|
|
|
|
return pidMap
|
|
|
|
}
|
|
|
|
lines := strings.Split(stdout, "\n")
|
|
|
|
for _, line := range lines {
|
|
|
|
parts := strings.Fields(line)
|
|
|
|
if len(parts) != 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pidMap[parts[0]] = parts[1]
|
|
|
|
}
|
|
|
|
return pidMap
|
|
|
|
}
|
|
|
|
|
|
|
|
func killBash(containerID, comm string, pidMap map[string]string) {
|
|
|
|
sudo := cmd.SudoHandleCmd()
|
|
|
|
newPidMap := loadMapFromDockerTop(containerID)
|
|
|
|
for pid, command := range newPidMap {
|
|
|
|
isOld := false
|
|
|
|
for pid2 := range pidMap {
|
|
|
|
if pid == pid2 {
|
|
|
|
isOld = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !isOld && command == comm {
|
|
|
|
_, _ = cmd.Execf("%s kill -9 %s", sudo, pid)
|
|
|
|
}
|
2023-04-25 06:32:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 11:02:48 +00:00
|
|
|
var upGrader = websocket.Upgrader{
|
|
|
|
ReadBufferSize: 1024,
|
|
|
|
WriteBufferSize: 1024 * 1024 * 10,
|
|
|
|
CheckOrigin: func(r *http.Request) bool {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
}
|