mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-01-19 08:19:15 +08:00
parent
15b1f28fb9
commit
dcc0264b67
@ -3,7 +3,6 @@ package v1
|
|||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -93,16 +92,17 @@ func (b *BaseApi) RedisWsSsh(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
defer wsConn.Close()
|
defer wsConn.Close()
|
||||||
commands := "redis-cli"
|
commands := []string{"redis-cli"}
|
||||||
if len(redisConf.Requirepass) != 0 {
|
if len(redisConf.Requirepass) != 0 {
|
||||||
commands = fmt.Sprintf("redis-cli -a %s --no-auth-warning", redisConf.Requirepass)
|
commands = []string{"redis-cli", "-a", redisConf.Requirepass, "--no-auth-warning"}
|
||||||
}
|
}
|
||||||
pidMap := loadMapFromDockerTop(redisConf.ContainerName)
|
pidMap := loadMapFromDockerTop(redisConf.ContainerName)
|
||||||
slave, err := terminal.NewCommand(fmt.Sprintf("docker exec -it %s %s", redisConf.ContainerName, commands))
|
itemCmds := append([]string{"exec", "-it", redisConf.ContainerName}, commands...)
|
||||||
|
slave, err := terminal.NewCommand(itemCmds)
|
||||||
if wshandleError(wsConn, err) {
|
if wshandleError(wsConn, err) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer killBash(redisConf.ContainerName, pidMap)
|
defer killBash(redisConf.ContainerName, strings.Join(commands, " "), pidMap)
|
||||||
defer slave.Close()
|
defer slave.Close()
|
||||||
|
|
||||||
tty, err := terminal.NewLocalWsSession(cols, rows, wsConn, slave, false)
|
tty, err := terminal.NewLocalWsSession(cols, rows, wsConn, slave, false)
|
||||||
@ -161,16 +161,16 @@ func (b *BaseApi) ContainerWsSsh(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
commands := fmt.Sprintf("docker exec -it %s %s", containerID, command)
|
commands := []string{"exec", "-it", containerID, command}
|
||||||
if len(user) != 0 {
|
if len(user) != 0 {
|
||||||
commands = fmt.Sprintf("docker exec -it -u %s %s %s", user, containerID, command)
|
commands = []string{"exec", "-it", "-u", user, containerID, command}
|
||||||
}
|
}
|
||||||
pidMap := loadMapFromDockerTop(containerID)
|
pidMap := loadMapFromDockerTop(containerID)
|
||||||
slave, err := terminal.NewCommand(commands)
|
slave, err := terminal.NewCommand(commands)
|
||||||
if wshandleError(wsConn, err) {
|
if wshandleError(wsConn, err) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer killBash(containerID, pidMap)
|
defer killBash(containerID, command, pidMap)
|
||||||
defer slave.Close()
|
defer slave.Close()
|
||||||
|
|
||||||
tty, err := terminal.NewLocalWsSession(cols, rows, wsConn, slave, true)
|
tty, err := terminal.NewLocalWsSession(cols, rows, wsConn, slave, true)
|
||||||
@ -221,26 +221,26 @@ func loadMapFromDockerTop(containerID string) map[string]string {
|
|||||||
lines := strings.Split(stdout, "\n")
|
lines := strings.Split(stdout, "\n")
|
||||||
for _, line := range lines {
|
for _, line := range lines {
|
||||||
parts := strings.Fields(line)
|
parts := strings.Fields(line)
|
||||||
if len(parts) == 0 {
|
if len(parts) < 2 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
pidMap[parts[0]] = strings.Join(parts, " ")
|
pidMap[parts[0]] = strings.Join(parts[1:], " ")
|
||||||
}
|
}
|
||||||
return pidMap
|
return pidMap
|
||||||
}
|
}
|
||||||
|
|
||||||
func killBash(containerID string, pidMap map[string]string) {
|
func killBash(containerID, comm string, pidMap map[string]string) {
|
||||||
sudo := cmd.SudoHandleCmd()
|
sudo := cmd.SudoHandleCmd()
|
||||||
newPidMap := loadMapFromDockerTop(containerID)
|
newPidMap := loadMapFromDockerTop(containerID)
|
||||||
for pid, newCmd := range newPidMap {
|
for pid, command := range newPidMap {
|
||||||
isOld := false
|
isOld := false
|
||||||
for pid2, oldCmd := range pidMap {
|
for pid2 := range pidMap {
|
||||||
if pid == pid2 && oldCmd == newCmd {
|
if pid == pid2 {
|
||||||
isOld = true
|
isOld = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !isOld {
|
if !isOld && command == comm {
|
||||||
_, _ = cmd.Execf("%s kill -9 %s", sudo, pid)
|
_, _ = cmd.Execf("%s kill -9 %s", sudo, pid)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,8 +25,8 @@ type LocalCommand struct {
|
|||||||
pty *os.File
|
pty *os.File
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCommand(commands string) (*LocalCommand, error) {
|
func NewCommand(commands []string) (*LocalCommand, error) {
|
||||||
cmd := exec.Command("sh", "-c", commands)
|
cmd := exec.Command("docker", commands...)
|
||||||
|
|
||||||
pty, err := pty.Start(cmd)
|
pty, err := pty.Start(cmd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user