Skip to content

Commit dc88083

Browse files
authored
fix: Modify the container terminal connection mode (#8301)
1 parent 1663bf8 commit dc88083

File tree

2 files changed

+25
-29
lines changed

2 files changed

+25
-29
lines changed

Diff for: backend/app/api/v1/terminal.go

+23-22
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func (b *BaseApi) ContainerWsSSH(c *gin.Context) {
9696
}
9797
source := c.Query("source")
9898
var containerID string
99-
var initCmd string
99+
var initCmd []string
100100
switch source {
101101
case "redis":
102102
containerID, initCmd, err = loadRedisInitCmd(c)
@@ -113,11 +113,11 @@ func (b *BaseApi) ContainerWsSSH(c *gin.Context) {
113113
return
114114
}
115115
pidMap := loadMapFromDockerTop(containerID)
116-
slave, err := terminal.NewCommand("clear && " + initCmd)
116+
slave, err := terminal.NewCommand(initCmd)
117117
if wshandleError(wsConn, err) {
118118
return
119119
}
120-
defer killBash(containerID, strings.ReplaceAll(initCmd, fmt.Sprintf("docker exec -it %s ", containerID), ""), pidMap)
120+
defer killBash(containerID, strings.ReplaceAll(strings.Join(initCmd, " "), fmt.Sprintf("exec -it %s ", containerID), ""), pidMap)
121121
defer slave.Close()
122122

123123
tty, err := terminal.NewLocalWsSession(cols, rows, wsConn, slave, false)
@@ -137,62 +137,63 @@ func (b *BaseApi) ContainerWsSSH(c *gin.Context) {
137137

138138
}
139139

140-
func loadRedisInitCmd(c *gin.Context) (string, string, error) {
140+
func loadRedisInitCmd(c *gin.Context) (string, []string, error) {
141141
name := c.Query("name")
142142
from := c.Query("from")
143-
commands := "redis-cli"
143+
commands := []string{"exec", "-it"}
144144
database, err := databaseService.Get(name)
145145
if err != nil {
146-
return "", "", fmt.Errorf("no such database in db, err: %v", err)
146+
return "", nil, fmt.Errorf("no such database in db, err: %v", err)
147147
}
148148
if from == "local" {
149149
redisInfo, err := appInstallService.LoadConnInfo(dto.OperationWithNameAndType{Name: name, Type: "redis"})
150150
if err != nil {
151-
return "", "", fmt.Errorf("no such app in db, err: %v", err)
151+
return "", nil, fmt.Errorf("no such app in db, err: %v", err)
152152
}
153153
name = redisInfo.ContainerName
154+
commands = append(commands, []string{name, "redis-cli"}...)
154155
if len(database.Password) != 0 {
155-
commands = "redis-cli -a " + database.Password + " --no-auth-warning"
156+
commands = append(commands, []string{"-a", database.Password, "--no-auth-warning"}...)
156157
}
157158
} else {
158-
commands = fmt.Sprintf("redis-cli -h %s -p %v", database.Address, database.Port)
159+
name = "1Panel-redis-cli-tools"
160+
commands = append(commands, []string{name, "redis-cli", "-h", database.Address, "-p", fmt.Sprintf("%v", database.Port)}...)
159161
if len(database.Password) != 0 {
160-
commands = fmt.Sprintf("redis-cli -h %s -p %v -a %s --no-auth-warning", database.Address, database.Port, database.Password)
162+
commands = append(commands, []string{"-a", database.Password, "--no-auth-warning"}...)
161163
}
162-
name = "1Panel-redis-cli-tools"
163164
}
164-
return name, fmt.Sprintf("docker exec -it %s %s", name, commands), nil
165+
return name, commands, nil
165166
}
166167

167-
func loadOllamaInitCmd(c *gin.Context) (string, string, error) {
168+
func loadOllamaInitCmd(c *gin.Context) (string, []string, error) {
168169
name := c.Query("name")
169170
if cmd.CheckIllegal(name) {
170-
return "", "", fmt.Errorf("ollama model %s contains illegal characters", name)
171+
return "", nil, fmt.Errorf("ollama model %s contains illegal characters", name)
171172
}
172173
ollamaInfo, err := appInstallService.LoadConnInfo(dto.OperationWithNameAndType{Name: "", Type: "ollama"})
173174
if err != nil {
174-
return "", "", fmt.Errorf("no such app in db, err: %v", err)
175+
return "", nil, fmt.Errorf("no such app in db, err: %v", err)
175176
}
176177
containerName := ollamaInfo.ContainerName
177-
return containerName, fmt.Sprintf("docker exec -it %s ollama run %s", containerName, name), nil
178+
return containerName, []string{"exec", "-it", containerName, "ollama", "run", name}, nil
178179
}
179180

180-
func loadContainerInitCmd(c *gin.Context) (string, string, error) {
181+
func loadContainerInitCmd(c *gin.Context) (string, []string, error) {
181182
containerID := c.Query("containerid")
182183
command := c.Query("command")
183184
user := c.Query("user")
184185
if cmd.CheckIllegal(user, containerID, command) {
185-
return "", "", fmt.Errorf("the command contains illegal characters. command: %s, user: %s, containerID: %s", command, user, containerID)
186+
return "", nil, fmt.Errorf("the command contains illegal characters. command: %s, user: %s, containerID: %s", command, user, containerID)
186187
}
187188
if len(command) == 0 || len(containerID) == 0 {
188-
return "", "", fmt.Errorf("error param of command: %s or containerID: %s", command, containerID)
189+
return "", nil, fmt.Errorf("error param of command: %s or containerID: %s", command, containerID)
189190
}
190-
command = fmt.Sprintf("docker exec -it %s %s", containerID, command)
191+
commands := []string{"exec", "-it", containerID, command}
191192
if len(user) != 0 {
192-
command = fmt.Sprintf("docker exec -it -u %s %s %s", user, containerID, command)
193+
commands = []string{"exec", "-it", "-u", user, containerID, command}
193194
}
194195

195-
return containerID, command, nil
196+
return containerID, commands, nil
196197
}
197198

198199
func wshandleError(ws *websocket.Conn, err error) bool {

Diff for: backend/utils/terminal/local_cmd.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ type LocalCommand struct {
2525
pty *os.File
2626
}
2727

28-
func NewCommand(initCmd string) (*LocalCommand, error) {
29-
cmd := exec.Command("bash")
28+
func NewCommand(initCmd []string) (*LocalCommand, error) {
29+
cmd := exec.Command("docker", initCmd...)
3030
if term := os.Getenv("TERM"); term != "" {
3131
cmd.Env = append(os.Environ(), "TERM="+term)
3232
} else {
@@ -38,11 +38,6 @@ func NewCommand(initCmd string) (*LocalCommand, error) {
3838
return nil, errors.Wrapf(err, "failed to start command")
3939
}
4040

41-
if len(initCmd) != 0 {
42-
time.Sleep(100 * time.Millisecond)
43-
_, _ = pty.Write([]byte(initCmd + "\n"))
44-
}
45-
4641
lcmd := &LocalCommand{
4742
closeSignal: DefaultCloseSignal,
4843
closeTimeout: DefaultCloseTimeout,

0 commit comments

Comments
 (0)